001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/PeriodicShift3DDescriptorTest.java,v 1.4 2007/09/07 18:12:41 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.Raster;
025: import java.awt.image.RenderedImage;
026: import java.awt.image.renderable.ParameterBlock;
027: import java.util.Arrays;
028: import java.util.Collection;
029: import java.util.Locale;
030: import javax.media.jai.JAI;
031: import junit.framework.TestCase;
032: import ca.forklabs.media.jai.RasterAdapter;
033: import ca.forklabs.media.jai.operator.PeriodicShift3DDescriptor;
034:
035: /**
036: * Class {@code PeriodicShift3DDescriptorTest} tests class
037: * {@link PeriodicShift3DDescriptor}.
038: *
039: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.PeriodicShift3DDescriptorTest">Daniel Léonard</a>
040: * @version $Revision: 1.4 $
041: */
042: @SuppressWarnings("nls")
043: public class PeriodicShift3DDescriptorTest extends TestCase {
044:
045: //---------------------------
046: // Constructors
047: //---------------------------
048:
049: /**
050: * Constructor.
051: * @param name the name of this test.
052: */
053: public PeriodicShift3DDescriptorTest(String name) {
054: super (name);
055: }
056:
057: //---------------------------
058: // Test methods
059: //---------------------------
060:
061: private void compareArrays(String message, float[] expected,
062: float[] got) {
063: assertEquals(message, expected.length, got.length);
064: for (int i = 0; i < expected.length; i++) {
065: assertEquals(message, expected[i], got[i], 10e-6f);
066: }
067: }
068:
069: /**
070: * Tests
071: * {@link PeriodicShift3DDescriptor#createCollection(Collection, int, int, int, RenderingHints)}.
072: */
073: @SuppressWarnings({"boxing","unchecked"})
074: public void testCreateCollection() {
075: RenderedImage[] sources = new RenderedImage[] {
076: RasterAdapter.buildFloatImage(new float[][] { {
077: 2080.0f, -32.0f, -32.0f, -32.0f, -128.0f, 0.0f,
078: 0.0f, 0.0f, -128.0f, 0.0f, 0.0f, 0.0f, -128.0f,
079: 0.0f, 0.0f, 0.0f, }, }, 4, 4),
080: RasterAdapter.buildFloatImage(new float[][] { {
081: -512.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
082: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
083: 0.0f, }, }, 4, 4),
084: RasterAdapter.buildFloatImage(new float[][] { {
085: -512.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
086: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
087: 0.0f, }, }, 4, 4),
088: RasterAdapter.buildFloatImage(new float[][] { {
089: -512.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
090: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
091: 0.0f, }, }, 4, 4), };
092:
093: Raster[] solutions = new Raster[] {
094: RasterAdapter.buildFloatImage(
095: new float[][] { { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
096: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -512.0f,
097: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, }, }, 4,
098: 4).getData(),
099: RasterAdapter.buildFloatImage(
100: new float[][] { { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
101: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -512.0f,
102: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, }, }, 4,
103: 4).getData(),
104: RasterAdapter.buildFloatImage(
105: new float[][] { { 0.0f, 0.0f, -128.0f, 0.0f,
106: 0.0f, 0.0f, -128.0f, 0.0f, -32.0f,
107: -32.0f, 2080.0f, -32.0f, 0.0f, 0.0f,
108: -128.0f, 0.0f, }, }, 4, 4).getData(),
109: RasterAdapter.buildFloatImage(
110: new float[][] { { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
111: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, -512.0f,
112: 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, }, }, 4,
113: 4).getData(), };
114:
115: Collection<RenderedImage> sinks = PeriodicShift3DDescriptor
116: .createCollection(Arrays.asList(sources), 2, 2, 2, null);
117:
118: int i = 0;
119: for (RenderedImage sink : sinks) {
120: Raster raster = sink.getData();
121:
122: assertEquals(0, raster.getMinX());
123: assertEquals(0, raster.getMinY());
124: assertEquals(4, raster.getWidth());
125: assertEquals(4, raster.getHeight());
126: assertEquals(1, raster.getNumBands());
127:
128: Raster solution = solutions[i];
129:
130: float[] got = null;
131: float[] expected = null;
132: for (int y = 0; y < 4; y++) {
133: for (int x = 0; x < 4; x++) {
134: got = raster.getPixel(x, y, got);
135: expected = solution.getPixel(x, y, expected);
136: this .compareArrays("(" + x + "," + y + "," + i
137: + ")", expected, got);
138: }
139: }
140:
141: i++;
142: }
143:
144: sinks = JAI.createCollection("periodicshift",
145: new ParameterBlock().addSource(sinks));
146:
147: i = 0;
148: for (RenderedImage sink : sinks) {
149: Raster raster = sink.getData();
150:
151: assertEquals(0, raster.getMinX());
152: assertEquals(0, raster.getMinY());
153: assertEquals(4, raster.getWidth());
154: assertEquals(4, raster.getHeight());
155: assertEquals(1, raster.getNumBands());
156:
157: Raster solution = sources[i].getData();
158:
159: float[] got = null;
160: float[] expected = null;
161: for (int y = 0; y < 4; y++) {
162: for (int x = 0; x < 4; x++) {
163: got = raster.getPixel(x, y, got);
164: expected = solution.getPixel(x, y, expected);
165: this .compareArrays("(" + x + "," + y + "," + i
166: + ")", expected, got);
167: }
168: }
169:
170: i++;
171: }
172:
173: }
174:
175: /**
176: * Tests the English messages.
177: */
178: public void testEnglishMessages() {
179: Locale locale = Locale.getDefault();
180: Locale.setDefault(Locale.ENGLISH);
181:
182: try {
183: String[] expected = new String[] {
184: "Periodic shift on a 3D image",
185: "The displacement in the X direction (optional)",
186: "The displacement in the Y direction (optional)",
187: "The displacement in the Z direction (optional)", };
188:
189: String[] got = new String[] {
190: PeriodicShift3DDescriptor.getDescription(),
191: PeriodicShift3DDescriptor.getArg0Description(),
192: PeriodicShift3DDescriptor.getArg1Description(),
193: PeriodicShift3DDescriptor.getArg2Description(), };
194:
195: assertEquals(expected.length, got.length);
196:
197: for (int i = 0; i < expected.length; i++) {
198: assertEquals("[" + i + "]", expected[i], got[i]);
199: }
200: } finally {
201: Locale.setDefault(locale);
202: }
203: }
204:
205: /**
206: * Tests the French messages.
207: */
208: public void testFrenchMessages() {
209: Locale locale = Locale.getDefault();
210: Locale.setDefault(Locale.FRENCH);
211:
212: try {
213: String[] expected = new String[] {
214: "Déplacement périodique sur une image 3D",
215: "Le déplacement selon la largeur (optionel)",
216: "Le déplacement selon la hauteur (optionel)",
217: "Le déplacement selon la profondeur (optionel)", };
218:
219: String[] got = new String[] {
220: PeriodicShift3DDescriptor.getDescription(),
221: PeriodicShift3DDescriptor.getArg0Description(),
222: PeriodicShift3DDescriptor.getArg1Description(),
223: PeriodicShift3DDescriptor.getArg2Description(), };
224:
225: assertEquals(expected.length, got.length);
226:
227: for (int i = 0; i < expected.length; i++) {
228: assertEquals("[" + i + "]", expected[i], got[i]);
229: }
230: } finally {
231: Locale.setDefault(locale);
232: }
233: }
234:
235: //---------------------------
236: // Class methods
237: //---------------------------
238:
239: /**
240: * Runs only this test.
241: * @param args ignored.
242: */
243: public static void main(String... args) {
244: junit.swingui.TestRunner
245: .run(PeriodicShift3DDescriptorTest.class);
246: }
247:
248: }
249:
250: /*
251: * $Log: PeriodicShift3DDescriptorTest.java,v $
252: * Revision 1.4 2007/09/07 18:12:41 forklabs
253: * Adapted the test to reflect the new descriptions.
254: *
255: * Revision 1.3 2007/07/17 16:51:07 forklabs
256: * Operation renamed from periodicshift3d to periodicshift.
257: *
258: * Revision 1.2 2007/06/13 18:57:21 forklabs
259: * Changed parent to use CollectionDescriptor.
260: *
261: * Revision 1.1 2007/06/05 20:44:47 forklabs
262: * Operator periodicshift3d.
263: *
264: */
|