001: package org.libtiff.jai.codecimpl;
002:
003: import org.libtiff.jai.codec.XTIFFTileCodecImpl;
004: import org.libtiff.jai.codec.XTIFFTileCodec;
005: import org.libtiff.jai.codec.XTIFFEncodeParam;
006: import org.libtiff.jai.codec.XTIFFField;
007: import org.libtiff.jai.codec.XTIFF;
008: import org.libtiff.jai.util.JaiI18N;
009: import java.awt.Rectangle;
010:
011: import com.sun.media.jai.codecimpl.TIFFLZWDecoder;
012:
013: /**
014: * This codec encapsulates all the logic for the TIFF
015: * "lzw" decoding codec algorithm.
016: */
017: public class XTIFFLZWTileCodec extends XTIFFTileCodecImpl {
018:
019: private TIFFLZWDecoder lzwDecoder = null;
020:
021: // LZW compression related variable
022: int predictor;
023: int samplesPerPixel;
024:
025: /**
026: * Public constructor
027: */
028: public XTIFFLZWTileCodec() {
029: }
030:
031: /**
032: * Creation method
033: */
034: public XTIFFTileCodec create() {
035: return new XTIFFLZWTileCodec();
036: }
037:
038: //public boolean canEncode() {return true;}
039:
040: /**
041: * Registration method
042: */
043: public void register() {
044: register(XTIFF.COMPRESSION_LZW);
045: }
046:
047: /**
048: * The initialization method particular to LZW decoding.
049: */
050: public void initializeDecoding() {
051: // Get the number of samples per pixel
052: XTIFFField sfield = directory
053: .getField(XTIFF.TIFFTAG_SAMPLES_PER_PIXEL);
054: if (sfield == null) {
055: samplesPerPixel = 1;
056: } else {
057: samplesPerPixel = (int) sfield.getAsLong(0);
058: }
059: XTIFFField predictorField = directory
060: .getField(XTIFF.TIFFTAG_PREDICTOR);
061:
062: if (predictorField == null) {
063: predictor = 1;
064: } else {
065: predictor = predictorField.getAsInt(0);
066: if (predictor != 1 && predictor != 2) {
067: throw new RuntimeException(JaiI18N
068: .getString("XTIFFImageDecoder16"));
069: }
070: if (predictor == 2 && bitsPerSample[0] != 8) {
071: throw new RuntimeException(bitsPerSample[0]
072: + JaiI18N.getString("XTIFFImageDecoder17"));
073: }
074: }
075: lzwDecoder = new TIFFLZWDecoder(tileWidth, predictor,
076: samplesPerPixel);
077: }
078:
079: /**
080: * Decode a rectangle of pixels
081: */
082: public void decodeTilePixels(byte[] input, Rectangle newRect,
083: byte[] bdata) {
084: lzwDecoder.decode(input, bdata, newRect.height);
085: }
086:
087: /**
088: * Decode a rectangle of pixels
089: */
090: public void decodeTilePixels(byte[] input, Rectangle newRect,
091: short[] sdata) {
092:
093: // Since unitsInThisTile is the number of shorts,
094: // but we do our decompression in terms of bytes, we
095: // need to multiply unitsInThisTile by 2 in order to
096: // figure out how many bytes we'll get after
097: // decompression.
098: byte byteArray[] = new byte[unitsInThisTile * 2];
099: lzwDecoder.decode(input, byteArray, newRect.height);
100: unpackShorts(byteArray, sdata, unitsInThisTile);
101: }
102: }
|