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