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