001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/DFT3DCompatibilityTest.java,v 1.5 2007/07/17 16:49:04 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.image.Raster;
024: import java.awt.image.RenderedImage;
025: import java.awt.image.renderable.ParameterBlock;
026: import java.util.Arrays;
027: import java.util.Collection;
028: import java.util.Iterator;
029: import javax.media.jai.JAI;
030: import javax.media.jai.operator.DFTDataNature;
031: import javax.media.jai.operator.DFTScalingType;
032: import junit.framework.TestCase;
033: import ca.forklabs.media.jai.ParameterBlockUtil;
034: import ca.forklabs.media.jai.RasterAdapter;
035:
036: /**
037: * Class {@code DFT3DCompatibilityTest} tests both operator <em>dft3d</em> and
038: * <em>idft3d</em> using the JAI architecture.
039: *
040: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.DFT3DCompatibilityTest">Daniel Léonard</a>
041: * @version $Revision: 1.5 $
042: */
043: @SuppressWarnings("nls")
044: public class DFT3DCompatibilityTest extends TestCase {
045:
046: //---------------------------
047: // Constructors
048: //---------------------------
049:
050: /**
051: * Constructor.
052: * @param name the name of this test.
053: */
054: public DFT3DCompatibilityTest(String name) {
055: super (name);
056: }
057:
058: //---------------------------
059: // Test methods
060: //---------------------------
061:
062: private void compareArrays(String message, float[] expected,
063: float[] got) {
064: assertEquals(message, expected.length, got.length);
065: for (int i = 0; i < expected.length; i++) {
066: assertEquals(message, expected[i], got[i], 10e-6f);
067: }
068: }
069:
070: /**
071: * Tests that both operator <em>dft</em> and <em>idft</em> work together
072: * when using the convenience methods.
073: */
074: @SuppressWarnings("unchecked")
075: public void testCompatibility() {
076: RenderedImage[] sources = new RenderedImage[] {
077: RasterAdapter.buildFloatImage(new float[][] { { 1.0f,
078: 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f,
079: 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f,
080: 16.0f, }, }, 4, 4),
081: RasterAdapter.buildFloatImage(new float[][] { { 17.0f,
082: 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f,
083: 24.0f, 25.0f, 26.0f, 27.0f, 28.0f, 29.0f,
084: 30.0f, 31.0f, 32.0f, }, }, 4, 4),
085: RasterAdapter.buildFloatImage(new float[][] { { 33.0f,
086: 34.0f, 35.0f, 36.0f, 37.0f, 38.0f, 39.0f,
087: 40.0f, 41.0f, 42.0f, 43.0f, 44.0f, 45.0f,
088: 46.0f, 47.0f, 48.0f, }, }, 4, 4),
089: RasterAdapter.buildFloatImage(new float[][] { { 49.0f,
090: 50.0f, 51.0f, 52.0f, 53.0f, 54.0f, 55.0f,
091: 56.0f, 57.0f, 58.0f, 59.0f, 60.0f, 61.0f,
092: 62.0f, 63.0f, 64.0f, }, }, 4, 4), };
093:
094: DFTScalingType scaling = DFT3DDescriptor.SCALING_DEFAULT_VALUE;
095: DFTDataNature nature = DFT3DDescriptor.NATURE_DEFAULT_VALUE;
096: Collection<RenderedImage> dft = DFT3DDescriptor
097: .createCollection(Arrays.asList(sources), scaling,
098: nature, null);
099:
100: assertEquals(4, dft.size());
101:
102: Raster[] solutions1 = new Raster[] {
103: RasterAdapter.buildFloatImage(
104: new float[][] {
105: { 2080.0f, -32.0f, -32.0f, -32.0f,
106: -128.0f, 0.0f, 0.0f, 0.0f,
107: -128.0f, 0.0f, 0.0f, 0.0f,
108: -128.0f, 0.0f, 0.0f, 0.0f, },
109: { 0.0f, 32.0f, 0.0f, -32.0f, 128.0f,
110: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
111: 0.0f, 0.0f, -128.0f, 0.0f,
112: 0.0f, 0.0f, }, }, 4, 4)
113: .getData(),
114: RasterAdapter
115: .buildFloatImage(
116: new float[][] {
117: { -512.0f, 0.0f, 0.0f, 0.0f,
118: 0.0f, 0.0f, 0.0f, 0.0f,
119: 0.0f, 0.0f, 0.0f, 0.0f,
120: 0.0f, 0.0f, 0.0f, 0.0f, },
121: { 512.0f, 0.0f, 0.0f, 0.0f,
122: 0.0f, 0.0f, 0.0f, 0.0f,
123: 0.0f, 0.0f, 0.0f, 0.0f,
124: 0.0f, 0.0f, 0.0f, 0.0f, }, },
125: 4, 4).getData(),
126: RasterAdapter
127: .buildFloatImage(
128: new float[][] {
129: { -512.0f, 0.0f, 0.0f, 0.0f,
130: 0.0f, 0.0f, 0.0f, 0.0f,
131: 0.0f, 0.0f, 0.0f, 0.0f,
132: 0.0f, 0.0f, 0.0f, 0.0f, },
133: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
134: 0.0f, 0.0f, 0.0f, 0.0f,
135: 0.0f, 0.0f, 0.0f, 0.0f,
136: 0.0f, 0.0f, 0.0f, }, },
137: 4, 4).getData(),
138: RasterAdapter.buildFloatImage(
139: new float[][] {
140: { -512.0f, 0.0f, 0.0f, 0.0f, 0.0f,
141: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
142: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
143: 0.0f, },
144: { -512.0f, 0.0f, 0.0f, 0.0f, 0.0f,
145: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
146: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
147: 0.0f, }, }, 4, 4).getData(), };
148:
149: int i = 0;
150: for (Object o : dft) {
151: RenderedImage sink = (RenderedImage) o;
152: Raster raster = sink.getData();
153:
154: assertEquals(0, raster.getMinX());
155: assertEquals(0, raster.getMinY());
156: assertEquals(4, raster.getWidth());
157: assertEquals(4, raster.getHeight());
158: assertEquals(2, raster.getNumBands());
159:
160: Raster solution = solutions1[i];
161:
162: float[] got = null;
163: float[] expected = null;
164: for (int y = 0; y < 4; y++) {
165: for (int x = 0; x < 4; x++) {
166: got = raster.getPixel(x, y, got);
167: expected = solution.getPixel(x, y, expected);
168: this .compareArrays("(" + x + "," + y + "," + i
169: + ")", expected, got);
170: }
171: }
172:
173: i++;
174: }
175:
176: scaling = IDFT3DDescriptor.SCALING_DEFAULT_VALUE;
177: nature = IDFT3DDescriptor.NATURE_DEFAULT_VALUE;
178: Collection<RenderedImage> idft = IDFT3DDescriptor
179: .createCollection(dft, scaling, nature, null);
180:
181: assertEquals(4, idft.size());
182:
183: Raster[] solutions2 = new Raster[] { sources[0].getData(),
184: sources[1].getData(), sources[2].getData(),
185: sources[3].getData(), };
186:
187: i = 0;
188: for (Object o : idft) {
189: RenderedImage sink = (RenderedImage) o;
190: Raster raster = sink.getData();
191:
192: assertEquals(0, raster.getMinX());
193: assertEquals(0, raster.getMinY());
194: assertEquals(4, raster.getWidth());
195: assertEquals(4, raster.getHeight());
196: assertEquals(1, raster.getNumBands());
197:
198: Raster solution = solutions2[i];
199:
200: float[] got = null;
201: float[] expected = null;
202: for (int y = 0; y < 4; y++) {
203: for (int x = 0; x < 4; x++) {
204: got = raster.getPixel(x, y, got);
205: expected = solution.getPixel(x, y, expected);
206: this .compareArrays("(" + x + "," + y + "," + i
207: + ")", expected, got);
208: }
209: }
210:
211: i++;
212: }
213: }
214:
215: /**
216: * Tests that both operator <em>dft</em> and <em>idft</em> work together
217: * when using multiple sources.
218: */
219: @SuppressWarnings("unchecked")
220: public void testCompatibility2() {
221: RenderedImage[] sources = new RenderedImage[] {
222: RasterAdapter.buildFloatImage(new float[][] { { 1.0f,
223: 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f,
224: 10.0f, 11.0f, 12.0f, 13.0f, 14.0f, 15.0f,
225: 16.0f, }, }, 4, 4),
226: RasterAdapter.buildFloatImage(new float[][] { { 17.0f,
227: 18.0f, 19.0f, 20.0f, 21.0f, 22.0f, 23.0f,
228: 24.0f, 25.0f, 26.0f, 27.0f, 28.0f, 29.0f,
229: 30.0f, 31.0f, 32.0f, }, }, 4, 4),
230: RasterAdapter.buildFloatImage(new float[][] { { 33.0f,
231: 34.0f, 35.0f, 36.0f, 37.0f, 38.0f, 39.0f,
232: 40.0f, 41.0f, 42.0f, 43.0f, 44.0f, 45.0f,
233: 46.0f, 47.0f, 48.0f, }, }, 4, 4),
234: RasterAdapter.buildFloatImage(new float[][] { { 49.0f,
235: 50.0f, 51.0f, 52.0f, 53.0f, 54.0f, 55.0f,
236: 56.0f, 57.0f, 58.0f, 59.0f, 60.0f, 61.0f,
237: 62.0f, 63.0f, 64.0f, }, }, 4, 4), };
238:
239: DFTScalingType scaling = DFT3DDescriptor.SCALING_DEFAULT_VALUE;
240: DFTDataNature nature = DFT3DDescriptor.NATURE_DEFAULT_VALUE;
241:
242: ParameterBlock pb_dft = new ParameterBlock().addSource(
243: sources[0]).addSource(sources[1]).addSource(sources[2])
244: .addSource(sources[3]).add(scaling).add(nature);
245: Collection dft = JAI.createCollection("dft", pb_dft, null);
246:
247: assertEquals(4, dft.size());
248:
249: Raster[] solutions1 = new Raster[] {
250: RasterAdapter.buildFloatImage(
251: new float[][] {
252: { 2080.0f, -32.0f, -32.0f, -32.0f,
253: -128.0f, 0.0f, 0.0f, 0.0f,
254: -128.0f, 0.0f, 0.0f, 0.0f,
255: -128.0f, 0.0f, 0.0f, 0.0f, },
256: { 0.0f, 32.0f, 0.0f, -32.0f, 128.0f,
257: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
258: 0.0f, 0.0f, -128.0f, 0.0f,
259: 0.0f, 0.0f, }, }, 4, 4)
260: .getData(),
261: RasterAdapter
262: .buildFloatImage(
263: new float[][] {
264: { -512.0f, 0.0f, 0.0f, 0.0f,
265: 0.0f, 0.0f, 0.0f, 0.0f,
266: 0.0f, 0.0f, 0.0f, 0.0f,
267: 0.0f, 0.0f, 0.0f, 0.0f, },
268: { 512.0f, 0.0f, 0.0f, 0.0f,
269: 0.0f, 0.0f, 0.0f, 0.0f,
270: 0.0f, 0.0f, 0.0f, 0.0f,
271: 0.0f, 0.0f, 0.0f, 0.0f, }, },
272: 4, 4).getData(),
273: RasterAdapter
274: .buildFloatImage(
275: new float[][] {
276: { -512.0f, 0.0f, 0.0f, 0.0f,
277: 0.0f, 0.0f, 0.0f, 0.0f,
278: 0.0f, 0.0f, 0.0f, 0.0f,
279: 0.0f, 0.0f, 0.0f, 0.0f, },
280: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
281: 0.0f, 0.0f, 0.0f, 0.0f,
282: 0.0f, 0.0f, 0.0f, 0.0f,
283: 0.0f, 0.0f, 0.0f, }, },
284: 4, 4).getData(),
285: RasterAdapter.buildFloatImage(
286: new float[][] {
287: { -512.0f, 0.0f, 0.0f, 0.0f, 0.0f,
288: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
289: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
290: 0.0f, },
291: { -512.0f, 0.0f, 0.0f, 0.0f, 0.0f,
292: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
293: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
294: 0.0f, }, }, 4, 4).getData(), };
295:
296: int i = 0;
297: for (Object o : dft) {
298: RenderedImage sink = (RenderedImage) o;
299: Raster raster = sink.getData();
300:
301: assertEquals(0, raster.getMinX());
302: assertEquals(0, raster.getMinY());
303: assertEquals(4, raster.getWidth());
304: assertEquals(4, raster.getHeight());
305: assertEquals(2, raster.getNumBands());
306:
307: Raster solution = solutions1[i];
308:
309: float[] got = null;
310: float[] expected = null;
311: for (int y = 0; y < 4; y++) {
312: for (int x = 0; x < 4; x++) {
313: got = raster.getPixel(x, y, got);
314: expected = solution.getPixel(x, y, expected);
315: this .compareArrays("(" + x + "," + y + "," + i
316: + ")", expected, got);
317: }
318: }
319:
320: i++;
321: }
322:
323: scaling = IDFT3DDescriptor.SCALING_DEFAULT_VALUE;
324: nature = IDFT3DDescriptor.NATURE_DEFAULT_VALUE;
325:
326: Iterator iterator = dft.iterator();
327: ParameterBlock pb_idft = new ParameterBlock().addSource(
328: iterator.next()).addSource(iterator.next()).addSource(
329: iterator.next()).addSource(iterator.next())
330: .add(scaling).add(nature);
331: Collection<RenderedImage> idft = JAI.createCollection("idft",
332: pb_idft, null);
333:
334: assertEquals(4, idft.size());
335:
336: Raster[] solutions2 = new Raster[] { sources[0].getData(),
337: sources[1].getData(), sources[2].getData(),
338: sources[3].getData(), };
339:
340: i = 0;
341: for (Object o : idft) {
342: RenderedImage sink = (RenderedImage) o;
343: Raster raster = sink.getData();
344:
345: assertEquals(0, raster.getMinX());
346: assertEquals(0, raster.getMinY());
347: assertEquals(4, raster.getWidth());
348: assertEquals(4, raster.getHeight());
349: assertEquals(1, raster.getNumBands());
350:
351: Raster solution = solutions2[i];
352:
353: float[] got = null;
354: float[] expected = null;
355: for (int y = 0; y < 4; y++) {
356: for (int x = 0; x < 4; x++) {
357: got = raster.getPixel(x, y, got);
358: expected = solution.getPixel(x, y, expected);
359: this .compareArrays("(" + x + "," + y + "," + i
360: + ")", expected, got);
361: }
362: }
363:
364: i++;
365: }
366: }
367:
368: /**
369: * Tests to see that operators "dft" and "idft" still work in rendered mode.
370: */
371: public void testOverrideJAIName() {
372: RenderedImage source = RasterAdapter.buildFloatImage(
373: new float[][] { { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f,
374: 7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f, 13.0f,
375: 14.0f, 15.0f, 16.0f, }, }, 4, 4);
376: ParameterBlock pb_dft = ParameterBlockUtil
377: .createDFTParameterBlock(source);
378: RenderedImage dft = JAI.create("dft", pb_dft);
379:
380: Raster solution = RasterAdapter.buildFloatImage(
381: new float[][] {
382: { 136.0f, -8.0f, -8.0f, -8.0f, -32.0f, 0.0f,
383: 0.0f, 0.0f, -32.0f, 0.0f, 0.0f, 0.0f,
384: -32.0f, 0.0f, 0.0f, 0.0f, },
385: { 0.0f, 8.0f, 0.0f, -8.0f, 32.0f, 0.0f, 0.0f,
386: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -32.0f,
387: 0.0f, 0.0f, 0.0f, }, }, 4, 4).getData();
388: Raster raster = dft.getData();
389:
390: float[] got = null;
391: float[] expected = null;
392: for (int y = 0; y < 4; y++) {
393: for (int x = 0; x < 4; x++) {
394: got = raster.getPixel(x, y, got);
395: expected = solution.getPixel(x, y, expected);
396: this .compareArrays("(" + x + "," + y + ")", expected,
397: got);
398: }
399: }
400:
401: ParameterBlock pb_idft = ParameterBlockUtil
402: .createIDFTParameterBlock(dft);
403: RenderedImage idft = JAI.create("idft", pb_idft);
404:
405: Raster solution2 = source.getData();
406: Raster raster2 = idft.getData();
407:
408: assertEquals(0, raster2.getMinX());
409: assertEquals(0, raster2.getMinY());
410: assertEquals(4, raster2.getWidth());
411: assertEquals(4, raster2.getHeight());
412: assertEquals(1, raster2.getNumBands());
413:
414: float[] got2 = null;
415: float[] expected2 = null;
416: for (int y = 0; y < 4; y++) {
417: for (int x = 0; x < 4; x++) {
418: got2 = raster2.getPixel(x, y, got2);
419: expected2 = solution2.getPixel(x, y, expected2);
420: this .compareArrays("(" + x + "," + y + ")", expected2,
421: got2);
422: }
423: }
424: }
425:
426: //---------------------------
427: // Class methods
428: //---------------------------
429:
430: /**
431: * Runs only this test.
432: * @param args ignored.
433: */
434: public static void main(String... args) {
435: junit.swingui.TestRunner.run(DFT3DCompatibilityTest.class);
436: }
437:
438: }
439:
440: /*
441: * $Log: DFT3DCompatibilityTest.java,v $
442: * Revision 1.5 2007/07/17 16:49:04 forklabs
443: * Operation renamed from dft3d to dft and from idft3d to idft.
444: *
445: * Revision 1.4 2007/07/05 18:27:32 forklabs
446: * Cleaned up the import statements.
447: *
448: * Revision 1.3 2007/06/13 18:57:21 forklabs
449: * Changed parent to use CollectionDescriptor.
450: *
451: * Revision 1.2 2007/06/05 20:45:37 forklabs
452: * Changed the way the operator are called to get more coverage.
453: *
454: * Revision 1.1 2007/06/05 02:27:51 forklabs
455: * Operators dft3d and idft3d
456: *
457: */
|