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