001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/AutoRescaleDescriptorTest.java,v 1.3 2007/05/10 18:00:06 forklabs Exp $
003: *
004: * Copyright (C) 2007 DIRO Daniel Léonard
005: */
006:
007: package ca.forklabs.media.jai.operator;
008:
009: import java.awt.RenderingHints;
010: import java.awt.image.Raster;
011: import java.awt.image.RenderedImage;
012: import java.awt.image.renderable.ParameterBlock;
013: import java.net.URL;
014: import java.util.Arrays;
015: import java.util.Locale;
016: import javax.media.jai.JAI;
017: import javax.media.jai.RenderedOp;
018: import junit.framework.TestCase;
019: import ca.forklabs.media.jai.ParameterBlockUtil;
020: import ca.forklabs.media.jai.operator.AutoRescaleDescriptor;
021:
022: /**
023: * Class {@code AutoRescaleDescriptorTest} tests class
024: * {@link AutoRescaleDescriptor}.
025: *
026: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.AutoRescaleDescriptorTest">Daniel Léonard</a>
027: * @version $Revision: 1.3 $
028: */
029: @SuppressWarnings("nls")
030: public class AutoRescaleDescriptorTest extends TestCase {
031:
032: //---------------------------
033: // Constructors
034: //---------------------------
035:
036: /**
037: * Constructor.
038: * @param name the name of this test.
039: */
040: public AutoRescaleDescriptorTest(String name) {
041: super (name);
042: }
043:
044: //---------------------------
045: // Test methods
046: //---------------------------
047:
048: /**
049: * Tests the error catching in
050: * {@link AutoRescaleDescriptor#validateParameters(String, ParameterBlock, StringBuffer)}
051: */
052: public void testValidateBadParameters() {
053: ParameterBlock pb_ok = new ParameterBlock().add(
054: new double[] { Math.E, })
055: .add(new double[] { Math.PI, });
056: ParameterBlock pb_no_lower = new ParameterBlock().add(
057: new double[] {}).add(new double[] { Math.PI, });
058: ParameterBlock pb_no_upper = new ParameterBlock().add(
059: new double[] { Math.E, }).add(new double[] {});
060: ParameterBlock pb_more_lower = new ParameterBlock().add(
061: new double[] { Math.E, 1.0, }).add(
062: new double[] { Math.PI, });
063: ParameterBlock pb_more_upper = new ParameterBlock().add(
064: new double[] { Math.E, }).add(
065: new double[] { Math.PI, 1.0, });
066: ParameterBlock pb_inverted = new ParameterBlock().add(
067: new double[] { Math.PI, })
068: .add(new double[] { Math.E, });
069:
070: StringBuffer sb = new StringBuffer();
071:
072: AutoRescaleDescriptor descriptor = new AutoRescaleDescriptor();
073: boolean got;
074:
075: // test no rendering mode - thrown in superclass
076: try {
077: got = descriptor.validateParameters(null, pb_ok, sb);
078: fail("should have thrown IllegalArgumentException");
079: } catch (IllegalArgumentException iae) {
080: // normal behaviour
081: }
082:
083: // bad rendering mode - thrown in superclass
084: try {
085: got = descriptor.validateParameters("badmode", pb_ok, sb);
086: fail("should have thrown IllegalArgumentException");
087: } catch (IllegalArgumentException iae) {
088: // normal behaviour
089: }
090:
091: // no parameter block - thrown in superclass
092: try {
093: got = descriptor.validateParameters("rendered", null, sb);
094: fail("should have thrown IllegalArgumentException");
095: } catch (IllegalArgumentException iae) {
096: // normal behaviour
097: }
098:
099: // no lower bounds
100: got = descriptor
101: .validateParameters("rendered", pb_no_lower, sb);
102: assertFalse(got);
103:
104: // no upper bounds
105: got = descriptor
106: .validateParameters("rendered", pb_no_upper, sb);
107: assertFalse(got);
108:
109: // more lower bounds
110: got = descriptor.validateParameters("rendered", pb_more_lower,
111: sb);
112: assertFalse(got);
113:
114: // more upper bounds
115: got = descriptor.validateParameters("rendered", pb_more_upper,
116: sb);
117: assertFalse(got);
118:
119: // inversion of bounds
120: got = descriptor
121: .validateParameters("rendered", pb_inverted, sb);
122: assertFalse(got);
123:
124: // all ok
125: got = descriptor.validateParameters("renderable", pb_ok, sb);
126: assertTrue(got);
127: }
128:
129: /**
130: * Tests
131: * {@link AutoRescaleDescriptor#create(RenderedImage, double[], double[], RenderingHints)}.
132: */
133: @SuppressWarnings("boxing")
134: public void testCreateRendered() {
135: Class<?> clazz = this .getClass();
136: URL url = clazz
137: .getResource("/ca/forklabs/media/jai/operator/auto_rescale_test_pic.png");
138:
139: assertNotNull(url);
140:
141: ParameterBlock pb = ParameterBlockUtil
142: .createUrlParameterBlock(url);
143: RenderedOp source = JAI.create("url", pb);
144:
145: double[] lower_bounds = new double[] { 100.0, };
146: double[] upper_bounds = new double[] { 200.0, };
147: RenderedOp image = AutoRescaleDescriptor.create(source,
148: lower_bounds, upper_bounds, null);
149:
150: Raster raster = image.getData();
151:
152: assertEquals(0, raster.getMinX());
153: assertEquals(0, raster.getMinY());
154: assertEquals(4, raster.getWidth());
155: assertEquals(4, raster.getHeight());
156: assertEquals(3, raster.getNumBands());
157:
158: int[] black = new int[] { 100, 100, 100 };
159: int[] dark_grey = new int[] { 150, 150, 150 };
160: int[] light_grey = new int[] { 175, 175, 175 };
161: int[] white = new int[] { 200, 200, 200 };
162: int[] got = null;
163:
164: assertTrue(Arrays.equals(black, raster.getPixel(0, 0, got)));
165: assertTrue(Arrays.equals(black, raster.getPixel(1, 0, got)));
166: assertTrue(Arrays.equals(black, raster.getPixel(2, 0, got)));
167: assertTrue(Arrays.equals(black, raster.getPixel(3, 0, got)));
168:
169: assertTrue(Arrays.equals(dark_grey, raster.getPixel(0, 1, got)));
170: assertTrue(Arrays.equals(dark_grey, raster.getPixel(1, 1, got)));
171: assertTrue(Arrays.equals(dark_grey, raster.getPixel(2, 1, got)));
172: assertTrue(Arrays.equals(dark_grey, raster.getPixel(3, 1, got)));
173:
174: assertTrue(Arrays
175: .equals(light_grey, raster.getPixel(0, 2, got)));
176: assertTrue(Arrays
177: .equals(light_grey, raster.getPixel(1, 2, got)));
178: assertTrue(Arrays
179: .equals(light_grey, raster.getPixel(2, 2, got)));
180: assertTrue(Arrays
181: .equals(light_grey, raster.getPixel(3, 2, got)));
182:
183: assertTrue(Arrays.equals(white, raster.getPixel(0, 3, got)));
184: assertTrue(Arrays.equals(white, raster.getPixel(1, 3, got)));
185: assertTrue(Arrays.equals(white, raster.getPixel(2, 3, got)));
186: assertTrue(Arrays.equals(white, raster.getPixel(3, 3, got)));
187: }
188:
189: /**
190: * Tests what happens when an autorescale is asked on a constant image.
191: */
192: @SuppressWarnings("boxing")
193: public void testConstant() {
194: ParameterBlock pb = ParameterBlockUtil
195: .createConstantParameterBlock(4.0f, 4.0f,
196: new Float[] { 12.34f, });
197: RenderedImage source = JAI.create("constant", pb);
198:
199: double[] lower_bounds = new double[] { 100.0, };
200: double[] upper_bounds = new double[] { 200.0, };
201: RenderedOp image = AutoRescaleDescriptor.create(source,
202: lower_bounds, upper_bounds, null);
203:
204: Raster raster = image.getData();
205:
206: assertEquals(0, raster.getMinX());
207: assertEquals(0, raster.getMinY());
208: assertEquals(4, raster.getWidth());
209: assertEquals(4, raster.getHeight());
210: assertEquals(1, raster.getNumBands());
211:
212: float[] middle = new float[] { 150.0f, };
213: float[] got = null;
214:
215: assertTrue(Arrays.equals(middle, raster.getPixel(0, 0, got)));
216: assertTrue(Arrays.equals(middle, raster.getPixel(1, 0, got)));
217: assertTrue(Arrays.equals(middle, raster.getPixel(2, 0, got)));
218: assertTrue(Arrays.equals(middle, raster.getPixel(3, 0, got)));
219:
220: assertTrue(Arrays.equals(middle, raster.getPixel(0, 1, got)));
221: assertTrue(Arrays.equals(middle, raster.getPixel(1, 1, got)));
222: assertTrue(Arrays.equals(middle, raster.getPixel(2, 1, got)));
223: assertTrue(Arrays.equals(middle, raster.getPixel(3, 1, got)));
224:
225: assertTrue(Arrays.equals(middle, raster.getPixel(0, 2, got)));
226: assertTrue(Arrays.equals(middle, raster.getPixel(1, 2, got)));
227: assertTrue(Arrays.equals(middle, raster.getPixel(2, 2, got)));
228: assertTrue(Arrays.equals(middle, raster.getPixel(3, 2, got)));
229:
230: assertTrue(Arrays.equals(middle, raster.getPixel(0, 3, got)));
231: assertTrue(Arrays.equals(middle, raster.getPixel(1, 3, got)));
232: assertTrue(Arrays.equals(middle, raster.getPixel(2, 3, got)));
233: assertTrue(Arrays.equals(middle, raster.getPixel(3, 3, got)));
234: }
235:
236: /**
237: * Tests the English messages.
238: */
239: public void testEnglishMessages() {
240: Locale locale = Locale.getDefault();
241: Locale.setDefault(Locale.ENGLISH);
242:
243: try {
244: String[] expected = new String[] {
245: "Rescale made easier",
246: "The lower bounds of the range",
247: "The upper bounds of the range",
248: "There are no lower bounds",
249: "There are no upper bounds",
250: "There are 1 lower bounds but only 2 upper bounds",
251: "A lower bound (5) is greater than its upper bound (4)", };
252:
253: AutoRescaleDescriptor descriptor = new AutoRescaleDescriptor();
254: String[] got = new String[] {
255: AutoRescaleDescriptor.getDescription(),
256: AutoRescaleDescriptor.getArg0Description(),
257: AutoRescaleDescriptor.getArg1Description(),
258: descriptor.getNoLowerBounds(),
259: descriptor.getNoUpperBounds(),
260: descriptor.getNotSameNumberOfBoundsErrorMessage(1,
261: 2),
262: descriptor
263: .getLowerBoundGreaterThanUpperBoundErrorMessage(
264: 5, 4), };
265:
266: assertEquals(expected.length, got.length);
267:
268: for (int i = 0; i < expected.length; i++) {
269: assertEquals("[" + i + "]", expected[i], got[i]);
270: }
271: } finally {
272: Locale.setDefault(locale);
273: }
274: }
275:
276: /**
277: * Tests the French messages.
278: */
279: public void testFrenchMessages() {
280: Locale locale = Locale.getDefault();
281: Locale.setDefault(Locale.FRENCH);
282:
283: try {
284: String[] expected = new String[] {
285: "\"Rescale\" plus facile",
286: "Les bornes inférieures de l'intervalle",
287: "Les bornes supérieures de l'intervalle",
288: "Il n'y a pas de bornes inférieures",
289: "Il n'y a pas de bornes supérieures",
290: "Il y a 1 bornes inférieures mais seulement 2 bornes supérieures",
291: "Une borne inférieure (5) est plus grande que sa borne supérieure (4)", };
292:
293: AutoRescaleDescriptor descriptor = new AutoRescaleDescriptor();
294: String[] got = new String[] {
295: AutoRescaleDescriptor.getDescription(),
296: AutoRescaleDescriptor.getArg0Description(),
297: AutoRescaleDescriptor.getArg1Description(),
298: descriptor.getNoLowerBounds(),
299: descriptor.getNoUpperBounds(),
300: descriptor.getNotSameNumberOfBoundsErrorMessage(1,
301: 2),
302: descriptor
303: .getLowerBoundGreaterThanUpperBoundErrorMessage(
304: 5, 4), };
305:
306: assertEquals(expected.length, got.length);
307:
308: for (int i = 0; i < expected.length; i++) {
309: assertEquals("[" + i + "]", expected[i], got[i]);
310: }
311: } finally {
312: Locale.setDefault(locale);
313: }
314: }
315:
316: //---------------------------
317: // Class methods
318: //---------------------------
319:
320: /**
321: * Runs only this test.
322: * @param args ignored.
323: */
324: public static void main(String... args) {
325: junit.swingui.TestRunner.run(AutoRescaleDescriptorTest.class);
326: }
327:
328: }
329:
330: /*
331: * $Log: AutoRescaleDescriptorTest.java,v $
332: * Revision 1.3 2007/05/10 18:00:06 forklabs
333: * Use operator url instead of operator stream.
334: *
335: * Revision 1.2 2007/05/03 20:34:13 forklabs
336: * Fixed the image path.
337: *
338: * Revision 1.1 2007/05/03 20:24:40 forklabs
339: * Operator "autorescale"
340: *
341: */
|