001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/opimage/UnaryFunctionOpImageTest.java,v 1.6 2007/06/05 20:51:48 forklabs Exp $
003: *
004: * Copyright (C) 2006 DIRO Daniel Léonard
005: */
006:
007: package ca.forklabs.media.jai.opimage;
008:
009: import java.awt.Rectangle;
010: import java.awt.image.BufferedImage;
011: import java.awt.image.ColorModel;
012: import java.awt.image.ComponentSampleModel;
013: import java.awt.image.DataBuffer;
014: import java.awt.image.Raster;
015: import java.awt.image.RenderedImage;
016: import java.awt.image.renderable.ParameterBlock;
017: import java.util.Arrays;
018: import java.util.Locale;
019: import javax.media.jai.JAI;
020: import javax.media.jai.RasterAccessor;
021: import javax.media.jai.RasterFormatTag;
022: import javax.media.jai.RenderedOp;
023: import junit.framework.TestCase;
024: import ca.forklabs.baselib.util.UnaryFunction;
025: import ca.forklabs.media.jai.RasterAdapter;
026: import ca.forklabs.media.jai.mock.MockRenderedImage;
027: import ca.forklabs.media.jai.opimage.UnaryFunctionOpImage;
028:
029: /**
030: * Class {@code UnaryFunctionOpImageTest} tests class
031: * {@link UnaryFunctionOpImage}.
032: *
033: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.opimage.UnaryFunctionOpImageTest">Daniel Léonard</a>
034: * @version $Revision: 1.6 $
035: */
036: @SuppressWarnings("nls")
037: public class UnaryFunctionOpImageTest extends TestCase {
038:
039: //---------------------------
040: // Constructors
041: //---------------------------
042:
043: /**
044: * Constructor.
045: * @param name the name of this test.
046: */
047: public UnaryFunctionOpImageTest(String name) {
048: super (name);
049: }
050:
051: //---------------------------
052: // Test methods
053: //---------------------------
054:
055: /**
056: * Tests the operator on an indexed image.
057: */
058: public void testIndexed() {
059: RenderedImage source = new MockRenderedImage() {
060: @Override
061: public int getHeight() {
062: return 1;
063: }
064:
065: @Override
066: public int getWidth() {
067: return 1;
068: }
069:
070: @Override
071: public int getTileHeight() {
072: return 1;
073: }
074:
075: @Override
076: public int getTileWidth() {
077: return 1;
078: }
079: };
080: UnaryFunction<Double, Double> function = ADDED_TO_ITSELF;
081:
082: UnaryFunctionOpImage image = new UnaryFunctionOpImage(source,
083: function, null, null);
084:
085: byte[][] expected_color_map = new byte[][] { { 2, 4, 6, },
086: { 8, 10, 12, }, };
087: byte[][] color_map = new byte[][] { { 1, 2, 3, }, { 4, 5, 6, }, };
088:
089: image.transformColormap(color_map);
090:
091: assertTrue(Arrays.equals(expected_color_map[0], color_map[0]));
092: assertTrue(Arrays.equals(expected_color_map[1], color_map[1]));
093: }
094:
095: /** The unary function used in the tests. */
096: private static final UnaryFunction<Double, Double> ADDED_TO_ITSELF = new UnaryFunction<Double, Double>() {
097: @SuppressWarnings("boxing")
098: public Double invoke(Double value) {
099: return value + value;
100: }
101:
102: };
103:
104: /**
105: * Tests an image with byte pixels.
106: */
107: public void testByteData() {
108: byte[][] pixels = new byte[][] { new byte[] { 1, 4, 7, 10, },
109: new byte[] { 2, 5, 8, 11, },
110: new byte[] { 3, 6, 9, 12, }, };
111: RenderedImage source = RasterAdapter.buildByteImage(pixels, 2,
112: 2);
113:
114: RenderedOp sink = JAI.create("unaryfunction",
115: new ParameterBlock().addSource(source).add(
116: ADDED_TO_ITSELF));
117: Raster raster = sink.getData();
118:
119: assertEquals(0, raster.getMinX());
120: assertEquals(0, raster.getMinY());
121: assertEquals(2, raster.getWidth());
122: assertEquals(2, raster.getHeight());
123: assertEquals(3, raster.getNumBands());
124:
125: int[] rgb = new int[3];
126: assertTrue(Arrays.equals(new int[] { 2, 4, 6, }, raster
127: .getPixel(0, 0, rgb)));
128: assertTrue(Arrays.equals(new int[] { 8, 10, 12, }, raster
129: .getPixel(1, 0, rgb)));
130: assertTrue(Arrays.equals(new int[] { 14, 16, 18, }, raster
131: .getPixel(0, 1, rgb)));
132: assertTrue(Arrays.equals(new int[] { 20, 22, 24, }, raster
133: .getPixel(1, 1, rgb)));
134: }
135:
136: /**
137: * Tests an image with float pixels.
138: */
139: public void testFloatData() {
140: float[][] pixels = new float[][] {
141: new float[] { 1.0f, 4.0f, 7.0f, 10.0f, },
142: new float[] { 2.0f, 5.0f, 8.0f, 11.0f, },
143: new float[] { 3.0f, 6.0f, 9.0f, 12.0f, }, };
144: RenderedImage source = RasterAdapter.buildFloatImage(pixels, 2,
145: 2);
146:
147: RenderedOp sink = JAI.create("unaryfunction",
148: new ParameterBlock().addSource(source).add(
149: ADDED_TO_ITSELF));
150: Raster raster = sink.getData();
151:
152: assertEquals(0, raster.getMinX());
153: assertEquals(0, raster.getMinY());
154: assertEquals(2, raster.getWidth());
155: assertEquals(2, raster.getHeight());
156: assertEquals(3, raster.getNumBands());
157:
158: float[] rgb = new float[3];
159: assertTrue(Arrays.equals(new float[] { 2.0f, 4.0f, 6.0f, },
160: raster.getPixel(0, 0, rgb)));
161: assertTrue(Arrays.equals(new float[] { 8.0f, 10.0f, 12.0f, },
162: raster.getPixel(1, 0, rgb)));
163: assertTrue(Arrays.equals(new float[] { 14.0f, 16.0f, 18.0f, },
164: raster.getPixel(0, 1, rgb)));
165: assertTrue(Arrays.equals(new float[] { 20.0f, 22.0f, 24.0f, },
166: raster.getPixel(1, 1, rgb)));
167: }
168:
169: /**
170: * Tests an image with double pixels.
171: */
172: public void testDoubleData() {
173: double[][] pixels = new double[][] {
174: new double[] { 1.0, 4.0, 7.0, 10.0, },
175: new double[] { 2.0, 5.0, 8.0, 11.0, },
176: new double[] { 3.0, 6.0, 9.0, 12.0, }, };
177: RenderedImage source = RasterAdapter.buildDoubleImage(pixels,
178: 2, 2);
179:
180: RenderedOp sink = JAI.create("unaryfunction",
181: new ParameterBlock().addSource(source).add(
182: ADDED_TO_ITSELF));
183: Raster raster = sink.getData();
184:
185: assertEquals(0, raster.getMinX());
186: assertEquals(0, raster.getMinY());
187: assertEquals(2, raster.getWidth());
188: assertEquals(2, raster.getHeight());
189: assertEquals(3, raster.getNumBands());
190:
191: double[] rgb = new double[3];
192: assertTrue(Arrays.equals(new double[] { 2.0, 4.0, 6.0, },
193: raster.getPixel(0, 0, rgb)));
194: assertTrue(Arrays.equals(new double[] { 8.0, 10.0, 12.0, },
195: raster.getPixel(1, 0, rgb)));
196: assertTrue(Arrays.equals(new double[] { 14.0, 16.0, 18.0, },
197: raster.getPixel(0, 1, rgb)));
198: assertTrue(Arrays.equals(new double[] { 20.0, 22.0, 24.0, },
199: raster.getPixel(1, 1, rgb)));
200: }
201:
202: /**
203: * Tests an image with integer pixels.
204: */
205: public void testIntData() {
206: int[][] pixels = new int[][] { new int[] { 1, 4, 7, 10, },
207: new int[] { 2, 5, 8, 11, }, new int[] { 3, 6, 9, 12, }, };
208: RenderedImage source = RasterAdapter
209: .buildIntImage(pixels, 2, 2);
210:
211: RenderedOp sink = JAI.create("unaryfunction",
212: new ParameterBlock().addSource(source).add(
213: ADDED_TO_ITSELF));
214: Raster raster = sink.getData();
215:
216: assertEquals(0, raster.getMinX());
217: assertEquals(0, raster.getMinY());
218: assertEquals(2, raster.getWidth());
219: assertEquals(2, raster.getHeight());
220: assertEquals(3, raster.getNumBands());
221:
222: int[] rgb = new int[3];
223: assertTrue(Arrays.equals(new int[] { 2, 4, 6, }, raster
224: .getPixel(0, 0, rgb)));
225: assertTrue(Arrays.equals(new int[] { 8, 10, 12, }, raster
226: .getPixel(1, 0, rgb)));
227: assertTrue(Arrays.equals(new int[] { 14, 16, 18, }, raster
228: .getPixel(0, 1, rgb)));
229: assertTrue(Arrays.equals(new int[] { 20, 22, 24, }, raster
230: .getPixel(1, 1, rgb)));
231: }
232:
233: /**
234: * Tests an image with short pixels.
235: */
236: public void testShortData() {
237: short[][] pixels = new short[][] {
238: new short[] { 1, 4, 7, 10, },
239: new short[] { 2, 5, 8, 11, },
240: new short[] { 3, 6, 9, 12, }, };
241: RenderedImage source = RasterAdapter.buildShortImage(pixels, 2,
242: 2);
243:
244: RenderedOp sink = JAI.create("unaryfunction",
245: new ParameterBlock().addSource(source).add(
246: ADDED_TO_ITSELF));
247: Raster raster = sink.getData();
248:
249: assertEquals(0, raster.getMinX());
250: assertEquals(0, raster.getMinY());
251: assertEquals(2, raster.getWidth());
252: assertEquals(2, raster.getHeight());
253: assertEquals(3, raster.getNumBands());
254:
255: int[] rgb = new int[3];
256: assertTrue(Arrays.equals(new int[] { 2, 4, 6, }, raster
257: .getPixel(0, 0, rgb)));
258: assertTrue(Arrays.equals(new int[] { 8, 10, 12, }, raster
259: .getPixel(1, 0, rgb)));
260: assertTrue(Arrays.equals(new int[] { 14, 16, 18, }, raster
261: .getPixel(0, 1, rgb)));
262: assertTrue(Arrays.equals(new int[] { 20, 22, 24, }, raster
263: .getPixel(1, 1, rgb)));
264: }
265:
266: /**
267: * Tests an image with unsigned short pixels.
268: */
269: public void testUShortData() {
270: short[][] pixels = new short[][] {
271: new short[] { 1, 4, 7, 10, },
272: new short[] { 2, 5, 8, 11, },
273: new short[] { 3, 6, 9, 12, }, };
274: RenderedImage source = RasterAdapter.buildUShortImage(pixels,
275: 2, 2);
276:
277: RenderedOp sink = JAI.create("unaryfunction",
278: new ParameterBlock().addSource(source).add(
279: ADDED_TO_ITSELF));
280: Raster raster = sink.getData();
281:
282: assertEquals(0, raster.getMinX());
283: assertEquals(0, raster.getMinY());
284: assertEquals(2, raster.getWidth());
285: assertEquals(2, raster.getHeight());
286: assertEquals(3, raster.getNumBands());
287:
288: int[] rgb = new int[3];
289: assertTrue(Arrays.equals(new int[] { 2, 4, 6, }, raster
290: .getPixel(0, 0, rgb)));
291: assertTrue(Arrays.equals(new int[] { 8, 10, 12, }, raster
292: .getPixel(1, 0, rgb)));
293: assertTrue(Arrays.equals(new int[] { 14, 16, 18, }, raster
294: .getPixel(0, 1, rgb)));
295: assertTrue(Arrays.equals(new int[] { 20, 22, 24, }, raster
296: .getPixel(1, 1, rgb)));
297: }
298:
299: /**
300: * Make sure that if the data type is unknown that the exception is thrown in
301: * {@link UnaryFunctionOpImage#compute(RasterAccessor[], RasterAccessor)}.
302: */
303: public void testUnknowDataType() {
304: // all this is to have a raster accessor
305: BufferedImage image = new BufferedImage(1, 1,
306: BufferedImage.TYPE_3BYTE_BGR);
307:
308: RasterFormatTag tag = new RasterFormatTag(
309: new ComponentSampleModel(DataBuffer.TYPE_DOUBLE, 1, 1,
310: 1, 1, new int[] { 0, }), 0);
311: ColorModel color_model = new ColorModel(8) {
312: @Override
313: @SuppressWarnings("unused")
314: public int getAlpha(int pixel) {
315: return 0;
316: }
317:
318: @Override
319: @SuppressWarnings("unused")
320: public int getBlue(int pixel) {
321: return 0;
322: }
323:
324: @Override
325: @SuppressWarnings("unused")
326: public int getGreen(int pixel) {
327: return 0;
328: }
329:
330: @Override
331: @SuppressWarnings("unused")
332: public int getRed(int pixel) {
333: return 0;
334: }
335: };
336: RasterAccessor sink = new RasterAccessor(image.getData(),
337: new Rectangle(0, 0, 1, 1), tag, color_model) {
338: @Override
339: public int getDataType() {
340: return 42;
341: }
342: };
343:
344: try {
345: UnaryFunctionOpImage op_image = new UnaryFunctionOpImage(
346: image, UnaryFunctionOpImageTest.ADDED_TO_ITSELF,
347: null, null);
348: op_image.compute(null, sink);
349: fail("did not throw IllegalStateException");
350: } catch (IllegalStateException ise) {
351: // normal behaviour
352: }
353: }
354:
355: /**
356: * Tests the English messages.
357: */
358: public void testEnglishMessages() {
359: Locale locale = Locale.getDefault();
360: Locale.setDefault(Locale.ENGLISH);
361:
362: try {
363: String[] expected = new String[] { "The pixel type (10) is unknown", };
364:
365: RenderedImage source = new BufferedImage(1, 1,
366: BufferedImage.TYPE_INT_ARGB);
367: UnaryFunctionOpImage op_image = new UnaryFunctionOpImage(
368: source, null, null, null);
369: String[] got = new String[] { op_image
370: .getUnknownDataTypeErrorMessage(10), };
371:
372: assertEquals(expected.length, got.length);
373:
374: for (int i = 0; i < expected.length; i++) {
375: assertEquals("[" + i + "]", expected[i], got[i]);
376: }
377: } finally {
378: Locale.setDefault(locale);
379: }
380: }
381:
382: /**
383: * Tests the French messages.
384: */
385: public void testFrenchMessages() {
386: Locale locale = Locale.getDefault();
387: Locale.setDefault(Locale.FRENCH);
388:
389: try {
390: String[] expected = new String[] { "Type de pixel inconnu (10)", };
391:
392: RenderedImage source = new BufferedImage(1, 1,
393: BufferedImage.TYPE_INT_ARGB);
394: UnaryFunctionOpImage op_image = new UnaryFunctionOpImage(
395: source, null, null, null);
396: String[] got = new String[] { op_image
397: .getUnknownDataTypeErrorMessage(10), };
398:
399: assertEquals(expected.length, got.length);
400:
401: for (int i = 0; i < expected.length; i++) {
402: assertEquals("[" + i + "]", expected[i], got[i]);
403: }
404: } finally {
405: Locale.setDefault(locale);
406: }
407: }
408:
409: //---------------------------
410: // Class methods
411: //---------------------------
412:
413: /**
414: * Runs only this test.
415: * @param args ignored.
416: */
417: public static void main(String... args) {
418: junit.swingui.TestRunner.run(UnaryFunctionOpImageTest.class);
419: }
420:
421: }
422:
423: /*
424: * $Log: UnaryFunctionOpImageTest.java,v $
425: * Revision 1.6 2007/06/05 20:51:48 forklabs
426: * Reflect a change in error messages.
427: *
428: * Revision 1.5 2007/06/04 21:23:46 forklabs
429: * Now uses the factory methods in class RasterAdapter.
430: *
431: * Revision 1.4 2007/05/10 18:10:40 forklabs
432: * Moved the indexed test from the descriptor to the opimage.
433: *
434: * Revision 1.3 2007/05/10 17:59:32 forklabs
435: * Removed unnecessary @Override
436: *
437: * Revision 1.2 2007/05/03 20:27:37 forklabs
438: * Trimmed trailing spaces.
439: *
440: * Revision 1.1 2007/05/03 18:32:28 forklabs
441: * Initial commit for the unaryfunction operator.
442: *
443: */
|