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 javax.media.jai.iterator.RectIter;
020:
021: /**
022: * A {@link RectIter} with subsampling.
023: *
024: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/SubsampledRectIter.java $
025: * @version $Id: SubsampledRectIter.java 26775 2007-08-30 15:58:26Z desruisseaux $
026: * @author Martin Desruisseaux
027: */
028: final class SubsampledRectIter implements RectIter {
029: /**
030: * The wrapped iterator.
031: */
032: private final RectIter iterator;
033:
034: /**
035: * Index of current band in the {@link #sourceBands} array.
036: */
037: private int bandIndex;
038:
039: /**
040: * The source bands.
041: */
042: private final int[] sourceBands;
043:
044: /**
045: * The subsampling parameters.
046: */
047: private final int dx, dy;
048:
049: /**
050: * Wraps the specified iterator.
051: */
052: public SubsampledRectIter(final RectIter iterator,
053: final int sourceXSubsampling, final int sourceYSubsampling,
054: final int[] sourceBands) {
055: this .iterator = iterator;
056: dx = sourceXSubsampling - 1;
057: dy = sourceYSubsampling - 1;
058: this .sourceBands = sourceBands;
059: }
060:
061: /**
062: * Sets the iterator to the first line of its bounding rectangle.
063: */
064: public void startLines() {
065: iterator.startLines();
066: }
067:
068: /**
069: * Sets the iterator to the next line of the image.
070: */
071: public void nextLine() {
072: nextLineDone();
073: }
074:
075: /**
076: * Sets the iterator to the next line in the image.
077: */
078: public boolean nextLineDone() {
079: if (iterator.nextLineDone()) {
080: return true;
081: }
082: iterator.jumpLines(dy);
083: return false;
084: }
085:
086: /**
087: * Jumps downward num lines from the current position.
088: */
089: public void jumpLines(final int num) {
090: iterator.jumpLines(num * (dy + 1));
091: }
092:
093: /**
094: * Returns true if the bottom row of the bounding rectangle has been passed.
095: */
096: public boolean finishedLines() {
097: return iterator.finishedLines();
098: }
099:
100: /**
101: * Sets the iterator to the leftmost pixel of its bounding rectangle.
102: */
103: public void startPixels() {
104: iterator.startPixels();
105: }
106:
107: /**
108: * Sets the iterator to the next pixel in image.
109: */
110: public void nextPixel() {
111: nextPixelDone();
112: }
113:
114: /**
115: * Sets the iterator to the next pixel in the image.
116: */
117: public boolean nextPixelDone() {
118: if (iterator.nextPixelDone()) {
119: return true;
120: }
121: iterator.jumpPixels(dx);
122: return false;
123: }
124:
125: /**
126: * Jumps rightward num pixels from the current position.
127: */
128: public void jumpPixels(final int num) {
129: iterator.jumpPixels(num * (dx + 1));
130: }
131:
132: /**
133: * Returns true if the right edge of the bounding rectangle has been passed.
134: */
135: public boolean finishedPixels() {
136: return iterator.finishedPixels();
137: }
138:
139: /**
140: * Sets the iterator to the first band of the image.
141: */
142: public void startBands() {
143: bandIndex = 0;
144: iterator.startBands();
145: for (int skip = sourceBands[0]; --skip >= 0;) {
146: iterator.nextBand();
147: }
148: }
149:
150: /**
151: * Sets the iterator to the next band in the image.
152: */
153: public void nextBand() {
154: nextBandDone();
155: }
156:
157: /**
158: * Sets the iterator to the next band in the image, and returns
159: * true if the max band has been exceeded.
160: */
161: public boolean nextBandDone() {
162: int skip = sourceBands[bandIndex];
163: if (++bandIndex >= sourceBands.length) {
164: return true;
165: }
166: skip = sourceBands[bandIndex] - skip;
167: if (skip < 0) {
168: iterator.startBands();
169: skip = sourceBands[bandIndex];
170: }
171: while (--skip >= 0) {
172: if (iterator.nextBandDone()) {
173: return true;
174: }
175: }
176: return false;
177: }
178:
179: /**
180: * Returns true if the max band in the image has been exceeded.
181: */
182: public boolean finishedBands() {
183: return iterator.finishedBands();
184: }
185:
186: public int getSample() {
187: return iterator.getSample();
188: }
189:
190: public int getSample(int b) {
191: return iterator.getSample(sourceBands[b]);
192: }
193:
194: public float getSampleFloat() {
195: return iterator.getSampleFloat();
196: }
197:
198: public float getSampleFloat(int b) {
199: return iterator.getSampleFloat(sourceBands[b]);
200: }
201:
202: public double getSampleDouble() {
203: return iterator.getSampleDouble();
204: }
205:
206: public double getSampleDouble(int b) {
207: return iterator.getSampleDouble(sourceBands[b]);
208: }
209:
210: public int[] getPixel(int[] a) {
211: final int length = sourceBands.length;
212: if (a == null) {
213: a = new int[length];
214: }
215: for (int i = 0; i < length; i++) {
216: a[i] = getSample(i);
217: }
218: return a;
219: }
220:
221: public float[] getPixel(float[] a) {
222: final int length = sourceBands.length;
223: if (a == null) {
224: a = new float[length];
225: }
226: for (int i = 0; i < length; i++) {
227: a[i] = getSampleFloat(i);
228: }
229: return a;
230: }
231:
232: public double[] getPixel(double[] a) {
233: final int length = sourceBands.length;
234: if (a == null) {
235: a = new double[length];
236: }
237: for (int i = 0; i < length; i++) {
238: a[i] = getSampleDouble(i);
239: }
240: return a;
241: }
242: }
|