001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/ImageFunction3DDescriptorTest.java,v 1.3 2007/09/07 18:12:16 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.util.Collection;
026: import java.util.Locale;
027: import javax.media.jai.ImageFunction;
028: import javax.media.jai.JAI;
029: import junit.framework.TestCase;
030: import ca.forklabs.media.jai.ImageFunction3D;
031: import ca.forklabs.media.jai.ParameterBlockUtil;
032: import ca.forklabs.media.jai.RasterAdapter;
033:
034: /**
035: * Class {@code ImageFunction3DDescriptorTest} tests class
036: * {@link ImageFunction3DDescriptor}.
037: *
038: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.ImageFunction3DDescriptorTest">Daniel Léonard</a>
039: * @version $Revision: 1.3 $
040: */
041: @SuppressWarnings("nls")
042: public class ImageFunction3DDescriptorTest extends TestCase {
043:
044: //---------------------------
045: // Constructors
046: //---------------------------
047:
048: /**
049: * Constructor.
050: * @param name the name of this test.
051: */
052: public ImageFunction3DDescriptorTest(String name) {
053: super (name);
054: }
055:
056: //---------------------------
057: // Test methods
058: //---------------------------
059:
060: private void compareArrays(String message, float[] expected,
061: float[] got) {
062: assertEquals(message, expected.length, got.length);
063: for (int i = 0; i < expected.length; i++) {
064: assertEquals(message, expected[i], got[i], 10e-6f);
065: }
066: }
067:
068: /**
069: * Tests that the simple case works. More complete tests are done in the
070: * op-image test class.
071: */
072: public void testCreateCollection() {
073: Raster[] solutions = new Raster[] {
074: RasterAdapter.buildFloatImage(
075: new float[][] {
076: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
077: 0.0f, 0.0f, 0.0f, },
078: { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
079: 0.0f, 0.0f, 0.0f, }, }, 3, 3)
080: .getData(),
081: RasterAdapter.buildFloatImage(
082: new float[][] {
083: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
084: 1.0f, 1.0f, 1.0f, },
085: { 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
086: 1.0f, 1.0f, 1.0f, }, }, 3, 3)
087: .getData(),
088: RasterAdapter.buildFloatImage(
089: new float[][] {
090: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
091: 2.0f, 2.0f, 2.0f, },
092: { 2.0f, 2.0f, 2.0f, 2.0f, 2.0f, 2.0f,
093: 2.0f, 2.0f, 2.0f, }, }, 3, 3)
094: .getData(), };
095:
096: ImageFunction3D function = new ImageFunction3D() {
097: @Override
098: public int getNumBands() {
099: return 2;
100: }
101:
102: @Override
103: public boolean isComplex() {
104: return false;
105: }
106:
107: @Override
108: public void getBand(int band, int slice, int dimX,
109: int dimY, int dimZ, float[] real, float[] imag) {
110: for (int i = 0; i < dimX * dimY; i++) {
111: real[i] = slice;
112: }
113: }
114:
115: @Override
116: public void getBand(int band, int slice, int dimX,
117: int dimY, int dimZ, double[] real, double[] imag) {
118: // nothing
119: }
120: };
121:
122: Collection<RenderedImage> images = ImageFunction3DDescriptor
123: .createCollection(function, 3, 3, 3, null);
124:
125: assertEquals(3, images.size());
126:
127: int i = 0;
128: for (Object o : images) {
129: RenderedImage image = (RenderedImage) o;
130: Raster raster = image.getData();
131:
132: assertEquals(0, raster.getMinX());
133: assertEquals(0, raster.getMinY());
134: assertEquals(3, raster.getWidth());
135: assertEquals(3, raster.getHeight());
136: assertEquals(2, raster.getNumBands());
137:
138: Raster solution = solutions[i];
139:
140: float[] got = null;
141: float[] expected = null;
142: for (int y = 0; y < 3; y++) {
143: for (int x = 0; x < 3; x++) {
144: got = raster.getPixel(x, y, got);
145: expected = solution.getPixel(x, y, expected);
146: this .compareArrays("(" + x + "," + y + "," + i
147: + ")", expected, got);
148: }
149: }
150:
151: i++;
152: }
153: }
154:
155: /**
156: * Tests the English messages.
157: */
158: public void testEnglishMessages() {
159: Locale locale = Locale.getDefault();
160: Locale.setDefault(Locale.ENGLISH);
161:
162: try {
163: String[] expected = new String[] {
164: "Generates a collection of images from a functional description",
165: "The functional description", "The width",
166: "The height", "The depth", };
167:
168: String[] got = new String[] {
169: ImageFunction3DDescriptor.getDescription(),
170: ImageFunction3DDescriptor.getArg0Description(),
171: ImageFunction3DDescriptor.getArg1Description(),
172: ImageFunction3DDescriptor.getArg2Description(),
173: ImageFunction3DDescriptor.getArg3Description(), };
174:
175: assertEquals(expected.length, got.length);
176:
177: for (int i = 0; i < expected.length; i++) {
178: assertEquals("[" + i + "]", expected[i], got[i]);
179: }
180: } finally {
181: Locale.setDefault(locale);
182: }
183: }
184:
185: /**
186: * Tests the French messages.
187: */
188: public void testFrenchMessages() {
189: Locale locale = Locale.getDefault();
190: Locale.setDefault(Locale.FRENCH);
191:
192: try {
193: String[] expected = new String[] {
194: "Génère une collection d'image selon une function",
195: "La fonction", "La largeur", "La hauteur",
196: "La profondeur", };
197:
198: String[] got = new String[] {
199: ImageFunction3DDescriptor.getDescription(),
200: ImageFunction3DDescriptor.getArg0Description(),
201: ImageFunction3DDescriptor.getArg1Description(),
202: ImageFunction3DDescriptor.getArg2Description(),
203: ImageFunction3DDescriptor.getArg3Description(), };
204:
205: assertEquals(expected.length, got.length);
206:
207: for (int i = 0; i < expected.length; i++) {
208: assertEquals("[" + i + "]", expected[i], got[i]);
209: }
210: } finally {
211: Locale.setDefault(locale);
212: }
213: }
214:
215: /**
216: * Tests to see that operator "imagefunction" still works in rendered mode.
217: */
218: public void testOverrideJAIName() {
219: Raster solution = RasterAdapter.buildFloatImage(
220: new float[][] {
221: { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 2.0f,
222: 2.0f, 2.0f, },
223: { 0.0f, 0.0f, 0.0f, 2.0f, 2.0f, 2.0f, 4.0f,
224: 4.0f, 4.0f, }, }, 3, 3).getData();
225:
226: ImageFunction function = new ImageFunction() {
227: @Override
228: public int getNumElements() {
229: return 1;
230: }
231:
232: @Override
233: public boolean isComplex() {
234: return true;
235: }
236:
237: @Override
238: public void getElements(float startX, float startY,
239: float deltaX, float deltaY, int countX, int countY,
240: int element, float[] real, float[] imag) {
241: int index = 0;
242:
243: for (int j = 0; j < countY; j++) {
244: for (int i = 0; i < countX; i++, index++) {
245: real[index] = j;
246: imag[index] = j * 2.0f;
247: }
248: }
249: }
250:
251: @Override
252: public void getElements(double startX, double startY,
253: double deltaX, double deltaY, int countX,
254: int countY, int element, double[] real,
255: double[] imag) {
256:
257: // nothing
258: }
259: };
260:
261: RenderedImage sink = JAI.create("imagefunction",
262: ParameterBlockUtil.createImageFunctionParameterBlock(
263: function, 3, 3));
264:
265: Raster raster = sink.getData();
266:
267: assertEquals(0, raster.getMinX());
268: assertEquals(0, raster.getMinY());
269: assertEquals(3, raster.getWidth());
270: assertEquals(3, raster.getHeight());
271: assertEquals(2, raster.getNumBands());
272:
273: float[] got = null;
274: float[] expected = null;
275: for (int y = 0; y < 3; y++) {
276: for (int x = 0; x < 3; x++) {
277: got = raster.getPixel(x, y, got);
278: expected = solution.getPixel(x, y, expected);
279: this .compareArrays("(" + x + "," + y + ")", expected,
280: got);
281: }
282: }
283: }
284:
285: //---------------------------
286: // Class methods
287: //---------------------------
288:
289: /**
290: * Runs only this test.
291: * @param args ignored.
292: */
293: public static void main(String... args) {
294: junit.swingui.TestRunner
295: .run(ImageFunction3DDescriptorTest.class);
296: }
297:
298: }
299:
300: /*
301: * $Log: ImageFunction3DDescriptorTest.java,v $
302: * Revision 1.3 2007/09/07 18:12:16 forklabs
303: * Condensed import statements.
304: *
305: * Revision 1.2 2007/07/17 16:50:10 forklabs
306: * Operation renamed from imagefunction3d to imagefunction.
307: *
308: * Revision 1.1 2007/07/05 18:30:38 forklabs
309: * Operator imagefunction3d.
310: *
311: */
|