001: /*
002: * $RCSfile: TIFFDecodeParam.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.1 $
009: * $Date: 2005/02/11 04:55:34 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.codec;
013:
014: /**
015: * An instance of <code>ImageDecodeParam</code> for decoding images in
016: * the TIFF format.
017: *
018: * <p> To determine the number of images present in a TIFF file, use
019: * the <code>getNumPages()</code> method on the
020: * <code>ImageDecoder</code> object that will be used to perform the
021: * decoding. The desired page number may be passed as an argument to
022: * the <code>ImageDecoder.decodeAsRaster)()</code> or
023: * <code>decodeAsRenderedImage()</code> methods.
024: *
025: * <p> For TIFF Palette color images, the colorMap always has entries
026: * of short data type, the color Black being represented by 0,0,0 and
027: * White by 65536,65536,65536. In order to display these images, the
028: * default behavior is to dither the short values down to 8 bits.
029: * The dithering is done by calling the <code>decode16BitsTo8Bits</code>
030: * method for each short value that needs to be dithered. The method has
031: * the following implementation:
032: * <code>
033: * byte b;
034: * short s;
035: * s = s & 0xffff;
036: * b = (byte)((s >> 8) & 0xff);
037: * </code>
038: * If a different algorithm is to be used for the dithering, this class
039: * should be subclassed and an appropriate implementation should be
040: * provided for the <code>decode16BitsTo8Bits</code> method in the subclass.
041: *
042: * <p>If the palette contains image data that is signed short, as specified
043: * by the SampleFormat tag, the dithering is done by calling
044: * <code>decodeSigned16BitsTo8Bits</code> instead. The method has the
045: * following implementation:
046: * <code>
047: * byte b;
048: * short s;
049: * b = (byte)((s + Short.MIN_VALUE) >> 8);
050: * </code>
051: * In order to use a different algorithm for the dithering, this class
052: * should be subclassed and the method overridden.
053: *
054: * <p> If it is desired that the Palette be decoded such that the output
055: * image is of short data type and no dithering is performed, the
056: * <code>setDecodePaletteAsShorts</code> method should be used.
057: *
058: * <p><b> This class is not a committed part of the JAI API. It may
059: * be removed or changed in future releases of JAI.</b>
060: *
061: * @see TIFFDirectory
062: */
063: public class TIFFDecodeParam implements ImageDecodeParam {
064:
065: private boolean decodePaletteAsShorts = false;
066: private Long ifdOffset = null;
067: private boolean convertJPEGYCbCrToRGB = true;
068:
069: /** Constructs a default instance of <code>TIFFDecodeParam</code>. */
070: public TIFFDecodeParam() {
071: }
072:
073: /**
074: * If set, the entries in the palette will be decoded as shorts
075: * and no short to byte lookup will be applied to them.
076: */
077: public void setDecodePaletteAsShorts(boolean decodePaletteAsShorts) {
078: this .decodePaletteAsShorts = decodePaletteAsShorts;
079: }
080:
081: /**
082: * Returns <code>true</code> if palette entries will be decoded as
083: * shorts, resulting in an output image with short datatype.
084: */
085: public boolean getDecodePaletteAsShorts() {
086: return decodePaletteAsShorts;
087: }
088:
089: /**
090: * Returns an unsigned 8 bit value computed by dithering the unsigned
091: * 16 bit value. Note that the TIFF specified short datatype is an
092: * unsigned value, while Java's <code>short</code> datatype is a
093: * signed value. Therefore the Java <code>short</code> datatype cannot
094: * be used to store the TIFF specified short value. A Java
095: * <code>int</code> is used as input instead to this method. The method
096: * deals correctly only with 16 bit unsigned values.
097: */
098: public byte decode16BitsTo8Bits(int s) {
099: return (byte) ((s >> 8) & 0xffff);
100: }
101:
102: /**
103: * Returns an unsigned 8 bit value computed by dithering the signed
104: * 16 bit value. This method deals correctly only with values in the
105: * 16 bit signed range.
106: */
107: public byte decodeSigned16BitsTo8Bits(short s) {
108: return (byte) ((s + Short.MIN_VALUE) >> 8);
109: }
110:
111: /**
112: * Sets the offset in the stream from which to read the image. There
113: * must be an Image File Directory (IFD) at that position or an error
114: * will occur. If <code>setIFDOffset()</code> is never invoked then
115: * the decoder will assume that the TIFF stream is at the beginning of
116: * the 8-byte image header. If the directory offset is set and a page
117: * number is supplied to the TIFF <code>ImageDecoder</code> then the
118: * page will be the zero-relative index of the IFD in linked list of
119: * IFDs beginning at the specified offset with a page of zero indicating
120: * the directory at that offset.
121: */
122: public void setIFDOffset(long offset) {
123: ifdOffset = new Long(offset);
124: }
125:
126: /**
127: * Returns the value set by <code>setIFDOffset()</code> or
128: * <code>null</code> if no value has been set.
129: */
130: public Long getIFDOffset() {
131: return ifdOffset;
132: }
133:
134: /**
135: * Sets a flag indicating whether to convert JPEG-compressed YCbCr data
136: * to RGB. The default value is <code>true</code>. This flag is
137: * ignored if the image data are not JPEG-compressed.
138: */
139: public void setJPEGDecompressYCbCrToRGB(
140: boolean convertJPEGYCbCrToRGB) {
141: this .convertJPEGYCbCrToRGB = convertJPEGYCbCrToRGB;
142: }
143:
144: /**
145: * Whether JPEG-compressed YCbCr data will be converted to RGB.
146: */
147: public boolean getJPEGDecompressYCbCrToRGB() {
148: return convertJPEGYCbCrToRGB;
149: }
150: }
|