KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > transcoder > image > TIFFTranscoder


1 /*
2
3    Copyright 1999-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17 */

18
19 package org.apache.batik.transcoder.image;
20
21 import java.awt.image.BufferedImage JavaDoc;
22 import java.awt.image.DataBuffer JavaDoc;
23 import java.awt.image.DataBufferInt JavaDoc;
24 import java.awt.image.PixelInterleavedSampleModel JavaDoc;
25 import java.awt.image.RenderedImage JavaDoc;
26 import java.awt.image.SampleModel JavaDoc;
27 import java.awt.image.SinglePixelPackedSampleModel JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.OutputStream JavaDoc;
30
31 import org.apache.batik.ext.awt.image.GraphicsUtil;
32 import org.apache.batik.ext.awt.image.codec.tiff.TIFFEncodeParam;
33 import org.apache.batik.ext.awt.image.codec.tiff.TIFFField;
34 import org.apache.batik.ext.awt.image.codec.tiff.TIFFImageDecoder;
35 import org.apache.batik.ext.awt.image.codec.tiff.TIFFImageEncoder;
36 import org.apache.batik.ext.awt.image.rendered.FormatRed;
37 import org.apache.batik.transcoder.TranscoderException;
38 import org.apache.batik.transcoder.TranscoderOutput;
39 import org.apache.batik.transcoder.TranscodingHints;
40 import org.apache.batik.transcoder.image.resources.Messages;
41
42
43 /**
44  * This class is an <tt>ImageTranscoder</tt> that produces a TIFF image.
45  *
46  * @author <a HREF="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
47  * @version $Id: TIFFTranscoder.java,v 1.10 2005/03/27 08:58:36 cam Exp $
48  */

49 public class TIFFTranscoder extends ImageTranscoder {
50
51     /**
52      * Constructs a new transcoder that produces tiff images.
53      */

54     public TIFFTranscoder() {
55         hints.put(KEY_FORCE_TRANSPARENT_WHITE, Boolean.FALSE);
56     }
57
58     /**
59      * Creates a new ARGB image with the specified dimension.
60      * @param width the image width in pixels
61      * @param height the image height in pixels
62      */

63     public BufferedImage JavaDoc createImage(int width, int height) {
64         return new BufferedImage JavaDoc(width, height, BufferedImage.TYPE_INT_ARGB);
65     }
66
67     /**
68      * Writes the specified image to the specified output.
69      * @param img the image to write
70      * @param output the output where to store the image
71      * @throws TranscoderException if an error occured while storing the image
72      */

73     public void writeImage(BufferedImage JavaDoc img, TranscoderOutput output)
74             throws TranscoderException {
75
76         OutputStream JavaDoc ostream = output.getOutputStream();
77         if (ostream == null) {
78             throw new TranscoderException(
79                 Messages.formatMessage("tiff.badoutput", null));
80         }
81
82         TIFFEncodeParam params = new TIFFEncodeParam();
83
84         float PixSzMM = userAgent.getPixelUnitToMillimeter();
85         // num Pixs in 100 Meters
86
int numPix = (int)(((1000*100)/PixSzMM)+0.5);
87         int denom = 100*100; // Centimeters per 100 Meters;
88
long [] rational = {numPix, denom};
89         TIFFField [] fields = {
90             new TIFFField(TIFFImageDecoder.TIFF_RESOLUTION_UNIT,
91                           TIFFField.TIFF_SHORT, 1,
92                           new char [] { (char)3 }),
93             new TIFFField(TIFFImageDecoder.TIFF_X_RESOLUTION,
94                           TIFFField.TIFF_RATIONAL, 1,
95                           new long [][] { rational }),
96             new TIFFField(TIFFImageDecoder.TIFF_Y_RESOLUTION,
97                           TIFFField.TIFF_RATIONAL, 1,
98                           new long [][] { rational })
99                 };
100
101         params.setExtraFields(fields);
102
103         //
104
// This is a trick so that viewers which do not support the alpha
105
// channel will see a white background (and not a black one).
106
//
107
boolean forceTransparentWhite = false;
108
109         if (hints.containsKey(KEY_FORCE_TRANSPARENT_WHITE)) {
110             forceTransparentWhite =
111                 ((Boolean JavaDoc)hints.get
112                  (KEY_FORCE_TRANSPARENT_WHITE)).booleanValue();
113         }
114
115         int w = img.getWidth();
116         int h = img.getHeight();
117         SinglePixelPackedSampleModel JavaDoc sppsm;
118         sppsm = (SinglePixelPackedSampleModel JavaDoc)img.getSampleModel();
119
120         if (forceTransparentWhite) {
121             //
122
// This is a trick so that viewers which do not support
123
// the alpha channel will see a white background (and not
124
// a black one).
125
//
126
DataBufferInt JavaDoc biDB=(DataBufferInt JavaDoc)img.getRaster().getDataBuffer();
127             int scanStride = sppsm.getScanlineStride();
128             int dbOffset = biDB.getOffset();
129             int pixels[] = biDB.getBankData()[0];
130             int p = dbOffset;
131             int adjust = scanStride - w;
132             int a=0, r=0, g=0, b=0, pel=0;
133             for(int i=0; i<h; i++){
134                 for(int j=0; j<w; j++){
135                     pel = pixels[p];
136                     a = (pel >> 24) & 0xff;
137                     r = (pel >> 16) & 0xff;
138                     g = (pel >> 8 ) & 0xff;
139                     b = pel & 0xff;
140                     r = (255*(255 -a) + a*r)/255;
141                     g = (255*(255 -a) + a*g)/255;
142                     b = (255*(255 -a) + a*b)/255;
143                     pixels[p++] =
144                         (a<<24 & 0xff000000) |
145                         (r<<16 & 0xff0000) |
146                         (g<<8 & 0xff00) |
147                         (b & 0xff);
148                 }
149                 p += adjust;
150             }
151         }
152
153         try {
154             TIFFImageEncoder tiffEncoder =
155                 new TIFFImageEncoder(ostream, params);
156             int bands = sppsm.getNumBands();
157             int [] off = new int[bands];
158             for (int i=0; i<bands; i++)
159                 off[i] = i;
160             SampleModel JavaDoc sm = new PixelInterleavedSampleModel JavaDoc
161                 (DataBuffer.TYPE_BYTE, w, h, bands, w*bands, off);
162             
163             RenderedImage JavaDoc rimg = new FormatRed(GraphicsUtil.wrap(img), sm);
164             tiffEncoder.encode(rimg);
165         } catch (IOException JavaDoc ex) {
166             throw new TranscoderException(ex);
167         }
168     }
169
170
171     // --------------------------------------------------------------------
172
// Keys definition
173
// --------------------------------------------------------------------
174

175     /**
176      * The forceTransparentWhite key.
177      *
178      * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
179      * <TR>
180      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Key: </TH>
181      * <TD VALIGN="TOP">KEY_FORCE_TRANSPARENT_WHITE</TD></TR>
182      * <TR>
183      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
184      * <TD VALIGN="TOP">Boolean</TD></TR>
185      * <TR>
186      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
187      * <TD VALIGN="TOP">false</TD></TR>
188      * <TR>
189      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
190      * <TD VALIGN="TOP">No</TD></TR>
191      * <TR>
192      * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
193      * <TD VALIGN="TOP">It controls whether the encoder should
194      * force the image's fully transparent pixels to be fully transparent
195      * white instead of fully transparent black. This is usefull when the
196      * encoded TIFF is displayed in a viewer which does not support TIFF
197      * transparency and lets the image display with a white background instead
198      * of a black background. <br />
199      *
200      * However, note that the modified image will display differently
201      * over a white background in a viewer that supports
202      * transparency.</TD></TR>
203      * </TABLE>
204      */

205     public static final TranscodingHints.Key KEY_FORCE_TRANSPARENT_WHITE
206         = ImageTranscoder.KEY_FORCE_TRANSPARENT_WHITE;
207
208 }
209
Popular Tags