diff --git a/src/main/java/mil/nga/tiff/compression/LZWCompression.java b/src/main/java/mil/nga/tiff/compression/LZWCompression.java index ea23c62..30bdd62 100644 --- a/src/main/java/mil/nga/tiff/compression/LZWCompression.java +++ b/src/main/java/mil/nga/tiff/compression/LZWCompression.java @@ -48,6 +48,11 @@ public class LZWCompression implements CompressionDecoder, CompressionEncoder { */ private int byteLength; + /** + * The next code to cause a bit size increase + */ + private int nextBitSizeIncrease; + /** * Any remaining code info from previous code reads. */ @@ -146,6 +151,7 @@ private void initializeTable() { table[i] = new byte[]{ (byte)i }; } maxCode = 257; + nextBitSizeIncrease = (2<<(MIN_BITS-1)) - 2;//510 byteLength = MIN_BITS; } @@ -153,9 +159,14 @@ private void initializeTable() { * Check the byte length and increase if needed */ private void checkByteLength() { - if (byteLength < MAX_BITS && maxCode >= Math.pow(2, byteLength) - 2) { - byteLength++; - } + if( maxCode == nextBitSizeIncrease ) + { + nextBitSizeIncrease = nextBitSizeIncrease*2 + 2; + if( byteLength < MAX_BITS ) + { + byteLength++; + } + } } /**