From 730c0758db4a48907875916093b6c2298a95a282 Mon Sep 17 00:00:00 2001 From: p002308A Date: Thu, 29 Aug 2024 12:13:21 -0400 Subject: [PATCH] Simplified the checking of the bit size. Removed the heavy math equation done each time, to a simpler one done rarely. This give a performance boost to the entire LZW decoding method of about 50%. --- .../nga/tiff/compression/LZWCompression.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) 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++; + } + } } /**