Skip to content

Commit 5af8737

Browse files
rolfhowarthharaldk
andauthored
Use signed arithmetic when reading rectangle (#1131)
* Use signed arithmetic when reading rectangle An operation such as DirectBitsRect can fail if the origin is -ve and we're applying a screen image ratio. See for example P564B1400.pict * Fix copy/paste error in testNegativeOrigin() --------- Co-authored-by: Harald Kuhr <harald.kuhr@gmail.com>
1 parent defadbb commit 5af8737

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

imageio/imageio-pict/src/main/java/com/twelvemonkeys/imageio/plugins/pict/PICTImageReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2406,10 +2406,10 @@ private void readOpBits(ImageInputStream pStream, boolean hasRegion) throws IOEx
24062406
* @throws IOException if an I/O error occurs while reading the image.
24072407
*/
24082408
private void readRectangle(DataInput pStream, Rectangle pDestRect) throws IOException {
2409-
int y = pStream.readUnsignedShort();
2410-
int x = pStream.readUnsignedShort();
2411-
int h = pStream.readUnsignedShort();
2412-
int w = pStream.readUnsignedShort();
2409+
int y = pStream.readShort();
2410+
int x = pStream.readShort();
2411+
int h = pStream.readShort();
2412+
int w = pStream.readShort();
24132413

24142414
pDestRect.setLocation(getXPtCoord(x), getYPtCoord(y));
24152415
pDestRect.setSize(getXPtCoord(w - x), getYPtCoord(h - y));

imageio/imageio-pict/src/test/java/com/twelvemonkeys/imageio/plugins/pict/PICTImageReaderTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ protected List<TestData> getTestData() {
8484
new TestData(getClassLoaderResource("/pict/FC10.PCT"), new Dimension(2265, 2593)),
8585
// 1000 DPI with bounding box not matching DPI
8686
new TestData(getClassLoaderResource("/pict/oom.pict"), new Dimension(1713, 1263)),
87+
new TestData(getClassLoaderResource("/pict/P564B1400.pict"), new Dimension(1745, 1022)),
8788
new TestData(getClassLoaderResource("/pict/cow.pict"), new Dimension(787, 548)),
8889
new TestData(getClassLoaderResource("/pict/CatDV==2.0=1=.pict"), new Dimension(375, 165)),
8990
new TestData(getClassLoaderResource("/pict/Picture14.pict"), new Dimension(404, 136)),
@@ -252,6 +253,23 @@ public void testDataV1CopyBits() throws IOException {
252253
reader.read(0);
253254
}
254255

256+
@Test
257+
public void testNegativeOrigin() throws IOException {
258+
PICTImageReader reader = createReader();
259+
260+
try (ImageInputStream stream = ImageIO.createImageInputStream(getClassLoaderResource("/pict/P564B1400.pict"))) {
261+
reader.setInput(stream);
262+
// This file has a DirectBitsRect opcode with a destination of (0,-1) which wraps to 65535 if we read
263+
// it using unsigned arithmetic
264+
BufferedImage image = reader.read(0, null);
265+
assertRGBEquals("RGB values differ", 0xfffcfcfc, image.getRGB(10, 10), 1); // was transparent 00ffffff
266+
assertRGBEquals("RGB values differ", 0xffe6e6e6, image.getRGB(100, 500), 1);
267+
}
268+
finally {
269+
reader.dispose();
270+
}
271+
}
272+
255273
@Test
256274
public void testFrameBoundsIssue() throws IOException {
257275
PICTImageReader reader = createReader();
Binary file not shown.

0 commit comments

Comments
 (0)