Source code: non_com/media/jai/codec/TIFFDecodeParam.java
1 /*
2 * Copyright (c) 2001 Sun Microsystems, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * -Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * -Redistribution in binary form must reproduct the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of Sun Microsystems, Inc. or the names of contributors may
15 * be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * This software is provided "AS IS," without a warranty of any kind. ALL
19 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
20 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
21 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE
22 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
23 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
24 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
25 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
26 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
27 * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
29 *
30 * You acknowledge that Software is not designed,licensed or intended for use in
31 * the design, construction, operation or maintenance of any nuclear facility.
32 */
33 package non_com.media.jai.codec;
34 /**
35 * An instance of <code>ImageDecodeParam</code> for decoding images in the TIFF
36 * format. <p>
37 *
38 * To determine the number of images present in a TIFF file, use the <code>getNumPages()</code>
39 * method on the <code>ImageDecoder</code> object that will be used to perform
40 * the decoding. The desired page number may be passed as an argument to the
41 * <code>ImageDecoder.decodeAsRaster)()</code> or <code>decodeAsRenderedImage()</code>
42 * methods. <p>
43 *
44 * For TIFF Palette color images, the colorMap always has entries of short data
45 * type, the color Black being represented by 0,0,0 and White by
46 * 65536,65536,65536. In order to display these images, the default behavior is
47 * to dither the short values down to 8 bits. The dithering is done by calling
48 * the <code>decode16BitsTo8Bits</code> method for each short value that needs
49 * to be dithered. The method has the following implementation: <code>
50 * byte b;
51 * short s;
52 * s = s & 0xffff;
53 * b = (byte)((s >> 8) & 0xff);
54 * </code> If a different algorithm is to be used for the dithering, this class
55 * should be subclassed and an appropriate implementation should be provided
56 * for the <code>decode16BitsTo8Bits</code> method in the subclass. <p>
57 *
58 * If the palette contains image data that is signed short, as specified by the
59 * SampleFormat tag, the dithering is done by calling <code>decodeSigned16BitsTo8Bits</code>
60 * instead. The method has the following implementation: <code>
61 * byte b;
62 * short s;
63 * b = (byte)((s + Short.MIN_VALUE) >> 8);
64 * </code> In order to use a different algorithm for the dithering, this class
65 * should be subclassed and the method overridden. <p>
66 *
67 * If it is desired that the Palette be decoded such that the output image is
68 * of short data type and no dithering is performed, the <code>setDecodePaletteAsShorts</code>
69 * method should be used. <p>
70 *
71 * <b> This class is not a committed part of the JAI API. It may be removed or
72 * changed in future releases of JAI.</b>
73 *
74 * @see TIFFDirectory
75 */
76 public class TIFFDecodeParam implements ImageDecodeParam {
77
78 private boolean decodePaletteAsShorts = false;
79 private Long ifdOffset = null;
80 private boolean convertJPEGYCbCrToRGB = true;
81
82
83 /**
84 * Constructs a default instance of <code>TIFFDecodeParam</code>.
85 */
86 public TIFFDecodeParam() {
87 }
88
89
90 /**
91 * If set, the entries in the palette will be decoded as shorts and no short
92 * to byte lookup will be applied to them.
93 *
94 * @param decodePaletteAsShorts The new DecodePaletteAsShorts value
95 */
96 public void setDecodePaletteAsShorts(boolean decodePaletteAsShorts) {
97 this.decodePaletteAsShorts = decodePaletteAsShorts;
98 }
99
100
101 /**
102 * Sets the offset in the stream from which to read the image. There must be
103 * an Image File Directory (IFD) at that position or an error will occur. If
104 * <code>setIFDOffset()</code> is never invoked then the decoder will assume
105 * that the TIFF stream is at the beginning of the 8-byte image header. If
106 * the directory offset is set and a page number is supplied to the TIFF
107 * <code>ImageDecoder</code> then the page will be the zero-relative index of
108 * the IFD in linked list of IFDs beginning at the specified offset with a
109 * page of zero indicating the directory at that offset.
110 *
111 * @param offset The new IFDOffset value
112 */
113 public void setIFDOffset(long offset) {
114 ifdOffset = new Long(offset);
115 }
116
117
118 /**
119 * Sets a flag indicating whether to convert JPEG-compressed YCbCr data to
120 * RGB. The default value is <code>true</code>. This flag is ignored if the
121 * image data are not JPEG-compressed.
122 *
123 * @param convertJPEGYCbCrToRGB The new JPEGDecompressYCbCrToRGB value
124 */
125 public void setJPEGDecompressYCbCrToRGB(boolean convertJPEGYCbCrToRGB) {
126 this.convertJPEGYCbCrToRGB = convertJPEGYCbCrToRGB;
127 }
128
129
130 /**
131 * Returns <code>true</code> if palette entries will be decoded as shorts,
132 * resulting in an output image with short datatype.
133 *
134 * @return The DecodePaletteAsShorts value
135 */
136 public boolean getDecodePaletteAsShorts() {
137 return decodePaletteAsShorts;
138 }
139
140
141 /**
142 * Returns the value set by <code>setIFDOffset()</code> or <code>null</code>
143 * if no value has been set.
144 *
145 * @return The IFDOffset value
146 */
147 public Long getIFDOffset() {
148 return ifdOffset;
149 }
150
151
152 /**
153 * Whether JPEG-compressed YCbCr data will be converted to RGB.
154 *
155 * @return The JPEGDecompressYCbCrToRGB value
156 */
157 public boolean getJPEGDecompressYCbCrToRGB() {
158 return convertJPEGYCbCrToRGB;
159 }
160
161
162 /**
163 * Returns an unsigned 8 bit value computed by dithering the unsigned 16 bit
164 * value. Note that the TIFF specified short datatype is an unsigned value,
165 * while Java's <code>short</code> datatype is a signed value. Therefore the
166 * Java <code>short</code> datatype cannot be used to store the TIFF
167 * specified short value. A Java <code>int</code> is used as input instead to
168 * this method. The method deals correctly only with 16 bit unsigned values.
169 *
170 * @param s Description of Parameter
171 * @return Description of the Returned Value
172 */
173 public byte decode16BitsTo8Bits(int s) {
174 return (byte) ((s >> 8) & 0xffff);
175 }
176
177
178 /**
179 * Returns an unsigned 8 bit value computed by dithering the signed 16 bit
180 * value. This method deals correctly only with values in the 16 bit signed
181 * range.
182 *
183 * @param s Description of Parameter
184 * @return Description of the Returned Value
185 */
186 public byte decodeSigned16BitsTo8Bits(short s) {
187 return (byte) ((s + Short.MIN_VALUE) >> 8);
188 }
189 }