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