001: /*
002: * $RCSfile: AddCollectionDescriptor.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:28 $
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.renderable.ParameterBlock;
017: import java.awt.image.renderable.RenderableImage;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import javax.media.jai.JAI;
021: import javax.media.jai.OperationDescriptorImpl;
022: import javax.media.jai.ParameterBlockJAI;
023: import javax.media.jai.ParameterListDescriptor;
024: import javax.media.jai.RenderableOp;
025: import javax.media.jai.RenderedOp;
026: import javax.media.jai.registry.RenderableRegistryMode;
027: import javax.media.jai.registry.RenderedRegistryMode;
028:
029: /**
030: * An <code>OperationDescriptor</code> describing the
031: * "AddCollection" operation.
032: *
033: * <p> The AddCollection operation takes a collection of rendered
034: * or renderable source images, and adds every set of pixels, one from
035: * each source image of the corresponding position and band. No
036: * additional parameters are required.
037: *
038: * <p> There is no restriction on the actual class type used to
039: * represent the source collection, but all elements of the collection
040: * must be instances of <code>RenderedImage</code> or
041: * <code>RenderableImage</code> depending on the mode. The number of
042: * images in the collection may vary from 2 to n. The source images
043: * may have different numbers of bands and data types.
044: *
045: * <p> By default, the destination image bounds are the intersection
046: * of all of the source image bounds. If any of the two sources are
047: * completely disjoint, the destination will have a width and a height
048: * of 0. The number of bands of the destination image is equal to
049: * the minimum number of bands of all the sources, and the data type is
050: * the biggest data type of all the sources. If the result of the
051: * operation underflows/overflows the minimum/maximum value supported
052: * by the destination data type, then it will be clamped to the
053: * minimum/maximum value respectively.
054: *
055: * <p> The destination pixel values are defined by the pseudocode:
056: * <pre>
057: * dst[x][y][b] = 0;
058: * for (int i = 0; i < numSources; i++) {
059: * dst[x][y][b] += srcs[i][x][y][b];
060: * }
061: * </pre>
062: *
063: * <p><table border=1>
064: * <caption>Resource List</caption>
065: * <tr><th>Name</th> <th>Value</th></tr>
066: * <tr><td>GlobalName</td> <td>AddCollection</td></tr>
067: * <tr><td>LocalName</td> <td>AddCollection</td></tr>
068: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
069: * <tr><td>Description</td> <td>Adds a collection of rendered images.</td></tr>
070: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/AddCollectionDescriptor.html</td></tr>
071: * <tr><td>Version</td> <td>1.0</td></tr>
072: * </table></p>
073: *
074: * <p> No parameters are needed for this operation.
075: *
076: * @see java.awt.image.RenderedImage
077: * @see java.awt.image.renderable.RenderableImage
078: * @see java.util.Collection
079: * @see javax.media.jai.OperationDescriptor
080: */
081: public class AddCollectionDescriptor extends OperationDescriptorImpl {
082:
083: /**
084: * The resource strings that provide the general documentation
085: * and specify the parameter list for this operation.
086: */
087: private static final String[][] resources = {
088: { "GlobalName", "AddCollection" },
089: { "LocalName", "AddCollection" },
090: { "Vendor", "com.sun.media.jai" },
091: { "Description",
092: JaiI18N.getString("AddCollectionDescriptor0") },
093: {
094: "DocURL",
095: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/AddCollectionDescriptor.html" },
096: { "Version", JaiI18N.getString("DescriptorVersion") } };
097:
098: /** The source class list for this operation. */
099: private static final Class[][] sourceClasses = {
100: { java.util.Collection.class },
101: { java.util.Collection.class } };
102:
103: private static final String[] supportedModes = { "rendered",
104: "renderable" };
105:
106: /** Constructor. */
107: public AddCollectionDescriptor() {
108: super (resources, supportedModes, null, sourceClasses,
109: (ParameterListDescriptor) null);
110: }
111:
112: /** Validates input source collection. */
113: protected boolean validateSources(String modeName,
114: ParameterBlock args, StringBuffer msg) {
115: if (!super .validateSources(modeName, args, msg)) {
116: return false;
117: }
118:
119: Collection col = (Collection) args.getSource(0);
120:
121: if (col.size() < 2) {
122: msg.append(getName() + " "
123: + JaiI18N.getString("AddCollectionDescriptor1"));
124: return false;
125: }
126:
127: Iterator iter = col.iterator();
128: if (modeName.equalsIgnoreCase(RenderedRegistryMode.MODE_NAME)) {
129: while (iter.hasNext()) {
130: Object o = iter.next();
131: if (!(o instanceof RenderedImage)) {
132: msg
133: .append(getName()
134: + " "
135: + JaiI18N
136: .getString("AddCollectionDescriptor2"));
137: return false;
138: }
139: }
140: } else if (modeName
141: .equalsIgnoreCase(RenderableRegistryMode.MODE_NAME)) {
142: while (iter.hasNext()) {
143: Object o = iter.next();
144: if (!(o instanceof RenderableImage)) {
145: msg
146: .append(getName()
147: + " "
148: + JaiI18N
149: .getString("AddCollectionDescriptor3"));
150: return false;
151: }
152: }
153: }
154:
155: return true;
156: }
157:
158: /**
159: * Adds a collection of images.
160: *
161: * <p>Creates a <code>ParameterBlockJAI</code> from all
162: * supplied arguments except <code>hints</code> and invokes
163: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
164: *
165: * @see JAI
166: * @see ParameterBlockJAI
167: * @see RenderedOp
168: *
169: * @param source0 <code>Collection</code> source 0.
170: * @param hints The <code>RenderingHints</code> to use.
171: * May be <code>null</code>.
172: * @return The <code>RenderedOp</code> destination.
173: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
174: */
175: public static RenderedOp create(Collection source0,
176: RenderingHints hints) {
177: ParameterBlockJAI pb = new ParameterBlockJAI("AddCollection",
178: RenderedRegistryMode.MODE_NAME);
179:
180: pb.setSource("source0", source0);
181:
182: return JAI.create("AddCollection", pb, hints);
183: }
184:
185: /**
186: * Adds a collection of images.
187: *
188: * <p>Creates a <code>ParameterBlockJAI</code> from all
189: * supplied arguments except <code>hints</code> and invokes
190: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
191: *
192: * @see JAI
193: * @see ParameterBlockJAI
194: * @see RenderableOp
195: *
196: * @param source0 <code>Collection</code> source 0.
197: * @param hints The <code>RenderingHints</code> to use.
198: * May be <code>null</code>.
199: * @return The <code>RenderableOp</code> destination.
200: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
201: */
202: public static RenderableOp createRenderable(Collection source0,
203: RenderingHints hints) {
204: ParameterBlockJAI pb = new ParameterBlockJAI("AddCollection",
205: RenderableRegistryMode.MODE_NAME);
206:
207: pb.setSource("source0", source0);
208:
209: return JAI.createRenderable("AddCollection", pb, hints);
210: }
211: }
|