001: /*
002: * @(#) $Header: /cvs/jai-operators/src/tests/ca/forklabs/media/jai/operator/UnaryFunctionDescriptorTest.java,v 1.5 2007/06/13 18:58: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.RenderingHints;
024: import java.awt.image.Raster;
025: import java.awt.image.RenderedImage;
026: import java.awt.image.renderable.ParameterBlock;
027: import java.net.URL;
028: import java.util.Arrays;
029: import java.util.Locale;
030: import javax.media.jai.JAI;
031: import javax.media.jai.RenderedOp;
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.UnaryFunctionDescriptor;
036:
037: /**
038: * Class {@code UnaryFunctionDescriptorTest} tests class
039: * {@link UnaryFunctionDescriptor}.
040: *
041: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.operator.UnaryFunctionDescriptorTest">Daniel Léonard</a>
042: * @version $Revision: 1.5 $
043: */
044: @SuppressWarnings("nls")
045: public class UnaryFunctionDescriptorTest extends TestCase {
046:
047: //---------------------------
048: // Constructors
049: //---------------------------
050:
051: /**
052: * Constructor.
053: * @param name the name of this test.
054: */
055: public UnaryFunctionDescriptorTest(String name) {
056: super (name);
057: }
058:
059: //---------------------------
060: // Test methods
061: //---------------------------
062:
063: /**
064: * Tests
065: * {@link UnaryFunctionDescriptor#create(RenderedImage, UnaryFunction, RenderingHints)}.
066: */
067: public void testCreateRendered() {
068: Class<?> clazz = this .getClass();
069: URL url = clazz
070: .getResource("/ca/forklabs/media/jai/operator/unary_function_descriptor_test.png");
071:
072: assertNotNull(url);
073:
074: ParameterBlock pb = ParameterBlockUtil
075: .createUrlParameterBlock(url);
076: RenderedOp source = JAI.create("url", pb);
077:
078: UnaryFunction<Double, Double> function = new UnaryFunction<Double, Double>() {
079: @SuppressWarnings("boxing")
080: public Double invoke(Double value) {
081: return Math.sqrt(value);
082: }
083: };
084:
085: RenderedOp sink = UnaryFunctionDescriptor.create(source,
086: function, null);
087:
088: Raster raster = sink.getData();
089:
090: assertEquals(0, raster.getMinX());
091: assertEquals(0, raster.getMinY());
092: assertEquals(4, raster.getWidth());
093: assertEquals(4, raster.getHeight());
094: assertEquals(3, raster.getNumBands());
095:
096: int sqrt_255 = (int) (Math.sqrt(255.0) + 0.5);
097: int[] red = new int[] { sqrt_255, 0, 0 };
098: int[] purple = new int[] { sqrt_255, 0, sqrt_255 };
099: int[] pixels = new int[3];
100:
101: assertTrue(Arrays.equals(red, raster.getPixel(0, 0, pixels)));
102: assertTrue(Arrays.equals(red, raster.getPixel(1, 0, pixels)));
103: assertTrue(Arrays.equals(red, raster.getPixel(2, 0, pixels)));
104: assertTrue(Arrays.equals(red, raster.getPixel(3, 0, pixels)));
105:
106: assertTrue(Arrays.equals(red, raster.getPixel(0, 1, pixels)));
107: assertTrue(Arrays.equals(purple, raster.getPixel(1, 1, pixels)));
108: assertTrue(Arrays.equals(purple, raster.getPixel(2, 1, pixels)));
109: assertTrue(Arrays.equals(red, raster.getPixel(3, 1, pixels)));
110:
111: assertTrue(Arrays.equals(red, raster.getPixel(0, 2, pixels)));
112: assertTrue(Arrays.equals(purple, raster.getPixel(1, 2, pixels)));
113: assertTrue(Arrays.equals(purple, raster.getPixel(2, 2, pixels)));
114: assertTrue(Arrays.equals(red, raster.getPixel(3, 2, pixels)));
115:
116: assertTrue(Arrays.equals(red, raster.getPixel(0, 3, pixels)));
117: assertTrue(Arrays.equals(red, raster.getPixel(1, 3, pixels)));
118: assertTrue(Arrays.equals(red, raster.getPixel(2, 3, pixels)));
119: assertTrue(Arrays.equals(red, raster.getPixel(3, 3, pixels)));
120: }
121:
122: // BUG : test UnaryFunctionDescriptor#createRenderable(RenderableImage, UnaryFunction<Double, Double>, RenderingHints)
123:
124: /**
125: * Tests the English messages.
126: */
127: public void testEnglishMessages() {
128: Locale locale = Locale.getDefault();
129: Locale.setDefault(Locale.ENGLISH);
130:
131: try {
132: String[] expected = new String[] {
133: "Application of an unary function on all the pixels",
134: "The unary function", };
135:
136: String[] got = new String[] {
137: UnaryFunctionDescriptor.getDescription(),
138: UnaryFunctionDescriptor.getArg0Description(), };
139:
140: assertEquals(expected.length, got.length);
141:
142: for (int i = 0; i < expected.length; i++) {
143: assertEquals("[" + i + "]", expected[i], got[i]);
144: }
145: } finally {
146: Locale.setDefault(locale);
147: }
148: }
149:
150: /**
151: * Tests the French messages.
152: */
153: public void testFrenchMessages() {
154: Locale locale = Locale.getDefault();
155: Locale.setDefault(Locale.FRENCH);
156:
157: try {
158: String[] expected = new String[] {
159: "Application d'une fonction unaire sur chacun des pixels",
160: "La fonction unaire", };
161:
162: String[] got = new String[] {
163: UnaryFunctionDescriptor.getDescription(),
164: UnaryFunctionDescriptor.getArg0Description(), };
165:
166: assertEquals(expected.length, got.length);
167:
168: for (int i = 0; i < expected.length; i++) {
169: assertEquals("[" + i + "]", expected[i], got[i]);
170: }
171: } finally {
172: Locale.setDefault(locale);
173: }
174: }
175:
176: //---------------------------
177: // Class methods
178: //---------------------------
179:
180: /**
181: * Runs only this test.
182: * @param args ignored.
183: */
184: public static void main(String... args) {
185: junit.swingui.TestRunner.run(UnaryFunctionDescriptorTest.class);
186: }
187:
188: }
189:
190: /*
191: * $Log: UnaryFunctionDescriptorTest.java,v $
192: * Revision 1.5 2007/06/13 18:58:16 forklabs
193: * Removed unused import directives.
194: *
195: * Revision 1.4 2007/05/10 18:10:40 forklabs
196: * Moved the indexed test from the descriptor to the opimage.
197: *
198: * Revision 1.3 2007/05/10 17:57:55 forklabs
199: * Now tests transformColorMap().
200: *
201: * Revision 1.2 2007/05/03 20:27:36 forklabs
202: * Trimmed trailing spaces.
203: *
204: * Revision 1.1 2007/05/03 18:32:27 forklabs
205: * Initial commit for the unaryfunction operator.
206: *
207: */
|