|
Préférences
Moteurs de recherche
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
JavaTM 2 Platform Std. Ed. v1.5.0
java.awt
|
Factor | Definition |
---|---|
As | the alpha component of the source pixel |
Cs | a color component of the source pixel in premultiplied form |
Ad | the alpha component of the destination pixel |
Cd | a color component of the destination pixel in premultiplied form |
Fs | the fraction of the source pixel that contributes to the output |
Fd | the fraction of the destination pixel that contributes to the output |
Ar | the alpha component of the result |
Cr | a color component of the result in premultiplied form |
Using these factors, Porter and Duff define 12 ways of choosing
the blending factors Fs and Fd to
produce each of 12 desirable visual effects.
The equations for determining Fs and Fd
are given in the descriptions of the 12 static fields
that specify visual effects.
For example,
the description for
SRC_OVER
specifies that Fs = 1 and Fd = (1-As).
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:
Fs = f(Ad) Fd = f(As) Ar = As*Fs + Ad*Fd Cr = Cs*Fs + Cd*Fd
The following factors will be used to discuss our extensions to the blending equation in the Porter and Duff paper:
Factor Definition Csr one of the raw color components of the source pixel Cdr one of the raw color components of the destination pixel Aac the "extra" alpha component from the AlphaComposite instance Asr the raw alpha component of the source pixel Adr the raw alpha component of the destination pixel Adf the final alpha component stored in the destination Cdf the final raw color component stored in the destination
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:
As = Asr * AacAll 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:
Cs = Csr * Asr * Aac (if source is not premultiplied) Cs = Csr * Aac (if source is premultiplied)No adjustment needs to be made to the destination alpha:
Ad = Adr
The destination color components need to be adjusted only if they are not in premultiplied form:
Cd = Cdr * Ad (if destination is not premultiplied) Cd = Cdr (if destination is premultiplied)
The adjusted As, Ad, Cs, and Cd are used in the standard Porter and Duff equations to calculate the blending factors Fs and Fd and then the resulting premultiplied components Ar and Cr.
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:
Adf = Ar Cdf = Cr (if dest is premultiplied) Cdf = Cr / Ar (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.
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.
BufferedImage
class, do not store alpha values
for their pixels. Such sources supply an alpha of 1.0 for
all of their pixels.
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.
Typically the integer values are related to the floating point values in such a way that the integer 0 is equated to the floating point value 0.0 and the integer 2^n-1 (where n is the number of bits in the representation) is equated to 1.0. For 8-bit representations, this means that 0x00 represents 0.0 and 0xff represents 1.0.
(A, R, G, B) = (0x01, 0xb0, 0x00, 0x00)
If integer math were being used and this value were being
composited in
SRC
mode with no extra alpha, then the math would
indicate that the results were (in integer format):
(A, R, G, B) = (0x01, 0x01, 0x00, 0x00)
Note that the intermediate values, which are always in premultiplied form, would only allow the integer red component to be either 0x00 or 0x01. When we try to store this result back into a destination that is not premultiplied, dividing out the alpha will give us very few choices for the non-premultiplied red value. In this case an implementation that performs the math in integer space without shortcuts is likely to end up with the final pixel values of:
(A, R, G, B) = (0x01, 0xff, 0x00, 0x00)
(Note that 0x01 divided by 0x01 gives you 1.0, which is equivalent to the value 0xff in an 8-bit storage format.)
Alternately, an implementation that uses floating point math might produce more accurate results and end up returning to the original pixel value with little, if any, roundoff error. Or, an implementation using integer math might decide that since the equations boil down to a virtual NOP on the color values if performed in a floating point space, it can transfer the pixel untouched to the destination and avoid all the math entirely.
These implementations all attempt to honor the same equations, but use different tradeoffs of integer and floating point math and reduced or full equations. To account for such differences, it is probably best to expect only that the premultiplied form of the results to match between implementations and image formats. In this case both answers, expressed in premultiplied form would equate to:
(A, R, G, B) = (0x01, 0x01, 0x00, 0x00)
and thus they would all match.
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 Clear 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 Destination 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 Destination Atop Source rule). |
static int |
DST_IN
The part of the destination lying inside of the source replaces the destination (Porter-Duff Destination In Source rule). |
static int |
DST_OUT
The part of the destination lying outside of the source replaces the destination (Porter-Duff Destination Held Out By Source rule). |
static int |
DST_OVER
The destination is composited over the source and the result replaces the destination (Porter-Duff Destination Over Source 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 Source rule). |
static int |
SRC_ATOP
The part of the source lying inside of the destination is composited onto the destination (Porter-Duff Source Atop Destination rule). |
static int |
SRC_IN
The part of the source lying inside of the destination replaces the destination (Porter-Duff Source In Destination rule). |
static int |
SRC_OUT
The part of the source lying outside of the destination replaces the destination (Porter-Duff Source Held Out By Destination rule). |
static int |
SRC_OVER
The source is composited over the destination (Porter-Duff Source Over Destination 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). |
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 |
---|
public static final int CLEAR
Fs = 0 and Fd = 0, thus:
Ar = 0 Cr = 0
public static final int SRC
Fs = 1 and Fd = 0, thus:
Ar = As Cr = Cs
public static final int DST
Fs = 0 and Fd = 1, thus:
Ar = Ad Cr = Cd
public static final int SRC_OVER
Fs = 1 and Fd = (1-As), thus:
Ar = As + Ad*(1-As) Cr = Cs + Cd*(1-As)
public static final int DST_OVER
Fs = (1-Ad) and Fd = 1, thus:
Ar = As*(1-Ad) + Ad Cr = Cs*(1-Ad) + Cd
public static final int SRC_IN
Fs = Ad and Fd = 0, thus:
Ar = As*Ad Cr = Cs*Ad
public static final int DST_IN
Fs = 0 and Fd = As, thus:
Ar = Ad*As Cr = Cd*As
public static final int SRC_OUT
Fs = (1-Ad) and Fd = 0, thus:
Ar = As*(1-Ad) Cr = Cs*(1-Ad)
public static final int DST_OUT
Fs = 0 and Fd = (1-As), thus:
Ar = Ad*(1-As) Cr = Cd*(1-As)
public static final int SRC_ATOP
Fs = Ad and Fd = (1-As), thus:
Ar = As*Ad + Ad*(1-As) = Ad Cr = Cs*Ad + Cd*(1-As)
public static final int DST_ATOP
Fs = (1-Ad) and Fd = As, thus:
Ar = As*(1-Ad) + Ad*As = As Cr = Cs*(1-Ad) + Cd*As
public static final int XOR
Fs = (1-Ad) and Fd = (1-As), thus:
Ar = As*(1-Ad) + Ad*(1-As) Cr = Cs*(1-Ad) + Cd*(1-As)
public static final AlphaComposite Clear
AlphaComposite
object that implements the opaque CLEAR rule
with an alpha of 1.0f.
CLEAR
public static final AlphaComposite Src
AlphaComposite
object that implements the opaque SRC rule
with an alpha of 1.0f.
SRC
public static final AlphaComposite Dst
AlphaComposite
object that implements the opaque DST rule
with an alpha of 1.0f.
DST
public static final AlphaComposite SrcOver
AlphaComposite
object that implements the opaque SRC_OVER rule
with an alpha of 1.0f.
SRC_OVER
public static final AlphaComposite DstOver
AlphaComposite
object that implements the opaque DST_OVER rule
with an alpha of 1.0f.
DST_OVER
public static final AlphaComposite SrcIn
AlphaComposite
object that implements the opaque SRC_IN rule
with an alpha of 1.0f.
SRC_IN
public static final AlphaComposite DstIn
AlphaComposite
object that implements the opaque DST_IN rule
with an alpha of 1.0f.
DST_IN
public static final AlphaComposite SrcOut
AlphaComposite
object that implements the opaque SRC_OUT rule
with an alpha of 1.0f.
SRC_OUT
public static final AlphaComposite DstOut
AlphaComposite
object that implements the opaque DST_OUT rule
with an alpha of 1.0f.
DST_OUT
public static final AlphaComposite SrcAtop
AlphaComposite
object that implements the opaque SRC_ATOP rule
with an alpha of 1.0f.
SRC_ATOP
public static final AlphaComposite DstAtop
AlphaComposite
object that implements the opaque DST_ATOP rule
with an alpha of 1.0f.
DST_ATOP
public static final AlphaComposite Xor
AlphaComposite
object that implements the opaque XOR rule
with an alpha of 1.0f.
XOR
Method Detail |
---|
public static AlphaComposite getInstance(int rule)
AlphaComposite
object with the specified rule.
public static AlphaComposite getInstance(int rule, float alpha)
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.
rule
- the compositing rulealpha
- 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].
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
public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel, RenderingHints hints)
createContext
in interface Composite
srcColorModel
- the ColorModel
of the sourcedstColorModel
- the ColorModel
of the destinationhints
- the hint that the context object uses to choose between
rendering alternatives
CompositeContext
object to be used to perform
compositing operations.public float getAlpha()
AlphaComposite
. If this
AlphaComposite
does not have an alpha value, 1.0 is returned.
AlphaComposite
.public int getRule()
AlphaComposite
.
AlphaComposite
.public int hashCode()
Object.equals(java.lang.Object)
,
Hashtable
public boolean equals(Object obj)
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.
obj
- the Object
to test for equality
true
if obj
equals this
AlphaComposite
; false
otherwise.Object.hashCode()
,
Hashtable