Utility method for decoding an LZW-compressed image strip. Adapted from the TIFF 6.0 Specification: http://partners.adobe.com/asn/developer/pdfs/tn/TIFF6.pdf (page 61) Author: Curtis Rueden (ctrueden at wisc.edu)
(byte[] input)
| 986 | * Author: Curtis Rueden (ctrueden at wisc.edu) |
| 987 | */ |
| 988 | public byte[] lzwUncompress(byte[] input) { |
| 989 | if (input==null || input.length==0) |
| 990 | return input; |
| 991 | byte[][] symbolTable = new byte[16384][1]; |
| 992 | int bitsToRead = 9; |
| 993 | int nextSymbol = 258; |
| 994 | int code; |
| 995 | int oldCode = -1; |
| 996 | ByteVector out = new ByteVector(8192); |
| 997 | BitBuffer bb = new BitBuffer(input); |
| 998 | byte[] byteBuffer1 = new byte[16]; |
| 999 | byte[] byteBuffer2 = new byte[16]; |
| 1000 | |
| 1001 | while (out.size()<byteCount) { |
| 1002 | code = bb.getBits(bitsToRead); |
| 1003 | if (code==EOI_CODE || code==-1) |
| 1004 | break; |
| 1005 | if (code==CLEAR_CODE) { |
| 1006 | // initialize symbol table |
| 1007 | for (int i = 0; i < 256; i++) |
| 1008 | symbolTable[i][0] = (byte)i; |
| 1009 | nextSymbol = 258; |
| 1010 | bitsToRead = 9; |
| 1011 | code = bb.getBits(bitsToRead); |
| 1012 | if (code==EOI_CODE || code==-1) |
| 1013 | break; |
| 1014 | out.add(symbolTable[code]); |
| 1015 | oldCode = code; |
| 1016 | } else { |
| 1017 | if (oldCode==-1) oldCode=0; |
| 1018 | if (code<nextSymbol) { |
| 1019 | // code is in table |
| 1020 | out.add(symbolTable[code]); |
| 1021 | // add string to table |
| 1022 | ByteVector symbol = new ByteVector(byteBuffer1); |
| 1023 | symbol.add(symbolTable[oldCode]); |
| 1024 | symbol.add(symbolTable[code][0]); |
| 1025 | symbolTable[nextSymbol] = symbol.toByteArray(); //** |
| 1026 | oldCode = code; |
| 1027 | nextSymbol++; |
| 1028 | } else { |
| 1029 | // out of table |
| 1030 | ByteVector symbol = new ByteVector(byteBuffer2); |
| 1031 | symbol.add(symbolTable[oldCode]); |
| 1032 | symbol.add(symbolTable[oldCode][0]); |
| 1033 | byte[] outString = symbol.toByteArray(); |
| 1034 | out.add(outString); |
| 1035 | symbolTable[nextSymbol] = outString; //** |
| 1036 | oldCode = code; |
| 1037 | nextSymbol++; |
| 1038 | } |
| 1039 | if (nextSymbol == 511) { bitsToRead = 10; } |
| 1040 | if (nextSymbol == 1023) { bitsToRead = 11; } |
| 1041 | if (nextSymbol == 2047) { bitsToRead = 12; } |
| 1042 | } |
| 1043 | } |
| 1044 | return out.toByteArray(); |
| 1045 | } |
no test coverage detected