001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2007, Geotools Project Managment Committee (PMC)
005: * (C) 2007, Geomatys
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.image.io;
018:
019: import java.awt.Insets;
020: import java.awt.Rectangle;
021: import java.io.IOException;
022: import javax.imageio.IIOImage;
023: import java.awt.image.BufferedImage;
024: import java.awt.image.WritableRaster;
025: import javax.imageio.ImageWriteParam;
026: import javax.media.jai.iterator.RectIter;
027:
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031:
032: /**
033: * Tests {@link GeographicImageWriter}, especially the {@link RectIter} creation.
034: *
035: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/test/java/org/geotools/image/io/GeographicImageWriterTest.java $
036: * @version $Id: GeographicImageWriterTest.java 27848 2007-11-12 13:10:32Z desruisseaux $
037: * @author Martin Desruisseaux
038: */
039: public class GeographicImageWriterTest extends TestCase {
040: /**
041: * Run the suite from the command line.
042: */
043: public static void main(final String[] args) {
044: org.geotools.util.logging.Logging.GEOTOOLS
045: .forceMonolineConsoleOutput();
046: junit.textui.TestRunner.run(suite());
047: }
048:
049: /**
050: * Returns the test suite.
051: */
052: public static Test suite() {
053: return new TestSuite(GeographicImageWriterTest.class);
054: }
055:
056: /**
057: * Constructs a test case with the given name.
058: */
059: public GeographicImageWriterTest(final String name) {
060: super (name);
061: }
062:
063: /**
064: * Tests the {@link RectIter}.
065: */
066: public void testRectIter() throws IOException {
067: final Insets margin = new Insets(5, 2, 2, 1); // top, left, bottom, right
068: runRectIter(10, 20, margin);
069: runRectIter(11, 21, margin);
070: runRectIter(10, 21, margin);
071: runRectIter(11, 20, margin);
072: }
073:
074: /**
075: * Creates a dummy image and tests immediately the iteration.
076: */
077: private static IIOImage runRectIter(final int width,
078: final int height, final Insets m) {
079: final BufferedImage image = new BufferedImage(width, height,
080: BufferedImage.TYPE_USHORT_GRAY);
081: final WritableRaster raster = image.getRaster();
082: int value = 0;
083: for (int y = image.getHeight(); --y >= 0;) {
084: for (int x = image.getWidth(); --x >= 0;) {
085: raster.setSample(x, y, 0, value++);
086: }
087: }
088: final int count = value;
089: final IIOImage iiom = new IIOImage(image, null, null);
090: assertEquals(count, GeographicImageWriter.computeSize(iiom,
091: null).getNumSampleValues());
092: /*
093: * Using JAI iterator on the whole image.
094: */
095: RectIter iterator = GeographicImageWriter.createRectIter(iiom,
096: null);
097: assertFalse(iterator instanceof SubsampledRectIter);
098: checkSampleSuite(iterator, --value, -1, 0);
099: /*
100: * Using JAI iterator with subregion and subsampling.
101: */
102: final ImageWriteParam param = new ImageWriteParam(null);
103: final Rectangle bounds = new Rectangle(m.left, m.top, width
104: - (m.left + m.right), height - (m.top + m.bottom));
105: param.setSourceRegion(bounds);
106: value -= (m.top * width + m.left);
107: for (int sourceYSubsampling = 1; sourceYSubsampling <= 6; sourceYSubsampling++) {
108: for (int sourceXSubsampling = 1; sourceXSubsampling <= 6; sourceXSubsampling++) {
109: param.setSourceSubsampling(sourceXSubsampling,
110: sourceYSubsampling, 0, 0);
111: iterator = GeographicImageWriter.createRectIter(iiom,
112: param);
113:
114: // Number of points on which to iterate in a row.
115: int n = (bounds.width - 1) / sourceXSubsampling + 1;
116:
117: // The range on which the iteration will take place.
118: n *= sourceXSubsampling;
119:
120: // Spaces skipped on the left and right sides.
121: n = width - n;
122:
123: // Adds the range occuped by the skipped lines.
124: n += width * (sourceYSubsampling - 1);
125: checkSampleSuite(iterator, value, -sourceXSubsampling,
126: -n);
127: }
128: }
129: return iiom;
130: }
131:
132: /**
133: * Tests the values returned by the iterator.
134: */
135: private static void checkSampleSuite(final RectIter iterator,
136: int value, final int dx, final int dy) {
137: if (!iterator.finishedBands())
138: do {
139: if (!iterator.finishedLines())
140: do {
141: if (!iterator.finishedPixels())
142: do {
143: assertEquals(value, iterator
144: .getSample());
145: value += dx;
146: } while (!iterator.nextPixelDone());
147: iterator.startPixels();
148: value += dy;
149: } while (!iterator.nextLineDone());
150: iterator.startLines();
151: } while (!iterator.nextBandDone());
152: }
153: }
|