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;
050
051import javax.imageio.stream.ImageOutputStream;
052
053/**
054 * Modified from original LZWCompressor to change interface to passing a
055 * buffer of data to be compressed.
056 **/
057public class LZWCompressor
058{
059    /** base underlying code size of data being compressed 8 for TIFF, 1 to 8 for GIF **/
060    int codeSize_;
061
062    /** reserved clear code based on code size **/
063    int clearCode_;
064
065    /** reserved end of data code based on code size **/
066    int endOfInfo_;
067
068    /** current number bits output for each code **/
069    int numBits_;
070
071    /** limit at which current number of bits code size has to be increased **/
072    int limit_;
073
074    /** the prefix code which represents the predecessor string to current input point **/
075    short prefix_;
076
077    /** output destination for bit codes **/
078    BitFile bf_;
079
080    /** general purpose LZW string table **/
081    LZWStringTable lzss_;
082
083    /** modify the limits of the code values in LZW encoding due to TIFF bug / feature **/
084    boolean tiffFudge_;
085
086    /**
087         * @param out destination for compressed data
088         * @param codeSize the initial code size for the LZW compressor
089         * @param TIFF flag indicating that TIFF lzw fudge needs to be applied
090         * @exception IOException if underlying output stream error
091         **/
092    public LZWCompressor(ImageOutputStream out, int codeSize, boolean TIFF) throws IOException
093    {
094        bf_ = new BitFile(out, !TIFF);  // set flag for GIF as NOT tiff
095        codeSize_  = codeSize;
096        tiffFudge_ = TIFF;
097        clearCode_ = 1 << codeSize_;
098        endOfInfo_ = clearCode_ + 1;
099        numBits_   = codeSize_ + 1;
100
101        limit_ = (1 << numBits_) - 1;
102        if (tiffFudge_)
103            --limit_;
104
105        prefix_ = (short)0xFFFF;
106        lzss_ = new LZWStringTable();
107        lzss_.ClearTable(codeSize_);
108        bf_.writeBits(clearCode_, numBits_);
109    }
110
111    /**
112         * @param buf data to be compressed to output stream
113         * @exception IOException if underlying output stream error
114         **/
115    public void compress(byte[] buf, int offset, int length)
116        throws IOException
117    {
118        int idx;
119        byte c;
120        short index;
121
122        int maxOffset = offset + length;
123        for (idx = offset; idx < maxOffset; ++idx)
124            {
125                c = buf[idx];
126                if ((index = lzss_.FindCharString(prefix_, c)) != -1)
127                    prefix_ = index;
128                else
129                    {
130                        bf_.writeBits(prefix_, numBits_);
131                        if (lzss_.AddCharString(prefix_, c) > limit_)
132                            {
133                                if (numBits_ == 12)
134                                    {
135                                        bf_.writeBits(clearCode_, numBits_);
136                                        lzss_.ClearTable(codeSize_);
137                                        numBits_ = codeSize_ + 1;
138                                    }
139                                else
140                                    ++numBits_;
141
142                                limit_ = (1 << numBits_) - 1;
143                                if (tiffFudge_)
144                                    --limit_;
145                            }
146                        prefix_ = (short)((short)c & 0xFF);
147                    }
148            }
149    }
150
151    /**
152         * Indicate to compressor that no more data to go so write out
153         * any remaining buffered data.
154         *
155         * @exception IOException if underlying output stream error
156         **/
157    public void flush() throws IOException
158    {
159        if (prefix_ != -1)
160            bf_.writeBits(prefix_, numBits_);
161                
162        bf_.writeBits(endOfInfo_, numBits_);
163        bf_.flush();
164    }
165
166    public void dump(PrintStream out)
167    {
168        lzss_.dump(out);
169    }
170
171}