001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/ApplyToCollectionDescriptorTest.java,v 1.2 2007/06/13 18:57:21 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.Iterator;
030: import java.util.Locale;
031: import javax.media.jai.JAI;
032: import junit.framework.TestCase;
033: import ca.forklabs.baselib.util.UnaryFunction;
034: import ca.forklabs.media.jai.ParameterBlockUtil;
035: import ca.forklabs.media.jai.operator.ApplyToCollectionDescriptor;
036:
037: /**
038: * Class {@code ApplyToCollectionDescriptorTest} tests class
039: * {@link ApplyToCollectionDescriptor}.
040: *
041: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.ApplyToCollectionDescriptorTest">Daniel Léonard</a>
042: * @version $Revision: 1.2 $
043: */
044: @SuppressWarnings("nls")
045: public class ApplyToCollectionDescriptorTest extends TestCase {
046:
047: //---------------------------
048: // Constructors
049: //---------------------------
050:
051: /**
052: * Constructor.
053: * @param name the name of this test.
054: */
055: public ApplyToCollectionDescriptorTest(String name) {
056: super (name);
057: }
058:
059: //---------------------------
060: // Test methods
061: //---------------------------
062:
063: private void checkImage(Object source, float expected) {
064: assertTrue(source instanceof RenderedImage);
065:
066: RenderedImage image = (RenderedImage) source;
067: Raster raster = image.getData();
068:
069: assertEquals(0, raster.getMinX());
070: assertEquals(0, raster.getMinY());
071: assertEquals(2, raster.getWidth());
072: assertEquals(2, raster.getHeight());
073: assertEquals(1, raster.getNumBands());
074:
075: float[] pixel = new float[] { expected, };
076: float[] got = null;
077:
078: assertTrue(Arrays.equals(pixel, raster.getPixel(0, 0, got)));
079: assertTrue(Arrays.equals(pixel, raster.getPixel(1, 0, got)));
080: assertTrue(Arrays.equals(pixel, raster.getPixel(2, 0, got)));
081: assertTrue(Arrays.equals(pixel, raster.getPixel(3, 0, got)));
082: }
083:
084: /**
085: * Tests
086: * {@link ApplyToCollectionDescriptor#createCollection(java.util.Collection, String, ParameterBlock, RenderingHints)}.
087: */
088: @SuppressWarnings("boxing")
089: public void testCreateCollection() {
090: RenderedImage[] images = new RenderedImage[] {
091: JAI.create("constant", ParameterBlockUtil
092: .createConstantParameterBlock(2.0f, 2.0f,
093: new Float[] { 4.0f, })),
094: JAI.create("constant", ParameterBlockUtil
095: .createConstantParameterBlock(2.0f, 2.0f,
096: new Float[] { 9.0f, })),
097: JAI.create("constant", ParameterBlockUtil
098: .createConstantParameterBlock(2.0f, 2.0f,
099: new Float[] { 16.0f, })),
100: JAI.create("constant", ParameterBlockUtil
101: .createConstantParameterBlock(2.0f, 2.0f,
102: new Float[] { 25.0f, })), };
103: Collection<RenderedImage> sources = Arrays.asList(images);
104:
105: String name = UnaryFunctionDescriptor.NAME;
106: ParameterBlock pb = new ParameterBlock()
107: .add(new UnaryFunction<Double, Double>() {
108: @Override
109: public Double invoke(Double value) {
110: return Math.sqrt(value);
111: }
112: });
113:
114: // check with the convenience method
115: Collection<?> sinks = ApplyToCollectionDescriptor
116: .createCollection(sources, name, pb, null);
117:
118: assertEquals(4, sinks.size());
119:
120: Iterator<?> iter = sinks.iterator();
121: checkImage(iter.next(), 2.0f);
122: checkImage(iter.next(), 3.0f);
123: checkImage(iter.next(), 4.0f);
124: checkImage(iter.next(), 5.0f);
125:
126: // check with multiple sources
127: ParameterBlock pb2 = new ParameterBlock().addSource(images[0])
128: .addSource(images[1]).addSource(images[2]).addSource(
129: images[3]).add(name).add(pb);
130: sinks = JAI.createCollection("applytocollection", pb2, null);
131:
132: assertEquals(4, sinks.size());
133:
134: iter = sinks.iterator();
135: checkImage(iter.next(), 2.0f);
136: checkImage(iter.next(), 3.0f);
137: checkImage(iter.next(), 4.0f);
138: checkImage(iter.next(), 5.0f);
139: }
140:
141: /**
142: * Tests the English messages.
143: */
144: public void testEnglishMessages() {
145: Locale locale = Locale.getDefault();
146: Locale.setDefault(Locale.ENGLISH);
147:
148: try {
149: String[] expected = new String[] {
150: "Applies the same operator to a collection of images",
151: "The operation name", "The parameter block", };
152:
153: String[] got = new String[] {
154: ApplyToCollectionDescriptor.getDescription(),
155: ApplyToCollectionDescriptor.getArg0Description(),
156: ApplyToCollectionDescriptor.getArg1Description(), };
157:
158: assertEquals(expected.length, got.length);
159:
160: for (int i = 0; i < expected.length; i++) {
161: assertEquals("[" + i + "]", expected[i], got[i]);
162: }
163: } finally {
164: Locale.setDefault(locale);
165: }
166: }
167:
168: /**
169: * Tests the French messages.
170: */
171: public void testFrenchMessages() {
172: Locale locale = Locale.getDefault();
173: Locale.setDefault(Locale.FRENCH);
174:
175: try {
176: String[] expected = new String[] {
177: "Applique une même opération à une collection d'image",
178: "Le nom de l'opération", "Le bloc de paramêtres", };
179:
180: String[] got = new String[] {
181: ApplyToCollectionDescriptor.getDescription(),
182: ApplyToCollectionDescriptor.getArg0Description(),
183: ApplyToCollectionDescriptor.getArg1Description(), };
184:
185: assertEquals(expected.length, got.length);
186:
187: for (int i = 0; i < expected.length; i++) {
188: assertEquals("[" + i + "]", expected[i], got[i]);
189: }
190: } finally {
191: Locale.setDefault(locale);
192: }
193: }
194:
195: //---------------------------
196: // Class methods
197: //---------------------------
198:
199: /**
200: * Runs only this test.
201: * @param args ignored.
202: */
203: public static void main(String... args) {
204: junit.swingui.TestRunner
205: .run(ApplyToCollectionDescriptorTest.class);
206: }
207:
208: }
209:
210: /*
211: * $Log: ApplyToCollectionDescriptorTest.java,v $
212: * Revision 1.2 2007/06/13 18:57:21 forklabs
213: * Changed parent to use CollectionDescriptor.
214: *
215: * Revision 1.1 2007/05/25 21:21:45 forklabs
216: * Operator "applytocollection"
217: *
218: */
|