001: /*
002: * $RCSfile: ThresholdDescriptor.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:45 $
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 "Threshold" operation.
028: *
029: * <p> The Threshold operation takes one rendered image, and maps all
030: * the pixels of this image whose value falls within a specified range
031: * to a specified constant. The range is specified by a low value and
032: * a high value.
033: *
034: * <p> If the number of elements supplied via the "high", "low", and
035: * "constants" arrays are less than the number of bands of the source
036: * image, then the element from entry 0 is applied to all the bands.
037: * Otherwise, the element from a different entry is applied to its
038: * corresponding band.
039: *
040: * <p> The destination pixel values are defined by the pseudocode:
041: * <pre>
042: * lowVal = (low.length < dstNumBands) ?
043: * low[0] : low[b];
044: * highVal = (high.length < dstNumBands) ?
045: * high[0] : high[b];
046: * const = (constants.length < dstNumBands) ?
047: * constants[0] : constants[b];
048: *
049: * if (src[x][y][b] >= lowVal && src[x][y][b] <= highVal) {
050: * dst[x][y][b] = const;
051: * } else {
052: * dst[x][y][b] = src[x][y][b];
053: * }
054: * </pre>
055: *
056: * <p><table border=1>
057: * <caption>Resource List</caption>
058: * <tr><th>Name</th> <th>Value</th></tr>
059: * <tr><td>GlobalName</td> <td>Threshold</td></tr>
060: * <tr><td>LocalName</td> <td>Threshold</td></tr>
061: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
062: * <tr><td>Description</td> <td>Maps the pixels whose value falls between a low
063: * value and a high value to a constant.</td></tr>
064: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ThresholdDescriptor.html</td></tr>
065: * <tr><td>Version</td> <td>1.0</td></tr>
066: * <tr><td>arg0Desc</td> <td>The low value.</td></tr>
067: * <tr><td>arg1Desc</td> <td>The high value.</td></tr>
068: * <tr><td>arg2Desc</td> <td>The constant the pixels are mapped to.</td></tr>
069: * </table></p>
070: *
071: * <p><table border=1>
072: * <caption>Parameter List</caption>
073: * <tr><th>Name</th> <th>Class Type</th>
074: * <th>Default Value</th></tr>
075: * <tr><td>low</td> <td>double[]</td>
076: * <td>NO_PARAMETER_DEFAULT</td>
077: * <tr><td>high</td> <td>double[]</td>
078: * <td>NO_PARAMETER_DEFAULT</td>
079: * <tr><td>constants</td> <td>double[]</td>
080: * <td>NO_PARAMETER_DEFAULT</td>
081: * </table></p>
082: *
083: * @see javax.media.jai.OperationDescriptor
084: */
085: public class ThresholdDescriptor extends OperationDescriptorImpl {
086:
087: /**
088: * The resource strings that provide the general documentation
089: * and specify the parameter list for this operation.
090: */
091: private static final String[][] resources = {
092: { "GlobalName", "Threshold" },
093: { "LocalName", "Threshold" },
094: { "Vendor", "com.sun.media.jai" },
095: { "Description", JaiI18N.getString("ThresholdDescriptor0") },
096: {
097: "DocURL",
098: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ThresholdDescriptor.html" },
099: { "Version", JaiI18N.getString("DescriptorVersion") },
100: { "arg0Desc", JaiI18N.getString("ThresholdDescriptor1") },
101: { "arg1Desc", JaiI18N.getString("ThresholdDescriptor2") },
102: { "arg2Desc", JaiI18N.getString("ThresholdDescriptor3") } };
103:
104: /** The parameter name list for this operation. */
105: private static final String[] paramNames = { "low", "high",
106: "constants" };
107:
108: /** The parameter class list for this operation. */
109: private static final Class[] paramClasses = { double[].class,
110: double[].class, double[].class };
111:
112: /** The parameter default value list for this operation. */
113: private static final Object[] paramDefaults = {
114: NO_PARAMETER_DEFAULT, NO_PARAMETER_DEFAULT,
115: NO_PARAMETER_DEFAULT };
116:
117: /** Constructor. */
118: public ThresholdDescriptor() {
119: super (resources, 1, paramClasses, paramNames, paramDefaults);
120: }
121:
122: /** Returns <code>true</code> since renderable operation is supported. */
123: public boolean isRenderableSupported() {
124: return true;
125: }
126:
127: /** Validates input parameters. */
128: protected boolean validateParameters(ParameterBlock args,
129: StringBuffer msg) {
130: int numParams = args.getNumParameters();
131: if (numParams < 3) {
132: msg.append(getName() + " "
133: + JaiI18N.getString("ThresholdDescriptor4"));
134: return false;
135: }
136:
137: for (int i = 0; i < 3; i++) {
138: Object p = args.getObjectParameter(i);
139:
140: if (p == null) {
141: msg.append(getName() + " "
142: + JaiI18N.getString("ThresholdDescriptor5"));
143: return false;
144: }
145:
146: if (!(p instanceof double[])) {
147: msg.append(getName() + " "
148: + JaiI18N.getString("ThresholdDescriptor6"));
149: return false;
150: }
151:
152: if (((double[]) p).length < 1) {
153: msg.append(getName() + " "
154: + JaiI18N.getString("ThresholdDescriptor7"));
155: return false;
156: }
157: }
158:
159: return true;
160: }
161:
162: /**
163: * Maps the pixels whose value falls between a low value and a high value to a constant.
164: *
165: * <p>Creates a <code>ParameterBlockJAI</code> from all
166: * supplied arguments except <code>hints</code> and invokes
167: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
168: *
169: * @see JAI
170: * @see ParameterBlockJAI
171: * @see RenderedOp
172: *
173: * @param source0 <code>RenderedImage</code> source 0.
174: * @param low The low value.
175: * @param high The high value.
176: * @param constants The constant the pixels are mapped to.
177: * @param hints The <code>RenderingHints</code> to use.
178: * May be <code>null</code>.
179: * @return The <code>RenderedOp</code> destination.
180: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
181: * @throws IllegalArgumentException if <code>low</code> is <code>null</code>.
182: * @throws IllegalArgumentException if <code>high</code> is <code>null</code>.
183: * @throws IllegalArgumentException if <code>constants</code> is <code>null</code>.
184: */
185: public static RenderedOp create(RenderedImage source0,
186: double[] low, double[] high, double[] constants,
187: RenderingHints hints) {
188: ParameterBlockJAI pb = new ParameterBlockJAI("Threshold",
189: RenderedRegistryMode.MODE_NAME);
190:
191: pb.setSource("source0", source0);
192:
193: pb.setParameter("low", low);
194: pb.setParameter("high", high);
195: pb.setParameter("constants", constants);
196:
197: return JAI.create("Threshold", pb, hints);
198: }
199:
200: /**
201: * Maps the pixels whose value falls between a low value and a high value to a constant.
202: *
203: * <p>Creates a <code>ParameterBlockJAI</code> from all
204: * supplied arguments except <code>hints</code> and invokes
205: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
206: *
207: * @see JAI
208: * @see ParameterBlockJAI
209: * @see RenderableOp
210: *
211: * @param source0 <code>RenderableImage</code> source 0.
212: * @param low The low value.
213: * @param high The high value.
214: * @param constants The constant the pixels are mapped to.
215: * @param hints The <code>RenderingHints</code> to use.
216: * May be <code>null</code>.
217: * @return The <code>RenderableOp</code> destination.
218: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
219: * @throws IllegalArgumentException if <code>low</code> is <code>null</code>.
220: * @throws IllegalArgumentException if <code>high</code> is <code>null</code>.
221: * @throws IllegalArgumentException if <code>constants</code> is <code>null</code>.
222: */
223: public static RenderableOp createRenderable(
224: RenderableImage source0, double[] low, double[] high,
225: double[] constants, RenderingHints hints) {
226: ParameterBlockJAI pb = new ParameterBlockJAI("Threshold",
227: RenderableRegistryMode.MODE_NAME);
228:
229: pb.setSource("source0", source0);
230:
231: pb.setParameter("low", low);
232: pb.setParameter("high", high);
233: pb.setParameter("constants", constants);
234:
235: return JAI.createRenderable("Threshold", pb, hints);
236: }
237: }
|