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:
020: package org.apache.batik.transcoder.image;
021:
022: import java.awt.image.BufferedImage;
023: import java.awt.image.SinglePixelPackedSampleModel;
024:
025: import org.apache.batik.bridge.UserAgent;
026: import org.apache.batik.transcoder.TranscoderException;
027: import org.apache.batik.transcoder.TranscoderOutput;
028: import org.apache.batik.transcoder.TranscodingHints;
029: import org.apache.batik.transcoder.keys.StringKey;
030:
031: /**
032: * This class is an <tt>ImageTranscoder</tt> that produces a TIFF image.
033: *
034: * @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
035: * @version $Id: TIFFTranscoder.java 475477 2006-11-15 22:44:28Z cam $
036: */
037: public class TIFFTranscoder extends ImageTranscoder {
038:
039: /**
040: * Constructs a new transcoder that produces tiff images.
041: */
042: public TIFFTranscoder() {
043: hints.put(KEY_FORCE_TRANSPARENT_WHITE, Boolean.FALSE);
044: }
045:
046: /** @return the transcoder's user agent */
047: public UserAgent getUserAgent() {
048: return this .userAgent;
049: }
050:
051: /**
052: * Creates a new ARGB image with the specified dimension.
053: * @param width the image width in pixels
054: * @param height the image height in pixels
055: */
056: public BufferedImage createImage(int width, int height) {
057: return new BufferedImage(width, height,
058: BufferedImage.TYPE_INT_ARGB);
059: }
060:
061: private WriteAdapter getWriteAdapter(String className) {
062: WriteAdapter adapter;
063: try {
064: Class clazz = Class.forName(className);
065: adapter = (WriteAdapter) clazz.newInstance();
066: return adapter;
067: } catch (ClassNotFoundException e) {
068: return null;
069: } catch (InstantiationException e) {
070: return null;
071: } catch (IllegalAccessException e) {
072: return null;
073: }
074: }
075:
076: /**
077: * Writes the specified image to the specified output.
078: * @param img the image to write
079: * @param output the output where to store the image
080: * @throws TranscoderException if an error occured while storing the image
081: */
082: public void writeImage(BufferedImage img, TranscoderOutput output)
083: throws TranscoderException {
084:
085: //
086: // This is a trick so that viewers which do not support the alpha
087: // channel will see a white background (and not a black one).
088: //
089: boolean forceTransparentWhite = false;
090:
091: if (hints
092: .containsKey(PNGTranscoder.KEY_FORCE_TRANSPARENT_WHITE)) {
093: forceTransparentWhite = ((Boolean) hints
094: .get(PNGTranscoder.KEY_FORCE_TRANSPARENT_WHITE))
095: .booleanValue();
096: }
097:
098: if (forceTransparentWhite) {
099: SinglePixelPackedSampleModel sppsm;
100: sppsm = (SinglePixelPackedSampleModel) img.getSampleModel();
101: forceTransparentWhite(img, sppsm);
102: }
103:
104: WriteAdapter adapter = getWriteAdapter("org.apache.batik.ext.awt.image.codec.tiff.TIFFTranscoderInternalCodecWriteAdapter");
105: if (adapter == null) {
106: adapter = getWriteAdapter("org.apache.batik.transcoder.image.TIFFTranscoderImageIOWriteAdapter");
107: }
108: if (adapter == null) {
109: throw new TranscoderException(
110: "Could not write TIFF file because no WriteAdapter is availble");
111: }
112: adapter.writeImage(this , img, output);
113: }
114:
115: // --------------------------------------------------------------------
116: // TIFF specific interfaces
117: // --------------------------------------------------------------------
118:
119: /**
120: * This interface is used by <tt>TIFFTranscoder</tt> to write TIFF images
121: * through different codecs.
122: *
123: * @version $Id: TIFFTranscoder.java 475477 2006-11-15 22:44:28Z cam $
124: */
125: public interface WriteAdapter {
126:
127: /**
128: * Writes the specified image to the specified output.
129: * @param transcoder the calling PNGTranscoder
130: * @param img the image to write
131: * @param output the output where to store the image
132: * @throws TranscoderException if an error occured while storing the image
133: */
134: void writeImage(TIFFTranscoder transcoder, BufferedImage img,
135: TranscoderOutput output) throws TranscoderException;
136:
137: }
138:
139: // --------------------------------------------------------------------
140: // Keys definition
141: // --------------------------------------------------------------------
142:
143: /**
144: * The forceTransparentWhite key.
145: *
146: * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
147: * <TR>
148: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
149: * <TD VALIGN="TOP">KEY_FORCE_TRANSPARENT_WHITE</TD></TR>
150: * <TR>
151: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
152: * <TD VALIGN="TOP">Boolean</TD></TR>
153: * <TR>
154: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
155: * <TD VALIGN="TOP">false</TD></TR>
156: * <TR>
157: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
158: * <TD VALIGN="TOP">No</TD></TR>
159: * <TR>
160: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
161: * <TD VALIGN="TOP">It controls whether the encoder should
162: * force the image's fully transparent pixels to be fully transparent
163: * white instead of fully transparent black. This is usefull when the
164: * encoded TIFF is displayed in a viewer which does not support TIFF
165: * transparency and lets the image display with a white background instead
166: * of a black background. <br />
167: *
168: * However, note that the modified image will display differently
169: * over a white background in a viewer that supports
170: * transparency.</TD></TR>
171: * </TABLE>
172: */
173: public static final TranscodingHints.Key KEY_FORCE_TRANSPARENT_WHITE = ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE;
174:
175: /**
176: * The compression method for the image.
177: * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
178: * <TR>
179: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
180: * <TD VALIGN="TOP">KEY_COMPRESSION_METHOD</TD></TR>
181: * <TR>
182: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
183: * <TD VALIGN="TOP">String ("none", "packbits", "jpeg" etc.)</TD></TR>
184: * <TR>
185: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
186: * <TD VALIGN="TOP">"none" (no compression)</TD></TR>
187: * <TR>
188: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
189: * <TD VALIGN="TOP">Recommended</TD></TR>
190: * <TR>
191: * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
192: * <TD VALIGN="TOP">Specify the compression method used to encode the image.</TD></TR>
193: * </TABLE>
194: */
195: public static final TranscodingHints.Key KEY_COMPRESSION_METHOD = new StringKey();
196:
197: }
|