TopBlend: Here is the first difference. There are 59 differences. is old. is new.

java.awt
Class AlphaComposite


java.lang.Object
  extended by java.awt.AlphaComposite
All Implemented Interfaces:
Composite

public final class AlphaComposite
extends Object
implements Composite

The This AlphaComposite class implements the basic alpha compositing rules for combining source and destination colors pixels to achieve blending and transparency effects with graphics and images. The specific rules implemented by this class are the basic set of 12 Porter-Duff 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. If any input does not have an alpha channel, an alpha value of 1.0, which is completely opaque, is assumed for all pixels. A constant alpha value can also be specified to be multiplied with the alpha value of the source pixels.

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. The following abbreviations are used in the description of the rules:

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: The color and alpha components produced by the compositing operation are calculated as follows:


 	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 
 	Cd = Cs*Fs + Cd*Fd
 	Ad = As*Fs + Ad*Fd
 
where Fs and Fd are specified by each rule. The above equations assume that both source and destination pixels have the color components premultiplied by the alpha component. Similarly, the equations expressed in the definitions of compositing rules below assume premultiplied alpha.

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

For performance reasons, it is preferrable that Rasters passed to the compose method of a CompositeContext object created by the AlphaComposite class have premultiplied data. If either source or destination Rasters are not premultiplied, however, appropriate conversions are performed before and after the compositing operation.

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 alpha resulting from the compositing operation is stored in the destination if the destination has an alpha channel. Otherwise, the resulting color is divided by the resulting alpha before being stored in the destination and the alpha is discarded. If the alpha value is 0.0, the color values are set to 0.0.

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 preferrable 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

See Also:
Composite , CompositeContext

Field Summary
static  AlphaComposite Clear
          AlphaComposite object that implements the opaque CLEAR rule with an alpha of 1.0f.
static int CLEAR
          Both the color and the alpha of the destination are cleared (Porter-Duff           Porter-Duff Clear rule). rule.
static  AlphaComposite Dst
          AlphaComposite object that implements the opaque DST rule with an alpha of 1.0f.
static int DST
          The destination is left untouched (Porter-Duff           Porter-Duff Destination rule). rule.
static int DST_ATOP
          The part of the destination lying inside of the source is composited over the source and replaces the destination (Porter-Duff           Porter-Duff Destination Atop Source rule). rule.
static int DST_IN
          The part of the destination lying inside of the source replaces the destination (Porter-Duff           Porter-Duff Destination In Source rule). rule.
static int DST_OUT
          The part of the destination lying outside of the source replaces the destination (Porter-Duff           Porter-Duff Destination Held Out By Source rule). rule.
static int DST_OVER
          The destination is composited over the source and the result replaces the destination (Porter-Duff           Porter-Duff Destination Over Source rule). rule.
static  AlphaComposite DstAtop
          AlphaComposite object that implements the opaque DST_ATOP rule with an alpha of 1.0f.
static  AlphaComposite DstIn
          AlphaComposite object that implements the opaque DST_IN rule with an alpha of 1.0f.
static  AlphaComposite DstOut
          AlphaComposite object that implements the opaque DST_OUT rule with an alpha of 1.0f.
static  AlphaComposite DstOver
          AlphaComposite object that implements the opaque DST_OVER rule with an alpha of 1.0f.
static  AlphaComposite Src
          AlphaComposite object that implements the opaque SRC rule with an alpha of 1.0f.
static int SRC
          The source is copied to the destination (Porter-Duff           Porter-Duff Source rule). rule.
static int SRC_ATOP
          The part of the source lying inside of the destination is composited onto the destination (Porter-Duff           Porter-Duff Source Atop Destination rule). rule.
static int SRC_IN
          The part of the source lying inside of the destination replaces the destination (Porter-Duff           Porter-Duff Source In Destination rule). rule.
static int SRC_OUT
          The part of the source lying outside of the destination replaces the destination (Porter-Duff           Porter-Duff Source Held Out By Destination rule). rule.
static int SRC_OVER
          The source is composited over the destination (Porter-Duff           Porter-Duff Source Over Destination rule). rule.
static  AlphaComposite SrcAtop
          AlphaComposite object that implements the opaque SRC_ATOP rule with an alpha of 1.0f.
static  AlphaComposite SrcIn
          AlphaComposite object that implements the opaque SRC_IN rule with an alpha of 1.0f.
static  AlphaComposite SrcOut
          AlphaComposite object that implements the opaque SRC_OUT rule with an alpha of 1.0f.
static  AlphaComposite SrcOver
          AlphaComposite object that implements the opaque SRC_OVER rule with an alpha of 1.0f.
static  AlphaComposite Xor
          AlphaComposite object that implements the opaque XOR rule with an alpha of 1.0f.
static int XOR
          The part of the source that lies outside of the destination is combined with the part of the destination that lies outside of the source (Porter-Duff Source Xor Destination rule).
static int XOR
          Porter-Duff Source Xor Destination rule.
 
Method Summary
  CompositeContext createContext ( ColorModel  srcColorModel, ColorModel  dstColorModel, RenderingHints  hints)
          Creates a context for the compositing operation.
 boolean equals ( Object  obj)
          Determines whether the specified object is equal to this AlphaComposite.
 float getAlpha ()
          Returns the alpha value of this AlphaComposite.
static  AlphaComposite getInstance (int rule)
          Creates an AlphaComposite object with the specified rule.
static  AlphaComposite getInstance (int rule, float alpha)
          Creates an AlphaComposite object with the specified rule and the constant alpha to multiply with the alpha of the source.
 int getRule ()
          Returns the compositing rule of this AlphaComposite.
 int hashCode ()
          Returns the hashcode for this composite.
 
Methods inherited from class java.lang. Object
clone , finalize , getClass , notify , notifyAll , toString , wait , wait , wait
 

Field Detail

CLEAR


public static final int CLEAR
Porter-Duff Clear rule. Both the color and the alpha of the destination are cleared (Porter-Duff Clear rule). cleared. Neither the source nor the destination is used as input.

F s Fs = 0 and F d Fd = 0, thus:

 	A r 	Cd = 0
 	C r 	Ad = 0
 

See Also:
Constant Field Values

SRC


public static final int SRC
Porter-Duff Source rule. The source is copied to the destination (Porter-Duff Source rule). destination. The destination is not used as input.

F s Fs = 1 and F d Fd = 0, thus:


 	A r = A s 
 	C r = C s 
 
 	Cd = Cs
 	Ad = As
 

See Also:
Constant Field Values

DST


public static final int DST
Porter-Duff Destination rule. The destination is left untouched (Porter-Duff Destination rule). untouched.

F s Fs = 0 and F d Fd = 1, thus:


 	A r = A d 
 	C r = C d 
 
 	Cd = Cd
 	Ad = Ad
 

Since:
1.4
See Also:
Constant Field Values

SRC_OVER


public static final int SRC_OVER
Porter-Duff Source Over Destination rule. The source is composited over the destination (Porter-Duff Source Over Destination rule). destination.

F s = 1 and F d = (1-A s ), thus: Fs = 1 and Fd = (1-As), thus:


 	A r = A s + A d *(1-A s )
 	C r = C s + C d *(1-A s )
 
 	Cd = Cs + Cd*(1-As)
 	Ad = As + Ad*(1-As)
 

See Also:
Constant Field Values

DST_OVER


public static final int DST_OVER
Porter-Duff Destination Over Source rule. The destination is composited over the source and the result replaces the destination (Porter-Duff Destination Over Source rule). destination.

F s = (1-A d ) and F d = 1, thus: Fs = (1-Ad) and Fd = 1, thus:


 	A r = A s *(1-A d ) + A d 
 	C r = C s *(1-A d ) + C d 
 
 	Cd = Cs*(1-Ad) + Cd
 	Ad = As*(1-Ad) + Ad
 

See Also:
Constant Field Values

SRC_IN


public static final int SRC_IN
Porter-Duff Source In Destination rule. The part of the source lying inside of the destination replaces the destination (Porter-Duff Source In Destination rule). destination.

F s = A d and F d = 0, thus: Fs = Ad and Fd = 0, thus:


 	A r = A s *A d 
 	C r = C s *A d 
 
 	Cd = Cs*Ad
 	Ad = As*Ad
 

See Also:
Constant Field Values

DST_IN


public static final int DST_IN
Porter-Duff Destination In Source rule. The part of the destination lying inside of the source replaces the destination (Porter-Duff Destination In Source rule). destination.

F s = 0 and F d = A s , thus: Fs = 0 and Fd = As, thus:


 	A r = A d *A s 
 	C r = C d *A s 
 
 	Cd = Cd*As
 	Ad = Ad*As
 

See Also:
Constant Field Values

SRC_OUT


public static final int SRC_OUT
Porter-Duff Source Held Out By Destination rule. The part of the source lying outside of the destination replaces the destination (Porter-Duff Source Held Out By Destination rule). destination.

F s = (1-A d ) and F d = 0, thus: Fs = (1-Ad) and Fd = 0, thus:


 	A r = A s *(1-A d )
 	C r = C s *(1-A d )
 
 	Cd = Cs*(1-Ad)
 	Ad = As*(1-Ad)
 

See Also:
Constant Field Values

DST_OUT


public static final int DST_OUT
Porter-Duff Destination Held Out By Source rule. The part of the destination lying outside of the source replaces the destination (Porter-Duff Destination Held Out By Source rule). destination.

F s = 0 and F d = (1-A s ), thus: Fs = 0 and Fd = (1-As), thus:


 	A r = A d *(1-A s )
 	C r = C d *(1-A s )
 
 	Cd = Cd*(1-As)
 	Ad = Ad*(1-As)
 

See Also:
Constant Field Values

SRC_ATOP


public static final int SRC_ATOP
Porter-Duff Source Atop Destination rule. The part of the source lying inside of the destination is composited onto the destination (Porter-Duff Source Atop Destination rule). destination.

F s = A d and F d = (1-A s ), thus: Fs = Ad and Fd = (1-As), thus:


 	A r = A s *A d + A d *(1-A s ) = A d 
 	C r = C s *A d + C d *(1-A s )
 
 	Cd = Cs*Ad + Cd*(1-As)
 	Ad = As*Ad + Ad*(1-As) = Ad
 

Since:
1.4
See Also:
Constant Field Values

DST_ATOP


public static final int DST_ATOP
Porter-Duff Destination Atop Source rule. The part of the destination lying inside of the source is composited over the source and replaces the destination (Porter-Duff Destination Atop Source rule). destination.

F s = (1-A d ) and F d = A s , thus: Fs = (1-Ad) and Fd = As, thus:


 	A r = A s *(1-A d ) + A d *A s = A s 
 	C r = C s *(1-A d ) + C d *A s 
 
 	Cd = Cs*(1-Ad) + Cd*As
 	Ad = As*(1-Ad) + Ad*As = As
 

Since:
1.4
See Also:
Constant Field Values

XOR


public static final int XOR
Porter-Duff Source Xor Destination rule. The part of the source that lies outside of the destination is combined with the part of the destination that lies outside of the source (Porter-Duff Source Xor Destination rule). source.

F s = (1-A d ) and F d = (1-A s ), thus: Fs = (1-Ad) and Fd = (1-As), thus:


 	A r = A s *(1-A d ) + A d *(1-A s )
 	C r = C s *(1-A d ) + C d *(1-A s )
 
 	Cd = Cs*(1-Ad) + Cd*(1-As)
 	Ad = As*(1-Ad) + Ad*(1-As)
 

Since:
1.4
See Also:
Constant Field Values

Clear


public static final AlphaComposite Clear
AlphaComposite object that implements the opaque CLEAR rule with an alpha of 1.0f.

See Also:
CLEAR

Src


public static final AlphaComposite Src
AlphaComposite object that implements the opaque SRC rule with an alpha of 1.0f.

See Also:
SRC

Dst


public static final AlphaComposite Dst
AlphaComposite object that implements the opaque DST rule with an alpha of 1.0f.

Since:
1.4
See Also:
DST

SrcOver


public static final AlphaComposite SrcOver
AlphaComposite object that implements the opaque SRC_OVER rule with an alpha of 1.0f.

See Also:
SRC_OVER

DstOver


public static final AlphaComposite DstOver
AlphaComposite object that implements the opaque DST_OVER rule with an alpha of 1.0f.

See Also:
DST_OVER

SrcIn


public static final AlphaComposite SrcIn
AlphaComposite object that implements the opaque SRC_IN rule with an alpha of 1.0f.

See Also:
SRC_IN

DstIn


public static final AlphaComposite DstIn
AlphaComposite object that implements the opaque DST_IN rule with an alpha of 1.0f.

See Also:
DST_IN

SrcOut


public static final AlphaComposite SrcOut
AlphaComposite object that implements the opaque SRC_OUT rule with an alpha of 1.0f.

See Also:
SRC_OUT

DstOut


public static final AlphaComposite DstOut
AlphaComposite object that implements the opaque DST_OUT rule with an alpha of 1.0f.

See Also:
DST_OUT

SrcAtop


public static final AlphaComposite SrcAtop
AlphaComposite object that implements the opaque SRC_ATOP rule with an alpha of 1.0f.

Since:
1.4
See Also:
SRC_ATOP

DstAtop


public static final AlphaComposite DstAtop
AlphaComposite object that implements the opaque DST_ATOP rule with an alpha of 1.0f.

Since:
1.4
See Also:
DST_ATOP

Xor


public static final AlphaComposite Xor
AlphaComposite object that implements the opaque XOR rule with an alpha of 1.0f.

Since:
1.4
See Also:
XOR
Method Detail

getInstance


public static AlphaComposite getInstance(int rule)
Creates an AlphaComposite object with the specified rule.

Parameters:
rule - the compositing rule
Throws:
IllegalArgumentException - if rule is not one of the following: CLEAR , SRC , DST , SRC_OVER , DST_OVER , SRC_IN , DST_IN , SRC_OUT , DST_OUT , SRC_ATOP , DST_ATOP , or XOR

getInstance


public static AlphaComposite getInstance(int rule,
                                         float alpha)
Creates an AlphaComposite object with the specified rule and the constant alpha to multiply with the alpha of the source. The source is multiplied with the specified alpha before being composited with the destination.

Parameters:
rule - the compositing rule
alpha - the constant alpha to be multiplied with the alpha of the source. alpha must be a floating point number in the inclusive range [0.0, 1.0].
Throws:
IllegalArgumentException - if alpha is less than 0.0 or greater than 1.0, or if rule is not one of the following: CLEAR , SRC , DST , SRC_OVER , DST_OVER , SRC_IN , DST_IN , SRC_OUT , DST_OUT , SRC_ATOP , DST_ATOP , or XOR

createContext


public CompositeContext createContext(ColorModel srcColorModel,
                                      ColorModel dstColorModel,
                                      RenderingHints hints)
Creates a context for the compositing operation. The context contains state that is used in performing the compositing operation.

Specified by:
createContext in interface Composite
Parameters:
srcColorModel - the ColorModel of the source
dstColorModel - the ColorModel of the destination
hints - the hint that the context object uses to choose between rendering alternatives
Returns:
the CompositeContext object to be used to perform compositing operations.

getAlpha


public float getAlpha()
Returns the alpha value of this AlphaComposite. If this AlphaComposite does not have an alpha value, 1.0 is returned.

Returns:
the alpha value of this AlphaComposite.

getRule


public int getRule()
Returns the compositing rule of this AlphaComposite.

Returns:
the compositing rule of this AlphaComposite.

hashCode


public int hashCode()
Returns the hashcode for this composite.

Overrides:
hashCode in class Object
Returns:
a hash code for this composite.
See Also:
Object.equals(java.lang.Object) , Hashtable

equals


public boolean equals(Object obj)
Determines whether the specified object is equal to this AlphaComposite.

The result is true if and only if the argument is not null and is an AlphaComposite object that has the same compositing rule and alpha value as this object.

Overrides:
equals in class Object
Parameters:
obj - the Object to test for equality
Returns:
true if obj equals this AlphaComposite; false otherwise.
See Also:
Object.hashCode() , Hashtable