001: /*
002: * $RCSfile: RescaleDescriptor.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:43 $
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 "Rescale" operation.
028: *
029: * <p> The "Rescale" operation takes a rendered or renderable source
030: * image and maps the pixel values of an image from one range to
031: * another range by multiplying each pixel value by one of a set of
032: * constants and then adding another constant to the result of the
033: * multiplication. If the number of constants supplied is less than
034: * the number of bands of the destination, then the constant from
035: * entry 0 is applied to all the bands. Otherwise, a constant from a
036: * different entry is applied to each band. There must be at least one
037: * entry in each of the contants and offsets arrays.
038: *
039: * <p> The destination pixel values are defined by the pseudocode:
040: * <pre>
041: * constant = (constants.length < dstNumBands) ?
042: * constants[0] : constants[b];
043: * offset = (offsets.length < dstNumBands) ?
044: * offsets[0] : offsets[b];
045: *
046: * dst[x][y][b] = src[x][y][b]*constant + offset;
047: * </pre>
048: *
049: * <p> The pixel arithmetic is performed using the data type of the
050: * destination image. By default, the destination will have the same
051: * data type as the source image unless an <code>ImageLayout</code>
052: * containing a <code>SampleModel</code> with a different data type
053: * is supplied as a rendering hint.
054: *
055: * <p><table border=1>
056: * <caption>Resource List</caption>
057: * <tr><th>Name</th> <th>Value</th></tr>
058: * <tr><td>GlobalName</td> <td>Rescale</td></tr>
059: * <tr><td>LocalName</td> <td>Rescale</td></tr>
060: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
061: * <tr><td>Description</td> <td>Maps the pixels values of an image from
062: * one range to another range.</td></tr>
063: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/RescaleDescriptor.html</td></tr>
064: * <tr><td>Version</td> <td>1.0</td></tr>
065: * <tr><td>arg0Desc</td> <td>The per-band constants to multiply by.</td></tr>
066: * <tr><td>arg1Desc</td> <td>The per-band offsets to be added.</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: * <tr><td>offsets</td> <td>double[]</td>
076: * <td>{0.0}</td>
077: * </table></p>
078: *
079: * @see javax.media.jai.OperationDescriptor
080: */
081: public class RescaleDescriptor extends OperationDescriptorImpl {
082:
083: /**
084: * The resource strings that provide the general documentation
085: * and specify the parameter list for this operation.
086: */
087: private static final String[][] resources = {
088: { "GlobalName", "Rescale" },
089: { "LocalName", "Rescale" },
090: { "Vendor", "com.sun.media.jai" },
091: { "Description", JaiI18N.getString("RescaleDescriptor0") },
092: {
093: "DocURL",
094: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/RescaleDescriptor.html" },
095: { "Version", JaiI18N.getString("DescriptorVersion") },
096: { "arg0Desc", JaiI18N.getString("RescaleDescriptor1") },
097: { "arg1Desc", JaiI18N.getString("RescaleDescriptor2") } };
098:
099: /** The parameter class list for this operation. */
100: private static final Class[] paramClasses = { double[].class,
101: double[].class };
102:
103: /** The parameter name list for this operation. */
104: private static final String[] paramNames = { "constants", "offsets" };
105:
106: /** The parameter default value list for this operation. */
107: private static final Object[] paramDefaults = {
108: new double[] { 1.0 }, new double[] { 0.0 } };
109:
110: /** Constructor. */
111: public RescaleDescriptor() {
112: super (resources, 1, paramClasses, paramNames, paramDefaults);
113: }
114:
115: /** Returns <code>true</code> since renderable operation is supported. */
116: public boolean isRenderableSupported() {
117: return true;
118: }
119:
120: /**
121: * Validates the input parameters.
122: *
123: * <p> In addition to the standard checks performed by the
124: * superclass method, this method checks that the length of the
125: * "constants" and "offsets" arrays are each at least 1.
126: */
127: protected boolean validateParameters(ParameterBlock args,
128: StringBuffer msg) {
129: if (!super .validateParameters(args, msg)) {
130: return false;
131: }
132:
133: int constantsLength = ((double[]) args.getObjectParameter(0)).length;
134: int offsetsLength = ((double[]) args.getObjectParameter(1)).length;
135:
136: if (constantsLength < 1) {
137: msg.append(getName() + " "
138: + JaiI18N.getString("RescaleDescriptor3"));
139: return false;
140: }
141:
142: if (offsetsLength < 1) {
143: msg.append(getName() + ": "
144: + JaiI18N.getString("RescaleDescriptor4"));
145: return false;
146: }
147:
148: return true;
149: }
150:
151: /**
152: * Maps the pixels values of an image from one range to another range.
153: *
154: * <p>Creates a <code>ParameterBlockJAI</code> from all
155: * supplied arguments except <code>hints</code> and invokes
156: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
157: *
158: * @see JAI
159: * @see ParameterBlockJAI
160: * @see RenderedOp
161: *
162: * @param source0 <code>RenderedImage</code> source 0.
163: * @param constants The per-band constants to multiply by.
164: * May be <code>null</code>.
165: * @param offsets The per-band offsets to be added.
166: * May be <code>null</code>.
167: * @param hints The <code>RenderingHints</code> to use.
168: * May be <code>null</code>.
169: * @return The <code>RenderedOp</code> destination.
170: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
171: */
172: public static RenderedOp create(RenderedImage source0,
173: double[] constants, double[] offsets, RenderingHints hints) {
174: ParameterBlockJAI pb = new ParameterBlockJAI("Rescale",
175: RenderedRegistryMode.MODE_NAME);
176:
177: pb.setSource("source0", source0);
178:
179: pb.setParameter("constants", constants);
180: pb.setParameter("offsets", offsets);
181:
182: return JAI.create("Rescale", pb, hints);
183: }
184:
185: /**
186: * Maps the pixels values of an image from one range to another range.
187: *
188: * <p>Creates a <code>ParameterBlockJAI</code> from all
189: * supplied arguments except <code>hints</code> and invokes
190: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
191: *
192: * @see JAI
193: * @see ParameterBlockJAI
194: * @see RenderableOp
195: *
196: * @param source0 <code>RenderableImage</code> source 0.
197: * @param constants The per-band constants to multiply by.
198: * May be <code>null</code>.
199: * @param offsets The per-band offsets to be added.
200: * May be <code>null</code>.
201: * @param hints The <code>RenderingHints</code> to use.
202: * May be <code>null</code>.
203: * @return The <code>RenderableOp</code> destination.
204: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
205: */
206: public static RenderableOp createRenderable(
207: RenderableImage source0, double[] constants,
208: double[] offsets, RenderingHints hints) {
209: ParameterBlockJAI pb = new ParameterBlockJAI("Rescale",
210: RenderableRegistryMode.MODE_NAME);
211:
212: pb.setSource("source0", source0);
213:
214: pb.setParameter("constants", constants);
215: pb.setParameter("offsets", offsets);
216:
217: return JAI.createRenderable("Rescale", pb, hints);
218: }
219: }
|