001: /**
002: *
003: */package org.geotools.coverage.grid.io.imageio;
004:
005: import java.awt.Dimension;
006:
007: import javax.imageio.ImageReadParam;
008: import javax.imageio.ImageReader;
009:
010: /**
011: * This class is the base class for building adapters/extensions to the ImageIO
012: * {@link ImageReadParam} class for controlling the processing of reading a
013: * coverage from an {@link ImageReader}.
014: *
015: * @author Simone Giannecchini
016: * @since 2.3.x
017: *
018: */
019: public abstract class GeoToolsReadParams extends ImageReadParam {
020: /** The {@link ImageReadParam} we are adapting/exteding. */
021: protected ImageReadParam adaptee;
022:
023: /**
024: * An array of preferred tile size range pairs. The default value is
025: * <code>null</code>, which indicates that there are no preferred sizes.
026: * If the value is non-<code>null</code>, it must have an even length of
027: * at least two.
028: *
029: * <p>
030: * Subclasses that do not support reading tiles may ignore this value.
031: *
032: * @see #getPreferredTileSizes
033: */
034: protected Dimension[] preferredTileSizes = null;
035:
036: /**
037: * The width of each tile if tiling has been set, or 0 otherwise.
038: *
039: * <p>
040: * Subclasses that do not support tiling may ignore this value.
041: */
042: protected int tileWidth = -1;
043:
044: /**
045: * The height of each tile if tiling has been set, or 0 otherwise. The
046: * initial value is <code>0</code>.
047: *
048: * <p>
049: * Subclasses that do not support tiling may ignore this value.
050: */
051: protected int tileHeight = -1;
052:
053: /**
054: * A <code>boolean</code> that is <code>true</code> if tiling parameters
055: * have been specified.
056: *
057: * <p>
058: * Subclasses that do not support reading tiles may ignore this value.
059: */
060: protected boolean tilingSet = false;
061:
062: /**
063: * Returns the width of each tile in an raster as it will be reader If
064: * tiling parameters have not been set, an
065: * <code>IllegalStateException</code> is thrown.
066: *
067: * @return the tile width to be used for decoding.
068: *
069: * @exception IllegalStateException
070: * if the tiling parameters have not been set.
071: *
072: * @see #setTiling(int, int, int, int)
073: * @see #getTileHeight()
074: */
075: public int getTileWidth() {
076:
077: if (!tilingSet) {
078: throw new IllegalStateException(
079: "Tiling parameters not set!");
080: }
081: return tileWidth;
082: }
083:
084: /**
085: * Returns the width of each tile in an raster as it will be reader If
086: * tiling parameters have not been set, an
087: * <code>IllegalStateException</code> is thrown.
088: *
089: * @return the tile height to be used for decoding.
090: *
091: * @exception IllegalStateException
092: * if the tiling parameters have not been set.
093: *
094: * @see #setTiling(int, int, int, int)
095: * @see #getTileWidth()
096: */
097: public int getTileHeight() {
098:
099: if (!tilingSet) {
100: throw new IllegalStateException(
101: "Tiling parameters not set!");
102: }
103: return tileHeight;
104: }
105:
106: /**
107: * Specifies that the image should be tiled. The <code>tileWidth</code>
108: * and <code>tileHeight</code> parameters specify the width and height of
109: * the tiles in memory. If the tile width or height is greater than the
110: * width or height of the image, the image is not tiled in that dimension.
111: *
112: *
113: * @param tileWidth
114: * the width of each tile.
115: * @param tileHeight
116: * the height of each tile.
117: *
118: * @exception IllegalArgumentException
119: * if the tile size is not within one of the allowable ranges
120: * returned by <code>getPreferredTileSizes</code>.
121: * @exception IllegalArgumentException
122: * if <code>tileWidth</code> or <code>tileHeight</code>
123: * is less than or equal to 0.
124: *
125: * @see #canWriteTiles
126: * @see #canOffsetTiles
127: * @see #getTileWidth()
128: * @see #getTileHeight()
129: * @see #getTileGridXOffset()
130: * @see #getTileGridYOffset()
131: */
132: public void setTiling(int tileWidth, int tileHeight) {
133:
134: if (tileWidth <= 0 || tileHeight <= 0) {
135: throw new IllegalArgumentException(
136: "tile dimensions are non-positive!");
137: }
138:
139: if (preferredTileSizes != null) {
140: boolean ok = true;
141: final int length = preferredTileSizes.length;
142: for (int i = 0; i < length; i += 2) {
143: Dimension min = preferredTileSizes[i];
144: Dimension max = preferredTileSizes[i + 1];
145: if ((tileWidth < min.width) || (tileWidth > max.width)
146: || (tileHeight < min.height)
147: || (tileHeight > max.height)) {
148: ok = false;
149: break;
150: }
151: }
152: if (!ok) {
153: throw new IllegalArgumentException("Illegal tile size!");
154: }
155: }
156:
157: this .tilingSet = true;
158: this .tileWidth = tileWidth;
159: this .tileHeight = tileHeight;
160: }
161:
162: /**
163: * Default contructor.
164: */
165: public GeoToolsReadParams() {
166:
167: }
168:
169: // Return a deep copy of the array
170: private static Dimension[] clonePreferredTileSizes(Dimension[] sizes) {
171: if (sizes == null) {
172: return null;
173: }
174: final int length = sizes.length;
175: Dimension[] temp = new Dimension[length];
176: for (int i = 0; i < length; i++) {
177: temp[i] = new Dimension(sizes[i]);
178: }
179: return temp;
180: }
181:
182: /**
183: * Returns an array of <code>Dimension</code>s indicating the legal size
184: * ranges for tiles. The returned array is a copy.
185: *
186: * <p>
187: * The information is returned as a set of pairs; the first element of a
188: * pair contains an (inclusive) minimum width and height, and the second
189: * element contains an (inclusive) maximum width and height. Together, each
190: * pair defines a valid range of sizes. To specify a fixed size, use the
191: * same width and height for both elements. To specify an arbitrary range, a
192: * value of <code>null</code> is used in place of an actual array of
193: * <code>Dimension</code>s.
194: *
195: * <p>
196: * If no array is specified on the constructor, but tiling is allowed, then
197: * this method returns <code>null</code>.
198: *
199: * @exception UnsupportedOperationException
200: * if the plug-in does not support tiling.
201: *
202: * @return an array of <code>Dimension</code>s with an even length of at
203: * least two, or <code>null</code>.
204: */
205: public Dimension[] getPreferredTileSizes() {
206: return clonePreferredTileSizes(preferredTileSizes);
207: }
208: }
|