001: /*
002: * $RCSfile: BandSelectDescriptor.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:30 $
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 javax.media.jai.JAI;
019: import javax.media.jai.OperationDescriptorImpl;
020: import javax.media.jai.ParameterBlockJAI;
021: import javax.media.jai.RenderableOp;
022: import javax.media.jai.RenderedOp;
023: import javax.media.jai.registry.RenderableRegistryMode;
024: import javax.media.jai.registry.RenderedRegistryMode;
025:
026: /**
027: * An <code>OperationDescriptor</code> describing the "BandSelect" operation.
028: *
029: * <p> The BandSelect operation chooses <code>N</code> bands from a
030: * rendered or renderable source image and copies the pixel data of
031: * these bands to the destination image in the order specified. The
032: * <code>bandIndices</code> parameter specifies the source band
033: * indices, and its size (<code>bandIndices.length</code>) determines
034: * the number of bands of the destination image. The destination
035: * image may have ay number of bands, and a particular band of the
036: * source image may be repeated in the destination image by specifying
037: * it multiple times in the <code>bandIndices</code> parameter.
038: *
039: * <p> Each of the <code>bandIndices</code> value should be a valid
040: * band index number of the source image. For example, if the source
041: * only has two bands, then 1 is a valid band index, but 3 is not. The
042: * first band is numbered 0.
043: *
044: * <p> The destination pixel values are defined by the pseudocode:
045: * <pre>
046: * dst[x][y][b] = src[x][y][bandIndices[b]];
047: * </pre>
048: *
049: * <p><table border=1>
050: * <caption>Resource List</caption>
051: * <tr><th>Name</th> <th>Value</th></tr>
052: * <tr><td>GlobalName</td> <td>BandSelect</td></tr>
053: * <tr><td>LocalName</td> <td>BandSelect</td></tr>
054: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
055: * <tr><td>Description</td> <td>Selects n number of bands from
056: * an image.</td></tr>
057: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/BandSelectDescriptor.html</td></tr>
058: * <tr><td>Version</td> <td>1.0</td></tr>
059: * <tr><td>arg0Desc</td> <td>The indices of the selected bands.</td></tr>
060: * </table></p>
061: *
062: * <p><table border=1>
063: * <caption>Parameter List</caption>
064: * <tr><th>Name</th> <th>Class Type</th>
065: * <th>Default Value</th></tr>
066: * <tr><td>bandIndices</td> <td>int[]</td>
067: * <td>NO_PARAMETER_DEFAULT</td>
068: * </table></p>
069: *
070: * @see javax.media.jai.OperationDescriptor
071: */
072: public class BandSelectDescriptor extends OperationDescriptorImpl {
073:
074: /**
075: * The resource strings that provide the general documentation
076: * and specify the parameter list for this operation.
077: */
078: private static final String[][] resources = {
079: { "GlobalName", "BandSelect" },
080: { "LocalName", "BandSelect" },
081: { "Vendor", "com.sun.media.jai" },
082: { "Description", JaiI18N.getString("BandSelectDescriptor0") },
083: {
084: "DocURL",
085: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/BandSelectDescriptor.html" },
086: { "Version", JaiI18N.getString("DescriptorVersion") },
087: { "arg0Desc", JaiI18N.getString("BandSelectDescriptor1") } };
088:
089: /** The parameter class list for this operation. */
090: private static final Class[] paramClasses = { int[].class };
091:
092: /** The parameter name list for this operation. */
093: private static final String[] paramNames = { "bandIndices" };
094:
095: /** The parameter default value list for this operation. */
096: private static final Object[] paramDefaults = { NO_PARAMETER_DEFAULT };
097:
098: private static final String[] supportedModes = { "rendered",
099: "renderable" };
100:
101: /** Constructor. */
102: public BandSelectDescriptor() {
103: super (resources, supportedModes, 1, paramNames, paramClasses,
104: paramDefaults, null);
105: }
106:
107: /**
108: * Validates the input source and parameters.
109: *
110: * <p> In addition to the standard checks performed by the
111: * superclass method, this method checks that "bandIndices" has a
112: * length of at least 1 and does not contain any values less than
113: * 0 or greater than the number of source bands minus 1.
114: */
115: public boolean validateArguments(String modeName,
116: ParameterBlock args, StringBuffer message) {
117: if (!super .validateArguments(modeName, args, message)) {
118: return false;
119: }
120:
121: if (!modeName.equalsIgnoreCase("rendered"))
122: return true;
123:
124: int[] indices = (int[]) args.getObjectParameter(0);
125: if (indices.length < 1) {
126: message.append(getName() + " "
127: + JaiI18N.getString("BandSelectDescriptor2"));
128: return false;
129: }
130:
131: RenderedImage src = args.getRenderedSource(0);
132:
133: int bands = src.getSampleModel().getNumBands();
134: for (int i = 0; i < indices.length; i++) {
135: if (indices[i] < 0 || indices[i] >= bands) {
136: message.append(getName() + " "
137: + JaiI18N.getString("BandSelectDescriptor3"));
138: return false;
139: }
140: }
141:
142: return true;
143: }
144:
145: /**
146: * Selects n number of bands from an image.
147: *
148: * <p>Creates a <code>ParameterBlockJAI</code> from all
149: * supplied arguments except <code>hints</code> and invokes
150: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
151: *
152: * @see JAI
153: * @see ParameterBlockJAI
154: * @see RenderedOp
155: *
156: * @param source0 <code>RenderedImage</code> source 0.
157: * @param bandIndices The indices of the selected bands.
158: * @param hints The <code>RenderingHints</code> to use.
159: * May be <code>null</code>.
160: * @return The <code>RenderedOp</code> destination.
161: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
162: * @throws IllegalArgumentException if <code>bandIndices</code> is <code>null</code>.
163: */
164: public static RenderedOp create(RenderedImage source0,
165: int[] bandIndices, RenderingHints hints) {
166: ParameterBlockJAI pb = new ParameterBlockJAI("BandSelect",
167: RenderedRegistryMode.MODE_NAME);
168:
169: pb.setSource("source0", source0);
170:
171: pb.setParameter("bandIndices", bandIndices);
172:
173: return JAI.create("BandSelect", pb, hints);
174: }
175:
176: /**
177: * Selects n number of bands from an image.
178: *
179: * <p>Creates a <code>ParameterBlockJAI</code> from all
180: * supplied arguments except <code>hints</code> and invokes
181: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
182: *
183: * @see JAI
184: * @see ParameterBlockJAI
185: * @see RenderableOp
186: *
187: * @param source0 <code>RenderableImage</code> source 0.
188: * @param bandIndices The indices of the selected bands.
189: * @param hints The <code>RenderingHints</code> to use.
190: * May be <code>null</code>.
191: * @return The <code>RenderableOp</code> destination.
192: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
193: * @throws IllegalArgumentException if <code>bandIndices</code> is <code>null</code>.
194: */
195: public static RenderableOp createRenderable(
196: RenderableImage source0, int[] bandIndices,
197: RenderingHints hints) {
198: ParameterBlockJAI pb = new ParameterBlockJAI("BandSelect",
199: RenderableRegistryMode.MODE_NAME);
200:
201: pb.setSource("source0", source0);
202:
203: pb.setParameter("bandIndices", bandIndices);
204:
205: return JAI.createRenderable("BandSelect", pb, hints);
206: }
207: }
|