Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/apache/batik/ext/awt/image/codec/tiff/TIFFEncodeParam.java


1   /*
2   
3      Copyright 2001,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  package org.apache.batik.ext.awt.image.codec.tiff;
19  
20  import java.util.Iterator;
21  import java.util.zip.Deflater;
22  
23  import org.apache.batik.ext.awt.image.codec.ImageEncodeParam;
24  
25  import com.sun.image.codec.jpeg.JPEGEncodeParam;
26  
27  /**
28   * An instance of <code>ImageEncodeParam</code> for encoding images in 
29   * the TIFF format.
30   *
31   * <p> This class allows for the specification of encoding parameters. By
32   * default, the image is encoded without any compression, and is written
33   * out consisting of strips, not tiles. The particular compression scheme 
34   * to be used can be specified by using the <code>setCompression()</code>
35   * method. The compression scheme specified will be honored only if it is 
36   * compatible with the type of image being written out. For example, 
37   * Group3 and Group4 compressions can only be used with Bilevel images.
38   * Writing of tiled TIFF images can be enabled by calling the
39   * <code>setWriteTiled()</code> method.
40   *
41   * <p><b> This class is not a committed part of the JAI API.  It may
42   * be removed or changed in future releases of JAI.</b>
43   *
44   */
45  public class TIFFEncodeParam implements ImageEncodeParam {
46  
47      /** No compression. */
48      public static final int COMPRESSION_NONE          = 1;
49  
50      /**
51       * Modified Huffman Compression (CCITT Group 3 1D facsimile compression).
52       * <p><b>Not currently supported.</b>
53       */
54      public static final int COMPRESSION_GROUP3_1D     = 2;
55  
56      /**
57       * CCITT T.4 bilevel compression (CCITT Group 3 2D facsimile compression).
58       * <p><b>Not currently supported.</b>
59       */
60      public static final int COMPRESSION_GROUP3_2D     = 3;
61  
62      /**
63       * CCITT T.6 bilevel compression (CCITT Group 4 facsimile compression).
64       * <p><b>Not currently supported.</b>
65       */
66      public static final int COMPRESSION_GROUP4        = 4;
67  
68      /**
69       * LZW compression.
70       * <p><b>Not supported.</b>
71       */
72      public static final int COMPRESSION_LZW           = 5;
73  
74      /**
75       * Code for original JPEG-in-TIFF compression which has been
76       * depricated (for many good reasons) in favor of Tech Note 2
77       * JPEG compression (compression scheme 7).
78       * <p><b>Not supported.</b>
79       */
80      public static final int COMPRESSION_JPEG_BROKEN   = 6;
81  
82      /**
83       * <a href="ftp://ftp.sgi.com/graphics/tiff/TTN2.draft.txt"> 
84       * JPEG-in-TIFF</a> compression.
85       */
86      public static final int COMPRESSION_JPEG_TTN2     = 7;
87  
88      /** Byte-oriented run-length encoding "PackBits" compression. */
89      public static final int COMPRESSION_PACKBITS      = 32773;
90  
91      /**
92       * <a href="http://info.internet.isi.edu:80/in-notes/rfc/files/rfc1951.txt"> 
93       * DEFLATE</a> lossless compression (also known as "Zip-in-TIFF").
94       */
95      public static final int COMPRESSION_DEFLATE       = 32946;
96  
97      private int compression = COMPRESSION_NONE;
98  
99      private boolean writeTiled = false;
100     private int tileWidth;
101     private int tileHeight;
102 
103     private Iterator extraImages;
104 
105     private TIFFField[] extraFields;
106 
107     private boolean convertJPEGRGBToYCbCr = true;
108     private JPEGEncodeParam jpegEncodeParam = null;
109 
110     private int deflateLevel = Deflater.DEFAULT_COMPRESSION;
111 
112     /** 
113      * Constructs a TIFFEncodeParam object with default values for
114      * all parameters.
115      */
116     public TIFFEncodeParam() {}
117 
118     /**
119      * Returns the value of the compression parameter.
120      */
121     public int getCompression() {
122   return compression;
123     }
124 
125     /**
126      * Specifies the type of compression to be used.  The compression type
127      * specified will be honored only if it is compatible with the image
128      * being written out.  Currently only PackBits, JPEG, and DEFLATE
129      * compression schemes are supported.
130      *
131      * <p> If <code>compression</code> is set to any value but
132      * <code>COMPRESSION_NONE</code> and the <code>OutputStream</code>
133      * supplied to the <code>ImageEncoder</code> is not a
134      * <code>SeekableOutputStream</code>, then the encoder will use either
135      * a temporary file or a memory cache when compressing the data
136      * depending on whether the file system is accessible.  Compression
137      * will therefore be more efficient if a <code>SeekableOutputStream</code>
138      * is supplied.
139      *
140      * @param compression    The compression type.
141      */
142     public void setCompression(int compression) {
143 
144         switch(compression) {
145         case COMPRESSION_NONE:
146         case COMPRESSION_PACKBITS:
147         case COMPRESSION_JPEG_TTN2:
148         case COMPRESSION_DEFLATE:
149             // Do nothing.
150             break;
151         default:
152       throw new Error("TIFFEncodeParam0");
153   }
154 
155   this.compression = compression;
156     }
157 
158     /**
159      * Returns the value of the writeTiled parameter. 
160      */
161     public boolean getWriteTiled() {
162   return writeTiled;
163     }
164 
165     /**
166      * If set, the data will be written out in tiled format, instead of
167      * in strips.
168      *
169      * @param writeTiled     Specifies whether the image data should be 
170      *                       wriiten out in tiled format.
171      */
172     public void setWriteTiled(boolean writeTiled) {
173   this.writeTiled = writeTiled;
174     }
175 
176     /**
177      * Sets the dimensions of the tiles to be written.  If either
178      * value is non-positive, the encoder will use a default value.
179      *
180      * <p> If the data are being written as tiles, i.e.,
181      * <code>getWriteTiled()</code> returns <code>true</code>, then the
182      * default tile dimensions used by the encoder are those of the tiles
183      * of the image being encoded.
184      *
185      * <p> If the data are being written as strips, i.e.,
186      * <code>getWriteTiled()</code> returns <code>false</code>, the width
187      * of each strip is always the width of the image and the default
188      * number of rows per strip is 8.
189      *
190      * <p> If JPEG compession is being used, the dimensions of the strips or
191      * tiles may be modified to conform to the JPEG-in-TIFF specification.
192      * 
193      * @param tileWidth The tile width; ignored if strips are used.
194      * @param tileHeight The tile height or number of rows per strip.
195      */
196     public void setTileSize(int tileWidth, int tileHeight) {
197         this.tileWidth = tileWidth;
198         this.tileHeight = tileHeight;
199     }
200 
201     /**
202      * Retrieves the tile width set via <code>setTileSize()</code>.
203      */
204     public int getTileWidth() {
205         return tileWidth;
206     }
207 
208     /**
209      * Retrieves the tile height set via <code>setTileSize()</code>.
210      */
211     public int getTileHeight() {
212         return tileHeight;
213     }
214 
215     /**
216      * Sets an <code>Iterator</code> of additional images to be written
217      * after the image passed as an argument to the <code>ImageEncoder</code>.
218      * The methods on the supplied <code>Iterator</code> must only be invoked
219      * by the <code>ImageEncoder</code> which will exhaust the available
220      * values unless an error occurs.
221      *
222      * <p> The value returned by an invocation of <code>next()</code> on the
223      * <code>Iterator</code> must return either a <code>RenderedImage</code>
224      * or an <code>Object[]</code> of length 2 wherein the element at index
225      * zero is a <code>RenderedImage</code> amd the other element is a
226      * <code>TIFFEncodeParam</code>.  If no <code>TIFFEncodeParam</code> is
227      * supplied in this manner for an additional image, the parameters used
228      * to create the <code>ImageEncoder</code> will be used.  The extra
229      * image <code>Iterator</code> set on any <code>TIFFEncodeParam</code>
230      * of an additional image will in all cases be ignored.
231      */
232     public synchronized void setExtraImages(Iterator extraImages) {
233         this.extraImages = extraImages;
234     }
235 
236     /**
237      * Returns the additional image <code>Iterator</code> specified via
238      * <code>setExtraImages()</code> or <code>null</code> if none was
239      * supplied or if a <code>null</code> value was supplied.
240      */
241     public synchronized Iterator getExtraImages() {
242         return extraImages;
243     }
244 
245     /**
246      * Sets the compression level for DEFLATE-compressed data which should
247      * either be <code>java.util.Deflater.DEFAULT_COMPRESSION</code> or a
248      * value in the range [1,9] where larger values indicate more compression.
249      * The default setting is <code>Deflater.DEFAULT_COMPRESSION</code>.  This
250      * setting is ignored if the compression type is not DEFLATE.
251      */
252     public void setDeflateLevel(int deflateLevel) {
253         if(deflateLevel < 1 && deflateLevel > 9 &&
254            deflateLevel != Deflater.DEFAULT_COMPRESSION) {
255       throw new Error("TIFFEncodeParam1");
256         }
257 
258         this.deflateLevel = deflateLevel;
259     }
260 
261     /**
262      * Gets the compression level for DEFLATE compression.
263      */
264     public int getDeflateLevel() {
265         return deflateLevel;
266     }
267 
268     /**
269      * Sets flag indicating whether to convert RGB data to YCbCr when the
270      * compression type is JPEG.  The default value is <code>true</code>.
271      * This flag is ignored if the compression type is not JPEG.
272      */
273     public void setJPEGCompressRGBToYCbCr(boolean convertJPEGRGBToYCbCr) {
274         this.convertJPEGRGBToYCbCr = convertJPEGRGBToYCbCr;
275     }
276 
277     /**
278      * Whether RGB data will be converted to YCbCr when using JPEG compression.
279      */
280     public boolean getJPEGCompressRGBToYCbCr() {
281         return convertJPEGRGBToYCbCr;
282     }
283 
284     /**
285      * Sets the JPEG compression parameters.  These parameters are ignored
286      * if the compression type is not JPEG.  The argument may be
287      * <code>null</code> to indicate that default compression parameters
288      * are to be used.  For maximum conformance with the specification it
289      * is recommended in most cases that only the quality compression
290      * parameter be set.
291      *
292      * <p> The <code>writeTablesOnly</code> and <code>JFIFHeader</code>
293      * flags of the <code>JPEGEncodeParam</code> are ignored.  The
294      * <code>writeImageOnly</code> flag is used to determine whether the
295      * JPEGTables field will be written to the TIFF stream: if
296      * <code>writeImageOnly</code> is <code>true</code>, then the JPEGTables
297      * field will be written and will contain a valid JPEG abbreviated
298      * table specification datastream.  In this case the data in each data
299      * segment (strip or tile) will contain an abbreviated JPEG image
300      * datastream.  If the <code>writeImageOnly</code> flag is
301      * <code>false</code>, then the JPEGTables field will not be written and
302      * each data segment will contain a complete JPEG interchange datastream.
303      */
304     public void setJPEGEncodeParam(JPEGEncodeParam jpegEncodeParam) {
305         if(jpegEncodeParam != null) {
306             jpegEncodeParam = (JPEGEncodeParam)jpegEncodeParam.clone();
307             jpegEncodeParam.setTableInfoValid(false);
308             jpegEncodeParam.setImageInfoValid(true);
309         }
310         this.jpegEncodeParam = jpegEncodeParam;
311     }
312 
313     /**
314      * Retrieves the JPEG compression parameters.
315      */
316     public JPEGEncodeParam getJPEGEncodeParam() {
317         return jpegEncodeParam;
318     }
319 
320     /**
321      * Sets an array of extra fields to be written to the TIFF Image File
322      * Directory (IFD).  Fields with tags equal to the tag of any
323      * automatically generated fields are ignored.  No error checking is
324      * performed with respect to the validity of the field contents or
325      * the appropriateness of the field for the image being encoded.
326      *
327      * @param extraFields An array of extra fields; the parameter is
328      * copied by reference.
329      */
330     public void setExtraFields(TIFFField[] extraFields) {
331         this.extraFields = extraFields;
332     }
333 
334     /**
335      * Returns the value set by <code>setExtraFields()</code>.
336      */
337     public TIFFField[] getExtraFields() {
338         return extraFields;
339     }
340 }