Added test for PICTImageReader to makes sure JPEGs or other images with PICT magic are recognized.

Better Exception handling.
This commit is contained in:
Harald Kuhr 2009-09-08 12:59:12 +02:00
parent a493cebe13
commit 79ca02c8b0
4 changed files with 30 additions and 12 deletions

View File

@ -204,7 +204,7 @@ public abstract class ImageReaderAbstractTestCase<T extends ImageReader> extends
reader.setInput(null); reader.setInput(null);
} }
public void testRead() throws IOException { public void testRead() {
ImageReader reader = createReader(); ImageReader reader = createReader();
for (TestData data : getTestData()) { for (TestData data : getTestData()) {
// TODO: Is it required to call reset before setInput? // TODO: Is it required to call reset before setInput?
@ -1283,7 +1283,7 @@ public abstract class ImageReaderAbstractTestCase<T extends ImageReader> extends
return getClass().getResource(pName); return getClass().getResource(pName);
} }
static final protected class TestData { static final protected class TestData {
private final Object mInput; private final Object mInput;
private final List<Dimension> mSizes; private final List<Dimension> mSizes;
private final List<BufferedImage> mImages; private final List<BufferedImage> mImages;

View File

@ -191,7 +191,7 @@ public class PICTImageReader extends ImageReaderBase {
// Get frame at 72 dpi // Get frame at 72 dpi
// NOTE: These are not pixel sizes! // NOTE: These are not pixel sizes!
// Need sto be multiplied with hRes/screenResolution and vRes/screenResolution // Need to be multiplied with hRes/screenResolution and vRes/screenResolution
int y = pStream.readUnsignedShort(); int y = pStream.readUnsignedShort();
int x = pStream.readUnsignedShort(); int x = pStream.readUnsignedShort();
int h = pStream.readUnsignedShort(); int h = pStream.readUnsignedShort();
@ -1664,10 +1664,18 @@ public class PICTImageReader extends ImageReaderBase {
throw e; throw e;
} }
catch (EOFException e) { catch (EOFException e) {
throw new IIOException("Error in PICT format: Unexpected end of File", e); String pos;
try {
pos = String.format("position %d", mImageInput.getStreamPosition());
}
catch (IOException ignore) {
pos = "unknown position";
}
throw new IIOException(String.format("Error in PICT format: Unexpected end of File at %s", pos), e);
} }
catch (IOException e) { catch (IOException e) {
throw new IIOException("Error in PICT format: " + e.getMessage(), e); throw new IIOException(String.format("Error in PICT format: %s", e.getMessage()), e);
} }
} }
@ -2528,7 +2536,7 @@ public class PICTImageReader extends ImageReaderBase {
/* /*
* Read a long comment from the stream. * Read a long comment from the stream.
*/ */
private void readLongComment(DataInput pStream) throws IOException { private void readLongComment(final DataInput pStream) throws IOException {
// Comment kind and data byte count // Comment kind and data byte count
pStream.readShort(); pStream.readShort();
@ -2604,12 +2612,12 @@ public class PICTImageReader extends ImageReaderBase {
// TODO: Might need to clear background // TODO: Might need to clear background
g.setTransform(AffineTransform.getScaleInstance(mScreenImageXRatio, mScreenImageYRatio)); g.setTransform(AffineTransform.getScaleInstance(mScreenImageXRatio, mScreenImageYRatio));
try { // try {
drawOnto(g); drawOnto(g);
} // }
catch (IOException e) { // catch (IOException e) {
e.printStackTrace(); // e.printStackTrace();
} // }
} }
finally { finally {
g.dispose(); g.dispose();

View File

@ -4,6 +4,7 @@ import com.twelvemonkeys.imageio.util.ImageReaderAbstractTestCase;
import javax.imageio.spi.ImageReaderSpi; import javax.imageio.spi.ImageReaderSpi;
import java.awt.*; import java.awt.*;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -18,7 +19,7 @@ public class PICTImageReaderTestCase extends ImageReaderAbstractTestCase<PICTIma
static ImageReaderSpi sProvider = new PICTImageReaderSpi(); static ImageReaderSpi sProvider = new PICTImageReaderSpi();
// TODO: Should also test the cliboard format (without 512 byte header) // TODO: Should also test the clipboard format (without 512 byte header)
protected List<TestData> getTestData() { protected List<TestData> getTestData() {
return Arrays.asList( return Arrays.asList(
new TestData(getClassLoaderResource("/pict/test.pct"), new Dimension(300, 200)), new TestData(getClassLoaderResource("/pict/test.pct"), new Dimension(300, 200)),
@ -57,4 +58,13 @@ public class PICTImageReaderTestCase extends ImageReaderAbstractTestCase<PICTIma
protected List<String> getMIMETypes() { protected List<String> getMIMETypes() {
return Arrays.asList("image/pict", "image/x-pict"); return Arrays.asList("image/pict", "image/x-pict");
} }
public void testProviderNotMatchJPEG() throws IOException {
// This JPEG contains PICT magic bytes at locations a PICT would normally have them.
// We should not claim to be able read it.
assertFalse(sProvider.canDecodeInput(new TestData(
getClassLoaderResource("/jpeg/R-7439-1151526181.jpeg"),
new Dimension(386, 396)
)));
}
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB