001: /*
002: * $RCSfile: AndConstDescriptor.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.DataBuffer;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.renderable.ParameterBlock;
018: import java.awt.image.renderable.RenderableImage;
019: import javax.media.jai.JAI;
020: import javax.media.jai.OperationDescriptorImpl;
021: import javax.media.jai.ParameterBlockJAI;
022: import javax.media.jai.RenderableOp;
023: import javax.media.jai.RenderedOp;
024: import javax.media.jai.registry.RenderableRegistryMode;
025: import javax.media.jai.registry.RenderedRegistryMode;
026:
027: /**
028: * An <code>OperationDescriptor</code> describing the "AndConst"
029: * operation.
030: *
031: * <p> This operation takes one rendered or renderable image and an
032: * array of integer constants, and performs a bit-wise logical "and"
033: * between every pixel in the same band of the source and the constant
034: * from the corresponding array entry. If the number of constants
035: * supplied is less than the number of bands of the destination, then
036: * the constant from entry 0 is applied to all the bands. Otherwise, a
037: * constant from a different entry is applied to each band.
038: *
039: * <p> The source image must have an integral data type. By default,
040: * the destination image bound, data type, and number of bands are the
041: * same as the source image.
042: *
043: * <p>The following matrix defines the logical "and" operation.
044: * <p><table border=1>
045: * <caption>Logical "and"</caption>
046: * <tr align=center><th>src</th> <th>const</th> <th>Result</th></tr>
047: * <tr align=center><td>0</td> <td>0</td> <td>0</td></tr>
048: * <tr align=center><td>0</td> <td>1</td> <td>0</td></tr>
049: * <tr align=center><td>1</td> <td>0</td> <td>0</td></tr>
050: * <tr align=center><td>1</td> <td>1</td> <td>1</td></tr>
051: * </table></p>
052: *
053: * <p> The destination pixel values are defined by the pseudocode:
054: * <pre>
055: * if (constants.length < dstNumBands) {
056: * dst[x][y][b] = srcs[x][y][b] & constants[0];
057: * } else {
058: * dst[x][y][b] = srcs[x][y][b] & constants[b];
059: * }
060: * </pre>
061: *
062: * <p><table border=1>
063: * <caption>Resource List</caption>
064: * <tr><th>Name</th> <th>Value</th></tr>
065: * <tr><td>GlobalName</td> <td>AndConst</td></tr>
066: * <tr><td>LocalName</td> <td>AndConst</td></tr>
067: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
068: * <tr><td>Description</td> <td>Logically "ands" an image
069: * with constants.</td></tr>
070: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/AndConstDescriptor.html</td></tr>
071: * <tr><td>Version</td> <td>1.0</td></tr>
072: * <tr><td>arg0Desc</td> <td>The constants to logically "and" with.</td></tr>
073: * </table></p>
074: *
075: * <p><table border=1>
076: * <caption>Parameter List</caption>
077: * <tr><th>Name</th> <th>Class Type</th>
078: * <th>Default Value</th></tr>
079: * <tr><td>constants</td> <td>int[]</td>
080: * <td>{0xFFFFFFFF}</td>
081: * </table></p>
082: *
083: * @see javax.media.jai.OperationDescriptor
084: */
085: public class AndConstDescriptor extends OperationDescriptorImpl {
086:
087: /**
088: * The resource strings that provide the general documentation
089: * and specify the parameter list for this operation.
090: */
091: private static final String[][] resources = {
092: { "GlobalName", "AndConst" },
093: { "LocalName", "AndConst" },
094: { "Vendor", "com.sun.media.jai" },
095: { "Description", JaiI18N.getString("AndConstDescriptor0") },
096: {
097: "DocURL",
098: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/AndConstDescriptor.html" },
099: { "Version", JaiI18N.getString("DescriptorVersion") },
100: { "arg0Desc", JaiI18N.getString("AndConstDescriptor1") } };
101:
102: /**
103: * The parameter class list for this operation.
104: * The number of constants provided should be either 1, in which case
105: * this same constant is applied to all the source bands; or the same
106: * number as the source bands, in which case one contant is applied
107: * to each band.
108: */
109: private static final Class[] paramClasses = { int[].class };
110:
111: /** The parameter name list for this operation. */
112: private static final String[] paramNames = { "constants" };
113:
114: /** The parameter default value list for this operation. */
115: private static final Object[] paramDefaults = { new int[] { 0xFFFFFFFF } };
116:
117: private static final String[] supportedModes = { "rendered",
118: "renderable" };
119:
120: /** Constructor. */
121: public AndConstDescriptor() {
122: super (resources, supportedModes, 1, paramNames, paramClasses,
123: paramDefaults, null);
124: }
125:
126: /**
127: * Validates the input source and parameter.
128: *
129: * <p> In addition to the standard checks performed by the
130: * superclass method, this method checks that the source image has
131: * an integral data type and that "constants" has length at least 1.
132: */
133: public boolean validateArguments(String modeName,
134: ParameterBlock args, StringBuffer message) {
135: if (!super .validateArguments(modeName, args, message)) {
136: return false;
137: }
138:
139: if (!modeName.equalsIgnoreCase("rendered"))
140: return true;
141:
142: RenderedImage src = args.getRenderedSource(0);
143:
144: int dtype = src.getSampleModel().getDataType();
145:
146: if ((dtype != DataBuffer.TYPE_BYTE)
147: && (dtype != DataBuffer.TYPE_USHORT)
148: && (dtype != DataBuffer.TYPE_SHORT)
149: && (dtype != DataBuffer.TYPE_INT)) {
150: message.append(getName() + " "
151: + JaiI18N.getString("AndConstDescriptor1"));
152: return false;
153: }
154:
155: int length = ((int[]) args.getObjectParameter(0)).length;
156:
157: if (length < 1) {
158: message.append(getName() + " "
159: + JaiI18N.getString("AndConstDescriptor2"));
160: return false;
161: }
162:
163: return true;
164: }
165:
166: /**
167: * Logically "ands" an image with constants.
168: *
169: * <p>Creates a <code>ParameterBlockJAI</code> from all
170: * supplied arguments except <code>hints</code> and invokes
171: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
172: *
173: * @see JAI
174: * @see ParameterBlockJAI
175: * @see RenderedOp
176: *
177: * @param source0 <code>RenderedImage</code> source 0.
178: * @param constants The constants to logically "and" with.
179: * May be <code>null</code>.
180: * @param hints The <code>RenderingHints</code> to use.
181: * May be <code>null</code>.
182: * @return The <code>RenderedOp</code> destination.
183: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
184: */
185: public static RenderedOp create(RenderedImage source0,
186: int[] constants, RenderingHints hints) {
187: ParameterBlockJAI pb = new ParameterBlockJAI("AndConst",
188: RenderedRegistryMode.MODE_NAME);
189:
190: pb.setSource("source0", source0);
191:
192: pb.setParameter("constants", constants);
193:
194: return JAI.create("AndConst", pb, hints);
195: }
196:
197: /**
198: * Logically "ands" an image with constants.
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 source0 <code>RenderableImage</code> source 0.
209: * @param constants The constants to logically "and" with.
210: * May be <code>null</code>.
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>source0</code> is <code>null</code>.
215: */
216: public static RenderableOp createRenderable(
217: RenderableImage source0, int[] constants,
218: RenderingHints hints) {
219: ParameterBlockJAI pb = new ParameterBlockJAI("AndConst",
220: RenderableRegistryMode.MODE_NAME);
221:
222: pb.setSource("source0", source0);
223:
224: pb.setParameter("constants", constants);
225:
226: return JAI.createRenderable("AndConst", pb, hints);
227: }
228: }
|