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