001: /*
002: * $RCSfile: ConstantDescriptor.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:32 $
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: import javax.media.jai.util.Range;
026:
027: /**
028: * An <code>OperationDescriptor</code> describing the "Constant"
029: * operation.
030: *
031: * <p> The Constant operation creates a multi-banded, tiled rendered
032: * image, where all the pixels from the same band have a constant
033: * value. The width and height of the image must be specified and
034: * greater than 0. At least one constant must be supplied. The number
035: * of bands of the image is determined by the number of constant pixel
036: * values supplied in the "bandValues" parameter. The data type is
037: * determined by the type of the constants; this means all elements of
038: * the <code>bandValues</code> array must be of the same type.
039: *
040: * <p> If the <code>bandValues</code> array is a <code>Short</code>
041: * array, then <code>TYPE_USHORT</code> is used if all values are
042: * non-negative; otherwise <code>TYPE_SHORT</code> is used.
043: *
044: * <p><table border=1>
045: * <caption>Resource List</caption>
046: * <tr><th>Name</th> <th>Value</th></tr>
047: * <tr><td>GlobalName</td> <td>Constant</td></tr>
048: * <tr><td>LocalName</td> <td>Constant</td></tr>
049: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
050: * <tr><td>Description</td> <td>Creates an image with
051: * constant pixel values.</td></tr>
052: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ConstantDescriptor.html</td></tr>
053: * <tr><td>Version</td> <td>1.0</td></tr>
054: * <tr><td>arg0Desc</td> <td>Image width in pixels.</td></tr>
055: * <tr><td>arg1Desc</td> <td>Image height in pixels.</td></tr>
056: * <tr><td>arg2Desc</td> <td>The constant pixel band values.</td></tr>
057: * </table></p>
058: *
059: * <p><table border=1>
060: * <caption>Parameter List</caption>
061: * <tr><th>Name</th> <th>Class Type</th>
062: * <th>Default Value</th></tr>
063: * <tr><td>width</td> <td>java.lang.Float</td>
064: * <td>NO_PARAMETER_DEFAULT</td>
065: * <tr><td>height</td> <td>java.lang.Float</td>
066: * <td>NO_PARAMETER_DEFAULT</td>
067: * <tr><td>bandValues</td> <td>java.lang.Number[]</td>
068: * <td>NO_PARAMETER_DEFAULT</td>
069: * </table></p>
070: *
071: * @see javax.media.jai.OperationDescriptor
072: */
073: public class ConstantDescriptor extends OperationDescriptorImpl {
074:
075: /**
076: * The resource strings that provide the general documentation
077: * and specify the parameter list for this operation.
078: */
079: private static final String[][] resources = {
080: { "GlobalName", "Constant" },
081: { "LocalName", "Constant" },
082: { "Vendor", "com.sun.media.jai" },
083: { "Description", JaiI18N.getString("ConstantDescriptor0") },
084: {
085: "DocURL",
086: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/ConstantDescriptor.html" },
087: { "Version", JaiI18N.getString("DescriptorVersion") },
088: { "arg0Desc", JaiI18N.getString("ConstantDescriptor1") },
089: { "arg1Desc", JaiI18N.getString("ConstantDescriptor2") },
090: { "arg2Desc", JaiI18N.getString("ConstantDescriptor3") } };
091:
092: /** The parameter class list for this operation. */
093: private static final Class[] paramClasses = {
094: java.lang.Float.class, java.lang.Float.class,
095: java.lang.Number[].class };
096:
097: /** The parameter name list for this operation. */
098: private static final String[] paramNames = { "width", "height",
099: "bandValues" };
100:
101: /** The parameter default value list for this operation. */
102: private static final Object[] paramDefaults = {
103: NO_PARAMETER_DEFAULT, NO_PARAMETER_DEFAULT,
104: NO_PARAMETER_DEFAULT };
105:
106: private static final String[] supportedModes = { "rendered",
107: "renderable" };
108:
109: private static final Object[] validParamValues = {
110: new Range(Float.class, new Float(0.0F), false, null, false),
111: new Range(Float.class, new Float(0.0F), false, null, false),
112: null };
113:
114: /** Constructor. */
115: public ConstantDescriptor() {
116: super (resources, supportedModes, 0, paramNames, paramClasses,
117: paramDefaults, validParamValues);
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 "width" and "height"
125: * are greater than 0 (for rendered mode) and that "bandValues" has
126: * length at least 1.
127: */
128: protected boolean validateParameters(String modeName,
129: ParameterBlock args, StringBuffer message) {
130: if (!super .validateParameters(modeName, args, message)) {
131: return false;
132: }
133:
134: int length = ((Number[]) args.getObjectParameter(2)).length;
135: if (length < 1) {
136: message.append(getName() + " "
137: + JaiI18N.getString("ConstantDescriptor4"));
138: return false;
139: }
140:
141: if (modeName.equalsIgnoreCase("rendered")) {
142: int width = Math.round(args.getFloatParameter(0));
143: int height = Math.round(args.getFloatParameter(1));
144:
145: if ((width < 1) || (height < 1)) {
146: message.append(getName() + " "
147: + JaiI18N.getString("ConstantDescriptor5"));
148: return false;
149: }
150: } else if (modeName.equalsIgnoreCase("renderable")) {
151: float width = args.getFloatParameter(0);
152: float height = args.getFloatParameter(1);
153:
154: if ((width <= 0.0F) || (height <= 0.0F)) {
155: message.append(getName() + " "
156: + JaiI18N.getString("ConstantDescriptor6"));
157: return false;
158: }
159: }
160:
161: return true;
162: }
163:
164: /**
165: * Creates an image with constant pixel values.
166: *
167: * <p>Creates a <code>ParameterBlockJAI</code> from all
168: * supplied arguments except <code>hints</code> and invokes
169: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
170: *
171: * @see JAI
172: * @see ParameterBlockJAI
173: * @see RenderedOp
174: *
175: * @param width Image width in pixels.
176: * @param height Image height in pixels.
177: * @param bandValues The constant pixel band values.
178: * @param hints The <code>RenderingHints</code> to use.
179: * May be <code>null</code>.
180: * @return The <code>RenderedOp</code> destination.
181: * @throws IllegalArgumentException if <code>width</code> is <code>null</code>.
182: * @throws IllegalArgumentException if <code>height</code> is <code>null</code>.
183: * @throws IllegalArgumentException if <code>bandValues</code> is <code>null</code>.
184: */
185: public static RenderedOp create(Float width, Float height,
186: Number[] bandValues, RenderingHints hints) {
187: ParameterBlockJAI pb = new ParameterBlockJAI("Constant",
188: RenderedRegistryMode.MODE_NAME);
189:
190: pb.setParameter("width", width);
191: pb.setParameter("height", height);
192: pb.setParameter("bandValues", bandValues);
193:
194: return JAI.create("Constant", pb, hints);
195: }
196:
197: /**
198: * Creates an image with constant pixel values.
199: *
200: * <p>Creates a <code>ParameterBlockJAI</code> from all
201: * supplied arguments except <code>hints</code> and invokes
202: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
203: *
204: * @see JAI
205: * @see ParameterBlockJAI
206: * @see RenderableOp
207: *
208: * @param width Image width in pixels.
209: * @param height Image height in pixels.
210: * @param bandValues The constant pixel band values.
211: * @param hints The <code>RenderingHints</code> to use.
212: * May be <code>null</code>.
213: * @return The <code>RenderableOp</code> destination.
214: * @throws IllegalArgumentException if <code>width</code> is <code>null</code>.
215: * @throws IllegalArgumentException if <code>height</code> is <code>null</code>.
216: * @throws IllegalArgumentException if <code>bandValues</code> is <code>null</code>.
217: */
218: public static RenderableOp createRenderable(Float width,
219: Float height, Number[] bandValues, RenderingHints hints) {
220: ParameterBlockJAI pb = new ParameterBlockJAI("Constant",
221: RenderableRegistryMode.MODE_NAME);
222:
223: pb.setParameter("width", width);
224: pb.setParameter("height", height);
225: pb.setParameter("bandValues", bandValues);
226:
227: return JAI.createRenderable("Constant", pb, hints);
228: }
229: }
|