001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id$ */
019:
020: package org.apache.xmlgraphics.image.writer.internal;
021:
022: import java.awt.image.RenderedImage;
023: import java.io.IOException;
024: import java.io.OutputStream;
025:
026: import org.apache.xmlgraphics.image.codec.tiff.TIFFEncodeParam;
027: import org.apache.xmlgraphics.image.codec.tiff.TIFFField;
028: import org.apache.xmlgraphics.image.codec.tiff.TIFFImageDecoder;
029: import org.apache.xmlgraphics.image.codec.tiff.TIFFImageEncoder;
030: import org.apache.xmlgraphics.image.writer.AbstractImageWriter;
031: import org.apache.xmlgraphics.image.writer.ImageWriter;
032: import org.apache.xmlgraphics.image.writer.ImageWriterParams;
033: import org.apache.xmlgraphics.image.writer.MultiImageWriter;
034:
035: /**
036: * ImageWriter implementation that uses the internal TIFF codec to
037: * write TIFF files.
038: *
039: * @version $Id$
040: */
041: public class TIFFImageWriter extends AbstractImageWriter {
042:
043: /**
044: * @see ImageWriter#writeImage(java.awt.image.RenderedImage, java.io.OutputStream)
045: */
046: public void writeImage(RenderedImage image, OutputStream out)
047: throws IOException {
048: writeImage(image, out, null);
049: }
050:
051: /**
052: * @see ImageWriter#writeImage(java.awt.image.RenderedImage, java.io.OutputStream, ImageWriterParams)
053: */
054: public void writeImage(RenderedImage image, OutputStream out,
055: ImageWriterParams params) throws IOException {
056: TIFFEncodeParam encodeParams = createTIFFEncodeParams(params);
057: TIFFImageEncoder encoder = new TIFFImageEncoder(out,
058: encodeParams);
059: encoder.encode(image);
060: }
061:
062: private TIFFEncodeParam createTIFFEncodeParams(
063: ImageWriterParams params) {
064: TIFFEncodeParam encodeParams = new TIFFEncodeParam();
065: if (params.getCompressionMethod() == null) {
066: //PackBits as default
067: encodeParams
068: .setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);
069: } else if ("PackBits".equalsIgnoreCase(params
070: .getCompressionMethod())) {
071: encodeParams
072: .setCompression(TIFFEncodeParam.COMPRESSION_PACKBITS);
073: } else if ("NONE".equalsIgnoreCase(params
074: .getCompressionMethod())) {
075: encodeParams
076: .setCompression(TIFFEncodeParam.COMPRESSION_NONE);
077: } else if ("JPEG".equalsIgnoreCase(params
078: .getCompressionMethod())) {
079: encodeParams
080: .setCompression(TIFFEncodeParam.COMPRESSION_JPEG_TTN2);
081: } else if ("Deflate".equalsIgnoreCase(params
082: .getCompressionMethod())) {
083: encodeParams
084: .setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
085: } else {
086: throw new UnsupportedOperationException(
087: "Compression method not supported: "
088: + params.getCompressionMethod());
089: }
090:
091: if (params.getResolution() != null) {
092: // Set target resolution
093: float pixSzMM = 25.4f / params.getResolution().floatValue();
094: // num Pixs in 100 Meters
095: int numPix = (int) (((1000 * 100) / pixSzMM) + 0.5);
096: int denom = 100 * 100; // Centimeters per 100 Meters;
097: long[] rational = { numPix, denom };
098: TIFFField[] fields = {
099: new TIFFField(
100: TIFFImageDecoder.TIFF_RESOLUTION_UNIT,
101: TIFFField.TIFF_SHORT, 1,
102: new char[] { (char) 3 }),
103: new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION,
104: TIFFField.TIFF_RATIONAL, 1,
105: new long[][] { rational }),
106: new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION,
107: TIFFField.TIFF_RATIONAL, 1,
108: new long[][] { rational }) };
109: encodeParams.setExtraFields(fields);
110: }
111: return encodeParams;
112: }
113:
114: /**
115: * @see ImageWriter#getMIMEType()
116: */
117: public String getMIMEType() {
118: return "image/tiff";
119: }
120:
121: /**
122: * @see org.apache.xmlgraphics.image.writer.ImageWriter#createMultiImageWriter(
123: * java.io.OutputStream)
124: */
125: public MultiImageWriter createMultiImageWriter(OutputStream out)
126: throws IOException {
127: return new TIFFMultiImageWriter(out);
128: }
129:
130: /** @see org.apache.xmlgraphics.image.writer.ImageWriter#supportsMultiImageWriter() */
131: public boolean supportsMultiImageWriter() {
132: return true;
133: }
134:
135: private class TIFFMultiImageWriter implements MultiImageWriter {
136:
137: private OutputStream out;
138: private TIFFEncodeParam encodeParams;
139: private TIFFImageEncoder encoder;
140: private Object context;
141:
142: public TIFFMultiImageWriter(OutputStream out)
143: throws IOException {
144: this .out = out;
145: }
146:
147: public void writeImage(RenderedImage image,
148: ImageWriterParams params) throws IOException {
149: if (encoder == null) {
150: encodeParams = createTIFFEncodeParams(params);
151: encoder = new TIFFImageEncoder(out, encodeParams);
152: }
153: context = encoder.encodeMultiple(context, image);
154: }
155:
156: public void close() throws IOException {
157: if (encoder != null) {
158: encoder.finishMultiple(context);
159: }
160: encoder = null;
161: encodeParams = null;
162: out.flush();
163: }
164:
165: }
166:
167: }
|