Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ private void interpretStrip(final ImageBuilder imageBuilder, final byte[] bytes,
}
}

static int checkStripSize(final long bytesPerStrip) {
return Allocator.check(bytesPerStrip, Byte.BYTES);
}

@Override
public ImageBuilder readImageData(final Rectangle subImageSpecification, final boolean hasAlpha, final boolean isAlphaPreMultiplied)
throws IOException, ImagingException {
Expand Down Expand Up @@ -297,6 +301,7 @@ public ImageBuilder readImageData(final Rectangle subImageSpecification, final b
final long rowsInThisStrip = Math.min(rowsRemaining, rowsPerStripLong);
final long bytesPerRow = (bitsPerPixel * width + 7) / 8;
final long bytesPerStrip = rowsInThisStrip * bytesPerRow;
final int expectedSize = checkStripSize(bytesPerStrip);
final long pixelsPerStrip = rowsInThisStrip * width;

final byte[] compressed = imageData.getImageData(strip).getData();
Expand All @@ -308,7 +313,7 @@ public ImageBuilder readImageData(final Rectangle subImageSpecification, final b
continue;
}

final byte[] decompressed = decompress(compressed, compression, (int) bytesPerStrip, width, (int) rowsInThisStrip);
final byte[] decompressed = decompress(compressed, compression, expectedSize, width, (int) rowsInThisStrip);

interpretStrip(workingBuilder, decompressed, (int) pixelsPerStrip, yLimit);
}
Expand All @@ -327,13 +332,14 @@ public ImageBuilder readImageData(final Rectangle subImageSpecification, final b
final long rowsInThisStrip = Math.min(rowsRemaining, rowsPerStripLong);
final long bytesPerRow = (bitsPerPixel * width + 7) / 8;
final long bytesPerStrip = rowsInThisStrip * bytesPerRow;
final int expectedSize = checkStripSize(bytesPerStrip);
final long pixelsPerStrip = rowsInThisStrip * width;

final byte[] b = Allocator.byteArray((int) bytesPerStrip);
final byte[] b = Allocator.byteArray(expectedSize);
for (int iPlane = 0; iPlane < 3; iPlane++) {
final int planeStrip = iPlane * nStripsInPlane + strip;
final byte[] compressed = imageData.getImageData(planeStrip).getData();
final byte[] decompressed = decompress(compressed, compression, (int) bytesPerStrip, width, (int) rowsInThisStrip);
final byte[] decompressed = decompress(compressed, compression, expectedSize, width, (int) rowsInThisStrip);
int index = iPlane;
for (final byte element : decompressed) {
b[index] = element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@
package org.apache.commons.imaging.formats.tiff.datareaders;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.apache.commons.imaging.common.AllocationRequestException;
import org.apache.commons.imaging.formats.tiff.constants.TiffPlanarConfiguration;
import org.junit.jupiter.api.Test;

class DataReaderStripsTest {
@Test
void testCheckStripSizeRejectsLongThatWouldOverflowInt() {
assertThrows(AllocationRequestException.class, () -> DataReaderStrips.checkStripSize(28_994_135_625L));
}

@Test
void testCheckStripSizeReturnsIntForValidRequest() {
assertEquals(4096, DataReaderStrips.checkStripSize(4096));
}

@Test
void testApplyPredictor() {
final int[] bitsPerPixel = { 1, 2, 3 };
Expand Down