001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/SpectralHomomorphicDescriptorTest.java,v 1.1 2007/07/05 18:31:30 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.DataBuffer;
025: import java.awt.image.Raster;
026: import java.awt.image.RenderedImage;
027: import java.awt.image.renderable.ParameterBlock;
028: import java.util.Locale;
029: import javax.media.jai.CollectionImage;
030: import javax.media.jai.JAI;
031: import javax.media.jai.RenderedOp;
032: import junit.framework.TestCase;
033: import ca.forklabs.media.jai.FormatDataType;
034: import ca.forklabs.media.jai.ParameterBlockUtil;
035: import ca.forklabs.media.jai.RasterAdapter;
036: import ca.forklabs.media.jai.SimpleCollectionImage;
037: import ca.forklabs.media.jai.SpectralFilter2D;
038: import ca.forklabs.media.jai.SpectralFilter3D;
039: import ca.forklabs.media.jai.operator.SpectralHomomorphicDescriptor;
040:
041: /**
042: * Class {@code SpectralHomomorphicDescriptorTest} tests class
043: * {@link SpectralHomomorphicDescriptor}.
044: *
045: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.SpectralHomomorphicDescriptorTest">Daniel Léonard</a>
046: * @version $Revision: 1.1 $
047: */
048: @SuppressWarnings("nls")
049: public class SpectralHomomorphicDescriptorTest extends TestCase {
050:
051: //---------------------------
052: // Constructors
053: //---------------------------
054:
055: /**
056: * Constructor.
057: * @param name the name of this test.
058: */
059: public SpectralHomomorphicDescriptorTest(String name) {
060: super (name);
061: }
062:
063: //---------------------------
064: // Test methods
065: //---------------------------
066:
067: private void compareArrays(String message, double[] expected,
068: double[] got) {
069: assertEquals(message, expected.length, got.length);
070: for (int i = 0; i < expected.length; i++) {
071: assertEquals(message, expected[i], got[i], 10e-6);
072: }
073: }
074:
075: private RenderedImage getTestImage() {
076: float[][] data = new float[][] { { 1.0f, 1.0f, 1.0f, 1.0f,
077: 2.0f, 2.0f, 2.0f, 2.0f, 3.0f, 3.0f, 3.0f, 3.0f, 4.0f,
078: 4.0f, 4.0f, 4.0f, }, };
079: RenderedImage image = RasterAdapter.buildFloatImage(data, 4, 4);
080: return image;
081: }
082:
083: /**
084: * Tests
085: * {@link SpectralHomomorphicDescriptor#create(RenderedImage, SpectralHomomorphicFilter, FormatDataType, RenderingHints)}.
086: */
087: @SuppressWarnings("boxing")
088: public void testCreateRendered() {
089: RenderedImage source = this .getTestImage();
090:
091: SpectralFilter2D filter = new SpectralFilter2D() {
092: private int width = 0;
093: private int height = 0;
094:
095: public void setWidth(int width) {
096: this .width = width;
097: }
098:
099: public void setHeight(int height) {
100: this .height = height;
101: }
102:
103: @SuppressWarnings("hiding")
104: public RenderedImage getFilterImage(int elements,
105: RenderingHints hints) {
106: float width = this .width;
107: float height = this .height;
108: Number[] values = new Double[] { 1.0, 0.0, };
109: ParameterBlock pb = ParameterBlockUtil
110: .createConstantParameterBlock(width, height,
111: values);
112: RenderedImage sink = JAI.create("constant", pb, hints);
113: return sink;
114: }
115: };
116: FormatDataType type = FormatDataType.DOUBLE;
117:
118: RenderedOp image = SpectralHomomorphicDescriptor.create(source,
119: filter, type, null);
120: Raster raster = image.getData();
121: Raster solution = source.getData();
122:
123: assertEquals(0, raster.getMinX());
124: assertEquals(0, raster.getMinY());
125: assertEquals(4, raster.getWidth());
126: assertEquals(4, raster.getHeight());
127: assertEquals(1, raster.getNumBands());
128: assertEquals(DataBuffer.TYPE_DOUBLE, raster.getDataBuffer()
129: .getDataType());
130:
131: double[] got = null;
132: double[] expected = null;
133: for (int y = 0; y < 4; y++) {
134: for (int x = 0; x < 4; x++) {
135: got = raster.getPixel(x, y, got);
136: expected = solution.getPixel(x, y, expected);
137: this .compareArrays("(" + x + "," + y + ")", expected,
138: got);
139: }
140: }
141: }
142:
143: /**
144: * Tests
145: * {@link SpectralHomomorphicDescriptor#createCollection(CollectionImage, SpectralFilter3D, FormatDataType, RenderingHints)}.
146: */
147: @SuppressWarnings({"boxing","hiding"})
148: public void testCreateCollection() {
149: CollectionImage sources = new SimpleCollectionImage(this
150: .getTestImage(), this .getTestImage(), this
151: .getTestImage(), this .getTestImage());
152: SpectralFilter3D filter = new SpectralFilter3D() {
153: private int width = 0;
154: private int height = 0;
155: private int depth = 0;
156:
157: public void setWidth(int width) {
158: this .width = width;
159: }
160:
161: public void setHeight(int height) {
162: this .height = height;
163: }
164:
165: public void setDepth(int depth) {
166: this .depth = depth;
167: }
168:
169: public CollectionImage getFilterImage(int elements,
170: RenderingHints hints) {
171: float width = this .width;
172: float height = this .height;
173: float depth = this .depth;
174: Number[] values = new Double[] { 1.0, 0.0, };
175: ParameterBlock pb = ParameterBlockUtil
176: .createConstantParameterBlock(width, height,
177: values);
178: RenderedImage image = JAI.create("constant", pb, hints);
179:
180: CollectionImage sinks = new SimpleCollectionImage();
181: for (int i = 0; i < depth; i++) {
182: sinks.add(image);
183: }
184: return sinks;
185: }
186: };
187: FormatDataType type = FormatDataType.DOUBLE;
188:
189: CollectionImage sinks = SpectralHomomorphicDescriptor
190: .createCollection(sources, filter, type, null);
191:
192: assertEquals(4, sinks.size());
193:
194: // TODO : simplify when issue 102 of jai-core is fixed
195: java.util.Iterator<?> iter = sinks.iterator();
196: for (int i = 0; i < sinks.size(); i++) {
197: //Raster raster = ((RenderedImage) sinks.get(i)).getData();
198: Raster raster = ((RenderedImage) iter.next()).getData();
199: Raster solution = ((RenderedImage) sources.get(i))
200: .getData();
201:
202: assertEquals(0, raster.getMinX());
203: assertEquals(0, raster.getMinY());
204: assertEquals(4, raster.getWidth());
205: assertEquals(4, raster.getHeight());
206: assertEquals(1, raster.getNumBands());
207: assertEquals(DataBuffer.TYPE_DOUBLE, raster.getDataBuffer()
208: .getDataType());
209:
210: double[] got = null;
211: double[] expected = null;
212: for (int y = 0; y < 4; y++) {
213: for (int x = 0; x < 4; x++) {
214: got = raster.getPixel(x, y, got);
215: expected = solution.getPixel(x, y, expected);
216: this .compareArrays("(" + x + "," + y + ")",
217: expected, got);
218: }
219: }
220: }
221: }
222:
223: /**
224: * Tests the English messages.
225: */
226: public void testEnglishMessages() {
227: Locale locale = Locale.getDefault();
228: Locale.setDefault(Locale.ENGLISH);
229:
230: try {
231: String[] expected = new String[] {
232: "Spectral homomorphic filter",
233: "The spectral filter",
234: "The data type for the format operation", };
235:
236: String[] got = new String[] {
237: SpectralHomomorphicDescriptor.getDescription(),
238: SpectralHomomorphicDescriptor.getArg0Description(),
239: SpectralHomomorphicDescriptor.getArg1Description(), };
240:
241: assertEquals(expected.length, got.length);
242:
243: for (int i = 0; i < expected.length; i++) {
244: assertEquals("[" + i + "]", expected[i], got[i]);
245: }
246: } finally {
247: Locale.setDefault(locale);
248: }
249: }
250:
251: /**
252: * Tests the French messages.
253: */
254: public void testFrenchMessages() {
255: Locale locale = Locale.getDefault();
256: Locale.setDefault(Locale.FRENCH);
257:
258: try {
259: String[] expected = new String[] {
260: "Filtre homomorphique spectral",
261: "Le filtre spectral",
262: "Le type pour l'opération de \"format\"", };
263:
264: String[] got = new String[] {
265: SpectralHomomorphicDescriptor.getDescription(),
266: SpectralHomomorphicDescriptor.getArg0Description(),
267: SpectralHomomorphicDescriptor.getArg1Description(), };
268:
269: assertEquals(expected.length, got.length);
270:
271: for (int i = 0; i < expected.length; i++) {
272: assertEquals("[" + i + "]", expected[i], got[i]);
273: }
274: } finally {
275: Locale.setDefault(locale);
276: }
277: }
278:
279: //---------------------------
280: // Class methods
281: //---------------------------
282:
283: /**
284: * Runs only this test.
285: * @param args ignored.
286: */
287: public static void main(String... args) {
288: junit.swingui.TestRunner
289: .run(SpectralHomomorphicDescriptorTest.class);
290: }
291:
292: }
293:
294: /*
295: * $Log: SpectralHomomorphicDescriptorTest.java,v $
296: * Revision 1.1 2007/07/05 18:31:30 forklabs
297: * Operator spectralhomomorphic.
298: *
299: */
|