Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/awt/AlphaComposite.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.awt;2627import java.awt.image.ColorModel;28import java.lang.annotation.Native;29import sun.java2d.SunCompositeContext;3031/**32* The <code>AlphaComposite</code> class implements basic alpha33* compositing rules for combining source and destination colors34* to achieve blending and transparency effects with graphics and35* images.36* The specific rules implemented by this class are the basic set37* of 12 rules described in38* T. Porter and T. Duff, "Compositing Digital Images", SIGGRAPH 84,39* 253-259.40* The rest of this documentation assumes some familiarity with the41* definitions and concepts outlined in that paper.42*43* <p>44* This class extends the standard equations defined by Porter and45* Duff to include one additional factor.46* An instance of the <code>AlphaComposite</code> class can contain47* an alpha value that is used to modify the opacity or coverage of48* every source pixel before it is used in the blending equations.49*50* <p>51* It is important to note that the equations defined by the Porter52* and Duff paper are all defined to operate on color components53* that are premultiplied by their corresponding alpha components.54* Since the <code>ColorModel</code> and <code>Raster</code> classes55* allow the storage of pixel data in either premultiplied or56* non-premultiplied form, all input data must be normalized into57* premultiplied form before applying the equations and all results58* might need to be adjusted back to the form required by the destination59* before the pixel values are stored.60*61* <p>62* Also note that this class defines only the equations63* for combining color and alpha values in a purely mathematical64* sense. The accurate application of its equations depends65* on the way the data is retrieved from its sources and stored66* in its destinations.67* See <a href="#caveats">Implementation Caveats</a>68* for further information.69*70* <p>71* The following factors are used in the description of the blending72* equation in the Porter and Duff paper:73*74* <blockquote>75* <table summary="layout">76* <tr><th align=left>Factor <th align=left>Definition77* <tr><td><em>A<sub>s</sub></em><td>the alpha component of the source pixel78* <tr><td><em>C<sub>s</sub></em><td>a color component of the source pixel in premultiplied form79* <tr><td><em>A<sub>d</sub></em><td>the alpha component of the destination pixel80* <tr><td><em>C<sub>d</sub></em><td>a color component of the destination pixel in premultiplied form81* <tr><td><em>F<sub>s</sub></em><td>the fraction of the source pixel that contributes to the output82* <tr><td><em>F<sub>d</sub></em><td>the fraction of the destination pixel that contributes83* to the output84* <tr><td><em>A<sub>r</sub></em><td>the alpha component of the result85* <tr><td><em>C<sub>r</sub></em><td>a color component of the result in premultiplied form86* </table>87* </blockquote>88*89* <p>90* Using these factors, Porter and Duff define 12 ways of choosing91* the blending factors <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em> to92* produce each of 12 desirable visual effects.93* The equations for determining <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em>94* are given in the descriptions of the 12 static fields95* that specify visual effects.96* For example,97* the description for98* <a href="#SRC_OVER"><code>SRC_OVER</code></a>99* specifies that <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>).100* Once a set of equations for determining the blending factors is101* known they can then be applied to each pixel to produce a result102* using the following set of equations:103*104* <pre>105* <em>F<sub>s</sub></em> = <em>f</em>(<em>A<sub>d</sub></em>)106* <em>F<sub>d</sub></em> = <em>f</em>(<em>A<sub>s</sub></em>)107* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*<em>F<sub>s</sub></em> + <em>A<sub>d</sub></em>*<em>F<sub>d</sub></em>108* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*<em>F<sub>s</sub></em> + <em>C<sub>d</sub></em>*<em>F<sub>d</sub></em></pre>109*110* <p>111* The following factors will be used to discuss our extensions to112* the blending equation in the Porter and Duff paper:113*114* <blockquote>115* <table summary="layout">116* <tr><th align=left>Factor <th align=left>Definition117* <tr><td><em>C<sub>sr</sub></em> <td>one of the raw color components of the source pixel118* <tr><td><em>C<sub>dr</sub></em> <td>one of the raw color components of the destination pixel119* <tr><td><em>A<sub>ac</sub></em> <td>the "extra" alpha component from the AlphaComposite instance120* <tr><td><em>A<sub>sr</sub></em> <td>the raw alpha component of the source pixel121* <tr><td><em>A<sub>dr</sub></em><td>the raw alpha component of the destination pixel122* <tr><td><em>A<sub>df</sub></em> <td>the final alpha component stored in the destination123* <tr><td><em>C<sub>df</sub></em> <td>the final raw color component stored in the destination124* </table>125*</blockquote>126*127* <h3>Preparing Inputs</h3>128*129* <p>130* The <code>AlphaComposite</code> class defines an additional alpha131* value that is applied to the source alpha.132* This value is applied as if an implicit SRC_IN rule were first133* applied to the source pixel against a pixel with the indicated134* alpha by multiplying both the raw source alpha and the raw135* source colors by the alpha in the <code>AlphaComposite</code>.136* This leads to the following equation for producing the alpha137* used in the Porter and Duff blending equation:138*139* <pre>140* <em>A<sub>s</sub></em> = <em>A<sub>sr</sub></em> * <em>A<sub>ac</sub></em> </pre>141*142* All of the raw source color components need to be multiplied143* by the alpha in the <code>AlphaComposite</code> instance.144* Additionally, if the source was not in premultiplied form145* then the color components also need to be multiplied by the146* source alpha.147* Thus, the equation for producing the source color components148* for the Porter and Duff equation depends on whether the source149* pixels are premultiplied or not:150*151* <pre>152* <em>C<sub>s</sub></em> = <em>C<sub>sr</sub></em> * <em>A<sub>sr</sub></em> * <em>A<sub>ac</sub></em> (if source is not premultiplied)153* <em>C<sub>s</sub></em> = <em>C<sub>sr</sub></em> * <em>A<sub>ac</sub></em> (if source is premultiplied) </pre>154*155* No adjustment needs to be made to the destination alpha:156*157* <pre>158* <em>A<sub>d</sub></em> = <em>A<sub>dr</sub></em> </pre>159*160* <p>161* The destination color components need to be adjusted only if162* they are not in premultiplied form:163*164* <pre>165* <em>C<sub>d</sub></em> = <em>C<sub>dr</sub></em> * <em>A<sub>d</sub></em> (if destination is not premultiplied)166* <em>C<sub>d</sub></em> = <em>C<sub>dr</sub></em> (if destination is premultiplied) </pre>167*168* <h3>Applying the Blending Equation</h3>169*170* <p>171* The adjusted <em>A<sub>s</sub></em>, <em>A<sub>d</sub></em>,172* <em>C<sub>s</sub></em>, and <em>C<sub>d</sub></em> are used in the standard173* Porter and Duff equations to calculate the blending factors174* <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em> and then the resulting175* premultiplied components <em>A<sub>r</sub></em> and <em>C<sub>r</sub></em>.176*177* <h3>Preparing Results</h3>178*179* <p>180* The results only need to be adjusted if they are to be stored181* back into a destination buffer that holds data that is not182* premultiplied, using the following equations:183*184* <pre>185* <em>A<sub>df</sub></em> = <em>A<sub>r</sub></em>186* <em>C<sub>df</sub></em> = <em>C<sub>r</sub></em> (if dest is premultiplied)187* <em>C<sub>df</sub></em> = <em>C<sub>r</sub></em> / <em>A<sub>r</sub></em> (if dest is not premultiplied) </pre>188*189* Note that since the division is undefined if the resulting alpha190* is zero, the division in that case is omitted to avoid the "divide191* by zero" and the color components are left as192* all zeros.193*194* <h3>Performance Considerations</h3>195*196* <p>197* For performance reasons, it is preferable that198* <code>Raster</code> objects passed to the <code>compose</code>199* method of a {@link CompositeContext} object created by the200* <code>AlphaComposite</code> class have premultiplied data.201* If either the source <code>Raster</code>202* or the destination <code>Raster</code>203* is not premultiplied, however,204* appropriate conversions are performed before and after the compositing205* operation.206*207* <h3><a name="caveats">Implementation Caveats</a></h3>208*209* <ul>210* <li>211* Many sources, such as some of the opaque image types listed212* in the <code>BufferedImage</code> class, do not store alpha values213* for their pixels. Such sources supply an alpha of 1.0 for214* all of their pixels.215*216* <li>217* Many destinations also have no place to store the alpha values218* that result from the blending calculations performed by this class.219* Such destinations thus implicitly discard the resulting220* alpha values that this class produces.221* It is recommended that such destinations should treat their stored222* color values as non-premultiplied and divide the resulting color223* values by the resulting alpha value before storing the color224* values and discarding the alpha value.225*226* <li>227* The accuracy of the results depends on the manner in which pixels228* are stored in the destination.229* An image format that provides at least 8 bits of storage per color230* and alpha component is at least adequate for use as a destination231* for a sequence of a few to a dozen compositing operations.232* An image format with fewer than 8 bits of storage per component233* is of limited use for just one or two compositing operations234* before the rounding errors dominate the results.235* An image format236* that does not separately store237* color components is not a238* good candidate for any type of translucent blending.239* For example, <code>BufferedImage.TYPE_BYTE_INDEXED</code>240* should not be used as a destination for a blending operation241* because every operation242* can introduce large errors, due to243* the need to choose a pixel from a limited palette to match the244* results of the blending equations.245*246* <li>247* Nearly all formats store pixels as discrete integers rather than248* the floating point values used in the reference equations above.249* The implementation can either scale the integer pixel250* values into floating point values in the range 0.0 to 1.0 or251* use slightly modified versions of the equations252* that operate entirely in the integer domain and yet produce253* analogous results to the reference equations.254*255* <p>256* Typically the integer values are related to the floating point257* values in such a way that the integer 0 is equated258* to the floating point value 0.0 and the integer259* 2^<em>n</em>-1 (where <em>n</em> is the number of bits260* in the representation) is equated to 1.0.261* For 8-bit representations, this means that 0x00262* represents 0.0 and 0xff represents263* 1.0.264*265* <li>266* The internal implementation can approximate some of the equations267* and it can also eliminate some steps to avoid unnecessary operations.268* For example, consider a discrete integer image with non-premultiplied269* alpha values that uses 8 bits per component for storage.270* The stored values for a271* nearly transparent darkened red might be:272*273* <pre>274* (A, R, G, B) = (0x01, 0xb0, 0x00, 0x00)</pre>275*276* <p>277* If integer math were being used and this value were being278* composited in279* <a href="#SRC"><code>SRC</code></a>280* mode with no extra alpha, then the math would281* indicate that the results were (in integer format):282*283* <pre>284* (A, R, G, B) = (0x01, 0x01, 0x00, 0x00)</pre>285*286* <p>287* Note that the intermediate values, which are always in premultiplied288* form, would only allow the integer red component to be either 0x00289* or 0x01. When we try to store this result back into a destination290* that is not premultiplied, dividing out the alpha will give us291* very few choices for the non-premultiplied red value.292* In this case an implementation that performs the math in integer293* space without shortcuts is likely to end up with the final pixel294* values of:295*296* <pre>297* (A, R, G, B) = (0x01, 0xff, 0x00, 0x00)</pre>298*299* <p>300* (Note that 0x01 divided by 0x01 gives you 1.0, which is equivalent301* to the value 0xff in an 8-bit storage format.)302*303* <p>304* Alternately, an implementation that uses floating point math305* might produce more accurate results and end up returning to the306* original pixel value with little, if any, roundoff error.307* Or, an implementation using integer math might decide that since308* the equations boil down to a virtual NOP on the color values309* if performed in a floating point space, it can transfer the310* pixel untouched to the destination and avoid all the math entirely.311*312* <p>313* These implementations all attempt to honor the314* same equations, but use different tradeoffs of integer and315* floating point math and reduced or full equations.316* To account for such differences, it is probably best to317* expect only that the premultiplied form of the results to318* match between implementations and image formats. In this319* case both answers, expressed in premultiplied form would320* equate to:321*322* <pre>323* (A, R, G, B) = (0x01, 0x01, 0x00, 0x00)</pre>324*325* <p>326* and thus they would all match.327*328* <li>329* Because of the technique of simplifying the equations for330* calculation efficiency, some implementations might perform331* differently when encountering result alpha values of 0.0332* on a non-premultiplied destination.333* Note that the simplification of removing the divide by alpha334* in the case of the SRC rule is technically not valid if the335* denominator (alpha) is 0.336* But, since the results should only be expected to be accurate337* when viewed in premultiplied form, a resulting alpha of 0338* essentially renders the resulting color components irrelevant339* and so exact behavior in this case should not be expected.340* </ul>341* @see Composite342* @see CompositeContext343*/344345public final class AlphaComposite implements Composite {346/**347* Both the color and the alpha of the destination are cleared348* (Porter-Duff Clear rule).349* Neither the source nor the destination is used as input.350*<p>351* <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = 0, thus:352*<pre>353* <em>A<sub>r</sub></em> = 0354* <em>C<sub>r</sub></em> = 0355*</pre>356*/357@Native public static final int CLEAR = 1;358359/**360* The source is copied to the destination361* (Porter-Duff Source rule).362* The destination is not used as input.363*<p>364* <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = 0, thus:365*<pre>366* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>367* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>368*</pre>369*/370@Native public static final int SRC = 2;371372/**373* The destination is left untouched374* (Porter-Duff Destination rule).375*<p>376* <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = 1, thus:377*<pre>378* <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>379* <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>380*</pre>381* @since 1.4382*/383@Native public static final int DST = 9;384// Note that DST was added in 1.4 so it is numbered out of order...385386/**387* The source is composited over the destination388* (Porter-Duff Source Over Destination rule).389*<p>390* <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:391*<pre>392* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em> + <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)393* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em> + <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)394*</pre>395*/396@Native public static final int SRC_OVER = 3;397398/**399* The destination is composited over the source and400* the result replaces the destination401* (Porter-Duff Destination Over Source rule).402*<p>403* <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = 1, thus:404*<pre>405* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>A<sub>d</sub></em>406* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>C<sub>d</sub></em>407*</pre>408*/409@Native public static final int DST_OVER = 4;410411/**412* The part of the source lying inside of the destination replaces413* the destination414* (Porter-Duff Source In Destination rule).415*<p>416* <em>F<sub>s</sub></em> = <em>A<sub>d</sub></em> and <em>F<sub>d</sub></em> = 0, thus:417*<pre>418* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*<em>A<sub>d</sub></em>419* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*<em>A<sub>d</sub></em>420*</pre>421*/422@Native public static final int SRC_IN = 5;423424/**425* The part of the destination lying inside of the source426* replaces the destination427* (Porter-Duff Destination In Source rule).428*<p>429* <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = <em>A<sub>s</sub></em>, thus:430*<pre>431* <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>*<em>A<sub>s</sub></em>432* <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>*<em>A<sub>s</sub></em>433*</pre>434*/435@Native public static final int DST_IN = 6;436437/**438* The part of the source lying outside of the destination439* replaces the destination440* (Porter-Duff Source Held Out By Destination rule).441*<p>442* <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = 0, thus:443*<pre>444* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>)445* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>)446*</pre>447*/448@Native public static final int SRC_OUT = 7;449450/**451* The part of the destination lying outside of the source452* replaces the destination453* (Porter-Duff Destination Held Out By Source rule).454*<p>455* <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:456*<pre>457* <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)458* <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)459*</pre>460*/461@Native public static final int DST_OUT = 8;462463// Rule 9 is DST which is defined above where it fits into the464// list logically, rather than numerically465//466// public static final int DST = 9;467468/**469* The part of the source lying inside of the destination470* is composited onto the destination471* (Porter-Duff Source Atop Destination rule).472*<p>473* <em>F<sub>s</sub></em> = <em>A<sub>d</sub></em> and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:474*<pre>475* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*<em>A<sub>d</sub></em> + <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>) = <em>A<sub>d</sub></em>476* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*<em>A<sub>d</sub></em> + <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)477*</pre>478* @since 1.4479*/480@Native public static final int SRC_ATOP = 10;481482/**483* The part of the destination lying inside of the source484* is composited over the source and replaces the destination485* (Porter-Duff Destination Atop Source rule).486*<p>487* <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = <em>A<sub>s</sub></em>, thus:488*<pre>489* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>A<sub>d</sub></em>*<em>A<sub>s</sub></em> = <em>A<sub>s</sub></em>490* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>C<sub>d</sub></em>*<em>A<sub>s</sub></em>491*</pre>492* @since 1.4493*/494@Native public static final int DST_ATOP = 11;495496/**497* The part of the source that lies outside of the destination498* is combined with the part of the destination that lies outside499* of the source500* (Porter-Duff Source Xor Destination rule).501*<p>502* <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:503*<pre>504* <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)505* <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>) + <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)506*</pre>507* @since 1.4508*/509@Native public static final int XOR = 12;510511/**512* <code>AlphaComposite</code> object that implements the opaque CLEAR rule513* with an alpha of 1.0f.514* @see #CLEAR515*/516public static final AlphaComposite Clear = new AlphaComposite(CLEAR);517518/**519* <code>AlphaComposite</code> object that implements the opaque SRC rule520* with an alpha of 1.0f.521* @see #SRC522*/523public static final AlphaComposite Src = new AlphaComposite(SRC);524525/**526* <code>AlphaComposite</code> object that implements the opaque DST rule527* with an alpha of 1.0f.528* @see #DST529* @since 1.4530*/531public static final AlphaComposite Dst = new AlphaComposite(DST);532533/**534* <code>AlphaComposite</code> object that implements the opaque SRC_OVER rule535* with an alpha of 1.0f.536* @see #SRC_OVER537*/538public static final AlphaComposite SrcOver = new AlphaComposite(SRC_OVER);539540/**541* <code>AlphaComposite</code> object that implements the opaque DST_OVER rule542* with an alpha of 1.0f.543* @see #DST_OVER544*/545public static final AlphaComposite DstOver = new AlphaComposite(DST_OVER);546547/**548* <code>AlphaComposite</code> object that implements the opaque SRC_IN rule549* with an alpha of 1.0f.550* @see #SRC_IN551*/552public static final AlphaComposite SrcIn = new AlphaComposite(SRC_IN);553554/**555* <code>AlphaComposite</code> object that implements the opaque DST_IN rule556* with an alpha of 1.0f.557* @see #DST_IN558*/559public static final AlphaComposite DstIn = new AlphaComposite(DST_IN);560561/**562* <code>AlphaComposite</code> object that implements the opaque SRC_OUT rule563* with an alpha of 1.0f.564* @see #SRC_OUT565*/566public static final AlphaComposite SrcOut = new AlphaComposite(SRC_OUT);567568/**569* <code>AlphaComposite</code> object that implements the opaque DST_OUT rule570* with an alpha of 1.0f.571* @see #DST_OUT572*/573public static final AlphaComposite DstOut = new AlphaComposite(DST_OUT);574575/**576* <code>AlphaComposite</code> object that implements the opaque SRC_ATOP rule577* with an alpha of 1.0f.578* @see #SRC_ATOP579* @since 1.4580*/581public static final AlphaComposite SrcAtop = new AlphaComposite(SRC_ATOP);582583/**584* <code>AlphaComposite</code> object that implements the opaque DST_ATOP rule585* with an alpha of 1.0f.586* @see #DST_ATOP587* @since 1.4588*/589public static final AlphaComposite DstAtop = new AlphaComposite(DST_ATOP);590591/**592* <code>AlphaComposite</code> object that implements the opaque XOR rule593* with an alpha of 1.0f.594* @see #XOR595* @since 1.4596*/597public static final AlphaComposite Xor = new AlphaComposite(XOR);598599@Native private static final int MIN_RULE = CLEAR;600@Native private static final int MAX_RULE = XOR;601602float extraAlpha;603int rule;604605private AlphaComposite(int rule) {606this(rule, 1.0f);607}608609private AlphaComposite(int rule, float alpha) {610if (rule < MIN_RULE || rule > MAX_RULE) {611throw new IllegalArgumentException("unknown composite rule");612}613if (alpha >= 0.0f && alpha <= 1.0f) {614this.rule = rule;615this.extraAlpha = alpha;616} else {617throw new IllegalArgumentException("alpha value out of range");618}619}620621/**622* Creates an <code>AlphaComposite</code> object with the specified rule.623* @param rule the compositing rule624* @throws IllegalArgumentException if <code>rule</code> is not one of625* the following: {@link #CLEAR}, {@link #SRC}, {@link #DST},626* {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},627* {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},628* {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}629*/630public static AlphaComposite getInstance(int rule) {631switch (rule) {632case CLEAR:633return Clear;634case SRC:635return Src;636case DST:637return Dst;638case SRC_OVER:639return SrcOver;640case DST_OVER:641return DstOver;642case SRC_IN:643return SrcIn;644case DST_IN:645return DstIn;646case SRC_OUT:647return SrcOut;648case DST_OUT:649return DstOut;650case SRC_ATOP:651return SrcAtop;652case DST_ATOP:653return DstAtop;654case XOR:655return Xor;656default:657throw new IllegalArgumentException("unknown composite rule");658}659}660661/**662* Creates an <code>AlphaComposite</code> object with the specified rule and663* the constant alpha to multiply with the alpha of the source.664* The source is multiplied with the specified alpha before being composited665* with the destination.666* @param rule the compositing rule667* @param alpha the constant alpha to be multiplied with the alpha of668* the source. <code>alpha</code> must be a floating point number in the669* inclusive range [0.0, 1.0].670* @throws IllegalArgumentException if671* <code>alpha</code> is less than 0.0 or greater than 1.0, or if672* <code>rule</code> is not one of673* the following: {@link #CLEAR}, {@link #SRC}, {@link #DST},674* {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},675* {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},676* {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}677*/678public static AlphaComposite getInstance(int rule, float alpha) {679if (alpha == 1.0f) {680return getInstance(rule);681}682return new AlphaComposite(rule, alpha);683}684685/**686* Creates a context for the compositing operation.687* The context contains state that is used in performing688* the compositing operation.689* @param srcColorModel the {@link ColorModel} of the source690* @param dstColorModel the <code>ColorModel</code> of the destination691* @return the <code>CompositeContext</code> object to be used to perform692* compositing operations.693*/694public CompositeContext createContext(ColorModel srcColorModel,695ColorModel dstColorModel,696RenderingHints hints) {697return new SunCompositeContext(this, srcColorModel, dstColorModel);698}699700/**701* Returns the alpha value of this <code>AlphaComposite</code>. If this702* <code>AlphaComposite</code> does not have an alpha value, 1.0 is returned.703* @return the alpha value of this <code>AlphaComposite</code>.704*/705public float getAlpha() {706return extraAlpha;707}708709/**710* Returns the compositing rule of this <code>AlphaComposite</code>.711* @return the compositing rule of this <code>AlphaComposite</code>.712*/713public int getRule() {714return rule;715}716717/**718* Returns a similar <code>AlphaComposite</code> object that uses719* the specified compositing rule.720* If this object already uses the specified compositing rule,721* this object is returned.722* @return an <code>AlphaComposite</code> object derived from723* this object that uses the specified compositing rule.724* @param rule the compositing rule725* @throws IllegalArgumentException if726* <code>rule</code> is not one of727* the following: {@link #CLEAR}, {@link #SRC}, {@link #DST},728* {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},729* {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},730* {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}731* @since 1.6732*/733public AlphaComposite derive(int rule) {734return (this.rule == rule)735? this736: getInstance(rule, this.extraAlpha);737}738739/**740* Returns a similar <code>AlphaComposite</code> object that uses741* the specified alpha value.742* If this object already has the specified alpha value,743* this object is returned.744* @return an <code>AlphaComposite</code> object derived from745* this object that uses the specified alpha value.746* @param alpha the constant alpha to be multiplied with the alpha of747* the source. <code>alpha</code> must be a floating point number in the748* inclusive range [0.0, 1.0].749* @throws IllegalArgumentException if750* <code>alpha</code> is less than 0.0 or greater than 1.0751* @since 1.6752*/753public AlphaComposite derive(float alpha) {754return (this.extraAlpha == alpha)755? this756: getInstance(this.rule, alpha);757}758759/**760* Returns the hashcode for this composite.761* @return a hash code for this composite.762*/763public int hashCode() {764return (Float.floatToIntBits(extraAlpha) * 31 + rule);765}766767/**768* Determines whether the specified object is equal to this769* <code>AlphaComposite</code>.770* <p>771* The result is <code>true</code> if and only if772* the argument is not <code>null</code> and is an773* <code>AlphaComposite</code> object that has the same774* compositing rule and alpha value as this object.775*776* @param obj the <code>Object</code> to test for equality777* @return <code>true</code> if <code>obj</code> equals this778* <code>AlphaComposite</code>; <code>false</code> otherwise.779*/780public boolean equals(Object obj) {781if (!(obj instanceof AlphaComposite)) {782return false;783}784785AlphaComposite ac = (AlphaComposite) obj;786787if (rule != ac.rule) {788return false;789}790791if (extraAlpha != ac.extraAlpha) {792return false;793}794795return true;796}797798}799800801