001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2005-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2005, Institut de Recherche pour le Développement
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.coverage.grid;
018:
019: // J2SE and JAI dependencies
020: import java.io.IOException;
021: import java.awt.image.Raster;
022: import java.awt.image.RenderedImage;
023: import javax.media.jai.OperationNode;
024:
025: // JUnit dependencies
026: import junit.framework.Test;
027: import junit.framework.TestSuite;
028:
029: // OpenGIS dependencies
030: import org.opengis.coverage.grid.GridCoverage;
031:
032: // Geotools dependencies
033: import org.geotools.test.TestData;
034: import org.geotools.resources.Arguments;
035: import org.geotools.coverage.grid.GridCoverage2D;
036: import org.geotools.coverage.processing.Operations;
037:
038: /**
039: * Tests JAI operation wrapped as {@link OperatorJAI}.
040: * <p>
041: * <strong>NOTE:</strong>
042: * This test may fails when executed on a machine without the <cite>mediaLib</cite> accelerator.
043: * On Windows, the {@code mlib_jai.dll} and {@code mlib_jai_mmx.dll} files should exist in the
044: * {@code jre/bin} directory, as well as {@code mlibwrapper_jai.jar} in {@code jre/lib/ext}.
045: * Those {@code .dll} files should be there if JAI has been installed with the Sun standard
046: * installation program ({@code jai-1_1_2_01-lib-windows-i586-jdk.exe}). With such installation,
047: * everything should run fine. The {@code .dll} files are probably missing if JAI has been put in
048: * the classpath by Maven, like our past attempt on the 2.1 branch.
049: * <p>
050: * This behavior looks like a JAI bug to me. In theory, the pure Java mode is supposed to produce
051: * exactly the same result than the <cite>mediaLib</cite> native mode; just slower. This test
052: * failure suggests that it is not always the case. The <cite>mediaLib</cite> native code seems
053: * right in this case (the bug would be in the pure Java code).
054: *
055: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/grid/OperationsTest.java $
056: * @version $Id: OperationsTest.java 25591 2007-05-20 10:43:59Z desruisseaux $
057: * @author Martin Desruisseaux
058: */
059: public final class OperationsTest extends GridCoverageTest {
060: /**
061: * Sets to {@code true} in order to display the coverage. This is used for manual inspection
062: * only. This field is set to {@code true} if this test suite is executed from the command line
063: * with the {@code -show} option.
064: */
065: private static boolean show;
066:
067: /**
068: * Sample image. This field is static in order to avoid reloading it for every test cases
069: * in this class.
070: */
071: private static GridCoverage2D SST;
072:
073: /**
074: * The grid coverage processor.
075: */
076: private Operations processor;
077:
078: /**
079: * Run the suite from the command line.
080: */
081: public static void main(final String[] args) {
082: final Arguments arguments = new Arguments(args);
083: show = arguments.getFlag("-show");
084: arguments.getRemainingArguments(0);
085: junit.textui.TestRunner.run(suite());
086: }
087:
088: /**
089: * Returns the test suite.
090: */
091: public static Test suite() {
092: return new TestSuite(OperationsTest.class);
093: }
094:
095: /**
096: * Constructs a test case with the given name.
097: */
098: public OperationsTest(final String name) {
099: super (name);
100: }
101:
102: /**
103: * Fetch the processor before each test.
104: */
105: protected void setUp() throws IOException {
106: processor = Operations.DEFAULT;
107: if (SST == null) {
108: SST = GridCoverageExamples.getExample(0);
109: }
110: }
111:
112: /**
113: * Applies an operation on the specified coverage. All tests in the parent classes will
114: * be executed on this transformed coverage.
115: *
116: * @todo Applies some operation.
117: */
118: //@Override
119: protected GridCoverage2D transform(final GridCoverage2D coverage) {
120: return ((GridCoverage2D) processor.nodataFilter(coverage
121: .geophysics(true))).geophysics(false);
122: }
123:
124: /**
125: * Show the specified coverage. This is used for debugging only.
126: */
127: private static void show(GridCoverage coverage) {
128: if (coverage instanceof GridCoverage2D) {
129: coverage = ((GridCoverage2D) coverage).geophysics(false);
130: }
131: final RenderedImage image = coverage.getRenderableImage(0, 1)
132: .createDefaultRendering();
133: try {
134: Class
135: .forName(
136: "org.geotools.gui.swing.OperationTreeBrowser")
137: .getMethod("show",
138: new Class[] { RenderedImage.class })
139: .invoke(null, new Object[] { image });
140: } catch (Exception e) {
141: /*
142: * The OperationTreeBrowser is not part of Geotools's core. It is optional and this
143: * class should not fails if it is not presents. This is only a helper for debugging.
144: */
145: }
146: }
147:
148: /**
149: * Tests {@link Operations#subtract}.
150: *
151: * @todo Investigate why the color palette is lost.
152: */
153: public void testSubtract() {
154: double[] constants = new double[] { 18.75 };
155: GridCoverage sourceCoverage = SST.geophysics(true);
156: GridCoverage targetCoverage = (GridCoverage) processor
157: .subtract(sourceCoverage, constants);
158: RenderedImage sourceImage = sourceCoverage.getRenderableImage(
159: 0, 1).createDefaultRendering();
160: RenderedImage targetImage = targetCoverage.getRenderableImage(
161: 0, 1).createDefaultRendering();
162: Raster sourceRaster = sourceImage.getData();
163: Raster targetRaster = targetImage.getData();
164: assertNotSame(sourceCoverage, targetCoverage);
165: assertNotSame(sourceImage, targetImage);
166: assertNotSame(sourceRaster, targetRaster);
167: assertSame(sourceCoverage.getCoordinateReferenceSystem(),
168: targetCoverage.getCoordinateReferenceSystem());
169: assertEquals(sourceCoverage.getEnvelope(), targetCoverage
170: .getEnvelope());
171: assertEquals(sourceCoverage.getGridGeometry(), targetCoverage
172: .getGridGeometry());
173: assertEquals(sourceRaster.getMinX(), targetRaster.getMinX());
174: assertEquals(sourceRaster.getMinY(), targetRaster.getMinY());
175: assertEquals(sourceRaster.getWidth(), targetRaster.getWidth());
176: assertEquals(sourceRaster.getHeight(), targetRaster.getHeight());
177: assertEquals(0, sourceRaster.getMinX());
178: assertEquals(0, sourceRaster.getMinY());
179: assertEquals("SubtractConst", ((OperationNode) targetImage)
180: .getOperationName());
181:
182: final boolean medialib = TestData.isMediaLibAvailable();
183: for (int y = sourceRaster.getHeight(); --y >= 0;) {
184: for (int x = sourceRaster.getWidth(); --x >= 0;) {
185: final float s = sourceRaster.getSampleFloat(x, y, 0);
186: final float t = targetRaster.getSampleFloat(x, y, 0);
187: if (Float.isNaN(s)) {
188: /*
189: * For a mysterious reason (JAI bug?), the following test seems to fail when
190: * JAI is running in pure Java mode. If you get an assertion failure on this
191: * line, then make sure that "<your_jdk_path>/jre/bin/mlib_jai.dll" (Windows)
192: * or "lib/i386/libmlib_jai.so" (Linux) is presents in your JDK installation.
193: */
194: if (medialib) {
195: assertTrue(Float.isNaN(t));
196: }
197: } else {
198: assertEquals(s - constants[0], t, 1E-3f);
199: }
200: }
201: }
202: if (show) {
203: show(targetCoverage);
204: }
205: }
206:
207: /**
208: * Tests {@link Operations#nodataFilter}.
209: */
210: public void testNodataFilter() {
211: GridCoverage sourceCoverage = SST.geophysics(true);
212: GridCoverage targetCoverage = processor
213: .nodataFilter(sourceCoverage);
214: RenderedImage sourceImage = sourceCoverage.getRenderableImage(
215: 0, 1).createDefaultRendering();
216: RenderedImage targetImage = targetCoverage.getRenderableImage(
217: 0, 1).createDefaultRendering();
218: Raster sourceRaster = sourceImage.getData();
219: Raster targetRaster = targetImage.getData();
220: assertNotSame(sourceCoverage, targetCoverage);
221: assertNotSame(sourceImage, targetImage);
222: assertNotSame(sourceRaster, targetRaster);
223: assertSame(sourceCoverage.getCoordinateReferenceSystem(),
224: targetCoverage.getCoordinateReferenceSystem());
225: assertEquals(sourceCoverage.getEnvelope(), targetCoverage
226: .getEnvelope());
227: assertEquals(sourceCoverage.getGridGeometry(), targetCoverage
228: .getGridGeometry());
229: assertEquals(sourceRaster.getMinX(), targetRaster.getMinX());
230: assertEquals(sourceRaster.getMinY(), targetRaster.getMinY());
231: assertEquals(sourceRaster.getWidth(), targetRaster.getWidth());
232: assertEquals(sourceRaster.getHeight(), targetRaster.getHeight());
233: assertEquals(0, sourceRaster.getMinX());
234: assertEquals(0, sourceRaster.getMinY());
235: assertEquals("org.geotools.NodataFilter",
236: ((OperationNode) targetImage).getOperationName());
237:
238: for (int y = sourceRaster.getHeight(); --y >= 0;) {
239: for (int x = sourceRaster.getWidth(); --x >= 0;) {
240: final float s = sourceRaster.getSampleFloat(x, y, 0);
241: final float t = targetRaster.getSampleFloat(x, y, 0);
242: if (Float.isNaN(s)) {
243: if (!Float.isNaN(t)) {
244: // TODO: put some test here.
245: }
246: } else {
247: assertEquals(s, t, 1E-5f);
248: }
249: }
250: }
251: if (show) {
252: show(targetCoverage);
253: }
254: }
255:
256: /**
257: * Tests {@link Operations#gradientMagnitude}.
258: *
259: * @todo Investigate why the geophysics view is much more visible than the non-geophysics one.
260: */
261: public void testGradientMagnitude() {
262: GridCoverage sourceCoverage = SST.geophysics(true);
263: GridCoverage targetCoverage = (GridCoverage) processor
264: .gradientMagnitude(sourceCoverage);
265: RenderedImage sourceImage = sourceCoverage.getRenderableImage(
266: 0, 1).createDefaultRendering();
267: RenderedImage targetImage = targetCoverage.getRenderableImage(
268: 0, 1).createDefaultRendering();
269: Raster sourceRaster = sourceImage.getData();
270: Raster targetRaster = targetImage.getData();
271: assertNotSame(sourceCoverage, targetCoverage);
272: assertNotSame(sourceImage, targetImage);
273: assertNotSame(sourceRaster, targetRaster);
274: assertSame(sourceCoverage.getCoordinateReferenceSystem(),
275: targetCoverage.getCoordinateReferenceSystem());
276: assertEquals(sourceCoverage.getEnvelope(), targetCoverage
277: .getEnvelope());
278: assertEquals(sourceCoverage.getGridGeometry(), targetCoverage
279: .getGridGeometry());
280: assertEquals(sourceRaster.getMinX(), targetRaster.getMinX());
281: assertEquals(sourceRaster.getMinY(), targetRaster.getMinY());
282: assertEquals(sourceRaster.getWidth(), targetRaster.getWidth());
283: assertEquals(sourceRaster.getHeight(), targetRaster.getHeight());
284: assertEquals(0, sourceRaster.getMinX());
285: assertEquals(0, sourceRaster.getMinY());
286: assertEquals("GradientMagnitude", ((OperationNode) targetImage)
287: .getOperationName());
288:
289: assertEquals(3.95f, targetRaster.getSampleFloat(304, 310, 0),
290: 1E-2f);
291: assertEquals(1.88f, targetRaster.getSampleFloat(262, 357, 0),
292: 1E-2f);
293:
294: if (show) {
295: show(targetCoverage);
296: }
297: }
298: }
|