1 /*
   2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.awt;
  27 
  28 import java.awt.image.ColorModel;
  29 import java.lang.annotation.Native;
  30 import sun.java2d.SunCompositeContext;
  31 
  32 /**
  33  * The <code>AlphaComposite</code> class implements basic alpha
  34  * compositing rules for combining source and destination colors
  35  * to achieve blending and transparency effects with graphics and
  36  * images.
  37  * The specific rules implemented by this class are the basic set
  38  * of 12 rules described in
  39  * T. Porter and T. Duff, "Compositing Digital Images", SIGGRAPH 84,
  40  * 253-259.
  41  * The rest of this documentation assumes some familiarity with the
  42  * definitions and concepts outlined in that paper.
  43  *
  44  * <p>
  45  * This class extends the standard equations defined by Porter and
  46  * Duff to include one additional factor.
  47  * An instance of the <code>AlphaComposite</code> class can contain
  48  * an alpha value that is used to modify the opacity or coverage of
  49  * every source pixel before it is used in the blending equations.
  50  *
  51  * <p>
  52  * It is important to note that the equations defined by the Porter
  53  * and Duff paper are all defined to operate on color components
  54  * that are premultiplied by their corresponding alpha components.
  55  * Since the <code>ColorModel</code> and <code>Raster</code> classes
  56  * allow the storage of pixel data in either premultiplied or
  57  * non-premultiplied form, all input data must be normalized into
  58  * premultiplied form before applying the equations and all results
  59  * might need to be adjusted back to the form required by the destination
  60  * before the pixel values are stored.
  61  *
  62  * <p>
  63  * Also note that this class defines only the equations
  64  * for combining color and alpha values in a purely mathematical
  65  * sense. The accurate application of its equations depends
  66  * on the way the data is retrieved from its sources and stored
  67  * in its destinations.
  68  * See <a href="#caveats">Implementation Caveats</a>
  69  * for further information.
  70  *
  71  * <p>
  72  * The following factors are used in the description of the blending
  73  * equation in the Porter and Duff paper:
  74  *
  75  * <blockquote>
  76  * <table summary="layout">
  77  * <tr><th align=left>Factor&nbsp;&nbsp;<th align=left>Definition
  78  * <tr><td><em>A<sub>s</sub></em><td>the alpha component of the source pixel
  79  * <tr><td><em>C<sub>s</sub></em><td>a color component of the source pixel in premultiplied form
  80  * <tr><td><em>A<sub>d</sub></em><td>the alpha component of the destination pixel
  81  * <tr><td><em>C<sub>d</sub></em><td>a color component of the destination pixel in premultiplied form
  82  * <tr><td><em>F<sub>s</sub></em><td>the fraction of the source pixel that contributes to the output
  83  * <tr><td><em>F<sub>d</sub></em><td>the fraction of the destination pixel that contributes
  84  * to the output
  85  * <tr><td><em>A<sub>r</sub></em><td>the alpha component of the result
  86  * <tr><td><em>C<sub>r</sub></em><td>a color component of the result in premultiplied form
  87  * </table>
  88  * </blockquote>
  89  *
  90  * <p>
  91  * Using these factors, Porter and Duff define 12 ways of choosing
  92  * the blending factors <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em> to
  93  * produce each of 12 desirable visual effects.
  94  * The equations for determining <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em>
  95  * are given in the descriptions of the 12 static fields
  96  * that specify visual effects.
  97  * For example,
  98  * the description for
  99  * <a href="#SRC_OVER"><code>SRC_OVER</code></a>
 100  * specifies that <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>).
 101  * Once a set of equations for determining the blending factors is
 102  * known they can then be applied to each pixel to produce a result
 103  * using the following set of equations:
 104  *
 105  * <pre>
 106  *      <em>F<sub>s</sub></em> = <em>f</em>(<em>A<sub>d</sub></em>)
 107  *      <em>F<sub>d</sub></em> = <em>f</em>(<em>A<sub>s</sub></em>)
 108  *      <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>
 109  *      <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>
 110  *
 111  * <p>
 112  * The following factors will be used to discuss our extensions to
 113  * the blending equation in the Porter and Duff paper:
 114  *
 115  * <blockquote>
 116  * <table summary="layout">
 117  * <tr><th align=left>Factor&nbsp;&nbsp;<th align=left>Definition
 118  * <tr><td><em>C<sub>sr</sub></em> <td>one of the raw color components of the source pixel
 119  * <tr><td><em>C<sub>dr</sub></em> <td>one of the raw color components of the destination pixel
 120  * <tr><td><em>A<sub>ac</sub></em>  <td>the "extra" alpha component from the AlphaComposite instance
 121  * <tr><td><em>A<sub>sr</sub></em> <td>the raw alpha component of the source pixel
 122  * <tr><td><em>A<sub>dr</sub></em><td>the raw alpha component of the destination pixel
 123  * <tr><td><em>A<sub>df</sub></em> <td>the final alpha component stored in the destination
 124  * <tr><td><em>C<sub>df</sub></em> <td>the final raw color component stored in the destination
 125  * </table>
 126  *</blockquote>
 127  *
 128  * <h3>Preparing Inputs</h3>
 129  *
 130  * <p>
 131  * The <code>AlphaComposite</code> class defines an additional alpha
 132  * value that is applied to the source alpha.
 133  * This value is applied as if an implicit SRC_IN rule were first
 134  * applied to the source pixel against a pixel with the indicated
 135  * alpha by multiplying both the raw source alpha and the raw
 136  * source colors by the alpha in the <code>AlphaComposite</code>.
 137  * This leads to the following equation for producing the alpha
 138  * used in the Porter and Duff blending equation:
 139  *
 140  * <pre>
 141  *      <em>A<sub>s</sub></em> = <em>A<sub>sr</sub></em> * <em>A<sub>ac</sub></em> </pre>
 142  *
 143  * All of the raw source color components need to be multiplied
 144  * by the alpha in the <code>AlphaComposite</code> instance.
 145  * Additionally, if the source was not in premultiplied form
 146  * then the color components also need to be multiplied by the
 147  * source alpha.
 148  * Thus, the equation for producing the source color components
 149  * for the Porter and Duff equation depends on whether the source
 150  * pixels are premultiplied or not:
 151  *
 152  * <pre>
 153  *      <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)
 154  *      <em>C<sub>s</sub></em> = <em>C<sub>sr</sub></em> * <em>A<sub>ac</sub></em>           (if source is premultiplied) </pre>
 155  *
 156  * No adjustment needs to be made to the destination alpha:
 157  *
 158  * <pre>
 159  *      <em>A<sub>d</sub></em> = <em>A<sub>dr</sub></em> </pre>
 160  *
 161  * <p>
 162  * The destination color components need to be adjusted only if
 163  * they are not in premultiplied form:
 164  *
 165  * <pre>
 166  *      <em>C<sub>d</sub></em> = <em>C<sub>dr</sub></em> * <em>A<sub>d</sub></em>    (if destination is not premultiplied)
 167  *      <em>C<sub>d</sub></em> = <em>C<sub>dr</sub></em>         (if destination is premultiplied) </pre>
 168  *
 169  * <h3>Applying the Blending Equation</h3>
 170  *
 171  * <p>
 172  * The adjusted <em>A<sub>s</sub></em>, <em>A<sub>d</sub></em>,
 173  * <em>C<sub>s</sub></em>, and <em>C<sub>d</sub></em> are used in the standard
 174  * Porter and Duff equations to calculate the blending factors
 175  * <em>F<sub>s</sub></em> and <em>F<sub>d</sub></em> and then the resulting
 176  * premultiplied components <em>A<sub>r</sub></em> and <em>C<sub>r</sub></em>.
 177  *
 178  * <p>
 179  * <h3>Preparing Results</h3>
 180  *
 181  * <p>
 182  * The results only need to be adjusted if they are to be stored
 183  * back into a destination buffer that holds data that is not
 184  * premultiplied, using the following equations:
 185  *
 186  * <pre>
 187  *      <em>A<sub>df</sub></em> = <em>A<sub>r</sub></em>
 188  *      <em>C<sub>df</sub></em> = <em>C<sub>r</sub></em>                 (if dest is premultiplied)
 189  *      <em>C<sub>df</sub></em> = <em>C<sub>r</sub></em> / <em>A<sub>r</sub></em>            (if dest is not premultiplied) </pre>
 190  *
 191  * Note that since the division is undefined if the resulting alpha
 192  * is zero, the division in that case is omitted to avoid the "divide
 193  * by zero" and the color components are left as
 194  * all zeros.
 195  *
 196  * <p>
 197  * <h3>Performance Considerations</h3>
 198  *
 199  * <p>
 200  * For performance reasons, it is preferrable that
 201  * <code>Raster</code> objects passed to the <code>compose</code>
 202  * method of a {@link CompositeContext} object created by the
 203  * <code>AlphaComposite</code> class have premultiplied data.
 204  * If either the source <code>Raster</code>
 205  * or the destination <code>Raster</code>
 206  * is not premultiplied, however,
 207  * appropriate conversions are performed before and after the compositing
 208  * operation.
 209  *
 210  * <h3><a name="caveats">Implementation Caveats</a></h3>
 211  *
 212  * <ul>
 213  * <li>
 214  * Many sources, such as some of the opaque image types listed
 215  * in the <code>BufferedImage</code> class, do not store alpha values
 216  * for their pixels.  Such sources supply an alpha of 1.0 for
 217  * all of their pixels.
 218  *
 219  * <p>
 220  * <li>
 221  * Many destinations also have no place to store the alpha values
 222  * that result from the blending calculations performed by this class.
 223  * Such destinations thus implicitly discard the resulting
 224  * alpha values that this class produces.
 225  * It is recommended that such destinations should treat their stored
 226  * color values as non-premultiplied and divide the resulting color
 227  * values by the resulting alpha value before storing the color
 228  * values and discarding the alpha value.
 229  *
 230  * <p>
 231  * <li>
 232  * The accuracy of the results depends on the manner in which pixels
 233  * are stored in the destination.
 234  * An image format that provides at least 8 bits of storage per color
 235  * and alpha component is at least adequate for use as a destination
 236  * for a sequence of a few to a dozen compositing operations.
 237  * An image format with fewer than 8 bits of storage per component
 238  * is of limited use for just one or two compositing operations
 239  * before the rounding errors dominate the results.
 240  * An image format
 241  * that does not separately store
 242  * color components is not a
 243  * good candidate for any type of translucent blending.
 244  * For example, <code>BufferedImage.TYPE_BYTE_INDEXED</code>
 245  * should not be used as a destination for a blending operation
 246  * because every operation
 247  * can introduce large errors, due to
 248  * the need to choose a pixel from a limited palette to match the
 249  * results of the blending equations.
 250  *
 251  * <p>
 252  * <li>
 253  * Nearly all formats store pixels as discrete integers rather than
 254  * the floating point values used in the reference equations above.
 255  * The implementation can either scale the integer pixel
 256  * values into floating point values in the range 0.0 to 1.0 or
 257  * use slightly modified versions of the equations
 258  * that operate entirely in the integer domain and yet produce
 259  * analogous results to the reference equations.
 260  *
 261  * <p>
 262  * Typically the integer values are related to the floating point
 263  * values in such a way that the integer 0 is equated
 264  * to the floating point value 0.0 and the integer
 265  * 2^<em>n</em>-1 (where <em>n</em> is the number of bits
 266  * in the representation) is equated to 1.0.
 267  * For 8-bit representations, this means that 0x00
 268  * represents 0.0 and 0xff represents
 269  * 1.0.
 270  *
 271  * <p>
 272  * <li>
 273  * The internal implementation can approximate some of the equations
 274  * and it can also eliminate some steps to avoid unnecessary operations.
 275  * For example, consider a discrete integer image with non-premultiplied
 276  * alpha values that uses 8 bits per component for storage.
 277  * The stored values for a
 278  * nearly transparent darkened red might be:
 279  *
 280  * <pre>
 281  *    (A, R, G, B) = (0x01, 0xb0, 0x00, 0x00)</pre>
 282  *
 283  * <p>
 284  * If integer math were being used and this value were being
 285  * composited in
 286  * <a href="#SRC"><code>SRC</code></a>
 287  * mode with no extra alpha, then the math would
 288  * indicate that the results were (in integer format):
 289  *
 290  * <pre>
 291  *    (A, R, G, B) = (0x01, 0x01, 0x00, 0x00)</pre>
 292  *
 293  * <p>
 294  * Note that the intermediate values, which are always in premultiplied
 295  * form, would only allow the integer red component to be either 0x00
 296  * or 0x01.  When we try to store this result back into a destination
 297  * that is not premultiplied, dividing out the alpha will give us
 298  * very few choices for the non-premultiplied red value.
 299  * In this case an implementation that performs the math in integer
 300  * space without shortcuts is likely to end up with the final pixel
 301  * values of:
 302  *
 303  * <pre>
 304  *    (A, R, G, B) = (0x01, 0xff, 0x00, 0x00)</pre>
 305  *
 306  * <p>
 307  * (Note that 0x01 divided by 0x01 gives you 1.0, which is equivalent
 308  * to the value 0xff in an 8-bit storage format.)
 309  *
 310  * <p>
 311  * Alternately, an implementation that uses floating point math
 312  * might produce more accurate results and end up returning to the
 313  * original pixel value with little, if any, roundoff error.
 314  * Or, an implementation using integer math might decide that since
 315  * the equations boil down to a virtual NOP on the color values
 316  * if performed in a floating point space, it can transfer the
 317  * pixel untouched to the destination and avoid all the math entirely.
 318  *
 319  * <p>
 320  * These implementations all attempt to honor the
 321  * same equations, but use different tradeoffs of integer and
 322  * floating point math and reduced or full equations.
 323  * To account for such differences, it is probably best to
 324  * expect only that the premultiplied form of the results to
 325  * match between implementations and image formats.  In this
 326  * case both answers, expressed in premultiplied form would
 327  * equate to:
 328  *
 329  * <pre>
 330  *    (A, R, G, B) = (0x01, 0x01, 0x00, 0x00)</pre>
 331  *
 332  * <p>
 333  * and thus they would all match.
 334  *
 335  * <p>
 336  * <li>
 337  * Because of the technique of simplifying the equations for
 338  * calculation efficiency, some implementations might perform
 339  * differently when encountering result alpha values of 0.0
 340  * on a non-premultiplied destination.
 341  * Note that the simplification of removing the divide by alpha
 342  * in the case of the SRC rule is technically not valid if the
 343  * denominator (alpha) is 0.
 344  * But, since the results should only be expected to be accurate
 345  * when viewed in premultiplied form, a resulting alpha of 0
 346  * essentially renders the resulting color components irrelevant
 347  * and so exact behavior in this case should not be expected.
 348  * </ul>
 349  * @see Composite
 350  * @see CompositeContext
 351  */
 352 
 353 public final class AlphaComposite implements Composite {
 354     /**
 355      * Both the color and the alpha of the destination are cleared
 356      * (Porter-Duff Clear rule).
 357      * Neither the source nor the destination is used as input.
 358      *<p>
 359      * <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = 0, thus:
 360      *<pre>
 361      *  <em>A<sub>r</sub></em> = 0
 362      *  <em>C<sub>r</sub></em> = 0
 363      *</pre>
 364      */
 365     @Native public static final int     CLEAR           = 1;
 366 
 367     /**
 368      * The source is copied to the destination
 369      * (Porter-Duff Source rule).
 370      * The destination is not used as input.
 371      *<p>
 372      * <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = 0, thus:
 373      *<pre>
 374      *  <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>
 375      *  <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>
 376      *</pre>
 377      */
 378     @Native public static final int     SRC             = 2;
 379 
 380     /**
 381      * The destination is left untouched
 382      * (Porter-Duff Destination rule).
 383      *<p>
 384      * <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = 1, thus:
 385      *<pre>
 386      *  <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>
 387      *  <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>
 388      *</pre>
 389      * @since 1.4
 390      */
 391     @Native public static final int     DST             = 9;
 392     // Note that DST was added in 1.4 so it is numbered out of order...
 393 
 394     /**
 395      * The source is composited over the destination
 396      * (Porter-Duff Source Over Destination rule).
 397      *<p>
 398      * <em>F<sub>s</sub></em> = 1 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:
 399      *<pre>
 400      *  <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>)
 401      *  <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>)
 402      *</pre>
 403      */
 404     @Native public static final int     SRC_OVER        = 3;
 405 
 406     /**
 407      * The destination is composited over the source and
 408      * the result replaces the destination
 409      * (Porter-Duff Destination Over Source rule).
 410      *<p>
 411      * <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = 1, thus:
 412      *<pre>
 413      *  <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>
 414      *  <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>
 415      *</pre>
 416      */
 417     @Native public static final int     DST_OVER        = 4;
 418 
 419     /**
 420      * The part of the source lying inside of the destination replaces
 421      * the destination
 422      * (Porter-Duff Source In Destination rule).
 423      *<p>
 424      * <em>F<sub>s</sub></em> = <em>A<sub>d</sub></em> and <em>F<sub>d</sub></em> = 0, thus:
 425      *<pre>
 426      *  <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*<em>A<sub>d</sub></em>
 427      *  <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*<em>A<sub>d</sub></em>
 428      *</pre>
 429      */
 430     @Native public static final int     SRC_IN          = 5;
 431 
 432     /**
 433      * The part of the destination lying inside of the source
 434      * replaces the destination
 435      * (Porter-Duff Destination In Source rule).
 436      *<p>
 437      * <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = <em>A<sub>s</sub></em>, thus:
 438      *<pre>
 439      *  <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>*<em>A<sub>s</sub></em>
 440      *  <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>*<em>A<sub>s</sub></em>
 441      *</pre>
 442      */
 443     @Native public static final int     DST_IN          = 6;
 444 
 445     /**
 446      * The part of the source lying outside of the destination
 447      * replaces the destination
 448      * (Porter-Duff Source Held Out By Destination rule).
 449      *<p>
 450      * <em>F<sub>s</sub></em> = (1-<em>A<sub>d</sub></em>) and <em>F<sub>d</sub></em> = 0, thus:
 451      *<pre>
 452      *  <em>A<sub>r</sub></em> = <em>A<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>)
 453      *  <em>C<sub>r</sub></em> = <em>C<sub>s</sub></em>*(1-<em>A<sub>d</sub></em>)
 454      *</pre>
 455      */
 456     @Native public static final int     SRC_OUT         = 7;
 457 
 458     /**
 459      * The part of the destination lying outside of the source
 460      * replaces the destination
 461      * (Porter-Duff Destination Held Out By Source rule).
 462      *<p>
 463      * <em>F<sub>s</sub></em> = 0 and <em>F<sub>d</sub></em> = (1-<em>A<sub>s</sub></em>), thus:
 464      *<pre>
 465      *  <em>A<sub>r</sub></em> = <em>A<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)
 466      *  <em>C<sub>r</sub></em> = <em>C<sub>d</sub></em>*(1-<em>A<sub>s</sub></em>)
 467      *</pre>
 468      */
 469     @Native public static final int     DST_OUT         = 8;
 470 
 471     // Rule 9 is DST which is defined above where it fits into the
 472     // list logically, rather than numerically
 473     //
 474     // public static final int  DST             = 9;
 475 
 476     /**
 477      * The part of the source lying inside of the destination
 478      * is composited onto the destination
 479      * (Porter-Duff Source Atop Destination rule).
 480      *<p>
 481      * <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:
 482      *<pre>
 483      *  <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>
 484      *  <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>)
 485      *</pre>
 486      * @since 1.4
 487      */
 488     @Native public static final int     SRC_ATOP        = 10;
 489 
 490     /**
 491      * The part of the destination lying inside of the source
 492      * is composited over the source and replaces the destination
 493      * (Porter-Duff Destination Atop Source rule).
 494      *<p>
 495      * <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:
 496      *<pre>
 497      *  <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>
 498      *  <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>
 499      *</pre>
 500      * @since 1.4
 501      */
 502     @Native public static final int     DST_ATOP        = 11;
 503 
 504     /**
 505      * The part of the source that lies outside of the destination
 506      * is combined with the part of the destination that lies outside
 507      * of the source
 508      * (Porter-Duff Source Xor Destination rule).
 509      *<p>
 510      * <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:
 511      *<pre>
 512      *  <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>)
 513      *  <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>)
 514      *</pre>
 515      * @since 1.4
 516      */
 517     @Native public static final int     XOR             = 12;
 518 
 519     /**
 520      * <code>AlphaComposite</code> object that implements the opaque CLEAR rule
 521      * with an alpha of 1.0f.
 522      * @see #CLEAR
 523      */
 524     public static final AlphaComposite Clear    = new AlphaComposite(CLEAR);
 525 
 526     /**
 527      * <code>AlphaComposite</code> object that implements the opaque SRC rule
 528      * with an alpha of 1.0f.
 529      * @see #SRC
 530      */
 531     public static final AlphaComposite Src      = new AlphaComposite(SRC);
 532 
 533     /**
 534      * <code>AlphaComposite</code> object that implements the opaque DST rule
 535      * with an alpha of 1.0f.
 536      * @see #DST
 537      * @since 1.4
 538      */
 539     public static final AlphaComposite Dst      = new AlphaComposite(DST);
 540 
 541     /**
 542      * <code>AlphaComposite</code> object that implements the opaque SRC_OVER rule
 543      * with an alpha of 1.0f.
 544      * @see #SRC_OVER
 545      */
 546     public static final AlphaComposite SrcOver  = new AlphaComposite(SRC_OVER);
 547 
 548     /**
 549      * <code>AlphaComposite</code> object that implements the opaque DST_OVER rule
 550      * with an alpha of 1.0f.
 551      * @see #DST_OVER
 552      */
 553     public static final AlphaComposite DstOver  = new AlphaComposite(DST_OVER);
 554 
 555     /**
 556      * <code>AlphaComposite</code> object that implements the opaque SRC_IN rule
 557      * with an alpha of 1.0f.
 558      * @see #SRC_IN
 559      */
 560     public static final AlphaComposite SrcIn    = new AlphaComposite(SRC_IN);
 561 
 562     /**
 563      * <code>AlphaComposite</code> object that implements the opaque DST_IN rule
 564      * with an alpha of 1.0f.
 565      * @see #DST_IN
 566      */
 567     public static final AlphaComposite DstIn    = new AlphaComposite(DST_IN);
 568 
 569     /**
 570      * <code>AlphaComposite</code> object that implements the opaque SRC_OUT rule
 571      * with an alpha of 1.0f.
 572      * @see #SRC_OUT
 573      */
 574     public static final AlphaComposite SrcOut   = new AlphaComposite(SRC_OUT);
 575 
 576     /**
 577      * <code>AlphaComposite</code> object that implements the opaque DST_OUT rule
 578      * with an alpha of 1.0f.
 579      * @see #DST_OUT
 580      */
 581     public static final AlphaComposite DstOut   = new AlphaComposite(DST_OUT);
 582 
 583     /**
 584      * <code>AlphaComposite</code> object that implements the opaque SRC_ATOP rule
 585      * with an alpha of 1.0f.
 586      * @see #SRC_ATOP
 587      * @since 1.4
 588      */
 589     public static final AlphaComposite SrcAtop  = new AlphaComposite(SRC_ATOP);
 590 
 591     /**
 592      * <code>AlphaComposite</code> object that implements the opaque DST_ATOP rule
 593      * with an alpha of 1.0f.
 594      * @see #DST_ATOP
 595      * @since 1.4
 596      */
 597     public static final AlphaComposite DstAtop  = new AlphaComposite(DST_ATOP);
 598 
 599     /**
 600      * <code>AlphaComposite</code> object that implements the opaque XOR rule
 601      * with an alpha of 1.0f.
 602      * @see #XOR
 603      * @since 1.4
 604      */
 605     public static final AlphaComposite Xor      = new AlphaComposite(XOR);
 606 
 607     @Native private static final int MIN_RULE = CLEAR;
 608     @Native private static final int MAX_RULE = XOR;
 609 
 610     float extraAlpha;
 611     int rule;
 612 
 613     private AlphaComposite(int rule) {
 614         this(rule, 1.0f);
 615     }
 616 
 617     private AlphaComposite(int rule, float alpha) {
 618         if (rule < MIN_RULE || rule > MAX_RULE) {
 619             throw new IllegalArgumentException("unknown composite rule");
 620         }
 621         if (alpha >= 0.0f && alpha <= 1.0f) {
 622             this.rule = rule;
 623             this.extraAlpha = alpha;
 624         } else {
 625             throw new IllegalArgumentException("alpha value out of range");
 626         }
 627     }
 628 
 629     /**
 630      * Creates an <code>AlphaComposite</code> object with the specified rule.
 631      * @param rule the compositing rule
 632      * @throws IllegalArgumentException if <code>rule</code> is not one of
 633      *         the following:  {@link #CLEAR}, {@link #SRC}, {@link #DST},
 634      *         {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},
 635      *         {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},
 636      *         {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}
 637      */
 638     public static AlphaComposite getInstance(int rule) {
 639         switch (rule) {
 640         case CLEAR:
 641             return Clear;
 642         case SRC:
 643             return Src;
 644         case DST:
 645             return Dst;
 646         case SRC_OVER:
 647             return SrcOver;
 648         case DST_OVER:
 649             return DstOver;
 650         case SRC_IN:
 651             return SrcIn;
 652         case DST_IN:
 653             return DstIn;
 654         case SRC_OUT:
 655             return SrcOut;
 656         case DST_OUT:
 657             return DstOut;
 658         case SRC_ATOP:
 659             return SrcAtop;
 660         case DST_ATOP:
 661             return DstAtop;
 662         case XOR:
 663             return Xor;
 664         default:
 665             throw new IllegalArgumentException("unknown composite rule");
 666         }
 667     }
 668 
 669     /**
 670      * Creates an <code>AlphaComposite</code> object with the specified rule and
 671      * the constant alpha to multiply with the alpha of the source.
 672      * The source is multiplied with the specified alpha before being composited
 673      * with the destination.
 674      * @param rule the compositing rule
 675      * @param alpha the constant alpha to be multiplied with the alpha of
 676      * the source. <code>alpha</code> must be a floating point number in the
 677      * inclusive range [0.0,&nbsp;1.0].
 678      * @throws IllegalArgumentException if
 679      *         <code>alpha</code> is less than 0.0 or greater than 1.0, or if
 680      *         <code>rule</code> is not one of
 681      *         the following:  {@link #CLEAR}, {@link #SRC}, {@link #DST},
 682      *         {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},
 683      *         {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},
 684      *         {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}
 685      */
 686     public static AlphaComposite getInstance(int rule, float alpha) {
 687         if (alpha == 1.0f) {
 688             return getInstance(rule);
 689         }
 690         return new AlphaComposite(rule, alpha);
 691     }
 692 
 693     /**
 694      * Creates a context for the compositing operation.
 695      * The context contains state that is used in performing
 696      * the compositing operation.
 697      * @param srcColorModel  the {@link ColorModel} of the source
 698      * @param dstColorModel  the <code>ColorModel</code> of the destination
 699      * @return the <code>CompositeContext</code> object to be used to perform
 700      * compositing operations.
 701      */
 702     public CompositeContext createContext(ColorModel srcColorModel,
 703                                           ColorModel dstColorModel,
 704                                           RenderingHints hints) {
 705         return new SunCompositeContext(this, srcColorModel, dstColorModel);
 706     }
 707 
 708     /**
 709      * Returns the alpha value of this <code>AlphaComposite</code>.  If this
 710      * <code>AlphaComposite</code> does not have an alpha value, 1.0 is returned.
 711      * @return the alpha value of this <code>AlphaComposite</code>.
 712      */
 713     public float getAlpha() {
 714         return extraAlpha;
 715     }
 716 
 717     /**
 718      * Returns the compositing rule of this <code>AlphaComposite</code>.
 719      * @return the compositing rule of this <code>AlphaComposite</code>.
 720      */
 721     public int getRule() {
 722         return rule;
 723     }
 724 
 725     /**
 726      * Returns a similar <code>AlphaComposite</code> object that uses
 727      * the specified compositing rule.
 728      * If this object already uses the specified compositing rule,
 729      * this object is returned.
 730      * @return an <code>AlphaComposite</code> object derived from
 731      * this object that uses the specified compositing rule.
 732      * @param rule the compositing rule
 733      * @throws IllegalArgumentException if
 734      *         <code>rule</code> is not one of
 735      *         the following:  {@link #CLEAR}, {@link #SRC}, {@link #DST},
 736      *         {@link #SRC_OVER}, {@link #DST_OVER}, {@link #SRC_IN},
 737      *         {@link #DST_IN}, {@link #SRC_OUT}, {@link #DST_OUT},
 738      *         {@link #SRC_ATOP}, {@link #DST_ATOP}, or {@link #XOR}
 739      * @since 1.6
 740      */
 741     public AlphaComposite derive(int rule) {
 742         return (this.rule == rule)
 743             ? this
 744             : getInstance(rule, this.extraAlpha);
 745     }
 746 
 747     /**
 748      * Returns a similar <code>AlphaComposite</code> object that uses
 749      * the specified alpha value.
 750      * If this object already has the specified alpha value,
 751      * this object is returned.
 752      * @return an <code>AlphaComposite</code> object derived from
 753      * this object that uses the specified alpha value.
 754      * @param alpha the constant alpha to be multiplied with the alpha of
 755      * the source. <code>alpha</code> must be a floating point number in the
 756      * inclusive range [0.0,&nbsp;1.0].
 757      * @throws IllegalArgumentException if
 758      *         <code>alpha</code> is less than 0.0 or greater than 1.0
 759      * @since 1.6
 760      */
 761     public AlphaComposite derive(float alpha) {
 762         return (this.extraAlpha == alpha)
 763             ? this
 764             : getInstance(this.rule, alpha);
 765     }
 766 
 767     /**
 768      * Returns the hashcode for this composite.
 769      * @return      a hash code for this composite.
 770      */
 771     public int hashCode() {
 772         return (Float.floatToIntBits(extraAlpha) * 31 + rule);
 773     }
 774 
 775     /**
 776      * Determines whether the specified object is equal to this
 777      * <code>AlphaComposite</code>.
 778      * <p>
 779      * The result is <code>true</code> if and only if
 780      * the argument is not <code>null</code> and is an
 781      * <code>AlphaComposite</code> object that has the same
 782      * compositing rule and alpha value as this object.
 783      *
 784      * @param obj the <code>Object</code> to test for equality
 785      * @return <code>true</code> if <code>obj</code> equals this
 786      * <code>AlphaComposite</code>; <code>false</code> otherwise.
 787      */
 788     public boolean equals(Object obj) {
 789         if (!(obj instanceof AlphaComposite)) {
 790             return false;
 791         }
 792 
 793         AlphaComposite ac = (AlphaComposite) obj;
 794 
 795         if (rule != ac.rule) {
 796             return false;
 797         }
 798 
 799         if (extraAlpha != ac.extraAlpha) {
 800             return false;
 801         }
 802 
 803         return true;
 804     }
 805 
 806 }