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.util.Locale;
020: import javax.imageio.ImageReader;
021: import javax.imageio.ImageReadParam;
022:
023: import org.geotools.resources.Utilities;
024: import org.geotools.resources.i18n.Errors;
025: import org.geotools.resources.i18n.ErrorKeys;
026: import org.geotools.resources.IndexedResourceBundle;
027:
028: /**
029: * Default parameters for {@link GeographicImageReader}.
030: *
031: * @since 2.4
032: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/GeographicImageReadParam.java $
033: * @version $Id: GeographicImageReadParam.java 26708 2007-08-27 19:42:59Z desruisseaux $
034: * @author Martin Desruisseaux
035: */
036: public class GeographicImageReadParam extends ImageReadParam {
037: /**
038: * The name of the default color palette to apply when none was explicitly specified.
039: *
040: * @see #getPaletteName
041: * @see #setPaletteName
042: */
043: public static final String DEFAULT_PALETTE_NAME = "rainbow";
044:
045: /**
046: * The name of the color palette.
047: */
048: private String palette;
049:
050: /**
051: * The band to display.
052: */
053: private int visibleBand;
054:
055: /**
056: * The locale for formatting error messages. Will be inferred from
057: * the {@linkplain ImageReader image reader} at construction time.
058: */
059: private final Locale locale;
060:
061: /**
062: * Creates a new, initially empty, set of parameters.
063: *
064: * @param reader The reader for which this parameter block is created
065: */
066: public GeographicImageReadParam(final ImageReader reader) {
067: locale = (reader != null) ? reader.getLocale() : null;
068: }
069:
070: /**
071: * Returns the resources for formatting error messages.
072: */
073: private IndexedResourceBundle getErrorResources() {
074: return Errors.getResources(locale);
075: }
076:
077: /**
078: * Ensures that the specified band number is valid.
079: */
080: private void ensureValidBand(final int band)
081: throws IllegalArgumentException {
082: if (band < 0) {
083: throw new IllegalArgumentException(getErrorResources()
084: .getString(ErrorKeys.BAD_BAND_NUMBER_$1,
085: new Integer(band)));
086: }
087: }
088:
089: /**
090: * Returns the band to display in the target image. In theory, images backed by
091: * {@linkplain java.awt.image.IndexColorModel index color model} should have only
092: * ony band. But sometime we want to load additional bands as numerical data, in
093: * order to perform computations. In such case, we need to specify which band in
094: * the destination image will be used as an index for displaying the colors. The
095: * default value is 0.
096: */
097: public int getVisibleBand() {
098: return visibleBand;
099: }
100:
101: /**
102: * Sets the band to make visible in the destination image.
103: *
104: * @param visibleBand The band to make visible.
105: * @throws IllegalArgumentException if the specified band index is invalid.
106: */
107: public void setVisibleBand(final int visibleBand)
108: throws IllegalArgumentException {
109: ensureValidBand(visibleBand);
110: this .visibleBand = visibleBand;
111: }
112:
113: /**
114: * Returns a name of the color palette, or a {@linkplain #DEFAULT_PALETTE_NAME default name}
115: * if none were explicitly specified.
116: */
117: final String getNonNullPaletteName() {
118: final String palette = getPaletteName();
119: return (palette != null) ? palette : DEFAULT_PALETTE_NAME;
120: }
121:
122: /**
123: * Returns the name of the color palette to apply when creating an
124: * {@linkplain java.awt.image.IndexColorModel index color model}.
125: * This is the name specified by the last call to {@link #setPaletteName}.
126: */
127: public String getPaletteName() {
128: return palette;
129: }
130:
131: /**
132: * Sets the color palette as one of the {@linkplain PaletteFactory#getAvailableNames available
133: * names} provided by the {@linkplain PaletteFactory#getDefault default palette factory}. This
134: * name will be given by the {@link GeographicImageReader} default implementation to the
135: * {@linkplain PaletteFactory#getDefault default palette factory} for creating a
136: * {@linkplain javax.imageio.ImageTypeSpecifier image type specifier}.
137: *
138: * @see PaletteFactory#getAvailableNames
139: */
140: public void setPaletteName(final String palette) {
141: this .palette = palette;
142: }
143:
144: /**
145: * Returns a string representation of this block of parameters.
146: */
147: //@Override
148: public String toString() {
149: final StringBuffer buffer = new StringBuffer(Utilities
150: .getShortClassName(this ));
151: buffer.append('[');
152: if (sourceRegion != null) {
153: buffer.append("sourceRegion=(").append(sourceRegion.x)
154: .append(':').append(
155: sourceRegion.x + sourceRegion.width)
156: .append(',').append(sourceRegion.y).append(':')
157: .append(sourceRegion.y + sourceRegion.height)
158: .append("), ");
159: }
160: if (sourceXSubsampling != 1 || sourceYSubsampling != 1) {
161: buffer.append("subsampling=(").append(sourceXSubsampling)
162: .append(',').append(sourceYSubsampling).append(
163: "), ");
164: }
165: if (sourceBands != null) {
166: buffer.append("sourceBands={");
167: for (int i = 0; i < sourceBands.length; i++) {
168: if (i != 0) {
169: buffer.append(',');
170: }
171: buffer.append(sourceBands[i]);
172: }
173: buffer.append("}, ");
174: }
175: buffer.append("palette=\"").append(palette).append("\", ")
176: .append(']');
177: return buffer.toString();
178: }
179: }
|