diff --git a/src/main/java/mil/nga/tiff/compression/LZWCompression.java b/src/main/java/mil/nga/tiff/compression/LZWCompression.java index a75e2ec..d8f5bac 100644 --- a/src/main/java/mil/nga/tiff/compression/LZWCompression.java +++ b/src/main/java/mil/nga/tiff/compression/LZWCompression.java @@ -38,6 +38,11 @@ public class LZWCompression implements CompressionDecoder, CompressionEncoder { */ private static final int MIN_BITS = 9; + /** + * Max bits + */ + private static final int MAX_BITS = 12; + /** * Table entries */ @@ -154,7 +159,7 @@ private void initializeTable() { * Check the byte length and increase if needed */ private void checkByteLength() { - if (maxCode >= Math.pow(2, byteLength) - 2) { + if (byteLength < MAX_BITS && maxCode >= Math.pow(2, byteLength) - 2) { byteLength++; } } diff --git a/src/test/java/mil/nga/tiff/TiffReadTest.java b/src/test/java/mil/nga/tiff/TiffReadTest.java index 29ec115..2f1ccc5 100644 --- a/src/test/java/mil/nga/tiff/TiffReadTest.java +++ b/src/test/java/mil/nga/tiff/TiffReadTest.java @@ -1,5 +1,6 @@ package mil.nga.tiff; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import java.io.File; @@ -8,6 +9,7 @@ import org.junit.Test; import junit.framework.TestCase; +import mil.nga.tiff.util.TiffConstants; import mil.nga.tiff.util.TiffException; /** @@ -373,4 +375,25 @@ public void testFloat32VsLZWPredictorFloatingPoint() throws IOException { } + /** + * Test the TIFF file where the LZW bit size requires the cap of 12. + * + * @throws IOException + * upon error + */ + @Test + public void testLzw12BitMax() throws IOException { + + File lzw12BitMaxFile = TiffTestUtils + .getTestFile(TiffTestConstants.FILE_LZW_12_BIT_MAX); + TIFFImage image = TiffReader.readTiff(lzw12BitMaxFile); + assertEquals( 1, image.getFileDirectories().size() ); + FileDirectory dir = image.getFileDirectory(); + assertEquals( TiffConstants.COMPRESSION_LZW, dir.getCompression().intValue() ); + assertEquals( 555, dir.getImageWidth().intValue() ); + assertEquals( 555, dir.getImageHeight().intValue() ); + + dir.readRasters();//this will throw a parsing exception if invalid + } + } diff --git a/src/test/java/mil/nga/tiff/TiffTestConstants.java b/src/test/java/mil/nga/tiff/TiffTestConstants.java index 710c549..2684fad 100644 --- a/src/test/java/mil/nga/tiff/TiffTestConstants.java +++ b/src/test/java/mil/nga/tiff/TiffTestConstants.java @@ -92,4 +92,9 @@ public class TiffTestConstants { */ public static final String FILE_LZW_PREDICTOR_FLOATING = "lzw_predictor_floating.tiff"; + /** + * LZW 12 bit max TIFF test file + */ + public static final String FILE_LZW_12_BIT_MAX = "lzw12bitmax.tiff"; + } diff --git a/src/test/resources/lzw12bitmax.tiff b/src/test/resources/lzw12bitmax.tiff new file mode 100644 index 0000000..f51f2d1 Binary files /dev/null and b/src/test/resources/lzw12bitmax.tiff differ