001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/opimage/ImageFunction3DOpImageTest.java,v 1.2 2007/07/17 16:51:51 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.RenderingHints;
010: import java.awt.image.BandedSampleModel;
011: import java.awt.image.DataBuffer;
012: import java.awt.image.Raster;
013: import java.awt.image.RenderedImage;
014: import java.awt.image.SampleModel;
015: import javax.media.jai.CollectionImage;
016: import javax.media.jai.ImageLayout;
017: import javax.media.jai.JAI;
018: import junit.framework.TestCase;
019: import ca.forklabs.media.jai.ImageFunction3D;
020: import ca.forklabs.media.jai.RasterAdapter;
021:
022: /**
023: * Class {@code ImageFunction3DOpImageTest} tests class
024: * {@link ImageFunction3DOpImage}.
025: *
026: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.opimage.ImageFunction3DOpImageTest">Daniel Léonard</a>
027: * @version $Revision: 1.2 $
028: */
029: @SuppressWarnings("nls")
030: public class ImageFunction3DOpImageTest extends TestCase {
031:
032: //---------------------------
033: // Constructors
034: //---------------------------
035:
036: /**
037: * Constructor.
038: * @param name the name of this test.
039: */
040: public ImageFunction3DOpImageTest(String name) {
041: super (name);
042: }
043:
044: //---------------------------
045: // Test methods
046: //---------------------------
047:
048: private void compareArrays(String message, float[] expected,
049: float[] got) {
050: assertEquals(message, expected.length, got.length);
051: for (int i = 0; i < expected.length; i++) {
052: assertEquals(message, expected[i], got[i], 10e-6f);
053: }
054: }
055:
056: private void compareArrays(String message, double[] expected,
057: double[] got) {
058: assertEquals(message, expected.length, got.length);
059: for (int i = 0; i < expected.length; i++) {
060: assertEquals(message, expected[i], got[i], 10e-6);
061: }
062: }
063:
064: private static class ConstantFunction implements ImageFunction3D {
065:
066: private int bands;
067: private boolean is_complex;
068:
069: /**
070: * Constructor.
071: * @param bands the desired number of bands.
072: * @param is_complex wheter the function is complex or not.
073: */
074: public ConstantFunction(int bands, boolean is_complex) {
075: this .bands = bands;
076: this .is_complex = is_complex;
077: }
078:
079: /**
080: * Generates the requested band
081: */
082: @Override
083: public void getBand(int band, int slice, int dimX, int dimY,
084: int dimZ, float[] real, float[] imag) {
085: for (int i = 0; i < dimX * dimY; i++) {
086: real[i] = slice;
087: if (null != imag) {
088: imag[i] = slice;
089: }
090: }
091: }
092:
093: /**
094: * Generates the requested band.
095: */
096: @Override
097: public void getBand(int band, int slice, int dimX, int dimY,
098: int dimZ, double[] real, double[] imag) {
099: for (int i = 0; i < dimX * dimY; i++) {
100: real[i] = slice;
101: if (null != imag) {
102: imag[i] = slice;
103: }
104: }
105: }
106:
107: /**
108: * Gets the number of band.
109: */
110: @Override
111: public int getNumBands() {
112: return this .bands;
113: }
114:
115: /**
116: * Determines if the function is complex.
117: */
118: @Override
119: public boolean isComplex() {
120: return this .is_complex;
121: }
122: }
123:
124: /**
125: * Tests that it works for real floats
126: */
127: public void testRealFloat() {
128: Raster[] solutions = new Raster[] {
129: RasterAdapter.buildFloatImage(
130: new float[][] {
131: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
132: 0.0f, 0.0f, 0.0f, },
133: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
134: 0.0f, 0.0f, 0.0f, }, }, 3, 3)
135: .getData(),
136: RasterAdapter.buildFloatImage(
137: new float[][] {
138: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
139: 1.0f, 1.0f, 1.0f, },
140: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
141: 1.0f, 1.0f, 1.0f, }, }, 3, 3)
142: .getData(),
143: RasterAdapter.buildFloatImage(
144: new float[][] {
145: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
146: 2.0f, 2.0f, 2.0f, },
147: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
148: 2.0f, 2.0f, 2.0f, }, }, 3, 3)
149: .getData(), };
150:
151: ImageFunction3D function = new ConstantFunction(2, false);
152: int width = 3;
153: int height = 3;
154: int depth = 3;
155:
156: SampleModel sample_model = new BandedSampleModel(
157: DataBuffer.TYPE_INT, 1, 1, 1);
158: ImageLayout layout = new ImageLayout();
159: layout.setSampleModel(sample_model);
160:
161: RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT,
162: layout);
163:
164: CollectionImage images = new ImageFunction3DOpImage(function,
165: width, height, depth, hints);
166:
167: for (int i = 0; i < images.size(); i++) {
168: RenderedImage image = (RenderedImage) images.get(i);
169: Raster raster = image.getData();
170:
171: assertEquals(0, raster.getMinX());
172: assertEquals(0, raster.getMinY());
173: assertEquals(3, raster.getWidth());
174: assertEquals(3, raster.getHeight());
175: assertEquals(2, raster.getNumBands());
176: assertEquals(DataBuffer.TYPE_FLOAT, raster.getSampleModel()
177: .getDataType());
178:
179: Raster solution = solutions[i];
180:
181: float[] got = null;
182: float[] expected = null;
183: for (int y = 0; y < 3; y++) {
184: for (int x = 0; x < 3; x++) {
185: got = raster.getPixel(x, y, got);
186: expected = solution.getPixel(x, y, expected);
187: this .compareArrays("(" + x + "," + y + "," + i
188: + ")", expected, got);
189: }
190: }
191: }
192: }
193:
194: /**
195: * Tests that it works for real doubles
196: */
197: public void testRealDouble() {
198: Raster[] solutions = new Raster[] {
199: RasterAdapter.buildFloatImage(
200: new float[][] {
201: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
202: 0.0f, 0.0f, 0.0f, },
203: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
204: 0.0f, 0.0f, 0.0f, }, }, 3, 3)
205: .getData(),
206: RasterAdapter.buildFloatImage(
207: new float[][] {
208: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
209: 1.0f, 1.0f, 1.0f, },
210: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
211: 1.0f, 1.0f, 1.0f, }, }, 3, 3)
212: .getData(),
213: RasterAdapter.buildFloatImage(
214: new float[][] {
215: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
216: 2.0f, 2.0f, 2.0f, },
217: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
218: 2.0f, 2.0f, 2.0f, }, }, 3, 3)
219: .getData(), };
220:
221: ImageFunction3D function = new ConstantFunction(2, false);
222: int width = 3;
223: int height = 3;
224: int depth = 3;
225:
226: SampleModel sample_model = new BandedSampleModel(
227: DataBuffer.TYPE_DOUBLE, 1, 1, 1);
228: ImageLayout layout = new ImageLayout();
229: layout.setSampleModel(sample_model);
230:
231: RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT,
232: layout);
233:
234: CollectionImage images = new ImageFunction3DOpImage(function,
235: width, height, depth, hints);
236:
237: for (int i = 0; i < images.size(); i++) {
238: RenderedImage image = (RenderedImage) images.get(i);
239: Raster raster = image.getData();
240:
241: assertEquals(0, raster.getMinX());
242: assertEquals(0, raster.getMinY());
243: assertEquals(3, raster.getWidth());
244: assertEquals(3, raster.getHeight());
245: assertEquals(2, raster.getNumBands());
246: assertEquals(DataBuffer.TYPE_DOUBLE, raster
247: .getSampleModel().getDataType());
248:
249: Raster solution = solutions[i];
250:
251: double[] got = null;
252: double[] expected = null;
253: for (int y = 0; y < 3; y++) {
254: for (int x = 0; x < 3; x++) {
255: got = raster.getPixel(x, y, got);
256: expected = solution.getPixel(x, y, expected);
257: this .compareArrays("(" + x + "," + y + "," + i
258: + ")", expected, got);
259: }
260: }
261: }
262: }
263:
264: /**
265: * Tests that it works for complex floats
266: */
267: public void testComplexFloat() {
268: Raster[] solutions = new Raster[] {
269: RasterAdapter.buildFloatImage(
270: new float[][] {
271: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
272: 0.0f, 0.0f, 0.0f, },
273: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
274: 0.0f, 0.0f, 0.0f, }, }, 3, 3)
275: .getData(),
276: RasterAdapter.buildFloatImage(
277: new float[][] {
278: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
279: 1.0f, 1.0f, 1.0f, },
280: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
281: 1.0f, 1.0f, 1.0f, }, }, 3, 3)
282: .getData(),
283: RasterAdapter.buildFloatImage(
284: new float[][] {
285: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
286: 2.0f, 2.0f, 2.0f, },
287: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
288: 2.0f, 2.0f, 2.0f, }, }, 3, 3)
289: .getData(), };
290:
291: ImageFunction3D function = new ConstantFunction(1, true);
292: int width = 3;
293: int height = 3;
294: int depth = 3;
295:
296: SampleModel sample_model = new BandedSampleModel(
297: DataBuffer.TYPE_INT, 1, 1, 1);
298: ImageLayout layout = new ImageLayout();
299: layout.setSampleModel(sample_model);
300:
301: RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT,
302: layout);
303:
304: CollectionImage images = new ImageFunction3DOpImage(function,
305: width, height, depth, hints);
306:
307: for (int i = 0; i < images.size(); i++) {
308: RenderedImage image = (RenderedImage) images.get(i);
309: Raster raster = image.getData();
310:
311: assertEquals(0, raster.getMinX());
312: assertEquals(0, raster.getMinY());
313: assertEquals(3, raster.getWidth());
314: assertEquals(3, raster.getHeight());
315: assertEquals(2, raster.getNumBands());
316: assertEquals(DataBuffer.TYPE_FLOAT, raster.getSampleModel()
317: .getDataType());
318:
319: Raster solution = solutions[i];
320:
321: float[] got = null;
322: float[] expected = null;
323: for (int y = 0; y < 3; y++) {
324: for (int x = 0; x < 3; x++) {
325: got = raster.getPixel(x, y, got);
326: expected = solution.getPixel(x, y, expected);
327: this .compareArrays("(" + x + "," + y + "," + i
328: + ")", expected, got);
329: }
330: }
331: }
332: }
333:
334: /**
335: * Tests that it works for complex floats
336: */
337: public void testComplexDouble() {
338: Raster[] solutions = new Raster[] {
339: RasterAdapter.buildFloatImage(
340: new float[][] {
341: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
342: 0.0f, 0.0f, 0.0f, },
343: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
344: 0.0f, 0.0f, 0.0f, }, }, 3, 3)
345: .getData(),
346: RasterAdapter.buildFloatImage(
347: new float[][] {
348: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
349: 1.0f, 1.0f, 1.0f, },
350: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
351: 1.0f, 1.0f, 1.0f, }, }, 3, 3)
352: .getData(),
353: RasterAdapter.buildFloatImage(
354: new float[][] {
355: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
356: 2.0f, 2.0f, 2.0f, },
357: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
358: 2.0f, 2.0f, 2.0f, }, }, 3, 3)
359: .getData(), };
360:
361: ImageFunction3D function = new ConstantFunction(1, true);
362: int width = 3;
363: int height = 3;
364: int depth = 3;
365:
366: SampleModel sample_model = new BandedSampleModel(
367: DataBuffer.TYPE_DOUBLE, 1, 1, 1);
368: ImageLayout layout = new ImageLayout();
369: layout.setSampleModel(sample_model);
370:
371: RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT,
372: layout);
373:
374: CollectionImage images = new ImageFunction3DOpImage(function,
375: width, height, depth, hints);
376:
377: for (int i = 0; i < images.size(); i++) {
378: RenderedImage image = (RenderedImage) images.get(i);
379: Raster raster = image.getData();
380:
381: assertEquals(0, raster.getMinX());
382: assertEquals(0, raster.getMinY());
383: assertEquals(3, raster.getWidth());
384: assertEquals(3, raster.getHeight());
385: assertEquals(2, raster.getNumBands());
386: assertEquals(DataBuffer.TYPE_DOUBLE, raster
387: .getSampleModel().getDataType());
388:
389: Raster solution = solutions[i];
390:
391: double[] got = null;
392: double[] expected = null;
393: for (int y = 0; y < 3; y++) {
394: for (int x = 0; x < 3; x++) {
395: got = raster.getPixel(x, y, got);
396: expected = solution.getPixel(x, y, expected);
397: this .compareArrays("(" + x + "," + y + "," + i
398: + ")", expected, got);
399: }
400: }
401: }
402: }
403:
404: //---------------------------
405: // Class methods
406: //---------------------------
407:
408: /**
409: * Runs only this test.
410: * @param args ignored.
411: */
412: public static void main(String... args) {
413: junit.swingui.TestRunner.run(ImageFunction3DOpImageTest.class);
414: }
415:
416: }
417:
418: /*
419: * $Log: ImageFunction3DOpImageTest.java,v $
420: * Revision 1.2 2007/07/17 16:51:51 forklabs
421: * Operation renamed from imagefunction3d to imagefunction.
422: *
423: * Revision 1.1 2007/07/05 18:30:38 forklabs
424: * Operator imagefunction3d.
425: *
426: * Revision 1.1 2007/06/05 02:27:50 forklabs
427: * Operators dft3d and idft3d
428: *
429: */
|