001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/RasterAdapterTest.java,v 1.3 2007/06/13 18:55:06 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;
022:
023: import java.awt.Point;
024: import java.awt.image.BandedSampleModel;
025: import java.awt.image.DataBuffer;
026: import java.awt.image.DataBufferFloat;
027: import java.awt.image.Raster;
028: import java.awt.image.RenderedImage;
029: import java.awt.image.SampleModel;
030: import java.awt.image.WritableRaster;
031: import java.util.Arrays;
032: import java.util.Locale;
033: import junit.framework.TestCase;
034: import ca.forklabs.media.jai.RasterAdapter;
035:
036: /**
037: * Class {@code RasterAdapterTest} tests class {@link RasterAdapter}.
038: *
039: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.RasterAdapterTest">Daniel Léonard</a>
040: * @version $Revision: 1.3 $
041: */
042: @SuppressWarnings("nls")
043: public class RasterAdapterTest extends TestCase {
044:
045: //---------------------------
046: // Constructor
047: //---------------------------
048:
049: /**
050: * Constructor.
051: * @param name the name of this test.
052: */
053: public RasterAdapterTest(String name) {
054: super (name);
055: }
056:
057: //---------------------------
058: // Test methods
059: //---------------------------
060:
061: /**
062: * Gets a simple writable raster.
063: * @return the raster.
064: */
065: private WritableRaster getWritableRaster() {
066: float[][] pixels = new float[][] { { 1.0f, 2.0f, 3.0f, 4.0f, }, };
067:
068: int columns = 2;
069: int lines = 2;
070: int size = columns * lines;
071: int bands = pixels.length;
072:
073: DataBuffer data_buffer = new DataBufferFloat(pixels, size);
074: SampleModel sample_model = new BandedSampleModel(data_buffer
075: .getDataType(), columns, lines, bands);
076: Point origin = new Point(0, 0);
077: WritableRaster raster = Raster.createWritableRaster(
078: sample_model, data_buffer, origin);
079: return raster;
080: }
081:
082: /**
083: * Tests that the returned tile is correct.
084: */
085: public void testGetTiles() {
086: WritableRaster writable_raster = this .getWritableRaster();
087:
088: RasterAdapter image = new RasterAdapter(writable_raster);
089:
090: assertSame("sample model", writable_raster.getSampleModel(),
091: image.getSampleModel());
092:
093: assertEquals("min tile x", 0, image.getMinTileX());
094: assertEquals("min tile y", 0, image.getMinTileY());
095: assertEquals("num tile x", 1, image.getNumXTiles());
096: assertEquals("num tile y", 1, image.getNumYTiles());
097:
098: Raster raster = image.getTile(0, 0);
099: assertNotNull("raster not null", raster);
100: assertEquals("raster min x", 0, raster.getMinX());
101: assertEquals("raster min y", 0, raster.getMinY());
102: assertEquals("raster width", 2, raster.getWidth());
103: assertEquals("raster height", 2, raster.getHeight());
104:
105: try {
106: image.getTile(1, 0);
107: fail("getTile(1, 0) did not throw exception");
108: } catch (IndexOutOfBoundsException ioobe) {
109: // normal behaviour
110: }
111:
112: try {
113: image.getTile(0, 1);
114: fail("getTile(0, 1) did not throw exception");
115: } catch (IndexOutOfBoundsException ioobe) {
116: // normal behaviour
117: }
118:
119: try {
120: image.getTile(1, 1);
121: fail("getTile(1, 1) did not throw exception");
122: } catch (IndexOutOfBoundsException ioobe) {
123: // normal behaviour
124: }
125: }
126:
127: /**
128: * Tests {@link RasterAdapter#buildByteImage(byte[][], int, int)}.
129: */
130: public void testBuildByteImage() {
131: byte[][] pixels = new byte[][] { new byte[] { 1, 4, 7, 10, },
132: new byte[] { 2, 5, 8, 11, },
133: new byte[] { 3, 6, 9, 12, }, };
134: RenderedImage image = RasterAdapter
135: .buildByteImage(pixels, 2, 2);
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(3, raster.getNumBands());
143:
144: int[] rgb = new int[3];
145: assertTrue(Arrays.equals(new int[] { 1, 2, 3, }, raster
146: .getPixel(0, 0, rgb)));
147: assertTrue(Arrays.equals(new int[] { 4, 5, 6, }, raster
148: .getPixel(1, 0, rgb)));
149: assertTrue(Arrays.equals(new int[] { 7, 8, 9, }, raster
150: .getPixel(0, 1, rgb)));
151: assertTrue(Arrays.equals(new int[] { 10, 11, 12, }, raster
152: .getPixel(1, 1, rgb)));
153: }
154:
155: /**
156: * Tests {@link RasterAdapter#buildShortImage(short[][], int, int)}.
157: */
158: public void testBuildShortImage() {
159: short[][] pixels = new short[][] {
160: new short[] { 1, 4, 7, 10, },
161: new short[] { 2, 5, 8, 11, },
162: new short[] { 3, 6, 9, 12, }, };
163: RenderedImage image = RasterAdapter.buildShortImage(pixels, 2,
164: 2);
165: Raster raster = image.getData();
166:
167: assertEquals(0, raster.getMinX());
168: assertEquals(0, raster.getMinY());
169: assertEquals(2, raster.getWidth());
170: assertEquals(2, raster.getHeight());
171: assertEquals(3, raster.getNumBands());
172:
173: int[] rgb = new int[3];
174: assertTrue(Arrays.equals(new int[] { 1, 2, 3, }, raster
175: .getPixel(0, 0, rgb)));
176: assertTrue(Arrays.equals(new int[] { 4, 5, 6, }, raster
177: .getPixel(1, 0, rgb)));
178: assertTrue(Arrays.equals(new int[] { 7, 8, 9, }, raster
179: .getPixel(0, 1, rgb)));
180: assertTrue(Arrays.equals(new int[] { 10, 11, 12, }, raster
181: .getPixel(1, 1, rgb)));
182: }
183:
184: /**
185: * Tests {@link RasterAdapter#buildUShortImage(short[][], int, int)}.
186: */
187: public void testBuildUShortImage() {
188: short[][] pixels = new short[][] {
189: new short[] { 1, 4, 7, 10, },
190: new short[] { 2, 5, 8, 11, },
191: new short[] { 3, 6, 9, 12, }, };
192: RenderedImage image = RasterAdapter.buildUShortImage(pixels, 2,
193: 2);
194: Raster raster = image.getData();
195:
196: assertEquals(0, raster.getMinX());
197: assertEquals(0, raster.getMinY());
198: assertEquals(2, raster.getWidth());
199: assertEquals(2, raster.getHeight());
200: assertEquals(3, raster.getNumBands());
201:
202: int[] rgb = new int[3];
203: assertTrue(Arrays.equals(new int[] { 1, 2, 3, }, raster
204: .getPixel(0, 0, rgb)));
205: assertTrue(Arrays.equals(new int[] { 4, 5, 6, }, raster
206: .getPixel(1, 0, rgb)));
207: assertTrue(Arrays.equals(new int[] { 7, 8, 9, }, raster
208: .getPixel(0, 1, rgb)));
209: assertTrue(Arrays.equals(new int[] { 10, 11, 12, }, raster
210: .getPixel(1, 1, rgb)));
211: }
212:
213: /**
214: * Tests {@link RasterAdapter#buildIntImage(int[][], int, int)}.
215: */
216: public void testBuildIntImage() {
217: int[][] pixels = new int[][] { new int[] { 1, 4, 7, 10, },
218: new int[] { 2, 5, 8, 11, }, new int[] { 3, 6, 9, 12, }, };
219: RenderedImage image = RasterAdapter.buildIntImage(pixels, 2, 2);
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(3, raster.getNumBands());
227:
228: int[] rgb = new int[3];
229: assertTrue(Arrays.equals(new int[] { 1, 2, 3, }, raster
230: .getPixel(0, 0, rgb)));
231: assertTrue(Arrays.equals(new int[] { 4, 5, 6, }, raster
232: .getPixel(1, 0, rgb)));
233: assertTrue(Arrays.equals(new int[] { 7, 8, 9, }, raster
234: .getPixel(0, 1, rgb)));
235: assertTrue(Arrays.equals(new int[] { 10, 11, 12, }, raster
236: .getPixel(1, 1, rgb)));
237: }
238:
239: /**
240: * Tests {@link RasterAdapter#buildFloatImage(float[][], int, int)}.
241: */
242: public void testBuildFloatImage() {
243: float[][] pixels = new float[][] {
244: new float[] { 1.1f, 4.2f, 7.3f, 10.4f, },
245: new float[] { 2.1f, 5.2f, 8.3f, 11.4f, },
246: new float[] { 3.1f, 6.2f, 9.3f, 12.4f, }, };
247: RenderedImage image = RasterAdapter.buildFloatImage(pixels, 2,
248: 2);
249: Raster raster = image.getData();
250:
251: assertEquals(0, raster.getMinX());
252: assertEquals(0, raster.getMinY());
253: assertEquals(2, raster.getWidth());
254: assertEquals(2, raster.getHeight());
255: assertEquals(3, raster.getNumBands());
256:
257: float[] rgb = new float[3];
258: assertTrue(Arrays.equals(new float[] { 1.1f, 2.1f, 3.1f, },
259: raster.getPixel(0, 0, rgb)));
260: assertTrue(Arrays.equals(new float[] { 4.2f, 5.2f, 6.2f, },
261: raster.getPixel(1, 0, rgb)));
262: assertTrue(Arrays.equals(new float[] { 7.3f, 8.3f, 9.3f, },
263: raster.getPixel(0, 1, rgb)));
264: assertTrue(Arrays.equals(new float[] { 10.4f, 11.4f, 12.4f, },
265: raster.getPixel(1, 1, rgb)));
266: }
267:
268: /**
269: * Tests {@link RasterAdapter#buildDoubleImage(double[][], int, int)}.
270: */
271: public void testBuildDoubleImage() {
272: double[][] pixels = new double[][] {
273: new double[] { 1.1, 4.2, 7.3, 10.4, },
274: new double[] { 2.1, 5.2, 8.3, 11.4, },
275: new double[] { 3.1, 6.2, 9.3, 12.4, }, };
276: RenderedImage image = RasterAdapter.buildDoubleImage(pixels, 2,
277: 2);
278: Raster raster = image.getData();
279:
280: assertEquals(0, raster.getMinX());
281: assertEquals(0, raster.getMinY());
282: assertEquals(2, raster.getWidth());
283: assertEquals(2, raster.getHeight());
284: assertEquals(3, raster.getNumBands());
285:
286: double[] rgb = new double[3];
287: assertTrue(Arrays.equals(new double[] { 1.1, 2.1, 3.1, },
288: raster.getPixel(0, 0, rgb)));
289: assertTrue(Arrays.equals(new double[] { 4.2, 5.2, 6.2, },
290: raster.getPixel(1, 0, rgb)));
291: assertTrue(Arrays.equals(new double[] { 7.3, 8.3, 9.3, },
292: raster.getPixel(0, 1, rgb)));
293: assertTrue(Arrays.equals(new double[] { 10.4, 11.4, 12.4, },
294: raster.getPixel(1, 1, rgb)));
295: }
296:
297: /**
298: * Tests the English error messages.
299: */
300: public void testEnglishErrorMessages() {
301: Locale locale = Locale.getDefault();
302: Locale.setDefault(Locale.ENGLISH);
303:
304: try {
305: String[] expected = new String[] { "Requested tile is (1, 1), only (0, 0) is available", };
306:
307: String[] got = new String[] { RasterAdapter
308: .getBadTileErrorMessage(1, 1), };
309:
310: assertEquals(expected.length, got.length);
311:
312: for (int i = 0; i < expected.length; i++) {
313: assertEquals("[" + i + "]", expected[i], got[i]);
314: }
315: } finally {
316: Locale.setDefault(locale);
317: }
318: }
319:
320: /**
321: * Tests the French error messages.
322: */
323: public void testFrenchErrorMessages() {
324: Locale locale = Locale.getDefault();
325: Locale.setDefault(Locale.FRENCH);
326:
327: try {
328: String[] expected = new String[] { "La tuile demandée est (1, 1), seulement la tuile (0, 0) est disponible", };
329:
330: String[] got = new String[] { RasterAdapter
331: .getBadTileErrorMessage(1, 1), };
332:
333: assertEquals(expected.length, got.length);
334:
335: for (int i = 0; i < expected.length; i++) {
336: assertEquals("[" + i + "]", expected[i], got[i]);
337: }
338: } finally {
339: Locale.setDefault(locale);
340: }
341: }
342:
343: //---------------------------
344: // Class methods
345: //---------------------------
346:
347: /**
348: * Runs only this test.
349: * @param args ignored.
350: */
351: public static void main(String... args) {
352: junit.swingui.TestRunner.run(RasterAdapterTest.class);
353: }
354:
355: }
356:
357: /*
358: * $Log: RasterAdapterTest.java,v $
359: * Revision 1.3 2007/06/13 18:55:06 forklabs
360: * Removed empty lines.
361: *
362: * Revision 1.2 2007/06/04 21:20:09 forklabs
363: * Added new method to easily create images from memory.
364: *
365: * Revision 1.1 2007/05/03 18:31:28 forklabs
366: * Intial commit for RasterAdapter.
367: *
368: */
|