001: /*
002: * $RCSfile: MagnitudeDescriptor.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:38 $
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.PropertyGenerator;
023: import javax.media.jai.RenderableOp;
024: import javax.media.jai.RenderedOp;
025: import javax.media.jai.registry.RenderableRegistryMode;
026: import javax.media.jai.registry.RenderedRegistryMode;
027:
028: /**
029: * An <code>OperationDescriptor</code> describing the "Magnitude" operation.
030: *
031: * <p> The "Magnitude" operation computes the magnitude of each pixel of a
032: * complex image. The source image must have an even number of bands, with
033: * the even bands (0, 2, ...) representing the real parts and the odd bands
034: * (1, 3, ...) the imaginary parts of each complex pixel. The destination
035: * image has at most half the number of bands of the source image with each
036: * sample in a pixel representing the magnitude of the corresponding complex
037: * source sample. The magnitude values of the destination image are defined
038: * for a given sample by the pseudocode:
039: *
040: * <pre>dstPixel[x][y][b] = sqrt(src[x][y][2*b]^2 + src[x][y][2*b + 1]^2)</pre>
041: *
042: * where the number of bands <i>b</i> varies from zero to one less than the
043: * number of bands in the destination image.
044: *
045: * <p> For integral image datatypes, the result will be rounded and clamped
046: * as needed.
047: *
048: * <p>"Magnitude" defines a PropertyGenerator that sets the "COMPLEX"
049: * property of the image to <code>java.lang.Boolean.FALSE</code>, which may
050: * be retrieved by calling the <code>getProperty()</code> method with
051: * "COMPLEX" as the property name.
052: *
053: * <p><table border=1>
054: * <caption>Resource List</caption>
055: * <tr><th>Name</th> <th>Value</th></tr>
056: * <tr><td>GlobalName</td> <td>Magnitude</td></tr>
057: * <tr><td>LocalName</td> <td>Magnitude</td></tr>
058: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
059: * <tr><td>Description</td> <td>Find the magnitude of each pixel of
060: * an image.</td></tr>
061: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/MagnitudeDescriptor.html</td></tr>
062: * <tr><td>Version</td> <td>1.0</td></tr>
063: * </table></p>
064: *
065: * <p> No parameters are needed for the "Magnitude" operation.
066: *
067: * @see javax.media.jai.OperationDescriptor
068: */
069: public class MagnitudeDescriptor extends OperationDescriptorImpl {
070:
071: /**
072: * The resource strings that provide the general documentation
073: * and specify the parameter list for this operation.
074: */
075: private static final String[][] resources = {
076: { "GlobalName", "Magnitude" },
077: { "LocalName", "Magnitude" },
078: { "Vendor", "com.sun.media.jai" },
079: { "Description", JaiI18N.getString("MagnitudeDescriptor0") },
080: {
081: "DocURL",
082: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/MagnitudeDescriptor.html" },
083: { "Version", JaiI18N.getString("DescriptorVersion") } };
084:
085: private static final String[] supportedModes = { "rendered",
086: "renderable" };
087:
088: /** Constructor. */
089: public MagnitudeDescriptor() {
090: super (resources, supportedModes, 1, null, null, null, null);
091: }
092:
093: /**
094: * Validates the input source.
095: *
096: * <p> In addition to the standard checks performed by the
097: * superclass method, this method checks that the source image
098: * has an even number of bands.
099: */
100: protected boolean validateSources(String modeName,
101: ParameterBlock args, StringBuffer msg) {
102: if (!super .validateSources(modeName, args, msg)) {
103: return false;
104: }
105:
106: if (!modeName.equalsIgnoreCase("rendered"))
107: return true;
108:
109: RenderedImage src = args.getRenderedSource(0);
110:
111: int bands = src.getSampleModel().getNumBands();
112:
113: if (bands % 2 != 0) {
114: msg.append(getName() + " "
115: + JaiI18N.getString("MagnitudeDescriptor1"));
116: return false;
117: }
118:
119: return true;
120: }
121:
122: /**
123: * Returns an array of <code>PropertyGenerators</code> implementing
124: * property inheritance for the "Magnitude" operation.
125: *
126: * @return An array of property generators.
127: */
128: public PropertyGenerator[] getPropertyGenerators(String modeName) {
129: PropertyGenerator[] pg = new PropertyGenerator[1];
130: pg[0] = new ComplexPropertyGenerator();
131: return pg;
132: }
133:
134: /**
135: * Find the magnitude of each pixel of an image.
136: *
137: * <p>Creates a <code>ParameterBlockJAI</code> from all
138: * supplied arguments except <code>hints</code> and invokes
139: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
140: *
141: * @see JAI
142: * @see ParameterBlockJAI
143: * @see RenderedOp
144: *
145: * @param source0 <code>RenderedImage</code> source 0.
146: * @param hints The <code>RenderingHints</code> to use.
147: * May be <code>null</code>.
148: * @return The <code>RenderedOp</code> destination.
149: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
150: */
151: public static RenderedOp create(RenderedImage source0,
152: RenderingHints hints) {
153: ParameterBlockJAI pb = new ParameterBlockJAI("Magnitude",
154: RenderedRegistryMode.MODE_NAME);
155:
156: pb.setSource("source0", source0);
157:
158: return JAI.create("Magnitude", pb, hints);
159: }
160:
161: /**
162: * Find the magnitude of each pixel of an image.
163: *
164: * <p>Creates a <code>ParameterBlockJAI</code> from all
165: * supplied arguments except <code>hints</code> and invokes
166: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
167: *
168: * @see JAI
169: * @see ParameterBlockJAI
170: * @see RenderableOp
171: *
172: * @param source0 <code>RenderableImage</code> source 0.
173: * @param hints The <code>RenderingHints</code> to use.
174: * May be <code>null</code>.
175: * @return The <code>RenderableOp</code> destination.
176: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
177: */
178: public static RenderableOp createRenderable(
179: RenderableImage source0, RenderingHints hints) {
180: ParameterBlockJAI pb = new ParameterBlockJAI("Magnitude",
181: RenderableRegistryMode.MODE_NAME);
182:
183: pb.setSource("source0", source0);
184:
185: return JAI.createRenderable("Magnitude", pb, hints);
186: }
187: }
|