001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.ext.awt.image.codec.imageio;
020:
021: import java.awt.image.BufferedImage;
022: import java.awt.image.DataBuffer;
023: import java.awt.image.PixelInterleavedSampleModel;
024: import java.awt.image.RenderedImage;
025: import java.awt.image.SampleModel;
026: import java.awt.image.SinglePixelPackedSampleModel;
027: import java.io.IOException;
028: import java.io.OutputStream;
029:
030: import org.apache.batik.ext.awt.image.GraphicsUtil;
031: import org.apache.batik.ext.awt.image.rendered.FormatRed;
032: import org.apache.batik.ext.awt.image.spi.ImageWriter;
033: import org.apache.batik.ext.awt.image.spi.ImageWriterParams;
034: import org.apache.batik.ext.awt.image.spi.ImageWriterRegistry;
035: import org.apache.batik.transcoder.TranscoderException;
036: import org.apache.batik.transcoder.TranscoderOutput;
037: import org.apache.batik.transcoder.TranscodingHints;
038: import org.apache.batik.transcoder.image.TIFFTranscoder;
039:
040: /**
041: * This class is a helper to <tt>TIFFTranscoder</tt> that writes TIFF images
042: * through the Image I/O API.
043: *
044: * @version $Id: TIFFTranscoderImageIOWriteAdapter.java 502538 2007-02-02 08:52:56Z dvholten $
045: */
046: public class TIFFTranscoderImageIOWriteAdapter implements
047: TIFFTranscoder.WriteAdapter {
048:
049: /**
050: * @throws TranscoderException
051: * @see org.apache.batik.transcoder.image.TIFFTranscoder.WriteAdapter#writeImage(TIFFTranscoder, java.awt.image.BufferedImage, org.apache.batik.transcoder.TranscoderOutput)
052: */
053: public void writeImage(TIFFTranscoder transcoder,
054: BufferedImage img, TranscoderOutput output)
055: throws TranscoderException {
056:
057: TranscodingHints hints = transcoder.getTranscodingHints();
058:
059: ImageWriter writer = ImageWriterRegistry.getInstance()
060: .getWriterFor("image/tiff");
061: ImageWriterParams params = new ImageWriterParams();
062:
063: float PixSzMM = transcoder.getUserAgent()
064: .getPixelUnitToMillimeter();
065: int PixSzInch = (int) (25.4 / PixSzMM + 0.5);
066: params.setResolution(PixSzInch);
067:
068: if (hints.containsKey(TIFFTranscoder.KEY_COMPRESSION_METHOD)) {
069: String method = (String) hints
070: .get(TIFFTranscoder.KEY_COMPRESSION_METHOD);
071: //Values set here as defined in TIFFImageWriteParam of JAI Image I/O Tools
072: if ("packbits".equals(method)) {
073: params.setCompressionMethod("PackBits");
074: } else if ("deflate".equals(method)) {
075: params.setCompressionMethod("Deflate");
076: } else if ("lzw".equals(method)) {
077: params.setCompressionMethod("LZW");
078: } else if ("jpeg".equals(method)) {
079: params.setCompressionMethod("JPEG");
080: } else {
081: //nop
082: }
083: }
084:
085: try {
086: OutputStream ostream = output.getOutputStream();
087: int w = img.getWidth();
088: int h = img.getHeight();
089: SinglePixelPackedSampleModel sppsm;
090: sppsm = (SinglePixelPackedSampleModel) img.getSampleModel();
091: int bands = sppsm.getNumBands();
092: int[] off = new int[bands];
093: for (int i = 0; i < bands; i++)
094: off[i] = i;
095: SampleModel sm = new PixelInterleavedSampleModel(
096: DataBuffer.TYPE_BYTE, w, h, bands, w * bands, off);
097:
098: RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(img),
099: sm);
100: writer.writeImage(rimg, ostream, params);
101: ostream.flush();
102: } catch (IOException ex) {
103: throw new TranscoderException(ex);
104: }
105: }
106:
107: }
|