AlphaComposite (Java Platform SE 8 )
Skip navigation links Overview Package Class Use Tree Deprecated Index Help Java™ Platform Standard Ed. 8 Prev Class Next Class Frames No Frames All Classes Summary: Nested | Field | Constr | Method Detail: Field | Constr | Method java.awt Class AlphaComposite java.lang.Object java.awt.AlphaComposite All Implemented Interfaces: Composite public final class AlphaComposite extends Object implements Composite The AlphaComposite class implements basic alpha compositing rules for combining source and destination colors to achieve blending and transparency effects with graphics and images.
The specific rules implemented by this class are the basic set of 12 rules described in T. Porter and T. Duff, "Compositing Digital Images", SIGGRAPH 84, 253-259. The rest of this documentation assumes some familiarity with the definitions and concepts outlined in that paper. This class extends the standard equations defined by Porter and Duff to include one additional factor. An instance of the AlphaComposite class can contain an alpha value that is used to modify the opacity or coverage of every source pixel before it is used in the blending equations.
It is important to note that the equations defined by the Porter and Duff paper are all defined to operate on color components that are premultiplied by their corresponding alpha components. Since the ColorModel and Raster classes allow the storage of pixel data in either premultiplied or non-premultiplied form, all input data must be normalized into premultiplied form before applying the equations and all results might need to be adjusted back to the form required by the destination before the pixel values are stored.
Also note that this class defines only the equations for combining color and alpha values in a purely mathematical sense. The accurate application of its equations depends on the way the data is retrieved from its sources and stored in its destinations. See Implementation Caveats for further information. The following factors are used in the description of the blending equation in the Porter and Duff paper: Factor Definition A s the alpha component of the source pixel C s a color component of the source pixel in premultiplied form A d the alpha component of the destination pixel C d a color component of the destination pixel in premultiplied form F s the fraction of the source pixel that contributes to the output F d the fraction of the destination pixel that contributes to the output A r the alpha component of the result C r a color component of the result in premultiplied form Using these factors, Porter and Duff define 12 ways of choosing the blending factors F s and F d to produce each of 12 desirable visual effects.
The equations for determining F s and F d are given in the descriptions of the 12 static fields that specify visual effects. For example, the description for SRC_OVER specifies that F s = 1 and F d = (1- A s ). Once a set of equations for determining the blending factors is known they can then be applied to each pixel to produce a result using the following set of equations: F s = f ( A d ) F d = f ( A s ) A r = A s * F s + A d * F d C r = C s * F s + C d * F d The following factors will be used to discuss our extensions to the blending equation in the Porter and Duff paper: Factor Definition C sr one of the raw color components of the source pixel C dr one of the raw color components of the destination pixel A ac the "extra" alpha component from the AlphaComposite instance A sr the raw alpha component of the source pixel A dr the raw alpha component of the destination pixel A df the final alpha component stored in the destination C df the final raw color component stored in the destination Preparing Inputs The AlphaComposite class defines an additional alpha value that is applied to the source alpha.
This value is applied as if an implicit SRC_IN rule were first applied to the source pixel against a pixel with the indicated alpha by multiplying both the raw source alpha and the raw source colors by the alpha in the AlphaComposite . This leads to the following equation for producing the alpha used in the Porter and Duff blending equation: A s = A sr * A ac All of the raw source color components need to be multiplied by the alpha in the AlphaComposite instance.
Additionally, if the source was not in premultiplied form then the color components also need to be multiplied by the source alpha. Thus, the equation for producing the source color components for the Porter and Duff equation depends on whether the source pixels are premultiplied or not: C s = C sr * A sr * A ac (if source is not premultiplied) C s = C sr * A ac (if source is premultiplied) No adjustment needs to be made to the destination alpha: A d = A dr The destination color components need to be adjusted only if they are not in premultiplied form: C d = C dr * A d (if destination is not premultiplied) C d = C dr (if destination is premultiplied) Applying the Blending Equation The adjusted A s , A d , C s , and C d are used in the standard Porter and Duff equations to calculate the blending factors F s and F d and then the resulting premultiplied components A r and C r .
Preparing Results The results only need to be adjusted if they are to be stored back into a destination buffer that holds data that is not premultiplied, using the following equations: A df = A r C df = C r (if dest is premultiplied) C df = C r / A r (if dest is not premultiplied) Note that since the division is undefined if the resulting alpha is zero, the division in that case is omitted to avoid the "divide by zero" and the color components are left as all zeros.
Performance Considerations For performance reasons, it is preferable that Raster objects passed to the compose method of a CompositeContext object created by the AlphaComposite class have premultiplied data. If either the source Raster or the destination Raster is not premultiplied, however, appropriate conversions are performed before and after the compositing operation. Implementation Caveats Many sources, such as some of the opaque image types listed in the BufferedImage class, do not store alpha values for their pixels.
Such sources supply an alpha of 1.0 for all of their pixels. Many destinations also have no place to store the alpha values that result from the blending calculations performed by this class. Such destinations thus implicitly discard the resulting alpha values that this class produces. It is recommended that such destinations should treat their stored color values as non-premultiplied and divide the resulting color values by the resulting alpha value before storing the color values and discarding the alpha value.
The accuracy of the results depends on the manner in which pixels are stored in the destination. An image format that provides at least 8 bits of storage per color and alpha component is at least adequate for use as a destination for a sequence of a few to a dozen compositing operations. An image format with fewer than 8 bits of storage per component is of limited use for just one or two compositing operations before the rounding errors dominate the results.
An image format that does not separately store color components is not a good candidate for any type of translucent blending. For example, BufferedImage.TYPE_BYTE_INDEXED should not be used as a destination for a blending operation because every operation can introduce large errors, due to the need to choose a pixel from a limited palette to match the results of the blending equations. Nearly all formats store pixels as discrete integers rather than the floating point values used in the reference equations above.
The implementation can either scale the integer pixel values into floating point values in the range 0.0 to 1.0 or use slightly modified versions of the equations that operate entirely in the integer domain and yet produce analogous results to the reference equations. Typically the integer values are relat
The rest of this page is on the live site.