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.wcs.responses.coverage;
006:
007: import org.geotools.coverage.grid.GridCoverage2D;
008: import org.geotools.gce.image.WorldImageWriter;
009: import org.geotools.image.ImageWorker;
010: import org.opengis.coverage.grid.Format;
011: import org.opengis.coverage.grid.GridCoverageWriter;
012: import org.opengis.parameter.GeneralParameterValue;
013: import org.vfny.geoserver.ServiceException;
014: import org.vfny.geoserver.global.GeoServer;
015: import org.vfny.geoserver.wcs.responses.CoverageResponseDelegate;
016: import java.awt.image.ComponentColorModel;
017: import java.io.IOException;
018: import java.io.OutputStream;
019: import java.util.Iterator;
020: import javax.imageio.IIOImage;
021: import javax.imageio.ImageIO;
022: import javax.imageio.ImageWriteParam;
023: import javax.imageio.ImageWriter;
024: import javax.imageio.stream.MemoryCacheImageOutputStream;
025: import javax.media.jai.PlanarImage;
026:
027: /**
028: * DOCUMENT ME!
029: *
030: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
031: * modification)
032: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
033: * modification)
034: */
035: public class IMGCoverageResponseDelegate implements
036: CoverageResponseDelegate {
037: /**
038: *
039: * @uml.property name="sourceCoverage"
040: * @uml.associationEnd multiplicity="(0 1)"
041: */
042: private GridCoverage2D sourceCoverage;
043: private String outputFormat;
044:
045: public IMGCoverageResponseDelegate() {
046: }
047:
048: public boolean canProduce(String outputFormat) {
049: if (outputFormat.equalsIgnoreCase("bmp")
050: || outputFormat.equalsIgnoreCase("gif")
051: || outputFormat.equalsIgnoreCase("tiff")
052: || outputFormat.equalsIgnoreCase("png")
053: || outputFormat.equalsIgnoreCase("jpeg")
054: || outputFormat.equalsIgnoreCase("tif")) {
055: return true;
056: }
057:
058: return false;
059: }
060:
061: public void prepare(String outputFormat, GridCoverage2D coverage)
062: throws IOException {
063: this .outputFormat = outputFormat;
064: this .sourceCoverage = coverage;
065: }
066:
067: public String getContentType(GeoServer gs) {
068: return new StringBuffer("image/").append(
069: outputFormat.toLowerCase()).toString();
070: }
071:
072: /**
073: * DOCUMENT ME!
074: *
075: * @return DOCUMENT ME!
076: */
077: public String getContentEncoding() {
078: return null;
079: }
080:
081: /**
082: * DOCUMENT ME!
083: *
084: * @return DOCUMENT ME!
085: */
086: public String getContentDisposition() {
087: return (outputFormat.equalsIgnoreCase("tiff") || outputFormat
088: .equalsIgnoreCase("tif")) ? new StringBuffer(
089: "attachment;filename=").append(
090: this .sourceCoverage.getName()).append(".").append(
091: outputFormat).toString() : null;
092: }
093:
094: public void encode(OutputStream output) throws ServiceException,
095: IOException {
096: if (sourceCoverage == null) {
097: throw new IllegalStateException(new StringBuffer(
098: "It seems prepare() has not been called").append(
099: " or has not succeed").toString());
100: }
101:
102: final GridCoverageWriter writer = new WorldImageWriter(output);
103:
104: // writing parameters for Image
105: final Format writerParams = writer.getFormat();
106: writerParams.getWriteParameters().parameter("Format").setValue(
107: this .outputFormat.toLowerCase());
108:
109: // writing
110: writer.write(sourceCoverage,
111: (GeneralParameterValue[]) writerParams
112: .getWriteParameters().values().toArray(
113: new GeneralParameterValue[1]));
114:
115: // freeing everything
116: writer.dispose();
117: this.sourceCoverage.dispose();
118: this.sourceCoverage = null;
119: }
120: }
|