001: /*
002: * $RCSfile: TIFFLZWUtil.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:48 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.tiff;
046:
047: import java.awt.Rectangle;
048: import java.io.IOException;
049: import javax.imageio.IIOException;
050: import javax.imageio.ImageReader;
051: import com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet;
052: import com.sun.media.imageio.plugins.tiff.TIFFTag;
053:
054: public class TIFFLZWUtil {
055:
056: private static final boolean debug = false;
057:
058: public TIFFLZWUtil() {
059: }
060:
061: byte[] srcData;
062: int srcIndex;
063:
064: byte[] dstData;
065: int dstIndex = 0;
066:
067: byte stringTable[][];
068: int tableIndex, bitsToGet = 9;
069:
070: int predictor, samplesPerPixel;
071: int nextData = 0;
072: int nextBits = 0;
073:
074: private static final int andTable[] = { 511, 1023, 2047, 4095 };
075:
076: public byte[] decode(byte[] data, int predictor,
077: int samplesPerPixel, int width, int height)
078: throws IOException {
079: if (data[0] == (byte) 0x00 && data[1] == (byte) 0x01) {
080: throw new IIOException(
081: "TIFF 5.0-style LZW compression is not supported!");
082: }
083:
084: this .srcData = data;
085: this .srcIndex = 0;
086: this .nextData = 0;
087: this .nextBits = 0;
088:
089: this .dstData = new byte[8192];
090: this .dstIndex = 0;
091:
092: initializeStringTable();
093:
094: int code, oldCode = 0;
095: byte[] string;
096:
097: while ((code = getNextCode()) != 257) {
098: if (code == 256) {
099: initializeStringTable();
100: code = getNextCode();
101: if (code == 257) {
102: break;
103: }
104:
105: writeString(stringTable[code]);
106: oldCode = code;
107: } else {
108: if (code < tableIndex) {
109: string = stringTable[code];
110:
111: writeString(string);
112: addStringToTable(stringTable[oldCode], string[0]);
113: oldCode = code;
114: } else {
115: string = stringTable[oldCode];
116: string = composeString(string, string[0]);
117: writeString(string);
118: addStringToTable(string);
119: oldCode = code;
120: }
121: }
122: }
123:
124: if (predictor == 2) {
125:
126: int count;
127: for (int j = 0; j < height; j++) {
128:
129: count = samplesPerPixel * (j * width + 1);
130:
131: for (int i = samplesPerPixel; i < width
132: * samplesPerPixel; i++) {
133:
134: dstData[count] += dstData[count - samplesPerPixel];
135: count++;
136: }
137: }
138: }
139:
140: byte[] newDstData = new byte[dstIndex];
141: System.arraycopy(dstData, 0, newDstData, 0, dstIndex);
142: return newDstData;
143: }
144:
145: /**
146: * Initialize the string table.
147: */
148: public void initializeStringTable() {
149: stringTable = new byte[4096][];
150:
151: for (int i = 0; i < 256; i++) {
152: stringTable[i] = new byte[1];
153: stringTable[i][0] = (byte) i;
154: }
155:
156: tableIndex = 258;
157: bitsToGet = 9;
158: }
159:
160: private void ensureCapacity(int bytesToAdd) {
161: if (dstIndex + bytesToAdd > dstData.length) {
162: byte[] newDstData = new byte[Math.max(
163: (int) (dstData.length * 1.2f), dstIndex
164: + bytesToAdd)];
165: System.arraycopy(dstData, 0, newDstData, 0, dstData.length);
166: dstData = newDstData;
167: }
168: }
169:
170: /**
171: * Write out the string just uncompressed.
172: */
173: public void writeString(byte string[]) {
174: ensureCapacity(string.length);
175: for (int i = 0; i < string.length; i++) {
176: dstData[dstIndex++] = string[i];
177: }
178: }
179:
180: /**
181: * Add a new string to the string table.
182: */
183: public void addStringToTable(byte oldString[], byte newString) {
184: int length = oldString.length;
185: byte string[] = new byte[length + 1];
186: System.arraycopy(oldString, 0, string, 0, length);
187: string[length] = newString;
188:
189: // Add this new String to the table
190: stringTable[tableIndex++] = string;
191:
192: if (tableIndex == 511) {
193: bitsToGet = 10;
194: } else if (tableIndex == 1023) {
195: bitsToGet = 11;
196: } else if (tableIndex == 2047) {
197: bitsToGet = 12;
198: }
199: }
200:
201: /**
202: * Add a new string to the string table.
203: */
204: public void addStringToTable(byte string[]) {
205: // Add this new String to the table
206: stringTable[tableIndex++] = string;
207:
208: if (tableIndex == 511) {
209: bitsToGet = 10;
210: } else if (tableIndex == 1023) {
211: bitsToGet = 11;
212: } else if (tableIndex == 2047) {
213: bitsToGet = 12;
214: }
215: }
216:
217: /**
218: * Append <code>newString</code> to the end of <code>oldString</code>.
219: */
220: public byte[] composeString(byte oldString[], byte newString) {
221: int length = oldString.length;
222: byte string[] = new byte[length + 1];
223: System.arraycopy(oldString, 0, string, 0, length);
224: string[length] = newString;
225:
226: return string;
227: }
228:
229: // Returns the next 9, 10, 11 or 12 bits
230: public int getNextCode() {
231: // Attempt to get the next code. The exception is caught to make
232: // this robust to cases wherein the EndOfInformation code has been
233: // omitted from a strip. Examples of such cases have been observed
234: // in practice.
235:
236: try {
237: nextData = (nextData << 8) | (srcData[srcIndex++] & 0xff);
238: nextBits += 8;
239:
240: if (nextBits < bitsToGet) {
241: nextData = (nextData << 8)
242: | (srcData[srcIndex++] & 0xff);
243: nextBits += 8;
244: }
245:
246: int code = (nextData >> (nextBits - bitsToGet))
247: & andTable[bitsToGet - 9];
248: nextBits -= bitsToGet;
249:
250: return code;
251: } catch (ArrayIndexOutOfBoundsException e) {
252: // Strip not terminated as expected: return EndOfInformation code.
253: return 257;
254: }
255: }
256: }
|