001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library 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 GNU
014: * Lesser General Public License for more details.
015: */
016: package org.geotools.coverage.grid;
017:
018: // J2SE and JAI dependencies
019: import java.awt.RenderingHints;
020: import java.awt.image.RenderedImage;
021: import java.io.IOException;
022: import javax.media.jai.BorderExtender;
023: import javax.media.jai.BorderExtenderCopy;
024: import javax.media.jai.Interpolation;
025: import javax.media.jai.RenderedOp;
026:
027: // JUnit dependencies
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031:
032: // OpenGIS dependencies
033: import org.opengis.parameter.ParameterValueGroup;
034:
035: // Geotools dependencies
036: import org.geotools.coverage.processing.AbstractProcessor;
037: import org.geotools.coverage.processing.DefaultProcessor;
038: import org.geotools.coverage.processing.Operations;
039: import org.geotools.factory.Hints;
040:
041: /**
042: * Tests the Subsample Average operation.
043: *
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/grid/SubsampleAverageTest.java $
045: * @version $Id: SubsampleAverageTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
046: * @author Simone Giannecchini
047: *
048: * @since 2.3
049: */
050: public class SubsampleAverageTest extends GridCoverageTest {
051: /**
052: * {@code true} if the result should be displayed in windows during test execution.
053: * Default to {@code false}. This flag is set to {@code true} only if this test suite
054: * is executed explicitly though the {@link #main} method.
055: */
056: private static boolean SHOW;
057:
058: /**
059: * The source grid coverage.
060: */
061: private GridCoverage2D indexedCoverage;
062:
063: private GridCoverage2D indexedCoverageWithTransparency;
064:
065: private GridCoverage2D originallyIndexedCoverage;
066:
067: /**
068: * Creates a test suite for the given name.
069: */
070: public SubsampleAverageTest(String name) {
071: super (name);
072: // TODO Auto-generated constructor stub
073: }
074:
075: /**
076: * Returns the test suite.
077: */
078: public static Test suite() {
079: return new TestSuite(SubsampleAverageTest.class);
080: }
081:
082: /**
083: * Run the suit from the command line.
084: */
085: public static void main(final String[] args) {
086: SHOW = true;
087: org.geotools.util.logging.Logging.GEOTOOLS
088: .forceMonolineConsoleOutput(AbstractProcessor.OPERATION);
089: junit.textui.TestRunner.run(SubsampleAverageTest.class);
090: }
091:
092: /**
093: * Set up common objects used for all tests.
094: */
095: //@Override
096: protected void setUp() throws Exception {
097: super .setUp();
098: originallyIndexedCoverage = GridCoverageExamples.getExample(0);
099: indexedCoverage = GridCoverageExamples.getExample(2);
100: indexedCoverageWithTransparency = GridCoverageExamples
101: .getExample(3);
102: }
103:
104: /**
105: * Tests the "SubsampleAverage" operation.
106: * @throws IOException
107: */
108: public void testSubsampleAverage() throws IOException {
109: // on this one the Subsample average should do an RGB expansion
110: subsampleAverage(indexedCoverage.geophysics(true));
111: // on this one the Subsample average should do an RGB expansion
112: // preserving alpha
113: subsampleAverage(indexedCoverageWithTransparency
114: .geophysics(true));
115: // on this one the subsample average should go back to the geophysiscs
116: // view before being applied
117: subsampleAverage(originallyIndexedCoverage.geophysics(true));
118:
119: // on this one the Subsample average should do an RGB expansion
120: subsampleAverage(indexedCoverage.geophysics(false));
121: // on this one the Subsample average should do an RGB expansion
122: // preserving alpha
123: subsampleAverage(indexedCoverageWithTransparency
124: .geophysics(false));
125: // on this one the subsample average should go back to the geophysiscs
126: // view before being applied
127: subsampleAverage(originallyIndexedCoverage.geophysics(false));
128:
129: // on this one the subsample average should NOT go back to the
130: // geophysiscs view before being applied
131: subsampleAverage(GridCoverageExamples.getExample(4).geophysics(
132: false), new Hints(Hints.REPLACE_NON_GEOPHYSICS_VIEW,
133: Boolean.FALSE));
134:
135: // on this one the subsample average should go back to the
136: // geophysiscs view before being applied
137: subsampleAverage(GridCoverageExamples.getExample(4).geophysics(
138: false), new Hints(Hints.REPLACE_NON_GEOPHYSICS_VIEW,
139: Boolean.FALSE));
140: }
141:
142: public void subsampleAverage(GridCoverage2D coverage) {
143: subsampleAverage(coverage, null);
144: }
145:
146: public void subsampleAverage(GridCoverage2D coverage,
147: RenderingHints hints) {
148: // caching initial properties
149: RenderedImage originalImage = coverage.getRenderedImage();
150: int w = originalImage.getWidth();
151: int h = originalImage.getHeight();
152:
153: // get the processor and prepare the first operation
154: final DefaultProcessor processor = new DefaultProcessor(hints);
155: final ParameterValueGroup param = processor.getOperation(
156: "SubsampleAverage").getParameters();
157: param.parameter("Source").setValue(coverage);
158: param.parameter("scaleX").setValue(new Double(0.5));
159: param.parameter("scaleY").setValue(new Double(0.5));
160: param
161: .parameter("Interpolation")
162: .setValue(
163: Interpolation
164: .getInstance(Interpolation.INTERP_NEAREST));
165: param.parameter("BorderExtender").setValue(
166: BorderExtenderCopy
167: .createInstance(BorderExtender.BORDER_COPY));
168: GridCoverage2D scaled = (GridCoverage2D) processor
169: .doOperation(param);
170: RenderedImage scaledImage = scaled.getRenderedImage();
171: assertTrue(scaledImage.getWidth() == (int) (w / 2.0f));
172: assertTrue(scaledImage.getHeight() == (int) (h / 2.0f));
173: w = scaledImage.getWidth();
174: h = scaledImage.getHeight();
175:
176: // show the result
177: if (SHOW) {
178: Viewer.show(coverage, coverage.getName().toString());
179: Viewer.show(scaled, scaled.getName().toString());
180: } else {
181: // Force computation
182: assertNotNull(coverage.getRenderedImage().getData());
183: assertNotNull(scaled.getRenderedImage().getData());
184: }
185:
186: // use the default processor and then scale again
187: scaled = (GridCoverage2D) Operations.DEFAULT.subsampleAverage(
188: scaled, 0.3333, 0.3333, Interpolation
189: .getInstance(Interpolation.INTERP_NEAREST),
190: BorderExtender
191: .createInstance(BorderExtender.BORDER_COPY));
192: scaledImage = scaled.getRenderedImage();
193: // I had to comment this out since sometimes this evaluation fails
194: // unexpectedly. I think it is a JAI issue because here below I using
195: // the rule they claim to follow.
196: // assertTrue(scaledImage.getWidth() == (int)(w / 3.0f));
197: // assertTrue(scaledImage.getHeight() == (int)(h / 3.0f));
198:
199: if (SHOW) {
200: Viewer.show(scaled, scaled.getName().toString());
201: } else {
202: // Force computation
203: assertNotNull(scaled.getRenderedImage().getData());
204: }
205: }
206: }
|