private static BufferedImage readBuffereImage(ImageReader reader, int w, int h) throws IOException { ImageReadParam param = reader.getDefaultReadParam(); int srcWidth = reader.getWidth(0); int srcHeight = reader.getHeight(0); Rectangle rect = null; if ((float) w / h > (float) srcWidth / srcHeight) { h = h * srcWidth / w; w = srcWidth; rect = new Rectangle(0, (srcHeight - h) / 2, w, h); } else { w = w * srcHeight / h; h = srcHeight; rect = new Rectangle((srcWidth - w) / 2, 0, w, h); } param.setSourceRegion(rect); BufferedImage srcBuffered = reader.read(0, param); return srcBuffered; }
Rectangle sourceRegion = region.calculate(width, height); irParam.setSourceRegion(sourceRegion);
Rectangle sourceRegion = region.calculate(width, height); irParam.setSourceRegion(sourceRegion);
@Test(expected = IllegalArgumentException.class) public void testGetDestinationParamDestinationExceedsIntegerMax() throws IIOException { ImageReadParam param = new ImageReadParam(); param.setSourceRegion(new Rectangle(3 * Short.MAX_VALUE, 2 * Short.MAX_VALUE)); // 6 442 057 734 pixels ImageReaderBase.getDestination(param, TYPES.iterator(), 6 * Short.MAX_VALUE, 4 * Short.MAX_VALUE); // 25 768 230 936 pixels }
@Test public void testGetDestinationParamSourceRegionWider() throws IIOException { ImageReadParam param = new ImageReadParam(); param.setSourceRegion(new Rectangle(42, 1)); BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 3, 3); assertEquals(3, destination.getWidth()); assertEquals(1, destination.getHeight()); assertEquals(TYPES.get(0).getBufferedImageType(), destination.getType()); }
@Test public void testGetDestinationSourceExceedsIntegerMax() throws IIOException { ImageReadParam param = new ImageReadParam(); param.setSourceRegion(new Rectangle(42, 7)); BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), Integer.MAX_VALUE, 42);// 90 194 313 174 pixels assertEquals(42, destination.getWidth()); assertEquals(7, destination.getHeight()); assertEquals(TYPES.get(0).getBufferedImageType(), destination.getType()); }
@Test public void testGetDestinationParamSourceRegionTaller() throws IIOException { ImageReadParam param = new ImageReadParam(); param.setSourceRegion(new Rectangle(1, 42)); BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 3, 3); assertEquals(1, destination.getWidth()); assertEquals(3, destination.getHeight()); assertEquals(TYPES.get(0).getBufferedImageType(), destination.getType()); }
/** * Checks that the provided <code>dimensions</code> when intersected with the source region used * by the provided {@link ImageReadParam} instance does not result in an empty {@link * Rectangle}. * * <p>Input parameters cannot be null. * * @param readParameters an instance of {@link ImageReadParam} for which we want to check the * source region element. * @param dimensions an instance of {@link Rectangle} to use for the check. * @return <code>true</code> if the intersection is not empty, <code>false</code> otherwise. */ public static final boolean checkEmptySourceRegion( final ImageReadParam readParameters, final Rectangle dimensions) { Utilities.ensureNonNull("readDimension", dimensions); Utilities.ensureNonNull("readP", readParameters); final Rectangle sourceRegion = readParameters.getSourceRegion(); Rectangle.intersect(sourceRegion, dimensions, sourceRegion); if (sourceRegion.isEmpty()) return true; readParameters.setSourceRegion(sourceRegion); return false; }
@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 testICCProfileCMYKClassOutputColors() throws IOException { // Make sure ICC profile with class output isn't converted to too bright values JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/cmyk-sample-custom-icc-bright.jpg"))); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(800, 800, 64, 8)); param.setSourceSubsampling(8, 8, 2, 2); BufferedImage image = reader.read(0, param); assertNotNull(image); byte[] data = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); byte[] expectedData = {34, 37, 34, 47, 47, 44, 22, 26, 28, 23, 26, 28, 20, 23, 26, 20, 22, 25, 22, 25, 27, 18, 21, 24}; assertEquals(expectedData.length, data.length); assertJPEGPixelsEqual(expectedData, data, 0); reader.dispose(); }
@Test public void testReadWithSubsampleAndSourceRegionParam() { // NOTE: The "standard" (com.sun.imageio.plugin.*) ImageReaders pass // this test, so the test should be correct... ImageReader reader = createReader(); TestData data = getTestData().get(0); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(2, 2, 0, 0); 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(), 5, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), 5, image.getHeight()); reader.dispose(); }
@Test public void testAdobeUnknownRGBComponentIds() throws IOException { JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/adobe-unknown-rgb-ids.jpg"))); assertEquals(225, reader.getWidth(0)); assertEquals(156, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(0, 0, 225, 8)); BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(225, image.getWidth()); assertEquals(8, image.getHeight()); // Validate strip colors for (int i = 0; i < image.getWidth() / 10; i++) { int actualRGB = image.getRGB(i * 10, 7); assertRGBEquals(0xffffffff, actualRGB); // Will be pink/purple if decoded as YCbCr and not RGB } }
@Test public void testYCbCrNotSubsampledNonstandardChannelIndexes() throws IOException { // Regression: Make sure 3 channel, non-subsampled JFIF, defaults to YCbCr, even if unstandard channel indexes JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/jfif-ycbcr-no-subsampling-intel.jpg"))); assertEquals(600, reader.getWidth(0)); assertEquals(600, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(8, 8)); BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(8, image.getWidth()); assertEquals(8, image.getHeight()); // QnD test: Make sure all pixels are white (if treated as RGB, they will be pink-ish) for (int y = 0; y < image.getHeight(); y++) { for (int x = 0; x < image.getWidth(); x++) { assertEquals(0xffffff, image.getRGB(x, y) & 0xffffff); } } }
@Test public void testInvalidICCSingleChunkBadSequence() throws IOException { // Regression // Single segment ICC profile, with chunk index/count == 0 JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/invalid-icc-single-chunk-bad-sequence-number.jpg"))); assertEquals(1772, reader.getWidth(0)); assertEquals(2126, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(reader.getWidth(0), 8)); IIOReadWarningListener warningListener = mock(IIOReadWarningListener.class); reader.addIIOReadWarningListener(warningListener); BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(1772, image.getWidth()); assertEquals(8, image.getHeight()); verify(warningListener, atLeast(1)).warningOccurred(eq(reader), anyString()); }
@Test public void testICCDuplicateSequenceZeroBased() throws IOException { // File contains multiple ICC chunks, with all counts and sequence numbers == 0 JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/invalid-icc-duplicate-sequence-numbers-rgb-xerox-dc250-heavyweight-1-progressive-jfif.jpg"))); assertEquals(3874, reader.getWidth(0)); assertEquals(5480, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(0, 0, 3874, 16)); // Save some memory BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(3874, image.getWidth()); assertEquals(16, image.getHeight()); reader.dispose(); }
@Test public void testInvertedColors() throws IOException { JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/exif-jpeg-thumbnail-sony-dsc-p150-inverted-colors.jpg"))); assertEquals(2437, reader.getWidth(0)); assertEquals(1662, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(0, 0, reader.getWidth(0), 8)); BufferedImage strip = reader.read(0, param); assertNotNull(strip); assertEquals(2437, strip.getWidth()); assertEquals(8, strip.getHeight()); int[] expectedRGB = new int[] { 0xffe9d0bc, 0xfff3decd, 0xfff5e6d3, 0xfff8ecdc, 0xfff8f0e5, 0xffe3ceb9, 0xff6d3923, 0xff5a2d18, 0xff00170b, 0xff131311, 0xff52402c, 0xff624a30, 0xff6a4f34, 0xfffbf8f1, 0xfff4efeb, 0xffefeae6, 0xffebe6e2, 0xffe3e0d9, 0xffe1d6d0, 0xff10100e }; // Validate strip colors for (int i = 0; i < strip.getWidth() / 128; i++) { int actualRGB = strip.getRGB(i * 128, 4); assertRGBEquals(expectedRGB[i], actualRGB); } }
@Test public void testTruncatedICCProfile() throws IOException { // File contains single 'ICC_PROFILE' chunk, with a truncated (32 000 bytes) "Europe ISO Coated FOGRA27" ICC profile (by Adobe). // Profile should have been about 550 000 bytes, split into multiple chunks. Written by GIMP 2.6.11 // See: https://bugzilla.redhat.com/show_bug.cgi?id=695246 JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/cmm-exception-invalid-icc-profile-data.jpg"))); assertEquals(1993, reader.getWidth(0)); assertEquals(1038, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(reader.getWidth(0), 8)); BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(1993, image.getWidth()); assertEquals(8, image.getHeight()); reader.dispose(); }
@Test public void testCMYKWithRGBProfile() throws IOException { // File contains JFIF (!), RGB ICC profile AND Adobe App14 specifying unknown conversion, // but image data is 4 channel CMYK (from SOF0 channel Ids 'C', 'M', 'Y', 'K'). JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/jfif-cmyk-invalid-icc-profile-srgb.jpg"))); assertEquals(493, reader.getWidth(0)); assertEquals(500, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(0, 0, 493, 16)); // Save some memory BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(493, image.getWidth()); assertEquals(16, image.getHeight()); // TODO: Need to test colors! assertFalse(reader.hasThumbnails(0)); // Should not blow up! }
@Test public void testReadGray() throws IOException { // Seems like the last scan lines have been overwritten by an unnecessary 768 byte palette + 1 byte magic... try (ImageInputStream input = ImageIO.createImageInputStream(getClassLoaderResource("/pcx/GMARBLES.PCX"))) { PCXImageReader reader = createReader(); reader.setInput(input); assertEquals(1, reader.getNumImages(true)); assertEquals(1419, reader.getWidth(0)); assertEquals(1001, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(1419, 1000)); // Ignore the last garbled line BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(BufferedImage.TYPE_BYTE_INDEXED, image.getType()); assertEquals(1419, image.getWidth()); assertEquals(1000, image.getHeight()); } }
@Test public void testNoImageTypesRGBWithCMYKProfile() throws IOException { // File contains CMYK ICC profile ("U.S. Web Coated (SWOP) v2") AND Adobe App14 specifying YCCK conversion (!), // but image data is plain 3 channel YCC/RGB. // EXIF/TIFF metadata says Software: "Microsoft Windows Photo Gallery 6.0.6001.18000"... JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/no-image-types-rgb-us-web-coated-v2-ms-photogallery-exif.jpg"))); assertEquals(1743, reader.getWidth(0)); assertEquals(2551, reader.getHeight(0)); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceRegion(new Rectangle(0, 0, 1743, 16)); // Save some memory BufferedImage image = reader.read(0, param); assertNotNull(image); assertEquals(1743, image.getWidth()); assertEquals(16, image.getHeight()); // TODO: Need to test colors! assertTrue(reader.hasThumbnails(0)); // Should not blow up! }