Skip to content

Commit

Permalink
Simplified the checking of the bit size.
Browse files Browse the repository at this point in the history
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%.
  • Loading branch information
p002308A authored and p002308A committed Aug 29, 2024
1 parent ae183e5 commit 730c075
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/main/java/mil/nga/tiff/compression/LZWCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -146,16 +151,22 @@ private void initializeTable() {
table[i] = new byte[]{ (byte)i };
}
maxCode = 257;
nextBitSizeIncrease = (2<<(MIN_BITS-1)) - 2;//510
byteLength = MIN_BITS;
}

/**
* 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++;
}
}
}

/**
Expand Down

0 comments on commit 730c075

Please sign in to comment.