FileDocCategorySizeDatePackage
LZWCompressor.javaAPI DocJava SE 6 API3768Tue Jun 10 00:21:52 BST 2008com.sun.imageio.plugins.common

LZWCompressor

public class LZWCompressor extends Object

Fields Summary
int
codeSize
base underlying code size of data being compressed 8 for TIFF, 1 to 8 for GIF
int
clearCode
reserved clear code based on code size
int
endOfInfo
reserved end of data code based on code size
int
numBits
current number bits output for each code
int
limit
limit at which current number of bits code size has to be increased
short
prefix
the prefix code which represents the predecessor string to current input point
BitFile
bf
output destination for bit codes
LZWStringTable
lzss
general purpose LZW string table
boolean
tiffFudge
modify the limits of the code values in LZW encoding due to TIFF bug / feature
Constructors Summary
public LZWCompressor(ImageOutputStream out, int codeSize, boolean TIFF)

param
out destination for compressed data
param
codeSize the initial code size for the LZW compressor
param
TIFF flag indicating that TIFF lzw fudge needs to be applied
exception
IOException if underlying output stream error

        bf = new BitFile(out, !TIFF); // set flag for GIF as NOT tiff
        this.codeSize = codeSize;
        tiffFudge = TIFF;
        clearCode = 1 << codeSize;
        endOfInfo = clearCode + 1;
        numBits = codeSize + 1;

        limit = (1 << numBits) - 1;
        if (tiffFudge) {
            --limit;
        }

        prefix = (short)0xFFFF;
        lzss = new LZWStringTable();
        lzss.clearTable(codeSize);
        bf.writeBits(clearCode, numBits);
    
Methods Summary
public voidcompress(byte[] buf, int offset, int length)

param
buf data to be compressed to output stream
exception
IOException if underlying output stream error

        int idx;
        byte c;
        short index;

        int maxOffset = offset + length;
        for (idx = offset; idx < maxOffset; ++idx) {
            c = buf[idx];
            if ((index = lzss.findCharString(prefix, c)) != -1) {
                prefix = index;
            } else {
                bf.writeBits(prefix, numBits);
                if (lzss.addCharString(prefix, c) > limit) {
                    if (numBits == 12) {
                        bf.writeBits(clearCode, numBits);
                        lzss.clearTable(codeSize);
                        numBits = codeSize + 1;
                    } else {
                        ++numBits;
                    }

                    limit = (1 << numBits) - 1;
                    if (tiffFudge) {
                        --limit;
                    }
                }
                prefix = (short)((short)c & 0xFF);
            }
        }
    
public voiddump(java.io.PrintStream out)

        lzss.dump(out);
    
public voidflush()

        if (prefix != -1) {
            bf.writeBits(prefix, numBits);
        }
        
        bf.writeBits(endOfInfo, numBits);
        bf.flush();