001/*
002 * $RCSfile: LZWCompressor.java,v $
003 *
004 * 
005 * Copyright (c) 2005 Sun Microsystems, Inc. All  Rights Reserved.
006 * 
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions
009 * are met: 
010 * 
011 * - Redistribution of source code must retain the above copyright 
012 *   notice, this  list of conditions and the following disclaimer.
013 * 
014 * - Redistribution in binary form must reproduce the above copyright
015 *   notice, this list of conditions and the following disclaimer in 
016 *   the documentation and/or other materials provided with the
017 *   distribution.
018 * 
019 * Neither the name of Sun Microsystems, Inc. or the names of 
020 * contributors may be used to endorse or promote products derived 
021 * from this software without specific prior written permission.
022 * 
023 * This software is provided "AS IS," without a warranty of any 
024 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 
025 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
026 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 
028 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 
029 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 
031 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035 * POSSIBILITY OF SUCH DAMAGES. 
036 * 
037 * You acknowledge that this software is not designed or intended for 
038 * use in the design, construction, operation or maintenance of any 
039 * nuclear facility. 
040 *
041 * $Revision: 1.1 $
042 * $Date: 2005/02/11 05:01:22 $
043 * $State: Exp $
044 */
045
046package com.github.jaiimageio.impl.common;
047
048import java.io.IOException;
049import java.io.PrintStream;
050import javax.imageio.stream.ImageOutputStream;
051
052/**
053 * Modified from original LZWCompressor to change interface to passing a
054 * buffer of data to be compressed.
055 **/
056public class LZWCompressor
057{
058    /** base underlying code size of data being compressed 8 for TIFF, 1 to 8 for GIF **/
059    int codeSize_;
060
061    /** reserved clear code based on code size **/
062    int clearCode_;
063
064    /** reserved end of data code based on code size **/
065    int endOfInfo_;
066
067    /** current number bits output for each code **/
068    int numBits_;
069
070    /** limit at which current number of bits code size has to be increased **/
071    int limit_;
072
073    /** the prefix code which represents the predecessor string to current input point **/
074    short prefix_;
075
076    /** output destination for bit codes **/
077    BitFile bf_;
078
079    /** general purpose LZW string table **/
080    LZWStringTable lzss_;
081
082    /** modify the limits of the code values in LZW encoding due to TIFF bug / feature **/
083    boolean tiffFudge_;
084
085    /**
086         * @param out destination for compressed data
087         * @param codeSize the initial code size for the LZW compressor
088         * @param TIFF flag indicating that TIFF lzw fudge needs to be applied
089         * @exception IOException if underlying output stream error
090         **/
091    public LZWCompressor(ImageOutputStream out, int codeSize, boolean TIFF) throws IOException
092    {
093        bf_ = new BitFile(out, !TIFF);  // set flag for GIF as NOT tiff
094        codeSize_  = codeSize;
095        tiffFudge_ = TIFF;
096        clearCode_ = 1 << codeSize_;
097        endOfInfo_ = clearCode_ + 1;
098        numBits_   = codeSize_ + 1;
099
100        limit_ = (1 << numBits_) - 1;
101        if (tiffFudge_)
102            --limit_;
103
104        prefix_ = (short)0xFFFF;
105        lzss_ = new LZWStringTable();
106        lzss_.ClearTable(codeSize_);
107        bf_.writeBits(clearCode_, numBits_);
108    }
109
110    /**
111         * @param buf data to be compressed to output stream
112         * @exception IOException if underlying output stream error
113         **/
114    public void compress(byte[] buf, int offset, int length)
115        throws IOException
116    {
117        int idx;
118        byte c;
119        short index;
120
121        int maxOffset = offset + length;
122        for (idx = offset; idx < maxOffset; ++idx)
123            {
124                c = buf[idx];
125                if ((index = lzss_.FindCharString(prefix_, c)) != -1)
126                    prefix_ = index;
127                else
128                    {
129                        bf_.writeBits(prefix_, numBits_);
130                        if (lzss_.AddCharString(prefix_, c) > limit_)
131                            {
132                                if (numBits_ == 12)
133                                    {
134                                        bf_.writeBits(clearCode_, numBits_);
135                                        lzss_.ClearTable(codeSize_);
136                                        numBits_ = codeSize_ + 1;
137                                    }
138                                else
139                                    ++numBits_;
140
141                                limit_ = (1 << numBits_) - 1;
142                                if (tiffFudge_)
143                                    --limit_;
144                            }
145                        prefix_ = (short)((short)c & 0xFF);
146                    }
147            }
148    }
149
150    /**
151         * Indicate to compressor that no more data to go so write out
152         * any remaining buffered data.
153         *
154         * @exception IOException if underlying output stream error
155         **/
156    public void flush() throws IOException
157    {
158        if (prefix_ != -1)
159            bf_.writeBits(prefix_, numBits_);
160                
161        bf_.writeBits(endOfInfo_, numBits_);
162        bf_.flush();
163    }
164
165    public void dump(PrintStream out)
166    {
167        lzss_.dump(out);
168    }
169
170}