001: /*
002: * $RCSfile: OverlayDescriptor.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:57:42 $
010: * $State: Exp $
011: */
012: package javax.media.jai.operator;
013:
014: import java.awt.RenderingHints;
015: import java.awt.image.RenderedImage;
016: import java.awt.image.SampleModel;
017: import java.awt.image.renderable.ParameterBlock;
018: import java.awt.image.renderable.RenderableImage;
019: import javax.media.jai.JAI;
020: import javax.media.jai.OperationDescriptorImpl;
021: import javax.media.jai.ParameterBlockJAI;
022: import javax.media.jai.RenderableOp;
023: import javax.media.jai.RenderedOp;
024: import javax.media.jai.registry.RenderableRegistryMode;
025: import javax.media.jai.registry.RenderedRegistryMode;
026:
027: /**
028: * An <code>OperationDescriptor</code> describing the "Overlay" operation.
029: *
030: * <p> The Overlay operation takes two rendered or renderable source
031: * images, and overlays the second source image on top of the first
032: * source image. No additional parameters are required.
033: *
034: * <p> The two source images must have the same data type and number
035: * of bands. However, their <code>SampleModel</code> types may
036: * differ. The destination image will always have the same bounding
037: * rectangle as the first source image, that is, the image on the
038: * bottom, and the same data type and number of bands as the two
039: * sources. In case the two sources don't intersect, the destination
040: * will be the same as the first source.
041: *
042: * <p> The destination pixel values are defined by the pseudocode:
043: * <pre>
044: * if (srcs[1] contains the point (x, y)) {
045: * dst[x][y][b] = srcs[1][x][y][b];
046: * } else {
047: * dst[x][y][b] = srcs[0][x][y][b];
048: * }
049: * </pre>
050: *
051: * <p><table border=1>
052: * <caption>Resource List</caption>
053: * <tr><th>Name</th> <th>Value</th></tr>
054: * <tr><td>GlobalName</td> <td>Overlay</td></tr>
055: * <tr><td>LocalName</td> <td>Overlay</td></tr>
056: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
057: * <tr><td>Description</td> <td>Overlays one image on top of
058: * another.</td></tr>
059: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/OverlayDescriptor.html</td></tr>
060: * <tr><td>Version</td> <td>1.0</td></tr>
061: * </table></p>
062: *
063: * <p> No parameters are needed for this operation.
064: *
065: * @see javax.media.jai.OperationDescriptor */
066: public class OverlayDescriptor extends OperationDescriptorImpl {
067:
068: /**
069: * The resource strings that provide the general documentation
070: * and specify the parameter list for this operation.
071: */
072: private static final String[][] resources = {
073: { "GlobalName", "Overlay" },
074: { "LocalName", "Overlay" },
075: { "Vendor", "com.sun.media.jai" },
076: { "Description", JaiI18N.getString("OverlayDescriptor0") },
077: {
078: "DocURL",
079: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/OverlayDescriptor.html" },
080: { "Version", JaiI18N.getString("DescriptorVersion") } };
081:
082: private static final String[] supportedModes = { "rendered",
083: "renderable" };
084:
085: /** Constructor. */
086: public OverlayDescriptor() {
087: super (resources, supportedModes, 2, null, null, null, null);
088: }
089:
090: /**
091: * Validates the input sources.
092: *
093: * <p> In addition to the standard checks performed by the
094: * superclass method, this method checks that the source image
095: * <code>SampleModel</code>s have the same number of bands and
096: * transfer types.
097: */
098: protected boolean validateSources(String modeName,
099: ParameterBlock args, StringBuffer msg) {
100: if (!super .validateSources(modeName, args, msg)) {
101: return false;
102: }
103:
104: if (!modeName.equalsIgnoreCase("rendered"))
105: return true;
106:
107: RenderedImage src1 = args.getRenderedSource(0);
108: RenderedImage src2 = args.getRenderedSource(1);
109:
110: SampleModel s1sm = src1.getSampleModel();
111: SampleModel s2sm = src2.getSampleModel();
112:
113: if (s1sm.getNumBands() != s2sm.getNumBands()
114: || s1sm.getTransferType() != s2sm.getTransferType()) {
115: msg.append(getName() + " "
116: + JaiI18N.getString("OverlayDescriptor1"));
117: return false;
118: }
119:
120: return true;
121: }
122:
123: /**
124: * Overlays one image on top of another.
125: *
126: * <p>Creates a <code>ParameterBlockJAI</code> from all
127: * supplied arguments except <code>hints</code> and invokes
128: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
129: *
130: * @see JAI
131: * @see ParameterBlockJAI
132: * @see RenderedOp
133: *
134: * @param source0 <code>RenderedImage</code> source 0.
135: * @param source1 <code>RenderedImage</code> source 1.
136: * @param hints The <code>RenderingHints</code> to use.
137: * May be <code>null</code>.
138: * @return The <code>RenderedOp</code> destination.
139: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
140: * @throws IllegalArgumentException if <code>source1</code> is <code>null</code>.
141: */
142: public static RenderedOp create(RenderedImage source0,
143: RenderedImage source1, RenderingHints hints) {
144: ParameterBlockJAI pb = new ParameterBlockJAI("Overlay",
145: RenderedRegistryMode.MODE_NAME);
146:
147: pb.setSource("source0", source0);
148: pb.setSource("source1", source1);
149:
150: return JAI.create("Overlay", pb, hints);
151: }
152:
153: /**
154: * Overlays one image on top of another.
155: *
156: * <p>Creates a <code>ParameterBlockJAI</code> from all
157: * supplied arguments except <code>hints</code> and invokes
158: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
159: *
160: * @see JAI
161: * @see ParameterBlockJAI
162: * @see RenderableOp
163: *
164: * @param source0 <code>RenderableImage</code> source 0.
165: * @param source1 <code>RenderableImage</code> source 1.
166: * @param hints The <code>RenderingHints</code> to use.
167: * May be <code>null</code>.
168: * @return The <code>RenderableOp</code> destination.
169: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
170: * @throws IllegalArgumentException if <code>source1</code> is <code>null</code>.
171: */
172: public static RenderableOp createRenderable(
173: RenderableImage source0, RenderableImage source1,
174: RenderingHints hints) {
175: ParameterBlockJAI pb = new ParameterBlockJAI("Overlay",
176: RenderableRegistryMode.MODE_NAME);
177:
178: pb.setSource("source0", source0);
179: pb.setSource("source1", source1);
180:
181: return JAI.createRenderable("Overlay", pb, hints);
182: }
183: }
|