Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tiff file lzw compression bit size needs to be capped at 12 #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/mil/nga/tiff/compression/LZWCompression.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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++;
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/mil/nga/tiff/TiffReadTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mil.nga.tiff;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.io.File;
Expand All @@ -8,6 +9,7 @@
import org.junit.Test;

import junit.framework.TestCase;
import mil.nga.tiff.util.TiffConstants;
import mil.nga.tiff.util.TiffException;

/**
Expand Down Expand Up @@ -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
}

}
5 changes: 5 additions & 0 deletions src/test/java/mil/nga/tiff/TiffTestConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

}
Binary file added src/test/resources/lzw12bitmax.tiff
Binary file not shown.