001: /*
002: * $RCSfile: SubtractFromConstDescriptor.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
028: * "SubtractFromConst" operation.
029: *
030: * <p> The SubtractFromConst operation takes one rendered or
031: * renderable image and an array of double constants, and subtracts
032: * every pixel of the same band of the source from the constant from
033: * the corresponding array entry. If the number of constants supplied
034: * is less than the number of bands of the destination, then the
035: * constant from entry 0 is applied to all the bands. Otherwise, a
036: * constant from a different entry is applied to each band.
037: *
038: * <p> By default, the destination image bound, data type, and number of
039: * bands are the same as the source image. If the result of the operation
040: * underflows/overflows the minimum/maximum value supported by the
041: * destination data type, then it will be clamped to the minimum/maximum
042: * value respectively.
043: *
044: * <p> The destination pixel values are defined by the pseudocode:
045: * <pre>
046: * if (constants.length < dstNumBands) {
047: * dst[x][y][b] = constants[0] - src[x][y][b];
048: * } else {
049: * dst[x][y][b] = constants[b] - src[x][y][b];
050: * }
051: * </pre>
052: *
053: * <p><table border=1>
054: * <caption>Resource List</caption>
055: * <tr><th>Name</th> <th>Value</th></tr>
056: * <tr><td>GlobalName</td> <td>SubtractFromConst</td></tr>
057: * <tr><td>LocalName</td> <td>SubtractFromConst</td></tr>
058: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
059: * <tr><td>Description</td> <td>Subtracts an image from
060: * constants.</td></tr>
061: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/SubtractFromConstDescriptor.html</td></tr>
062: * <tr><td>Version</td> <td>1.0</td></tr>
063: * <tr><td>arg0Desc</td> <td>The constants to be subtracted from.</td></tr>
064: * </table></p>
065: *
066: * <p><table border=1>
067: * <caption>Parameter List</caption>
068: * <tr><th>Name</th> <th>Class Type</th>
069: * <th>Default Value</th></tr>
070: * <tr><td>constants</td> <td>double[]</td>
071: * <td>NO_PARAMETER_DEFAULT</td>
072: * </table></p>
073: *
074: * @see javax.media.jai.OperationDescriptor
075: */
076: public class SubtractFromConstDescriptor extends
077: OperationDescriptorImpl {
078:
079: /**
080: * The resource strings that provide the general documentation
081: * and specify the parameter list for this operation.
082: */
083: private static final String[][] resources = {
084: { "GlobalName", "SubtractFromConst" },
085: { "LocalName", "SubtractFromConst" },
086: { "Vendor", "com.sun.media.jai" },
087: { "Description",
088: JaiI18N.getString("SubtractFromConstDescriptor0") },
089: {
090: "DocURL",
091: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/SubtractFromConstDescriptor.html" },
092: { "Version", JaiI18N.getString("DescriptorVersion") },
093: { "arg0Desc",
094: JaiI18N.getString("SubtractFromConstDescriptor1") } };
095:
096: /**
097: * The parameter class list for this operation.
098: * The number of constants provided should be either 1, in which case
099: * this same constant is applied to all the source bands; or the same
100: * number as the source bands, in which case one contant is applied
101: * to each band.
102: */
103: private static final Class[] paramClasses = { double[].class };
104:
105: /** The parameter name list for this operation. */
106: private static final String[] paramNames = { "constants" };
107:
108: /** The parameter default value list for this operation. */
109: private static final Object[] paramDefaults = { NO_PARAMETER_DEFAULT };
110:
111: /** Constructor. */
112: public SubtractFromConstDescriptor() {
113: super (resources, 1, paramClasses, paramNames, paramDefaults);
114: }
115:
116: /** Returns <code>true</code> since renderable operation is supported. */
117: public boolean isRenderableSupported() {
118: return true;
119: }
120:
121: /**
122: * Validates the input parameter.
123: *
124: * <p> In addition to the standard checks performed by the
125: * superclass method, this method checks that the length of the
126: * "constants" array is at least 1.
127: */
128: protected boolean validateParameters(ParameterBlock args,
129: StringBuffer message) {
130: if (!super .validateParameters(args, message)) {
131: return false;
132: }
133:
134: int length = ((double[]) args.getObjectParameter(0)).length;
135: if (length < 1) {
136: message
137: .append(getName()
138: + " "
139: + JaiI18N
140: .getString("SubtractFromConstDescriptor2"));
141: return false;
142: }
143:
144: return true;
145: }
146:
147: /**
148: * Subtracts an image from constants.
149: *
150: * <p>Creates a <code>ParameterBlockJAI</code> from all
151: * supplied arguments except <code>hints</code> and invokes
152: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
153: *
154: * @see JAI
155: * @see ParameterBlockJAI
156: * @see RenderedOp
157: *
158: * @param source0 <code>RenderedImage</code> source 0.
159: * @param constants The constants to be subtracted from.
160: * @param hints The <code>RenderingHints</code> to use.
161: * May be <code>null</code>.
162: * @return The <code>RenderedOp</code> destination.
163: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
164: * @throws IllegalArgumentException if <code>constants</code> is <code>null</code>.
165: */
166: public static RenderedOp create(RenderedImage source0,
167: double[] constants, RenderingHints hints) {
168: ParameterBlockJAI pb = new ParameterBlockJAI(
169: "SubtractFromConst", RenderedRegistryMode.MODE_NAME);
170:
171: pb.setSource("source0", source0);
172:
173: pb.setParameter("constants", constants);
174:
175: return JAI.create("SubtractFromConst", pb, hints);
176: }
177:
178: /**
179: * Subtracts an image from constants.
180: *
181: * <p>Creates a <code>ParameterBlockJAI</code> from all
182: * supplied arguments except <code>hints</code> and invokes
183: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
184: *
185: * @see JAI
186: * @see ParameterBlockJAI
187: * @see RenderableOp
188: *
189: * @param source0 <code>RenderableImage</code> source 0.
190: * @param constants The constants to be subtracted from.
191: * @param hints The <code>RenderingHints</code> to use.
192: * May be <code>null</code>.
193: * @return The <code>RenderableOp</code> destination.
194: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
195: * @throws IllegalArgumentException if <code>constants</code> is <code>null</code>.
196: */
197: public static RenderableOp createRenderable(
198: RenderableImage source0, double[] constants,
199: RenderingHints hints) {
200: ParameterBlockJAI pb = new ParameterBlockJAI(
201: "SubtractFromConst", RenderableRegistryMode.MODE_NAME);
202:
203: pb.setSource("source0", source0);
204:
205: pb.setParameter("constants", constants);
206:
207: return JAI.createRenderable("SubtractFromConst", pb, hints);
208: }
209: }
|