|
Added
Link Here
|
| 1 |
package org.eclipse.swt.internal.image; |
| 2 |
|
| 3 |
/* |
| 4 |
|
| 5 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 6 |
contributor license agreements. See the NOTICE file distributed with |
| 7 |
this work for additional information regarding copyright ownership. |
| 8 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 9 |
(the "License"); you may not use this file except in compliance with |
| 10 |
the License. You may obtain a copy of the License at |
| 11 |
|
| 12 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
|
| 14 |
Unless required by applicable law or agreed to in writing, software |
| 15 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
See the License for the specific language governing permissions and |
| 18 |
limitations under the License. |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* A class for performing LZW decoding. |
| 24 |
* |
| 25 |
* @version $Id$ |
| 26 |
*/ |
| 27 |
public class TIFFLZWDecoder { |
| 28 |
|
| 29 |
byte[][] stringTable; |
| 30 |
byte[] data = null; |
| 31 |
byte[] uncompData; |
| 32 |
int tableIndex, bitsToGet = 9; |
| 33 |
int bytePointer, bitPointer; |
| 34 |
int dstIndex; |
| 35 |
int w, h; |
| 36 |
int predictor, samplesPerPixel; |
| 37 |
int nextData = 0; |
| 38 |
int nextBits = 0; |
| 39 |
|
| 40 |
int[] andTable = { |
| 41 |
511, |
| 42 |
1023, |
| 43 |
2047, |
| 44 |
4095 |
| 45 |
}; |
| 46 |
|
| 47 |
public TIFFLZWDecoder(int w, int predictor, int samplesPerPixel) { |
| 48 |
this.w = w; |
| 49 |
this.predictor = predictor; |
| 50 |
this.samplesPerPixel = samplesPerPixel; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Method to decode LZW compressed data. |
| 55 |
* |
| 56 |
* @param data The compressed data. |
| 57 |
* @param uncompData Array to return the uncompressed data in. |
| 58 |
* @param h The number of rows the compressed data contains. |
| 59 |
*/ |
| 60 |
public int decode(byte[] data, byte[] uncompData, int destIndex, int h) { |
| 61 |
|
| 62 |
if(data[0] == (byte)0x00 && data[1] == (byte)0x01) { |
| 63 |
throw new UnsupportedOperationException("TIFFLZWDecoder0"); |
| 64 |
} |
| 65 |
|
| 66 |
initializeStringTable(); |
| 67 |
|
| 68 |
this.data = data; |
| 69 |
this.h = h; |
| 70 |
this.uncompData = uncompData; |
| 71 |
|
| 72 |
// Initialize pointers |
| 73 |
bytePointer = 0; |
| 74 |
bitPointer = 0; |
| 75 |
dstIndex = destIndex; |
| 76 |
|
| 77 |
|
| 78 |
nextData = 0; |
| 79 |
nextBits = 0; |
| 80 |
|
| 81 |
int code, oldCode = 0; |
| 82 |
byte[] string; |
| 83 |
|
| 84 |
while ( ((code = getNextCode()) != 257) && |
| 85 |
dstIndex != uncompData.length) { |
| 86 |
|
| 87 |
if (code == 256) { |
| 88 |
|
| 89 |
initializeStringTable(); |
| 90 |
code = getNextCode(); |
| 91 |
|
| 92 |
if (code == 257) { |
| 93 |
break; |
| 94 |
} |
| 95 |
|
| 96 |
writeString(stringTable[code]); |
| 97 |
oldCode = code; |
| 98 |
|
| 99 |
} else { |
| 100 |
|
| 101 |
if (code < tableIndex) { |
| 102 |
|
| 103 |
string = stringTable[code]; |
| 104 |
|
| 105 |
writeString(string); |
| 106 |
addStringToTable(stringTable[oldCode], string[0]); |
| 107 |
oldCode = code; |
| 108 |
|
| 109 |
} else { |
| 110 |
|
| 111 |
string = stringTable[oldCode]; |
| 112 |
string = composeString(string, string[0]); |
| 113 |
writeString(string); |
| 114 |
addStringToTable(string); |
| 115 |
oldCode = code; |
| 116 |
} |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
// Horizontal Differencing Predictor |
| 123 |
if (predictor == 2) { |
| 124 |
|
| 125 |
int count; |
| 126 |
for (int j = 0; j < h; j++) { |
| 127 |
|
| 128 |
count = samplesPerPixel * (j * w + 1)+ destIndex; |
| 129 |
|
| 130 |
for (int i = samplesPerPixel; i < w * samplesPerPixel; i++) { |
| 131 |
|
| 132 |
uncompData[count] += uncompData[count - samplesPerPixel]; |
| 133 |
count++; |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return dstIndex; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Initialize the string table. |
| 144 |
*/ |
| 145 |
public void initializeStringTable() { |
| 146 |
|
| 147 |
stringTable = new byte[4096][]; |
| 148 |
|
| 149 |
for (int i=0; i<256; i++) { |
| 150 |
stringTable[i] = new byte[1]; |
| 151 |
stringTable[i][0] = (byte)i; |
| 152 |
} |
| 153 |
|
| 154 |
tableIndex = 258; |
| 155 |
bitsToGet = 9; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Write out the string just uncompressed. |
| 160 |
*/ |
| 161 |
public void writeString(byte[] string) { |
| 162 |
|
| 163 |
for (int i=0; i<string.length; i++) { |
| 164 |
uncompData[dstIndex++] = string[i]; |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Add a new string to the string table. |
| 170 |
*/ |
| 171 |
public void addStringToTable(byte[] oldString, byte newString) { |
| 172 |
int length = oldString.length; |
| 173 |
byte[] string = new byte[length + 1]; |
| 174 |
System.arraycopy(oldString, 0, string, 0, length); |
| 175 |
string[length] = newString; |
| 176 |
|
| 177 |
// Add this new String to the table |
| 178 |
stringTable[tableIndex++] = string; |
| 179 |
|
| 180 |
if (tableIndex == 511) { |
| 181 |
bitsToGet = 10; |
| 182 |
} else if (tableIndex == 1023) { |
| 183 |
bitsToGet = 11; |
| 184 |
} else if (tableIndex == 2047) { |
| 185 |
bitsToGet = 12; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
/** |
| 190 |
* Add a new string to the string table. |
| 191 |
*/ |
| 192 |
public void addStringToTable(byte[] string) { |
| 193 |
|
| 194 |
// Add this new String to the table |
| 195 |
stringTable[tableIndex++] = string; |
| 196 |
|
| 197 |
if (tableIndex == 511) { |
| 198 |
bitsToGet = 10; |
| 199 |
} else if (tableIndex == 1023) { |
| 200 |
bitsToGet = 11; |
| 201 |
} else if (tableIndex == 2047) { |
| 202 |
bitsToGet = 12; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Append <code>newString</code> to the end of <code>oldString</code>. |
| 208 |
*/ |
| 209 |
public byte[] composeString(byte[] oldString, byte newString) { |
| 210 |
int length = oldString.length; |
| 211 |
byte[] string = new byte[length + 1]; |
| 212 |
System.arraycopy(oldString, 0, string, 0, length); |
| 213 |
string[length] = newString; |
| 214 |
|
| 215 |
return string; |
| 216 |
} |
| 217 |
|
| 218 |
// Returns the next 9, 10, 11 or 12 bits |
| 219 |
public int getNextCode() { |
| 220 |
// Attempt to get the next code. The exception is caught to make |
| 221 |
// this robust to cases wherein the EndOfInformation code has been |
| 222 |
// omitted from a strip. Examples of such cases have been observed |
| 223 |
// in practice. |
| 224 |
try { |
| 225 |
nextData = (nextData << 8) | (data[bytePointer++] & 0xff); |
| 226 |
nextBits += 8; |
| 227 |
|
| 228 |
if (nextBits < bitsToGet) { |
| 229 |
nextData = (nextData << 8) | (data[bytePointer++] & 0xff); |
| 230 |
nextBits += 8; |
| 231 |
} |
| 232 |
|
| 233 |
int code = |
| 234 |
(nextData >> (nextBits - bitsToGet)) & andTable[bitsToGet-9]; |
| 235 |
nextBits -= bitsToGet; |
| 236 |
|
| 237 |
return code; |
| 238 |
} catch(ArrayIndexOutOfBoundsException e) { |
| 239 |
// Strip not terminated as expected: return EndOfInformation code. |
| 240 |
return 257; |
| 241 |
} |
| 242 |
} |
| 243 |
} |