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