001: /*
002: * $RCSfile: BinarizeDescriptor.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:30 $
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 "Binarize" operation.
028: *
029: * <p> The "Binarize" operation takes one rendered or renderable single-banded
030: * source image and a threshold value and applies a thresholding operation to
031: * the produce a bilevel image.
032: *
033: * <p> By default the destination image bounds are equal to those of the
034: * source image. The <code>SampleModel</code> of the destination image is
035: * an instance of <code>MultiPixelPackedSampleModel</code>.
036: *
037: * <p> The pseudocode for "Binarize" is as follows:
038: * <pre>
039: * dst(x, y) = src(x, y) >= threshold ? 1 : 0;
040: * </pre>
041: *
042: * <p><table border=1>
043: * <caption>Resource List</caption>
044: * <tr><th>Name</th> <th>Value</th></tr>
045: * <tr><td>GlobalName</td> <td>Binarize</td></tr>
046: * <tr><td>LocalName</td> <td>Binarize</td></tr>
047: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
048: * <tr><td>Description</td> <td>Thresholds an image into a bilevel image.<td></tr>
049: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/BinarizeDescriptor.html</td></tr>
050: * <tr><td>Version</td> <td>1.1</td></tr>
051: * <tr><td>arg0Desc</td> <td>The threshold value.</td></tr>
052: * </table></p>
053: *
054: * <p><table border=1>
055: * <caption>Parameter List</caption>
056: * <tr><th>Name</th> <th>Class Type</th>
057: * <th>Default Value</th></tr>
058: * <tr><td>threshold</td> <td>java.lang.Double</td>
059: * <td>NO_PARAMETER_DEFAULT</td>
060: * </table></p>
061: *
062: * @see javax.media.jai.OperationDescriptor
063: *
064: * @since JAI 1.1
065: */
066: public class BinarizeDescriptor extends OperationDescriptorImpl {
067:
068: /**
069: * The resource strings that provide the general documentation
070: * and specify the parameter list for this operation.
071: */
072: private static final String[][] resources = {
073: { "GlobalName", "Binarize" },
074: { "LocalName", "Binarize" },
075: { "Vendor", "com.sun.media.jai" },
076: { "Description", JaiI18N.getString("BinarizeDescriptor0") },
077: {
078: "DocURL",
079: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/BinarizeDescriptor.html" },
080: { "Version", JaiI18N.getString("DescriptorVersion") },
081: { "arg0Desc", JaiI18N.getString("BinarizeDescriptor1") } };
082:
083: /** The parameter name list for this operation. */
084: private static final String[] paramNames = { "threshold" };
085:
086: /**
087: * The parameter class list for this operation.
088: * The number of threshold value provided should be 1.
089: */
090: private static final Class[] paramClasses = { java.lang.Double.class };
091:
092: /** The parameter default value list for this operation. */
093: private static final Object[] paramDefaults = { NO_PARAMETER_DEFAULT };
094:
095: private static final String[] supportedModes = { "rendered",
096: "renderable" };
097:
098: /** Constructor. */
099: public BinarizeDescriptor() {
100: super (resources, supportedModes, 1, paramNames, paramClasses,
101: paramDefaults, null);
102: }
103:
104: /**
105: * Validates the input source.
106: *
107: * <p> In addition to the standard checks performed by the
108: * superclass method, this method checks that the source image
109: * is single-banded.
110: */
111: protected boolean validateSources(String modeName,
112: ParameterBlock args, StringBuffer msg) {
113: if (!super .validateSources(modeName, args, msg)) {
114: return false;
115: }
116:
117: if (!modeName.equalsIgnoreCase("rendered"))
118: return true;
119:
120: RenderedImage source = (RenderedImage) (args.getSource(0));
121: int numBands = source.getSampleModel().getNumBands();
122: if (numBands != 1) {
123: msg.append(getName() + " "
124: + JaiI18N.getString("BinarizeDescriptor2"));
125: return false;
126: }
127:
128: return true;
129: }
130:
131: /**
132: * Binarize an image from a threshold value.
133: *
134: * <p>Creates a <code>ParameterBlockJAI</code> from all
135: * supplied arguments except <code>hints</code> and invokes
136: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
137: *
138: * @see JAI
139: * @see ParameterBlockJAI
140: * @see RenderedOp
141: *
142: * @param source0 <code>RenderedImage</code> source 0.
143: * @param threshold Argment must be of type java.lang.Double.
144: * @param hints The <code>RenderingHints</code> to use.
145: * May be <code>null</code>.
146: * @return The <code>RenderedOp</code> destination.
147: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
148: * @throws IllegalArgumentException if <code>threshold</code> is <code>null</code>.
149: */
150: public static RenderedOp create(RenderedImage source0,
151: Double threshold, RenderingHints hints) {
152: ParameterBlockJAI pb = new ParameterBlockJAI("Binarize",
153: RenderedRegistryMode.MODE_NAME);
154:
155: pb.setSource("source0", source0);
156:
157: pb.setParameter("threshold", threshold);
158:
159: return JAI.create("Binarize", pb, hints);
160: }
161:
162: /**
163: * Binarize an image from a threshold value.
164: *
165: * <p>Creates a <code>ParameterBlockJAI</code> from all
166: * supplied arguments except <code>hints</code> and invokes
167: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
168: *
169: * @see JAI
170: * @see ParameterBlockJAI
171: * @see RenderableOp
172: *
173: * @param source0 <code>RenderableImage</code> source 0.
174: * @param threshold Argment must be of type java.lang.Double.
175: * @param hints The <code>RenderingHints</code> to use.
176: * May be <code>null</code>.
177: * @return The <code>RenderableOp</code> destination.
178: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
179: * @throws IllegalArgumentException if <code>threshold</code> is <code>null</code>.
180: */
181: public static RenderableOp createRenderable(
182: RenderableImage source0, Double threshold,
183: RenderingHints hints) {
184: ParameterBlockJAI pb = new ParameterBlockJAI("Binarize",
185: RenderableRegistryMode.MODE_NAME);
186:
187: pb.setSource("source0", source0);
188:
189: pb.setParameter("threshold", threshold);
190:
191: return JAI.createRenderable("Binarize", pb, hints);
192: }
193: }
|