001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wms.responses.map.tiff;
006:
007: import java.awt.image.RenderedImage;
008: import java.io.IOException;
009: import java.io.OutputStream;
010: import java.util.logging.Level;
011: import java.util.logging.Logger;
012:
013: import javax.imageio.ImageIO;
014: import javax.imageio.ImageWriter;
015: import javax.imageio.spi.ImageWriterSpi;
016: import javax.imageio.stream.ImageOutputStream;
017:
018: import org.vfny.geoserver.global.WMS;
019: import org.vfny.geoserver.wms.GetMapProducer;
020: import org.vfny.geoserver.wms.WmsException;
021: import org.vfny.geoserver.wms.responses.DefaultRasterMapProducer;
022:
023: import com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriterSpi;
024:
025: /**
026: * Map producer for producing Tiff images out of a map.
027: *
028: * @author Simone Giannecchini
029: * @since 1.4.x
030: *
031: */
032: public final class TiffMapProducer extends DefaultRasterMapProducer {
033: /** A logger for this class. */
034: private static final Logger LOGGER = org.geotools.util.logging.Logging
035: .getLogger("org.vfny.geoserver.responses.wms.map");
036:
037: private final static ImageWriterSpi writerSPI = new TIFFImageWriterSpi();
038:
039: /**
040: * Creates a {@link GetMapProducer} to encode the {@link RenderedImage}
041: * generated in <code>outputFormat</code> format.
042: *
043: * @param outputFormat
044: * the output format.
045: */
046: public TiffMapProducer(String outputFormat, String mime, WMS wms) {
047: super (outputFormat, mime, wms);
048: }
049:
050: /**
051: * Transforms the rendered image into the appropriate format, streaming to
052: * the output stream.
053: *
054: * @param format
055: * The name of the format
056: * @param image
057: * The image to be formatted.
058: * @param outStream
059: * The stream to write to.
060: *
061: * @throws WmsException
062: * not really.
063: * @throws IOException
064: * if the image writing fails.
065: */
066: public void formatImageOutputStream(RenderedImage image,
067: OutputStream outStream) throws WmsException, IOException {
068: // getting a writer
069: if (LOGGER.isLoggable(Level.FINE)) {
070: LOGGER.fine("Getting a writer for tiff");
071: }
072:
073: // get a writer
074: final ImageWriter writer = writerSPI.createWriterInstance();
075:
076: // getting a stream caching in memory
077: final ImageOutputStream ioutstream = ImageIO
078: .createImageOutputStream(outStream);
079:
080: // tiff
081: if (LOGGER.isLoggable(Level.FINE)) {
082: LOGGER.fine("Writing tiff image ...");
083: }
084:
085: // do we want it to be 8 bits?
086: if (this .format.equalsIgnoreCase("image/tiff8")
087: || (this .mapContext.getPaletteInverter() != null)) {
088: image = forceIndexed8Bitmask(image);
089: }
090:
091: // write it out
092: writer.setOutput(ioutstream);
093: writer.write(image);
094: ioutstream.close();
095: writer.dispose();
096:
097: if (LOGGER.isLoggable(Level.FINE)) {
098: LOGGER.fine("Writing tiff image done!");
099: }
100: }
101:
102: public String getContentDisposition() {
103: // can be null
104: return null;
105: }
106: }
|