001: /*
002: * $RCSfile: BandCombineDescriptor.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.ColorModel;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.SampleModel;
018: import java.awt.image.renderable.ParameterBlock;
019: import java.awt.image.renderable.RenderableImage;
020: import javax.media.jai.JAI;
021: import javax.media.jai.OpImage;
022: import javax.media.jai.OperationDescriptorImpl;
023: import javax.media.jai.ParameterBlockJAI;
024: import javax.media.jai.RenderableOp;
025: import javax.media.jai.RenderedOp;
026: import javax.media.jai.registry.RenderableRegistryMode;
027: import javax.media.jai.registry.RenderedRegistryMode;
028:
029: /**
030: * An <code>OperationDescriptor</code> describing the "BandCombine" operation.
031: *
032: * <p> The BandCombing operation computes a set of arbitrary linear
033: * combinations of the bands of a rendered or renderable source image,
034: * using a specified matrix. The matrix must a number of rows equal to
035: * the number of desired destination bands and a number of columns equal to the
036: * number of source bands plus one. In other words, the array may be
037: * constructed using the syntax:
038: *
039: * <pre>
040: * double[][] matrix = new double[destBands][sourceBands + 1];
041: * </pre>
042: *
043: * <p> The number of source bands used to determine the matrix dimensions
044: * is given by <code>source.getSampleModel().getNumBands()</code> regardless
045: * of the type of <code>ColorModel</code> the source has.
046: *
047: * <p> The extra column in the matrix contains constant values each of which
048: * is added to the respective band of the destination. The transformation is
049: * therefore defined by the pseudocode:
050: *
051: * <pre>
052: * // s = source pixel
053: * // d = destination pixel
054: * for(int i = 0; i < destBands; i++) {
055: * d[i] = matrix[i][sourceBands];
056: * for(int j = 0; j < sourceBands; j++) {
057: * d[i] += matrix[i][j]*s[j];
058: * }
059: * }
060: * </pre>
061: *
062: * <p> If the result of the computation underflows/overflows the
063: * minimum/maximum value supported by the destination image, then it
064: * will be clamped to the minimum/maximum value respectively.
065: *
066: * <p><table border=1>
067: * <caption>Resource List</caption>
068: * <tr><th>Name</th> <th>Value</th></tr>
069: * <tr><td>GlobalName</td> <td>BandCombine</td></tr>
070: * <tr><td>LocalName</td> <td>BandCombine</td></tr>
071: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
072: * <tr><td>Description</td> <td>Performs arbitrary interband linear combination
073: * using a specified matrix.</td></tr>
074: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/BandCombineDescriptor.html</td></tr>
075: * <tr><td>Version</td> <td>1.0</td></tr>
076: * <tr><td>arg0Desc</td> <td>The matrix specifying the band combination.</td></tr>
077: * </table></p>
078: *
079: * <p><table border=1>
080: * <caption>Parameter List</caption>
081: * <tr><th>Name</th> <th>Class Type</th>
082: * <th>Default Value</th></tr>
083: * <tr><td>matrix</td> <td>double[][]</td>
084: * <td>NO_PARAMETER_DEFAULT</td>
085: * </table></p>
086: *
087: * @see javax.media.jai.OperationDescriptor
088: */
089: public class BandCombineDescriptor extends OperationDescriptorImpl {
090:
091: /**
092: * The resource strings that provide the general documentation
093: * and specify the parameter list for this operation.
094: */
095: private static final String[][] resources = {
096: { "GlobalName", "BandCombine" },
097: { "LocalName", "BandCombine" },
098: { "Vendor", "com.sun.media.jai" },
099: { "Description",
100: JaiI18N.getString("BandCombineDescriptor0") },
101: {
102: "DocURL",
103: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/BandCombineDescriptor.html" },
104: { "Version", JaiI18N.getString("DescriptorVersion") },
105: { "arg0Desc", JaiI18N.getString("BandCombineDescriptor1") } };
106:
107: /** The parameter class list for this operation. */
108: private static final Class[] paramClasses = { double[][].class };
109:
110: /** The parameter name list for this operation. */
111: private static final String[] paramNames = { "matrix" };
112:
113: /** The parameter default value list for this operation. */
114: private static final Object[] paramDefaults = { NO_PARAMETER_DEFAULT };
115:
116: private static final String[] supportedModes = { "rendered",
117: "renderable" };
118:
119: /** Constructor. */
120: public BandCombineDescriptor() {
121: super (resources, supportedModes, 1, paramNames, paramClasses,
122: paramDefaults, null);
123: }
124:
125: /**
126: * Validates the input source and parameters.
127: *
128: * <p> In addition to the standard checks performed by the
129: * superclass method, this method checks that "matrix" has at
130: * least 1 row and (source bands + 1) columns.
131: *
132: * <p> The number of source bands is considered to be equal to
133: * <code>source.getSampleModel().getNumBands()</code>.
134: */
135: public boolean validateArguments(String modeName,
136: ParameterBlock args, StringBuffer message) {
137: if (!super .validateArguments(modeName, args, message)) {
138: return false;
139: }
140:
141: if (!modeName.equalsIgnoreCase("rendered"))
142: return true;
143:
144: RenderedImage src = args.getRenderedSource(0);
145:
146: double[][] matrix = (double[][]) args.getObjectParameter(0);
147: SampleModel sm = src.getSampleModel();
148: int rowLength = sm.getNumBands() + 1;
149:
150: if (matrix.length < 1) {
151: message.append(getName() + ": "
152: + JaiI18N.getString("BandCombineDescriptor2"));
153: return false;
154: }
155:
156: for (int i = 0; i < matrix.length; i++) {
157: if (matrix[i].length != rowLength) {
158: message.append(getName() + ": "
159: + JaiI18N.getString("BandCombineDescriptor2"));
160: return false;
161: }
162: }
163:
164: return true;
165: }
166:
167: /**
168: * Performs arbitrary interband linear combination using a specified matrix.
169: *
170: * <p>Creates a <code>ParameterBlockJAI</code> from all
171: * supplied arguments except <code>hints</code> and invokes
172: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
173: *
174: * @see JAI
175: * @see ParameterBlockJAI
176: * @see RenderedOp
177: *
178: * @param source0 <code>RenderedImage</code> source 0.
179: * @param matrix The matrix specifying the band combination.
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: * @throws IllegalArgumentException if <code>matrix</code> is <code>null</code>.
185: */
186: public static RenderedOp create(RenderedImage source0,
187: double[][] matrix, RenderingHints hints) {
188: ParameterBlockJAI pb = new ParameterBlockJAI("BandCombine",
189: RenderedRegistryMode.MODE_NAME);
190:
191: pb.setSource("source0", source0);
192:
193: pb.setParameter("matrix", matrix);
194:
195: return JAI.create("BandCombine", pb, hints);
196: }
197:
198: /**
199: * Performs arbitrary interband linear combination using a specified matrix.
200: *
201: * <p>Creates a <code>ParameterBlockJAI</code> from all
202: * supplied arguments except <code>hints</code> and invokes
203: * {@link JAI#createRenderable(String,ParameterBlock,RenderingHints)}.
204: *
205: * @see JAI
206: * @see ParameterBlockJAI
207: * @see RenderableOp
208: *
209: * @param source0 <code>RenderableImage</code> source 0.
210: * @param matrix The matrix specifying the band combination.
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: * @throws IllegalArgumentException if <code>matrix</code> is <code>null</code>.
216: */
217: public static RenderableOp createRenderable(
218: RenderableImage source0, double[][] matrix,
219: RenderingHints hints) {
220: ParameterBlockJAI pb = new ParameterBlockJAI("BandCombine",
221: RenderableRegistryMode.MODE_NAME);
222:
223: pb.setSource("source0", source0);
224:
225: pb.setParameter("matrix", matrix);
226:
227: return JAI.createRenderable("BandCombine", pb, hints);
228: }
229: }
|