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