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.text;
018:
019: import java.io.BufferedWriter;
020: import java.io.IOException;
021: import java.text.FieldPosition;
022: import java.text.NumberFormat;
023: import java.util.Locale;
024: import javax.imageio.IIOImage;
025: import javax.imageio.ImageWriter;
026: import javax.imageio.ImageWriteParam;
027: import javax.imageio.spi.ImageWriterSpi;
028: import javax.imageio.metadata.IIOMetadata;
029: import javax.media.jai.iterator.RectIter;
030:
031: import org.geotools.image.ImageDimension;
032: import org.geotools.resources.Utilities;
033: import org.geotools.resources.i18n.Descriptions;
034: import org.geotools.resources.i18n.DescriptionKeys;
035:
036: /**
037: * An image encoder for matrix of floating-point numbers.
038: *
039: * @since 2.4
040: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/text/TextMatrixImageWriter.java $
041: * @version $Id: TextMatrixImageWriter.java 27629 2007-10-26 09:59:20Z desruisseaux $
042: * @author Martin Desruisseaux
043: */
044: public class TextMatrixImageWriter extends TextImageWriter {
045: /**
046: * Amount of spaces to put between columns.
047: */
048: private static final int SEPARATOR_WIDTH = 1;
049:
050: /**
051: * Constructs a new image writer.
052: *
053: * @param provider the provider that is invoking this constructor, or {@code null} if none.
054: */
055: protected TextMatrixImageWriter(final ImageWriterSpi provider) {
056: super (provider);
057: }
058:
059: /**
060: * Appends a complete image stream containing a single image.
061: *
062: * @param streamMetadata The stream metadata (ignored in default implementation).
063: * @param image The image or raster to be written.
064: * @param parameters The write parameters, or null if the whole image will be written.
065: */
066: public void write(final IIOMetadata streamMetadata,
067: final IIOImage image, final ImageWriteParam parameters)
068: throws IOException {
069: processImageStarted();
070: final BufferedWriter out = getWriter(parameters);
071: final String lineSeparator = getLineSeparator(parameters);
072: final NumberFormat format = createNumberFormat(image,
073: parameters);
074: final FieldPosition pos = getExpectedFractionPosition(format);
075: final int fractionWidth = pos.getEndIndex()
076: - pos.getBeginIndex();
077: final int width = pos.getEndIndex() + SEPARATOR_WIDTH;
078: final StringBuffer buffer = new StringBuffer(width);
079: final RectIter iterator = createRectIter(image, parameters);
080: final ImageDimension size = computeSize(image, parameters);
081: final float progressScale = 100f / size.getNumSampleValues();
082: int numSampleValues = 0, nextProgress = 0;
083: if (!iterator.finishedBands())
084: do {
085: if (!iterator.finishedLines())
086: do {
087: if (numSampleValues >= nextProgress) {
088: // Informs about progress only every 32 lines.
089: processImageProgress(progressScale
090: * numSampleValues);
091: nextProgress = numSampleValues + 2000; // Reports after every 2000 numbers.
092: }
093: if (!iterator.finishedPixels())
094: do {
095: buffer.setLength(0);
096: String n = format.format(
097: iterator.getSampleDouble(),
098: buffer, pos).toString();
099: final int fractionOffset = Math
100: .max(
101: 0,
102: fractionWidth
103: - (pos
104: .getEndIndex() - pos
105: .getBeginIndex()));
106: out.write(Utilities.spaces(width
107: - n.length() - fractionOffset));
108: out.write(n);
109: out.write(Utilities
110: .spaces(fractionOffset));
111: } while (!iterator.nextPixelDone());
112: out.write(lineSeparator);
113: numSampleValues += size.width;
114: iterator.startPixels();
115: } while (!iterator.nextLineDone());
116: out.write(lineSeparator); // Separate bands by a blank line.
117: iterator.startLines();
118: } while (!iterator.nextBandDone());
119: out.flush();
120: processImageComplete();
121: }
122:
123: /**
124: * Service provider interface (SPI) for {@link TextMatrixImageWriter}s.
125: *
126: * @since 2.4
127: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/coverageio/src/main/java/org/geotools/image/io/text/TextMatrixImageWriter.java $
128: * @version $Id: TextMatrixImageWriter.java 27629 2007-10-26 09:59:20Z desruisseaux $
129: * @author Martin Desruisseaux
130: */
131: public static class Spi extends TextImageWriter.Spi {
132: /**
133: * The format names for the default {@link TextMatrixImageWriter} configuration.
134: */
135: private static final String[] NAMES = { "matrix" };
136:
137: /**
138: * The mime types for the default {@link TextMatrixImageWriter} configuration.
139: */
140: private static final String[] MIME_TYPES = { "text/x-matrix" };
141:
142: /**
143: * Constructs a default {@code TextMatrixImageWriter.Spi}. This constructor
144: * provides the following defaults in addition to the defaults defined in the
145: * {@linkplain TextImageWriter.Spi#Spi super-class constructor}:
146: *
147: * <ul>
148: * <li>{@link #names} = {@code "matrix"}</li>
149: * <li>{@link #MIMETypes} = {@code "text/x-matrix"}</li>
150: * <li>{@link #pluginClassName} = {@code "org.geotools.image.io.text.TextMatrixImageWriter"}</li>
151: * <li>{@link #vendorName} = {@code "Geotools"}</li>
152: * </ul>
153: *
154: * For efficienty reasons, the above fields are initialized to shared arrays. Subclasses
155: * can assign new arrays, but should not modify the default array content.
156: */
157: public Spi() {
158: names = NAMES;
159: MIMETypes = MIME_TYPES;
160: pluginClassName = "org.geotools.image.io.text.TextMatrixImageWriter";
161: vendorName = "Geotools";
162: version = "2.4";
163: }
164:
165: /**
166: * Returns a brief, human-readable description of this service provider
167: * and its associated implementation. The resulting string should be
168: * localized for the supplied locale, if possible.
169: *
170: * @param locale A Locale for which the return value should be localized.
171: * @return A String containing a description of this service provider.
172: */
173: public String getDescription(final Locale locale) {
174: return Descriptions.getResources(locale).getString(
175: DescriptionKeys.CODEC_MATRIX);
176: }
177:
178: /**
179: * Returns an instance of the {@code ImageWriter} implementation associated
180: * with this service provider.
181: *
182: * @param extension An optional extension object, which may be null.
183: * @return An image writer instance.
184: * @throws IOException if the attempt to instantiate the writer fails.
185: */
186: public ImageWriter createWriterInstance(final Object extension)
187: throws IOException {
188: return new TextMatrixImageWriter(this);
189: }
190: }
191: }
|