001: /*
002: * $RCSfile: DivideByConstDescriptor.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: * "DivideByConst" operation.
029: *
030: * <p> The DivideByConst operation takes one rendered or renderable
031: * source image and an array of double constants, and divides every
032: * pixel 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> In case of division by 0, if the numerator is 0, then the
039: * result is set to 0; otherwise, the result is set to the maximum
040: * value supported by the destination data type.
041: *
042: * <p> By default, the destination image bound, data type, and number
043: * of bands are the same as the source image. If the result of the
044: * operation underflows/overflows the minimum/maximum value supported
045: * by the destination data type, then it will be clamped to the
046: * minimum/maximum value respectively.
047: *
048: * <p> The destination pixel values are defined by the pseudocode:
049: * <pre>
050: * if (constants.length < dstNumBands) {
051: * dst[x][y][b] = srcs[x][y][b]/constants[0];
052: * } else {
053: * dst[x][y][b] = srcs[x][y][b]/constants[b];
054: * }
055: * </pre>
056: *
057: * <p><table border=1>
058: * <caption>Resource List</caption>
059: * <tr><th>Name</th> <th>Value</th></tr>
060: * <tr><td>GlobalName</td> <td>DivideByConst</td></tr>
061: * <tr><td>LocalName</td> <td>DivideByConst</td></tr>
062: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
063: * <tr><td>Description</td> <td>Divides an image by constants.</td></tr>
064: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/DivideByConstDescriptor.html</td></tr>
065: * <tr><td>Version</td> <td>1.0</td></tr>
066: * <tr><td>arg0Desc</td> <td>The constants to be divided by.</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>{1.0}</td>
075: * </table></p>
076: *
077: * @see javax.media.jai.OperationDescriptor */
078: public class DivideByConstDescriptor extends OperationDescriptorImpl {
079:
080: /**
081: * The resource strings that provide the general documentation
082: * and specify the parameter list for this operation.
083: */
084: private static final String[][] resources = {
085: { "GlobalName", "DivideByConst" },
086: { "LocalName", "DivideByConst" },
087: { "Vendor", "com.sun.media.jai" },
088: { "Description",
089: JaiI18N.getString("DivideByConstDescriptor0") },
090: {
091: "DocURL",
092: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/DivideByConstDescriptor.html" },
093: { "Version", JaiI18N.getString("DescriptorVersion") },
094: { "arg0Desc", JaiI18N.getString("DivideByConstDescriptor1") } };
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 = { new double[] { 1.0 } };
110:
111: /** Constructor. */
112: public DivideByConstDescriptor() {
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 parameters.
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.append(getName() + " "
137: + JaiI18N.getString("DivideByConstDescriptor2"));
138: return false;
139: }
140:
141: return true;
142: }
143:
144: /**
145: * Divides an image by constants.
146: *
147: * <p>Creates a <code>ParameterBlockJAI</code> from all
148: * supplied arguments except <code>hints</code> and invokes
149: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
150: *
151: * @see JAI
152: * @see ParameterBlockJAI
153: * @see RenderedOp
154: *
155: * @param source0 <code>RenderedImage</code> source 0.
156: * @param constants The constants to be divided by.
157: * May be <code>null</code>.
158: * @param hints The <code>RenderingHints</code> to use.
159: * May be <code>null</code>.
160: * @return The <code>RenderedOp</code> destination.
161: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
162: */
163: public static RenderedOp create(RenderedImage source0,
164: double[] constants, RenderingHints hints) {
165: ParameterBlockJAI pb = new ParameterBlockJAI("DivideByConst",
166: RenderedRegistryMode.MODE_NAME);
167:
168: pb.setSource("source0", source0);
169:
170: pb.setParameter("constants", constants);
171:
172: return JAI.create("DivideByConst", pb, hints);
173: }
174:
175: /**
176: * Divides an image by constants.
177: *
178: * <p>Creates a <code>ParameterBlockJAI</code> from all
179: * supplied arguments except <code>hints</code> and invokes
180: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
181: *
182: * @see JAI
183: * @see ParameterBlockJAI
184: * @see RenderableOp
185: *
186: * @param source0 <code>RenderableImage</code> source 0.
187: * @param constants The constants to be divided by.
188: * May be <code>null</code>.
189: * @param hints The <code>RenderingHints</code> to use.
190: * May be <code>null</code>.
191: * @return The <code>RenderableOp</code> destination.
192: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
193: */
194: public static RenderableOp createRenderable(
195: RenderableImage source0, double[] constants,
196: RenderingHints hints) {
197: ParameterBlockJAI pb = new ParameterBlockJAI("DivideByConst",
198: RenderableRegistryMode.MODE_NAME);
199:
200: pb.setSource("source0", source0);
201:
202: pb.setParameter("constants", constants);
203:
204: return JAI.createRenderable("DivideByConst", pb, hints);
205: }
206: }
|