001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/PipelineDescriptorTest.java,v 1.4 2007/06/13 18:57:21 forklabs Exp $
003: *
004: * Copyright (C) 2007 Forklabs Daniel Léonard
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package ca.forklabs.media.jai.operator;
022:
023: import java.awt.RenderingHints;
024: import java.awt.image.DataBuffer;
025: import java.awt.image.Raster;
026: import java.awt.image.RenderedImage;
027: import java.awt.image.renderable.ParameterBlock;
028: import java.util.Arrays;
029: import java.util.Collection;
030: import java.util.Iterator;
031: import java.util.Locale;
032: import javax.media.jai.JAI;
033: import javax.media.jai.RenderedOp;
034: import junit.framework.TestCase;
035: import ca.forklabs.baselib.util.UnaryFunction;
036: import ca.forklabs.media.jai.ParameterBlockUtil;
037: import ca.forklabs.media.jai.RasterAdapter;
038: import ca.forklabs.media.jai.operator.PipelineDescriptor;
039:
040: /**
041: * Class {@code PipelineDescriptorTest} tests class {@link PipelineDescriptor}.
042: *
043: * @author <a href="mailto:daniel.leonard at umontreal.ca?subject=ca.umontreal.iro.image.arcticice.media.jai.operator.PipelineDescriptorTest">Daniel Léonard</a>
044: * @version $Revision: 1.4 $
045: */
046: @SuppressWarnings("nls")
047: public class PipelineDescriptorTest extends TestCase {
048:
049: //---------------------------
050: // Constructors
051: //---------------------------
052:
053: /**
054: * Constructor.
055: * @param name the name of this test.
056: */
057: public PipelineDescriptorTest(String name) {
058: super (name);
059: }
060:
061: //---------------------------
062: // Test methods
063: //---------------------------
064:
065: /**
066: * Tests the error catching in
067: * {@link PipelineDescriptor#validateParameters(String, ParameterBlock, StringBuffer)}
068: */
069: public void testValidateBadParameters() {
070: String[] operations = new String[] { "fileload", "crop",
071: "filestore", };
072:
073: ParameterBlock[] parameters = new ParameterBlock[] {
074: new ParameterBlock().add("image.png"),
075: new ParameterBlock().add("crop.png"), };
076:
077: ParameterBlock pb = new ParameterBlock().add(operations).add(
078: parameters);
079:
080: StringBuffer sb = new StringBuffer();
081:
082: PipelineDescriptor descriptor = new PipelineDescriptor();
083: boolean got;
084:
085: // test no rendering mode - thrown in superclass
086: try {
087: got = descriptor.validateParameters(null, pb, sb);
088: fail("should have thrown IllegalArgumentException");
089: } catch (IllegalArgumentException iae) {
090: // normal behaviour
091: }
092:
093: // bad rendering mode - thrown in superclass
094: try {
095: got = descriptor.validateParameters("badmode", pb, sb);
096: fail("should have thrown IllegalArgumentException");
097: } catch (IllegalArgumentException iae) {
098: // normal behaviour
099: }
100:
101: // no parameter block - thrown in superclass
102: try {
103: got = descriptor.validateParameters("rendered", null, sb);
104: fail("should have thrown IllegalArgumentException");
105: } catch (IllegalArgumentException iae) {
106: // normal behaviour
107: }
108:
109: // not same number of operations and parameters
110: got = descriptor.validateParameters("rendered", pb, sb);
111: assertFalse(got);
112: got = descriptor.validateParameters("renderable", pb, sb);
113: assertFalse(got);
114: }
115:
116: /**
117: * Tests
118: * {@link PipelineDescriptor#create(RenderedImage, String[], ParameterBlock[], RenderingHints)}.
119: */
120: @SuppressWarnings("boxing")
121: public void testCreateRenderedWithoutSource() {
122: String[] operations = new String[] { "constant", "format",
123: "addconst", };
124:
125: ParameterBlock[] parameters = new ParameterBlock[] {
126: ParameterBlockUtil.createConstantParameterBlock(2.0f,
127: 2.0f, new Integer[] { 2, 4, }),
128: ParameterBlockUtil.createFormatParameterBlock(null,
129: DataBuffer.TYPE_FLOAT),
130: ParameterBlockUtil.createAddConstParameterBlock(null,
131: new double[] { Math.PI, Math.E }), };
132:
133: RenderedOp image = PipelineDescriptor.create(null, operations,
134: parameters, null);
135:
136: Raster raster = image.getData();
137:
138: assertEquals(0, raster.getMinX());
139: assertEquals(0, raster.getMinY());
140: assertEquals(2, raster.getWidth());
141: assertEquals(2, raster.getHeight());
142: assertEquals(2, raster.getNumBands());
143:
144: float[] expected = new float[] { (float) (2.0 + Math.PI),
145: (float) (4.0 + Math.E), };
146: float[] got = null;
147:
148: got = raster.getPixel(0, 0, got);
149: assertEquals(expected[0], got[0], 10e-6f);
150: assertEquals(expected[1], got[1], 10e-6f);
151:
152: got = raster.getPixel(0, 1, got);
153: assertEquals(expected[0], got[0], 10e-6f);
154: assertEquals(expected[1], got[1], 10e-6f);
155:
156: got = raster.getPixel(1, 0, got);
157: assertEquals(expected[0], got[0], 10e-6f);
158: assertEquals(expected[1], got[1], 10e-6f);
159:
160: got = raster.getPixel(1, 1, got);
161: assertEquals(expected[0], got[0], 10e-6f);
162: assertEquals(expected[1], got[1], 10e-6f);
163: }
164:
165: /**
166: * Tests
167: * {@link PipelineDescriptor#create(RenderedImage, String[], ParameterBlock[], RenderingHints)}.
168: */
169: @SuppressWarnings("boxing")
170: public void testCreateRenderedWithSource() {
171: int[][] pixels = new int[][] { { 2, 2, 2, 2, },
172: { 4, 4, 4, 4, }, };
173: RenderedImage source = RasterAdapter
174: .buildIntImage(pixels, 2, 2);
175:
176: String[] operations = new String[] { "format", "addconst", };
177:
178: ParameterBlock[] parameters = new ParameterBlock[] {
179: ParameterBlockUtil.createFormatParameterBlock(null,
180: DataBuffer.TYPE_FLOAT),
181: ParameterBlockUtil.createAddConstParameterBlock(null,
182: new double[] { Math.PI, Math.E }), };
183:
184: RenderedOp image = PipelineDescriptor.create(source,
185: operations, parameters, null);
186:
187: Raster raster = image.getData();
188:
189: assertEquals(0, raster.getMinX());
190: assertEquals(0, raster.getMinY());
191: assertEquals(2, raster.getWidth());
192: assertEquals(2, raster.getHeight());
193: assertEquals(2, raster.getNumBands());
194:
195: float[] expected = new float[] { (float) (2.0 + Math.PI),
196: (float) (4.0 + Math.E), };
197: float[] got = null;
198:
199: got = raster.getPixel(0, 0, got);
200: assertEquals(expected[0], got[0], 10e-6f);
201: assertEquals(expected[1], got[1], 10e-6f);
202:
203: got = raster.getPixel(0, 1, got);
204: assertEquals(expected[0], got[0], 10e-6f);
205: assertEquals(expected[1], got[1], 10e-6f);
206:
207: got = raster.getPixel(1, 0, got);
208: assertEquals(expected[0], got[0], 10e-6f);
209: assertEquals(expected[1], got[1], 10e-6f);
210:
211: got = raster.getPixel(1, 1, got);
212: assertEquals(expected[0], got[0], 10e-6f);
213: assertEquals(expected[1], got[1], 10e-6f);
214: }
215:
216: private void checkImage(Object source, float expected) {
217: assertTrue(source instanceof RenderedImage);
218:
219: RenderedImage image = (RenderedImage) source;
220: Raster raster = image.getData();
221:
222: assertEquals(0, raster.getMinX());
223: assertEquals(0, raster.getMinY());
224: assertEquals(2, raster.getWidth());
225: assertEquals(2, raster.getHeight());
226: assertEquals(1, raster.getNumBands());
227:
228: float[] pixel = new float[] { expected, };
229: float[] got = null;
230:
231: assertTrue(Arrays.equals(pixel, raster.getPixel(0, 0, got)));
232: assertTrue(Arrays.equals(pixel, raster.getPixel(1, 0, got)));
233: assertTrue(Arrays.equals(pixel, raster.getPixel(0, 1, got)));
234: assertTrue(Arrays.equals(pixel, raster.getPixel(1, 1, got)));
235: }
236:
237: /**
238: * Tests
239: * {@link PipelineDescriptor#createCollection(Collection, String[], ParameterBlock[], RenderingHints)}.
240: */
241: @SuppressWarnings("boxing")
242: public void testCreateCollectionWithoutSource() {
243: RenderedImage[] images = new RenderedImage[] {
244: JAI.create("constant", ParameterBlockUtil
245: .createConstantParameterBlock(2.0f, 2.0f,
246: new Float[] { 4.0f, })),
247: JAI.create("constant", ParameterBlockUtil
248: .createConstantParameterBlock(2.0f, 2.0f,
249: new Float[] { 9.0f, })),
250: JAI.create("constant", ParameterBlockUtil
251: .createConstantParameterBlock(2.0f, 2.0f,
252: new Float[] { 16.0f, })),
253: JAI.create("constant", ParameterBlockUtil
254: .createConstantParameterBlock(2.0f, 2.0f,
255: new Float[] { 25.0f, })), };
256: Collection<RenderedImage> sources = Arrays.asList(images);
257:
258: String op1 = ApplyToCollectionDescriptor.NAME;
259: ParameterBlock pb1;
260: {
261: String name = UnaryFunctionDescriptor.NAME;
262:
263: UnaryFunction<Double, Double> function = new UnaryFunction<Double, Double>() {
264: @Override
265: public Double invoke(Double value) {
266: return Math.sqrt(value);
267: }
268: };
269: ParameterBlock pb = new ParameterBlock().add(function);
270:
271: pb1 = new ParameterBlock().add(name).add(pb);
272: }
273:
274: String op2 = "addconsttocollection";
275: ParameterBlock pb2 = new ParameterBlock()
276: .add(new double[] { 2.0, });
277:
278: String[] operators = new String[] { op1, op2, };
279: ParameterBlock[] parameters = new ParameterBlock[] { pb1, pb2, };
280:
281: Collection<?> sinks = PipelineDescriptor.createCollection(
282: sources, operators, parameters, null);
283:
284: assertEquals(4, sinks.size());
285:
286: Iterator<?> iter = sinks.iterator();
287: checkImage(iter.next(), 4.0f);
288: checkImage(iter.next(), 5.0f);
289: checkImage(iter.next(), 6.0f);
290: checkImage(iter.next(), 7.0f);
291: }
292:
293: /**
294: * Tests the English messages.
295: */
296: public void testEnglishMessages() {
297: Locale locale = Locale.getDefault();
298: Locale.setDefault(Locale.ENGLISH);
299:
300: try {
301: String[] expected = new String[] {
302: "Multiple JAI operations seen as one",
303: "The list of operations",
304: "The list of parameter blocks",
305: "There are 3 operations but only 2 parameter blocks", };
306:
307: PipelineDescriptor descriptor = new PipelineDescriptor();
308: String[] got = new String[] {
309: PipelineDescriptor.getDescription(),
310: PipelineDescriptor.getArg0Description(),
311: PipelineDescriptor.getArg1Description(),
312: descriptor.getBadAlignmentErrorMessage(3, 2), };
313:
314: assertEquals(expected.length, got.length);
315:
316: for (int i = 0; i < expected.length; i++) {
317: assertEquals("[" + i + "]", expected[i], got[i]);
318: }
319: } finally {
320: Locale.setDefault(locale);
321: }
322: }
323:
324: /**
325: * Tests the French messages.
326: */
327: public void testFrenchMessages() {
328: Locale locale = Locale.getDefault();
329: Locale.setDefault(Locale.FRENCH);
330:
331: try {
332: String[] expected = new String[] {
333: "Plusieurs opération JAI vues comme une seule",
334: "La liste des opérations",
335: "La liste des blocs de paramêtres",
336: "Il y a 3 opérations mais seulement 2 blocs de paramêtres", };
337:
338: PipelineDescriptor descriptor = new PipelineDescriptor();
339: String[] got = new String[] {
340: PipelineDescriptor.getDescription(),
341: PipelineDescriptor.getArg0Description(),
342: PipelineDescriptor.getArg1Description(),
343: descriptor.getBadAlignmentErrorMessage(3, 2), };
344:
345: assertEquals(expected.length, got.length);
346:
347: for (int i = 0; i < expected.length; i++) {
348: assertEquals("[" + i + "]", expected[i], got[i]);
349: }
350: } finally {
351: Locale.setDefault(locale);
352: }
353: }
354:
355: //---------------------------
356: // Class methods
357: //---------------------------
358:
359: /**
360: * Runs only this test.
361: * @param args ignored.
362: */
363: public static void main(String... args) {
364: junit.swingui.TestRunner.run(PipelineDescriptorTest.class);
365: }
366:
367: }
368:
369: /*
370: * $Log: PipelineDescriptorTest.java,v $
371: * Revision 1.4 2007/06/13 18:57:21 forklabs
372: * Changed parent to use CollectionDescriptor.
373: *
374: * Revision 1.3 2007/06/07 23:47:39 forklabs
375: * Operator pipeline is now on all four modes.
376: *
377: * Revision 1.2 2007/05/03 20:27:36 forklabs
378: * Trimmed trailing spaces.
379: *
380: * Revision 1.1 2007/05/03 19:51:52 forklabs
381: * The pipeline descriptor.
382: *
383: */
|