public ImageInputStream getInputStream() { try { ImageInputStream stream = ImageIO.createImageInputStream(input); assertNotNull("Could not create ImageInputStream for input: " + input, stream); return stream; } catch (IOException e) { failBecause("Could not create ImageInputStream for input: " + input, e); } return null; }
@Test public void testProviderCanReadNull() { boolean canRead = false; try { canRead = createProvider().canDecodeInput(null); } catch (IllegalArgumentException ignore) { } catch (RuntimeException e) { failBecause("RuntimeException other than IllegalArgumentException thrown", e); } catch (IOException e) { failBecause("Could not test data for read", e); } assertFalse("ImageReader can read null input", canRead); }
@Test public void testReadWithSourceRegionParam() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(0, 0, 10, 10)); BufferedImage image = null; try { image = reader.read(0, param); } catch (IOException e) { failBecause("Image could not be read", e); } assertNotNull("Image was null!", image); assertEquals("Read image has wrong width: " + image.getWidth(), 10, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), 10, image.getHeight()); reader.dispose(); }
@Test public void testReadWithNullParam() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); BufferedImage image = null; try { image = reader.read(0, null); } catch (IOException e) { failBecause("Image could not be read", e); } assertNotNull("Image was null!", image); assertEquals("Read image has wrong width: " + image.getWidth(), data.getDimension(0).width, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), data.getDimension(0).height, image.getHeight()); reader.dispose(); }
@Test public void testReadWithNewParam() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); BufferedImage image = null; try { image = reader.read(0, new ImageReadParam()); } catch (IOException e) { failBecause("Image could not be read", e); } assertNotNull("Image was null!", image); assertEquals("Read image has wrong width: " + image.getWidth(), data.getDimension(0).width, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), data.getDimension(0).height, image.getHeight()); reader.dispose(); }
@Test public void testReadAsRenderedImage() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); RenderedImage image = null; try { image = reader.readAsRenderedImage(0, null); } catch (IOException e) { failBecause("Image could not be read", e); } assertNotNull("Image was null!", image); assertEquals("Read image has wrong width: " + image.getWidth(), data.getDimension(0).width, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), data.getDimension(0).height, image.getHeight()); reader.dispose(); }
@Test public void testReadWithSizeParam() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); if (param.canSetSourceRenderSize()) { param.setSourceRenderSize(new Dimension(10, 10)); BufferedImage image = null; try { image = reader.read(0, param); } catch (IOException e) { failBecause("Image could not be read", e); } assertNotNull("Image was null!", image); assertEquals("Read image has wrong width: " + image.getWidth(), 10, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), 10, image.getHeight()); } reader.dispose(); }
@Test public void testReadWithDefaultParam() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); BufferedImage image = null; try { image = reader.read(0, reader.getDefaultReadParam()); } catch (IOException e) { failBecause("Image could not be read", e); } assertNotNull("Image was null!", image); assertEquals("Read image has wrong width: " + image.getWidth(), data.getDimension(0).width, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), data.getDimension(0).height, image.getHeight()); reader.dispose(); }
@Test public void testReadAsRenderedImageWithDefaultParam() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); RenderedImage image = null; try { image = reader.readAsRenderedImage(0, reader.getDefaultReadParam()); } catch (IOException e) { failBecause("Image could not be read", e); } assertNotNull("Image was null!", image); assertEquals("Read image has wrong width: " + image.getWidth(), data.getDimension(0).width, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), data.getDimension(0).height, image.getHeight()); reader.dispose(); }
@Test public void testReadIndexNegative() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); BufferedImage image = null; try { image = reader.read(-1); fail("Read image with illegal index"); } catch (IndexOutOfBoundsException ignore) { } catch (IOException e) { failBecause("Image could not be read", e); } assertNull(image); reader.dispose(); }
@Test public void testReadIndexOutOfBounds() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); BufferedImage image = null; try { image = reader.read(Integer.MAX_VALUE); // TODO: This might actually not throw exception... fail("Read image with index out of bounds"); } catch (IndexOutOfBoundsException ignore) { } catch (IOException e) { failBecause("Image could not be read", e); } assertNull(image); reader.dispose(); }
@Test public void testReadAsRenderedImageIndexNegative() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); RenderedImage image = null; try { image = reader.readAsRenderedImage(-1, reader.getDefaultReadParam()); fail("Read image with illegal index"); } catch (IndexOutOfBoundsException expected) { // Ignore } catch (IOException e) { failBecause("Image could not be read", e); } assertNull(image); reader.dispose(); }
@Test public void testReadIndexNegativeWithParam() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); BufferedImage image = null; try { image = reader.read(-1, reader.getDefaultReadParam()); fail("Read image with illegal index"); } catch (IndexOutOfBoundsException ignore) { } catch (IOException e) { failBecause("Image could not be read", e); } assertNull(image); reader.dispose(); }
@Test public void testReadIndexOutOfBoundsWithParam() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); BufferedImage image = null; try { image = reader.read(Short.MAX_VALUE, reader.getDefaultReadParam()); fail("Read image with index out of bounds"); } catch (IndexOutOfBoundsException ignore) { } catch (IOException e) { failBecause("Image could not be read", e); } assertNull(image); reader.dispose(); }
@Test public void testSetDestination() throws IOException { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); Iterator<ImageTypeSpecifier> types = reader.getImageTypes(0); while (types.hasNext()) { ImageTypeSpecifier type = types.next(); BufferedImage destination = type.createBufferedImage(50, 50); param.setDestination(destination); BufferedImage result = null; try { result = reader.read(0, param); } catch (Exception e) { failBecause("Could not read " + data.getInput() + " with explicit destination " + destination, e); } assertSame(destination, result); } reader.dispose(); }
@Test public void testReadWithSubsampleParamDimensions() { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(5, 5, 0, 0); BufferedImage image = null; try { image = reader.read(0, param); } catch (IOException e) { failBecause("Image could not be read", e); } assertNotNull("Image was null!", image); assertEquals("Read image has wrong width: ", (data.getDimension(0).width + 4) / 5, image.getWidth()); assertEquals("Read image has wrong height: ", (data.getDimension(0).height + 4) / 5, image.getHeight()); reader.dispose(); }
@Test public void testReadWithSubsampleParamPixels() throws IOException { ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); BufferedImage image = null; BufferedImage subsampled = null; try { image = reader.read(0, param); param.setSourceSubsampling(2, 2, 0, 0); subsampled = reader.read(0, param); } catch (IOException e) { failBecause("Image could not be read", e); } assertSubsampledImageDataEquals("Subsampled image data does not match expected", image, subsampled, param); reader.dispose(); }
@Test public void testReadNoInput() { ImageReader reader = createReader(); // Do not set input BufferedImage image = null; try { image = reader.read(0); fail("Read image with no input"); } catch (IllegalStateException ignore) { } catch (IOException e) { failBecause("Image could not be read", e); } assertNull(image); reader.dispose(); }
@Test public void testReadNoInputWithParam() { ImageReader reader = createReader(); // Do not set input BufferedImage image = null; try { image = reader.read(0, reader.getDefaultReadParam()); fail("Read image with no input"); } catch (IllegalStateException ignore) { } catch (IOException e) { failBecause("Image could not be read", e); } assertNull(image); reader.dispose(); }
@Test public void testReadAsRenderedImageNoInput() { ImageReader reader = createReader(); // Do not set input RenderedImage image = null; try { image = reader.readAsRenderedImage(0, reader.getDefaultReadParam()); fail("Read image with no input"); } catch (IllegalStateException expected) { // Ignore } catch (IOException e) { failBecause("Image could not be read", e); } assertNull(image); reader.dispose(); }