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