home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 9.8 KB | 341 lines |
- /*
- * @(#)AlphaComposite.java 1.25 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- package java.awt;
-
- import java.awt.image.ColorModel;
-
- /**
- * This class implements the basic alpha compositing rules for combining
- * source and destination pixels to achieve blending and transparency
- * effects with graphics and images.
- * The rules implemented by this class are a subset of the Porter-Duff
- * rules described in
- * T. Porter and T. Duff, "Compositing Digital Images", SIGGRAPH 84,
- * 253-259.
- *<p>
- * If any input does not have an alpha channel,
- * an alpha value of 1.0 (completely opaque) is assumed for all pixels.
- *<p>
- * A constant alpha value can also be specified to be
- * multiplied with the alpha value of the source pixels.
- * <p>
- * The following abbreviations are used in the description of the rules:
- * <li>Cs = one of the Color components of the Source Pixel.
- * <li>Cd = one of the Color components of the Destination Pixel.
- * <li>As = Alpha component of the Source Pixel.
- * <li>Ad = Alpha component of the Destination Pixel.
- * <li>Fs = fraction of the Source Pixel which contributes to the output.
- * <li>Fd = fraction of the input Destination Pixel which contributes to the output.
- *<p>
- * The color and alpha components produced by the compositing operation are
- * calculated as follows:
- *<pre>
- * Cd = Cs*Fs + Cd*Fd
- * Ad = As*Fs + Ad*Fd
- *</pre>
- * where Fs and Fd are specified by each rule. The above equations assume
- * that both source and destination pixels have the color components
- * premultiplied by the alpha component. Similarly, the equations expressed
- * in the definitions of compositing rules below assume premultiplied alpha.
- *<p>
- * For performance reasons, it is
- * preferable that Tiles passed to the compose method of a CompositeContext
- * object created by the AlphaComposite class have premultiplied data.
- * However, if either source or destination Tiles are not premultiplied,
- * appropriate conversions will be performed before and after the compositing
- * operation.
- *<p>
- * The resulting alpha of the compositing operation is stored in
- * the destination if the destination has an alpha channel.
- * Otherwise, the resulting color is divided by the resulting
- * alpha before being stored in the destination and the alpha is discarded
- * (if the alpha value is 0.0, the color values are set to 0.0).
- * @see Composite
- * @see CompositeContext
- * @version 10 Feb 1997
- */
-
-
- public final class AlphaComposite implements Composite {
- /**
- * Porter-Duff Clear rule.
- * Both the color and the alpha of destination are cleared.
- * Neither the source nor the destination is used as input.
- *<p>
- * Fs = 0 and Fd = 0, thus:
- *<pre>
- * Cd = 0
- * Ad = 0
- *</pre>
- *
- */
- public static final int CLEAR = 1;
-
- /**
- * Porter-Duff Source rule.
- * The source is copied to the destination.
- * The destination is not used as input.
- *<p>
- * Fs = 1 and Fd = 0, thus:
- *<pre>
- * Cd = Cs
- * Ad = As
- *</pre>
- */
- public static final int SRC = 2;
-
- /**
- * Porter-Duff Source Over Destination rule.
- * The source is composited over the destination.
- *<p>
- * Fs = 1 and Fd = (1-As), thus:
- *<pre>
- * Cd = Cs + Cd*(1-As)
- * Ad = As + Ad*(1-As)
- *</pre>
- */
- public static final int SRC_OVER = 3;
-
- /**
- * Porter-Duff Destination Over Source rule.
- * The destination is composited over the source and
- * the result replaces the destination.
- *<p>
- * Fs = (1-Ad) and Fd = 1, thus:
- *<pre>
- * Cd = Cs*(1-Ad) + Cd
- * Ad = As*(1-Ad) + Ad
- *</pre>
- */
- public static final int DST_OVER = 4;
-
- /**
- * Porter-Duff Source In Destination rule.
- * The part of the source lying inside of the destination replaces
- * the destination.
- *<p>
- * Fs = Ad and Fd = 0, thus:
- *<pre>
- * Cd = Cs*Ad
- * Ad = As*Ad
- *</pre>
- */
- public static final int SRC_IN = 5;
-
- /**
- * Porter-Duff Destination In Source rule.
- * The part of the destination lying inside of the source
- * replaces the destination.
- *<p>
- * Fs = 0 and Fd = As, thus:
- *<pre>
- * Cd = Cd*As
- * Ad = Ad*As
- *</pre>
- */
- public static final int DST_IN = 6;
-
- /**
- * Porter-Duff Source Held Out By Destination rule.
- * The part of the source lying outside of the destination
- * replaces the destination.
- *<p>
- * Fs = (1-Ad) and Fd = 0, thus:
- *<pre>
- * Cd = Cs*(1-Ad)
- * Ad = As*(1-Ad)
- *</pre>
- */
- public static final int SRC_OUT = 7;
-
- /**
- * Porter-Duff Destination Held Out By Source rule.
- * The part of the destination lying outside of the source
- * replaces the destination.
- *<p>
- * Fs = 0 and Fd = (1-As), thus:
- *<pre>
- * Cd = Cd*(1-As)
- * Ad = Ad*(1-As)
- *</pre>
- */
- public static final int DST_OUT = 8;
-
- /**
- * AlphaComposite object which implements the opaque CLEAR rule.
- * @see #CLEAR
- */
- public static final AlphaComposite Clear = new AlphaComposite(CLEAR);
-
- /**
- * AlphaComposite object which implements the opaque SRC rule.
- * @see #SRC
- */
- public static final AlphaComposite Src = new AlphaComposite(SRC);
-
- /**
- * AlphaComposite object which implements the opaque SRC_OVER rule.
- * @see #SRC_OVER
- */
- public static final AlphaComposite SrcOver = new AlphaComposite(SRC_OVER);
-
- /**
- * AlphaComposite object which implements the opaque DST_OVER rule.
- * @see #DST_OVER
- */
- public static final AlphaComposite DstOver = new AlphaComposite(DST_OVER);
-
- /**
- * AlphaComposite object which implements the opaque SRC_IN rule.
- * @see #SRC_IN
- */
- public static final AlphaComposite SrcIn = new AlphaComposite(SRC_IN);
-
- /**
- * AlphaComposite object which implements the opaque DST_IN rule.
- * @see #DST_IN
- */
- public static final AlphaComposite DstIn = new AlphaComposite(DST_IN);
-
- /**
- * AlphaComposite object which implements the opaque SRC_OUT rule.
- * @see #SRC_OUT
- */
- public static final AlphaComposite SrcOut = new AlphaComposite(SRC_OUT);
-
- /**
- * AlphaComposite object which implements the opaque DST_OUT rule.
- * @see #DST_OUT
- */
- public static final AlphaComposite DstOut = new AlphaComposite(DST_OUT);
-
- private static final int MIN_RULE = CLEAR;
- private static final int MAX_RULE = DST_OUT;
-
- float extraAlpha;
- int rule;
-
- private AlphaComposite(int rule) {
- this(rule, 1.0f);
- }
-
- private AlphaComposite(int rule, float alpha) {
- if (alpha < 0.0f || alpha > 1.0f) {
- throw new IllegalArgumentException("alpha value out of range");
- }
- if (rule < MIN_RULE || rule > MAX_RULE) {
- throw new IllegalArgumentException("unknown composite rule");
- }
- this.rule = rule;
- this.extraAlpha = alpha;
- }
-
- /**
- * Creates an AlphaComposite object with the given rule.
- * @param rule Rule for the composition.
- */
- public static AlphaComposite getInstance(int rule) {
- switch (rule) {
- case CLEAR:
- return Clear;
- case SRC:
- return Src;
- case SRC_OVER:
- return SrcOver;
- case DST_OVER:
- return DstOver;
- case SRC_IN:
- return SrcIn;
- case DST_IN:
- return DstIn;
- case SRC_OUT:
- return SrcOut;
- case DST_OUT:
- return DstOut;
- default:
- throw new IllegalArgumentException("unknown composite rule");
- }
- }
-
- /**
- * Creates an AlphaComposite object with the given rule and
- * the constant alpha to multiply with the alpha of the source.
- * The source is multiplied with the given alpha before being composited
- * with the destination.
- * @param rule Rule for the composition.
- * @param alpha The constant alpha to be multiplied with the alpha of
- * the source. Alpha must be a floating point number in the inclusive
- * range [0.0, 1.0].
- */
- public static AlphaComposite getInstance(int rule, float alpha) {
- if (alpha == 1.0f) {
- return getInstance(rule);
- }
- return new AlphaComposite(rule, alpha);
- }
-
- /**
- * Creates a context for the compositing operation.
- * The context contains state that is used in performing
- * the compositing operation.
- * @param srcColorModel The ColorModel of the source.
- * @param dstColorModel The ColorModel of the destination.
- * @return The CompositeContext object to be used to perform
- * compositing operations.
- */
- public CompositeContext createContext(ColorModel srcColorModel,
- ColorModel dstColorModel) {
- return new AlphaCompositeContext(srcColorModel, dstColorModel,
- rule, extraAlpha);
- }
-
- /**
- * Returns the additional alpha value that was given when this
- * AlphaComposiste instance was created. If this instance was
- * created without the addtional alpha value, the value
- * 1.0 is returned.
- */
- public float getAlpha() {
- return extraAlpha;
- }
-
- /**
- * Returns the compositing rule that was specified when this
- * AlphaComposiste instance was created.
- */
- public int getRule() {
- return rule;
- }
-
- public boolean equals(Object obj) {
- if (!(obj instanceof AlphaComposite)) {
- return false;
- }
-
- AlphaComposite ac = (AlphaComposite) obj;
-
- if (rule != ac.rule) {
- return false;
- }
-
- if (extraAlpha != ac.extraAlpha) {
- return false;
- }
-
- return true;
- }
-
- }
-