private void initDelegate(boolean seekForwardOnly, boolean ignoreMetadata) throws IOException { // JPEGSegmentImageInputStream that filters out/skips bad/unnecessary segments delegate.setInput(imageInput != null ? new JPEGSegmentImageInputStream(new SubImageInputStream(imageInput, Long.MAX_VALUE), new JPEGSegmentStreamWarningDelegate()) : null, seekForwardOnly, ignoreMetadata); }
static Frame read(final int marker, final ImageInputStream data) throws IOException { int length = data.readUnsignedShort(); return read(marker, new SubImageInputStream(data, length), length); }
private ImageReader initReaderDelegate(int compression) throws IOException { ImageReader reader = getImageReaderDelegate(compression); imageInput.seek(pixelOffset); reader.setInput(new SubImageInputStream(imageInput, header.getImageSize())); return reader; }
private ImageReader initPNGReader(final DirectoryEntry pEntry) throws IOException { ImageReader pngReader = getPNGReader(); imageInput.seek(pEntry.getOffset()); // InputStream inputStream = IIOUtil.createStreamAdapter(imageInput, pEntry.getSize()); // ImageInputStream stream = ImageIO.createImageInputStream(inputStream); ImageInputStream stream = new SubImageInputStream(imageInput, pEntry.getSize()); // NOTE: Will throw IOException on later reads if input is not PNG pngReader.setInput(stream); return pngReader; }
private void initIndex(final int imageIndex) throws IOException { checkBounds(imageIndex); if (index != imageIndex) { imageInput.seek(header.getOffset(imageIndex)); index = imageIndex; readerDelegate.setInput(new SubImageInputStream(imageInput, Long.MAX_VALUE)); } }
@Test(expected = IllegalArgumentException.class) public void testCreateNullStream() throws IOException { new SubImageInputStream(null, 1); fail("Expected IllegalArgumentException with null stream"); }
@Test(expected = IllegalArgumentException.class) public void testCreateNegativeLength() throws IOException { new SubImageInputStream(createStream(0), -1); fail("Expected IllegalArgumentException with negative length"); }
@Test public void testWraphBeyondWrappedLength() throws IOException { SubImageInputStream stream = new SubImageInputStream(createStream(5), 6); assertEquals(5, stream.length()); }
@Test public void testReadResetRead() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(32), 16); stream.mark(); byte[] first = new byte[16]; stream.readFully(first); stream.reset(); byte[] second = new byte[16]; stream.readFully(second); assertTrue(Arrays.equals(first, second)); }
@Test public void testPhotoshopResourcePHUT() throws IOException { // Test sample contains non-8BIM resource: PHUT (PhotoDeluxe) try (ImageInputStream stream = ImageIO.createImageInputStream(getResource("/psd/friends-phut-resource.jpg"))) { stream.seek(38); Directory directory = createReader().read(new SubImageInputStream(stream, 298)); assertEquals(9, directory.size()); // 6 8BIM + 2 PHUT + 1 8 BIM } } }
@Test public void testSeekAfterEOF() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(7), 5); stream.seek(6); assertEquals(-1, stream.read()); }
@Test public void testSeekNegative() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(7), 5); try { stream.seek(-2); fail("Expected IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException expected) { assertTrue(expected.getMessage().contains("pos")); } assertEquals(0, stream.getStreamPosition()); }
@Test public void testCreate() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(11), 7); assertEquals(0, stream.getStreamPosition()); assertEquals(7, stream.length()); }
@Test public void testGrape8BIM() throws IOException { ImageInputStream stream = resourceAsIIOStream("/psd/grape_with_path.psd"); // PSD image resources from position 34, length 32598 stream.seek(34); stream = new SubImageInputStream(stream, 32598); Path2D path = Paths.readPath(stream); assertNotNull(path); assertPathEquals(readExpectedPath("/ser/grape-path.ser"), path); }
@Test public void testReadBadDataZeroCount() throws IOException { // This image seems to contain bad Exif. But as other tools are able to read, so should we.. ImageInputStream stream = ImageIO.createImageInputStream(getResource("/jpeg/exif-rgb-thumbnail-bad-exif-kodak-dc210.jpg")); stream.seek(12); Directory directory = createReader().read(new SubImageInputStream(stream, 21674)); assertEquals(22, directory.size()); // Special case: Ascii string with count == 0, not ok according to spec (?), but we'll let it pass assertEquals("", directory.getEntryById(TIFF.TAG_IMAGE_DESCRIPTION).getValue()); }
@Test public void testReadBadDataZeroCount() throws IOException { // This image seems to contain bad Exif. But as other tools are able to read, so should we.. ImageInputStream stream = ImageIO.createImageInputStream(getResource("/jpeg/exif-rgb-thumbnail-bad-exif-kodak-dc210.jpg")); stream.seek(12); Directory directory = createReader().read(new SubImageInputStream(stream, 21674)); assertEquals(22, directory.size()); // Special case: Ascii string with count == 0, not ok according to spec (?), but we'll let it pass assertEquals("", directory.getEntryById(TIFF.TAG_IMAGE_DESCRIPTION).getValue()); }
@Test public void testReadBadDataRationalZeroDenominator() throws IOException { // This image seems to contain bad Exif. But as other tools are able to read, so should we.. ImageInputStream stream = ImageIO.createImageInputStream(getResource("/jpeg/exif-rgb-thumbnail-bad-exif-kodak-dc210.jpg")); stream.seek(12); Directory directory = createReader().read(new SubImageInputStream(stream, 21674)); // Special case: Rational with zero-denominator inside EXIF data Directory exif = (Directory) directory.getEntryById(TIFF.TAG_EXIF_IFD).getValue(); Entry entry = exif.getEntryById(EXIF.TAG_COMPRESSED_BITS_PER_PIXEL); assertNotNull(entry); assertEquals(Rational.NaN, entry.getValue()); }
@Test public void testSeek() throws IOException { ImageInputStream stream = new SubImageInputStream(createStream(7), 5); stream.seek(5); assertEquals(5, stream.getStreamPosition()); stream.seek(1); assertEquals(1, stream.getStreamPosition()); } }
@Test public void testReadBadDirectoryCount() throws IOException { // This image seems to contain bad Exif. But as other tools are able to read, so should we.. ImageInputStream stream = ImageIO.createImageInputStream(getResource("/jpeg/exif-bad-directory-entry-count.jpg")); stream.seek(4424 + 10); Directory directory = createReader().read(new SubImageInputStream(stream, 214 - 6)); assertEquals(7, directory.size()); // TIFF structure says 8, but the last entry isn't there Directory exif = (Directory) directory.getEntryById(TIFF.TAG_EXIF_IFD).getValue(); assertNotNull(exif); assertEquals(3, exif.size()); }
@Test public void testReadBadDirectoryCount() throws IOException { // This image seems to contain bad Exif. But as other tools are able to read, so should we.. ImageInputStream stream = ImageIO.createImageInputStream(getResource("/jpeg/exif-bad-directory-entry-count.jpg")); stream.seek(4424 + 10); Directory directory = createReader().read(new SubImageInputStream(stream, 214 - 6)); assertEquals(7, directory.size()); // TIFF structure says 8, but the last entry isn't there Directory exif = (Directory) directory.getEntryById(TIFF.TAG_EXIF_IFD).getValue(); assertNotNull(exif); assertEquals(3, exif.size()); }