001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: TIFFDecodeParam.java 496556 2007-01-16 00:59:48Z cam $ */
019:
020: package org.apache.xmlgraphics.image.codec.tiff;
021:
022: import org.apache.xmlgraphics.image.codec.util.ImageDecodeParam;
023:
024: /**
025: * An instance of <code>ImageDecodeParam</code> for decoding images in
026: * the TIFF format.
027: *
028: * <p> To determine the number of images present in a TIFF file, use
029: * the <code>getNumPages()</code> method on the
030: * <code>ImageDecoder</code> object that will be used to perform the
031: * decoding. The desired page number may be passed as an argument to
032: * the <code>ImageDecoder.decodeAsRaster)()</code> or
033: * <code>decodeAsRenderedImage()</code> methods.
034: *
035: * <p> For TIFF Palette color images, the colorMap always has entries
036: * of short data type, the color Black being represented by 0,0,0 and
037: * White by 65536,65536,65536. In order to display these images, the
038: * default behavior is to dither the short values down to 8 bits.
039: * The dithering is done by calling the <code>decode16BitsTo8Bits</code>
040: * method for each short value that needs to be dithered. The method has
041: * the following implementation:
042: * <code>
043: * byte b;
044: * short s;
045: * s = s & 0xffff;
046: * b = (byte)((s >> 8) & 0xff);
047: * </code>
048: * If a different algorithm is to be used for the dithering, this class
049: * should be subclassed and an appropriate implementation should be
050: * provided for the <code>decode16BitsTo8Bits</code> method in the subclass.
051: *
052: * <p>If the palette contains image data that is signed short, as specified
053: * by the SampleFormat tag, the dithering is done by calling
054: * <code>decodeSigned16BitsTo8Bits</code> instead. The method has the
055: * following implementation:
056: * <code>
057: * byte b;
058: * short s;
059: * b = (byte)((s + Short.MIN_VALUE) >> 8);
060: * </code>
061: * In order to use a different algorithm for the dithering, this class
062: * should be subclassed and the method overridden.
063: *
064: * <p> If it is desired that the Palette be decoded such that the output
065: * image is of short data type and no dithering is performed, the
066: * <code>setDecodePaletteAsShorts</code> method should be used.
067: *
068: * <p><b> This class is not a committed part of the JAI API. It may
069: * be removed or changed in future releases of JAI.</b>
070: *
071: * @see TIFFDirectory
072: */
073: public class TIFFDecodeParam implements ImageDecodeParam {
074:
075: private boolean decodePaletteAsShorts = false;
076: private Long ifdOffset = null;
077: private boolean convertJPEGYCbCrToRGB = true;
078:
079: /** Constructs a default instance of <code>TIFFDecodeParam</code>. */
080: public TIFFDecodeParam() {
081: }
082:
083: /**
084: * If set, the entries in the palette will be decoded as shorts
085: * and no short to byte lookup will be applied to them.
086: */
087: public void setDecodePaletteAsShorts(boolean decodePaletteAsShorts) {
088: this .decodePaletteAsShorts = decodePaletteAsShorts;
089: }
090:
091: /**
092: * Returns <code>true</code> if palette entries will be decoded as
093: * shorts, resulting in an output image with short datatype.
094: */
095: public boolean getDecodePaletteAsShorts() {
096: return decodePaletteAsShorts;
097: }
098:
099: /**
100: * Returns an unsigned 8 bit value computed by dithering the unsigned
101: * 16 bit value. Note that the TIFF specified short datatype is an
102: * unsigned value, while Java's <code>short</code> datatype is a
103: * signed value. Therefore the Java <code>short</code> datatype cannot
104: * be used to store the TIFF specified short value. A Java
105: * <code>int</code> is used as input instead to this method. The method
106: * deals correctly only with 16 bit unsigned values.
107: */
108: public byte decode16BitsTo8Bits(int s) {
109: return (byte) ((s >> 8) & 0xffff);
110: }
111:
112: /**
113: * Returns an unsigned 8 bit value computed by dithering the signed
114: * 16 bit value. This method deals correctly only with values in the
115: * 16 bit signed range.
116: */
117: public byte decodeSigned16BitsTo8Bits(short s) {
118: return (byte) ((s + Short.MIN_VALUE) >> 8);
119: }
120:
121: /**
122: * Sets the offset in the stream from which to read the image. There
123: * must be an Image File Directory (IFD) at that position or an error
124: * will occur. If <code>setIFDOffset()</code> is never invoked then
125: * the decoder will assume that the TIFF stream is at the beginning of
126: * the 8-byte image header. If the directory offset is set and a page
127: * number is supplied to the TIFF <code>ImageDecoder</code> then the
128: * page will be the zero-relative index of the IFD in linked list of
129: * IFDs beginning at the specified offset with a page of zero indicating
130: * the directory at that offset.
131: */
132: public void setIFDOffset(long offset) {
133: ifdOffset = new Long(offset);
134: }
135:
136: /**
137: * Returns the value set by <code>setIFDOffset()</code> or
138: * <code>null</code> if no value has been set.
139: */
140: public Long getIFDOffset() {
141: return ifdOffset;
142: }
143:
144: /**
145: * Sets a flag indicating whether to convert JPEG-compressed YCbCr data
146: * to RGB. The default value is <code>true</code>. This flag is
147: * ignored if the image data are not JPEG-compressed.
148: */
149: public void setJPEGDecompressYCbCrToRGB(
150: boolean convertJPEGYCbCrToRGB) {
151: this .convertJPEGYCbCrToRGB = convertJPEGYCbCrToRGB;
152: }
153:
154: /**
155: * Whether JPEG-compressed YCbCr data will be converted to RGB.
156: */
157: public boolean getJPEGDecompressYCbCrToRGB() {
158: return convertJPEGYCbCrToRGB;
159: }
160: }
|