KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > common > LZWCompressor


1 /*
2  * @(#)LZWCompressor.java 1.2 05/11/17
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.common;
9
10 import java.io.IOException JavaDoc;
11 import java.io.PrintStream JavaDoc;
12 import javax.imageio.stream.ImageOutputStream JavaDoc;
13
14 /*
15  * Modified from original LZWCompressor to change interface to passing a
16  * buffer of data to be compressed.
17  */

18 public class LZWCompressor {
19     /** base underlying code size of data being compressed 8 for TIFF, 1 to 8 for GIF **/
20     int codeSize;
21     
22     /** reserved clear code based on code size **/
23     int clearCode;
24     
25     /** reserved end of data code based on code size **/
26     int endOfInfo;
27     
28     /** current number bits output for each code **/
29     int numBits;
30     
31     /** limit at which current number of bits code size has to be increased **/
32     int limit;
33     
34     /** the prefix code which represents the predecessor string to current input point **/
35     short prefix;
36     
37     /** output destination for bit codes **/
38     BitFile bf;
39     
40     /** general purpose LZW string table **/
41     LZWStringTable lzss;
42     
43     /** modify the limits of the code values in LZW encoding due to TIFF bug / feature **/
44     boolean tiffFudge;
45
46     /**
47      * @param out destination for compressed data
48      * @param codeSize the initial code size for the LZW compressor
49      * @param TIFF flag indicating that TIFF lzw fudge needs to be applied
50      * @exception IOException if underlying output stream error
51      **/

52     public LZWCompressor(ImageOutputStream JavaDoc out, int codeSize, boolean TIFF)
53         throws IOException JavaDoc
54     {
55         bf = new BitFile(out, !TIFF); // set flag for GIF as NOT tiff
56
this.codeSize = codeSize;
57         tiffFudge = TIFF;
58         clearCode = 1 << codeSize;
59         endOfInfo = clearCode + 1;
60         numBits = codeSize + 1;
61
62         limit = (1 << numBits) - 1;
63         if (tiffFudge) {
64             --limit;
65         }
66
67         prefix = (short)0xFFFF;
68         lzss = new LZWStringTable();
69         lzss.clearTable(codeSize);
70         bf.writeBits(clearCode, numBits);
71     }
72
73     /**
74      * @param buf data to be compressed to output stream
75      * @exception IOException if underlying output stream error
76      **/

77     public void compress(byte[] buf, int offset, int length)
78         throws IOException JavaDoc
79     {
80         int idx;
81         byte c;
82         short index;
83
84         int maxOffset = offset + length;
85         for (idx = offset; idx < maxOffset; ++idx) {
86             c = buf[idx];
87             if ((index = lzss.findCharString(prefix, c)) != -1) {
88                 prefix = index;
89             } else {
90                 bf.writeBits(prefix, numBits);
91                 if (lzss.addCharString(prefix, c) > limit) {
92                     if (numBits == 12) {
93                         bf.writeBits(clearCode, numBits);
94                         lzss.clearTable(codeSize);
95                         numBits = codeSize + 1;
96                     } else {
97                         ++numBits;
98                     }
99
100                     limit = (1 << numBits) - 1;
101                     if (tiffFudge) {
102                         --limit;
103                     }
104                 }
105                 prefix = (short)((short)c & 0xFF);
106             }
107         }
108     }
109
110     /*
111      * Indicate to compressor that no more data to go so write out
112      * any remaining buffered data.
113      *
114      * @exception IOException if underlying output stream error
115      */

116     public void flush() throws IOException JavaDoc {
117         if (prefix != -1) {
118             bf.writeBits(prefix, numBits);
119         }
120         
121         bf.writeBits(endOfInfo, numBits);
122         bf.flush();
123     }
124
125     public void dump(PrintStream JavaDoc out) {
126         lzss.dump(out);
127     }
128 }
129
Popular Tags