TMI-23: Better handling of SOS segment (variable length). Now treats the rest of the stream after SOS as single segment. Not really ideal, but gives better performance than scanning for EOI...

This commit is contained in:
Harald Kuhr 2012-06-21 16:08:03 +02:00
parent c2245a503d
commit 73a880a358
4 changed files with 40 additions and 2 deletions

View File

@ -101,8 +101,18 @@ final class JPEGSegmentImageInputStream extends ImageInputStreamImpl {
segments.add(segment); segments.add(segment);
} }
else { else {
int length = stream.readUnsignedShort(); // Length including length field itself long length;
segment = new Segment(marker, realPosition, segment.end(), 2 + length);
if (marker == JPEG.SOS) {
// Treat rest of stream as a single segment (scanning for EOI is too much work)
length = Long.MAX_VALUE - realPosition;
}
else {
// Length including length field itself
length = stream.readUnsignedShort() + 2;
}
segment = new Segment(marker, realPosition, segment.end(), length);
segments.add(segment); segments.add(segment);
} }

View File

@ -276,6 +276,22 @@ public class JPEGImageReaderTest extends ImageReaderAbstractTestCase<JPEGImageRe
// TODO: Need to test colors! // TODO: Need to test colors!
} }
@Test
public void testEOFSOSSegment() throws IOException {
// Regression...
JPEGImageReader reader = createReader();
reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/eof-sos-segment-bug.jpg")));
assertEquals(266, reader.getWidth(0));
assertEquals(400, reader.getHeight(0));
BufferedImage image = reader.read(0);
assertNotNull(image);
assertEquals(266, image.getWidth());
assertEquals(400, image.getHeight());
}
@Test @Test
public void testInvalidICCSingleChunkBadSequence() throws IOException { public void testInvalidICCSingleChunkBadSequence() throws IOException {
// Regression // Regression

View File

@ -123,4 +123,16 @@ public class JPEGSegmentImageInputStreamTest {
// And thus, no XMP, no ICC_PROFILE or other segments // And thus, no XMP, no ICC_PROFILE or other segments
} }
@Test
public void testEOFSOSSegmentBug() throws IOException {
ImageInputStream stream = new JPEGSegmentImageInputStream(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/eof-sos-segment-bug.jpg")));
long length = 0;
while (stream.read() != -1) {
length++;
}
assertEquals(9299l, length); // Sanity check: same as file size
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.1 KiB