001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/MedianCollectionDescriptorTest.java,v 1.2 2007/08/16 21:25:15 forklabs Exp $
003: *
004: * Copyright (C) 2006 DIRO 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.io.InputStream;
028: import java.util.Arrays;
029: import java.util.Collection;
030: import java.util.LinkedList;
031: import java.util.List;
032: import java.util.Locale;
033: import javax.media.jai.JAI;
034: import javax.media.jai.RenderedOp;
035: import com.sun.media.jai.codec.ForwardSeekableStream;
036: import junit.framework.TestCase;
037: import ca.forklabs.baselib.util.Algorithm;
038: import ca.forklabs.baselib.util.Iterators;
039: import ca.forklabs.baselib.util.UnaryFunction;
040: import ca.forklabs.media.jai.operator.MedianCollectionDescriptor;
041:
042: /**
043: * Class {@code MedianCollectionDescriptorTest} tests class
044: * {@link MedianCollectionDescriptor}.
045: *
046: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.MedianCollectionDescriptorTest">Daniel Léonard</a>
047: * @version $Revision: 1.2 $
048: */
049: @SuppressWarnings("nls")
050: public class MedianCollectionDescriptorTest extends TestCase {
051:
052: //---------------------------
053: // Constructors
054: //---------------------------
055:
056: /**
057: * Constructor.
058: * @param name the name of this test.
059: */
060: public MedianCollectionDescriptorTest(String name) {
061: super (name);
062: }
063:
064: //---------------------------
065: // Test methods
066: //---------------------------
067:
068: /**
069: * Tests the error catching in
070: * {@link MedianCollectionDescriptor#validateSources(String, ParameterBlock, StringBuffer)}
071: */
072: public void testValidateBadSources() {
073: Class<?> clazz = this .getClass();
074: InputStream[] sources = new InputStream[] {
075: clazz
076: .getResourceAsStream("/ca/forklabs/media/jai/operator/median_collection_test_pic1.png"),
077: clazz
078: .getResourceAsStream("/ca/forklabs/media/jai/operator/median_collection_test_pic2.png"),
079: clazz
080: .getResourceAsStream("/ca/forklabs/media/jai/operator/median_collection_test_pic3.png"), };
081:
082: assertNotNull(sources[0]);
083: assertNotNull(sources[1]);
084: assertNotNull(sources[2]);
085:
086: List<RenderedImage> images = new LinkedList<RenderedImage>();
087: Algorithm.transform(Iterators.asIterator(sources), Iterators
088: .backInserter(images),
089: new UnaryFunction<RenderedImage, InputStream>() {
090: public RenderedImage invoke(InputStream is) {
091: RenderedOp image = JAI.create("stream",
092: new ParameterBlock()
093: .add(new ForwardSeekableStream(
094: is)));
095: return image;
096: }
097: });
098: ParameterBlock pb_one = new ParameterBlock().addSource(images
099: .subList(0, 1));
100: ParameterBlock pb_single = new ParameterBlock()
101: .addSource(images.get(0));
102: ParameterBlock pb_two = new ParameterBlock().addSource(images
103: .subList(0, 2));
104: ParameterBlock pb_string = new ParameterBlock()
105: .addSource(Arrays.asList(new String[] { "hello",
106: "world", "!" }));
107:
108: StringBuffer sb = new StringBuffer();
109:
110: MedianCollectionDescriptor descriptor = new MedianCollectionDescriptor();
111: boolean got;
112:
113: // one source in collection - our code
114: got = descriptor.validateSources("rendered", pb_one, sb);
115: assertFalse(got);
116:
117: // one source alone - checked in superclass
118: got = descriptor.validateSources("rendered", pb_single, sb);
119: assertFalse(got);
120:
121: // two source in collection - our code
122: got = descriptor.validateSources("rendered", pb_two, sb);
123: assertTrue(got);
124:
125: // sources not images - our code
126: got = descriptor.validateSources("rendered", pb_string, sb);
127: assertFalse(got);
128: got = descriptor.validateSources("renderable", pb_string, sb);
129: assertFalse(got);
130: }
131:
132: /**
133: * Tests
134: * {@link MedianCollectionDescriptor#create(Collection, RenderingHints)}.
135: */
136: public void testCreateRendered() {
137: Class<?> clazz = this .getClass();
138: InputStream[] sources = new InputStream[] {
139: clazz
140: .getResourceAsStream("/ca/forklabs/media/jai/operator/median_collection_test_pic1.png"),
141: clazz
142: .getResourceAsStream("/ca/forklabs/media/jai/operator/median_collection_test_pic2.png"),
143: clazz
144: .getResourceAsStream("/ca/forklabs/media/jai/operator/median_collection_test_pic3.png"), };
145:
146: assertNotNull(sources[0]);
147: assertNotNull(sources[1]);
148: assertNotNull(sources[2]);
149:
150: List<RenderedImage> images = new LinkedList<RenderedImage>();
151: Algorithm.transform(Iterators.asIterator(sources), Iterators
152: .backInserter(images),
153: new UnaryFunction<RenderedImage, InputStream>() {
154: public RenderedImage invoke(InputStream is) {
155: RenderedOp image = JAI.create("stream",
156: new ParameterBlock()
157: .add(new ForwardSeekableStream(
158: is)));
159: return image;
160: }
161: });
162:
163: RenderedOp median = MedianCollectionDescriptor.create(images,
164: null);
165:
166: Raster raster = median.getData();
167:
168: assertEquals(0, raster.getMinX());
169: assertEquals(0, raster.getMinY());
170: assertEquals(4, raster.getWidth());
171: assertEquals(4, raster.getHeight());
172: assertEquals(3, raster.getNumBands());
173:
174: int[] red = new int[] { 255, 0, 0 };
175: int[] purple = new int[] { 255, 0, 255 };
176: int[] pixels = new int[3];
177:
178: assertTrue(Arrays.equals(red, raster.getPixel(0, 0, pixels)));
179: assertTrue(Arrays.equals(red, raster.getPixel(1, 0, pixels)));
180: assertTrue(Arrays.equals(red, raster.getPixel(2, 0, pixels)));
181: assertTrue(Arrays.equals(red, raster.getPixel(3, 0, pixels)));
182:
183: assertTrue(Arrays.equals(red, raster.getPixel(0, 1, pixels)));
184: assertTrue(Arrays.equals(purple, raster.getPixel(1, 1, pixels)));
185: assertTrue(Arrays.equals(purple, raster.getPixel(2, 1, pixels)));
186: assertTrue(Arrays.equals(red, raster.getPixel(3, 1, pixels)));
187:
188: assertTrue(Arrays.equals(red, raster.getPixel(0, 2, pixels)));
189: assertTrue(Arrays.equals(purple, raster.getPixel(1, 2, pixels)));
190: assertTrue(Arrays.equals(purple, raster.getPixel(2, 2, pixels)));
191: assertTrue(Arrays.equals(red, raster.getPixel(3, 2, pixels)));
192:
193: assertTrue(Arrays.equals(red, raster.getPixel(0, 3, pixels)));
194: assertTrue(Arrays.equals(red, raster.getPixel(1, 3, pixels)));
195: assertTrue(Arrays.equals(red, raster.getPixel(2, 3, pixels)));
196: assertTrue(Arrays.equals(red, raster.getPixel(3, 3, pixels)));
197: }
198:
199: /**
200: * Tests the English messages.
201: */
202: public void testEnglishMessages() {
203: Locale locale = Locale.getDefault();
204: Locale.setDefault(Locale.ENGLISH);
205:
206: try {
207: String[] expected = new String[] {
208: "Calculates the median of a collection of images",
209: "MedianCollection requires the source collection to have at least 2 elements", };
210:
211: MedianCollectionDescriptor descriptor = new MedianCollectionDescriptor();
212: String[] got = new String[] {
213: MedianCollectionDescriptor
214: .getMedianCollectionDescription(),
215: descriptor.getBadCardinalityErrorMessage(), };
216:
217: assertEquals(expected.length, got.length);
218:
219: for (int i = 0; i < expected.length; i++) {
220: assertEquals("[" + i + "]", expected[i], got[i]);
221: }
222: } finally {
223: Locale.setDefault(locale);
224: }
225: }
226:
227: /**
228: * Tests the French messages.
229: */
230: public void testFrenchMessages() {
231: Locale locale = Locale.getDefault();
232: Locale.setDefault(Locale.FRENCH);
233:
234: try {
235: String[] expected = new String[] {
236: "Calcule la médiane d'une collection d'image",
237: "L'opération MedianCollection requière que la collection source ait au moins 2 images", };
238:
239: MedianCollectionDescriptor descriptor = new MedianCollectionDescriptor();
240: String[] got = new String[] {
241: MedianCollectionDescriptor
242: .getMedianCollectionDescription(),
243: descriptor.getBadCardinalityErrorMessage(), };
244:
245: assertEquals(expected.length, got.length);
246:
247: for (int i = 0; i < expected.length; i++) {
248: assertEquals("[" + i + "]", expected[i], got[i]);
249: }
250: } finally {
251: Locale.setDefault(locale);
252: }
253: }
254:
255: //---------------------------
256: // Class methods
257: //---------------------------
258:
259: /**
260: * Runs only this test.
261: * @param args ignored.
262: */
263: public static void main(String... args) {
264: junit.swingui.TestRunner
265: .run(MedianCollectionDescriptorTest.class);
266: }
267:
268: }
269:
270: /*
271: * $Log: MedianCollectionDescriptorTest.java,v $
272: * Revision 1.2 2007/08/16 21:25:15 forklabs
273: * Added license.
274: *
275: * Revision 1.1 2007/06/13 18:56:36 forklabs
276: * Operator mediancollection.
277: *
278: */
|