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.InterpolationBilinear;
026: import javax.media.jai.InterpolationNearest;
027:
028: // JUnit dependencies
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: // OpenGIS dependencies
033: import org.opengis.parameter.ParameterValueGroup;
034: import org.geotools.coverage.processing.AbstractProcessor;
035: import org.geotools.coverage.processing.DefaultProcessor;
036:
037: // Geotools dependencies
038: import org.geotools.coverage.processing.Operations;
039: import org.geotools.factory.Hints;
040:
041: /**
042: * Tests the scale operation.
043: *
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/coverage/src/test/java/org/geotools/coverage/grid/ScaleTest.java $
045: * @version $Id: ScaleTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
046: * @author Simone Giannecchini
047: *
048: * @since 2.3
049: */
050: public class ScaleTest 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: private GridCoverage2D originallyIndexedCoverage;
059:
060: private GridCoverage2D indexedCoverage;
061:
062: private GridCoverage2D indexedCoverageWithTransparency;
063:
064: private GridCoverage2D floatCoverage;
065:
066: /**
067: * Creates a test suite for the given name.
068: */
069: public ScaleTest(String name) {
070: super (name);
071: }
072:
073: /**
074: * Returns the test suite.
075: */
076: public static Test suite() {
077: return new TestSuite(ScaleTest.class);
078: }
079:
080: /**
081: * Run the suit from the command line.
082: */
083: public static void main(final String[] args) {
084: SHOW = true;
085: org.geotools.util.logging.Logging.GEOTOOLS
086: .forceMonolineConsoleOutput(AbstractProcessor.OPERATION);
087: junit.textui.TestRunner.run(suite());
088: }
089:
090: /**
091: * Set up common objects used for all tests.
092: */
093: protected void setUp() throws Exception {
094: super .setUp();
095: originallyIndexedCoverage = GridCoverageExamples.getExample(0);
096: indexedCoverage = GridCoverageExamples.getExample(2);
097: indexedCoverageWithTransparency = GridCoverageExamples
098: .getExample(3);
099: floatCoverage = GridCoverageExamples.getExample(4);
100: }
101:
102: /**
103: * Tests the "Scale" operation.
104: *
105: * @throws IOException
106: */
107:
108: public void testScale() throws IOException {
109: ///////////////////////////////////////////////////////////////////////
110: //
111: // Nearest neighbor interpolation and non-geo view
112: //
113: ///////////////////////////////////////////////////////////////////////
114: scale(originallyIndexedCoverage.geophysics(false),
115: new InterpolationNearest(), new RenderingHints(
116: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
117: Boolean.FALSE));
118: scale(indexedCoverage.geophysics(false),
119: new InterpolationNearest(), new RenderingHints(
120: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
121: Boolean.FALSE));
122: scale(indexedCoverageWithTransparency.geophysics(false),
123: new InterpolationNearest(), new RenderingHints(
124: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
125: Boolean.FALSE));
126:
127: ///////////////////////////////////////////////////////////////////////
128: //
129: // Nearest neighbor interpolation and geo view
130: //
131: ///////////////////////////////////////////////////////////////////////
132: scale(originallyIndexedCoverage.geophysics(true),
133: new InterpolationNearest(), new RenderingHints(
134: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
135: Boolean.FALSE));
136: scale(indexedCoverage.geophysics(true),
137: new InterpolationNearest(), new RenderingHints(
138: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
139: Boolean.FALSE));
140: scale(indexedCoverageWithTransparency.geophysics(true),
141: new InterpolationNearest(), new RenderingHints(
142: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
143: Boolean.FALSE));
144:
145: ///////////////////////////////////////////////////////////////////////
146: //
147: // Bilinear interpolation and non-geo view
148: //
149: ///////////////////////////////////////////////////////////////////////
150: scale(originallyIndexedCoverage.geophysics(false),
151: new InterpolationNearest(), new RenderingHints(
152: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
153: Boolean.FALSE));
154: scale(indexedCoverage.geophysics(false),
155: new InterpolationNearest(), new RenderingHints(
156: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
157: Boolean.FALSE));
158: scale(indexedCoverageWithTransparency.geophysics(false),
159: new InterpolationNearest(), new RenderingHints(
160: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
161: Boolean.FALSE));
162:
163: ///////////////////////////////////////////////////////////////////////
164: //
165: // Bilinear interpolation and geo view
166: //
167: ///////////////////////////////////////////////////////////////////////
168: scale(originallyIndexedCoverage.geophysics(true),
169: new InterpolationBilinear(), new RenderingHints(
170: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
171: Boolean.FALSE));
172: scale(indexedCoverage.geophysics(true),
173: new InterpolationBilinear(), new RenderingHints(
174: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
175: Boolean.FALSE));
176: scale(indexedCoverageWithTransparency.geophysics(true),
177: new InterpolationBilinear(), new RenderingHints(
178: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
179: Boolean.FALSE));
180:
181: ///////////////////////////////////////////////////////////////////////
182: //
183: // Bilinear interpolation and non-geo view for a float coverage
184: //
185: ///////////////////////////////////////////////////////////////////////
186: // on this one the subsample average should NOT go back to the
187: // geophysiscs view before being applied
188: scale(floatCoverage.geophysics(false),
189: new InterpolationBilinear(), new RenderingHints(
190: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
191: Boolean.FALSE));
192:
193: ///////////////////////////////////////////////////////////////////////
194: //
195: // Nearest neighbor interpolation and non-geo view for a float coverage
196: //
197: ///////////////////////////////////////////////////////////////////////
198: // on this one the subsample average should NOT go back to the
199: // geophysiscs
200: // view before being applied
201: scale(floatCoverage.geophysics(false),
202: new InterpolationNearest(), new RenderingHints(
203: Hints.REPLACE_NON_GEOPHYSICS_VIEW,
204: Boolean.FALSE));
205: }
206:
207: public void scale(GridCoverage2D coverage, Interpolation interp) {
208: scale(coverage, interp, null);
209: }
210:
211: public void scale(GridCoverage2D coverage, Interpolation interp,
212: RenderingHints hints) {
213: // caching initial properties
214: RenderedImage originalImage = coverage.getRenderedImage();
215: final int w = originalImage.getWidth();
216: final int h = originalImage.getHeight();
217:
218: // creating a default processor
219: final DefaultProcessor processor = new DefaultProcessor(hints);
220: // getting parameters for doing a scale
221: final ParameterValueGroup param = processor.getOperation(
222: "Scale").getParameters();
223: param.parameter("Source").setValue(coverage);
224: param.parameter("xScale").setValue(new Float(0.5));
225: param.parameter("yScale").setValue(new Float(0.5));
226: param.parameter("xTrans").setValue(new Float(0));
227: param.parameter("yTrans").setValue(new Float(0));
228: param.parameter("Interpolation").setValue(interp);
229: param.parameter("BorderExtender").setValue(
230: BorderExtenderCopy
231: .createInstance(BorderExtender.BORDER_COPY));
232: // doing a first scale
233: GridCoverage2D scaled = (GridCoverage2D) processor
234: .doOperation(param);
235: RenderedImage scaledImage = scaled.getRenderedImage();
236: assertTrue(scaledImage.getWidth() == w / 2.0f);
237: assertTrue(scaledImage.getHeight() == h / 2.0f);
238:
239: if (SHOW) {
240: Viewer.show(coverage, coverage.getName().toString());
241: Viewer.show(scaled, scaled.getName().toString());
242: } else {
243: // Force computation
244: assertNotNull(coverage.getRenderedImage().getData());
245: assertNotNull(scaledImage.getData());
246: }
247:
248: // doing another scale using the Default processor
249: scaled = (GridCoverage2D) Operations.DEFAULT.scale(scaled, 3,
250: 3, 0.0, 0.0, interp, BorderExtender
251: .createInstance(BorderExtender.BORDER_COPY));
252: scaledImage = scaled.getRenderedImage();
253: assertTrue(scaledImage.getWidth() == 3 * w / 2.0f);
254: assertTrue(scaledImage.getHeight() == 3 * h / 2.0f);
255:
256: if (SHOW) {
257: Viewer.show(scaled, scaled.getName().toString());
258: } else {
259: // Force computation
260: assertNotNull(scaledImage.getData());
261: }
262: }
263: }
|