subX = pParam.getSourceXSubsampling(); subY = pParam.getSourceYSubsampling(); Graphics2D g = image.createGraphics(); try { g.fillRect(0, 0, image.getWidth(), image.getHeight()); if (pParam != null && pParam.getSourceRegion() != null) { Rectangle rectangle = pParam.getSourceRegion(); instance.translate(-rectangle.x, -rectangle.y);
try( FileInputStream fis =new FileInputStream(srcPath); ImageInputStream iis = ImageIO.createImageInputStream(fis) ) { Iterator it = ImageIO.getImageReadersByFormatName(readImageFormat); ImageReader reader = (ImageReader) it.next(); reader.setInput(iis, true); ImageReadParam param = reader.getDefaultReadParam(); Rectangle rect = new Rectangle(x, y, width, height); param.setSourceRegion(rect); BufferedImage bi = reader.read(0, param); ImageIO.write(bi, writeImageFormat, new File(toPath)); }catch (IOException e){ e.printStackTrace();
ImageInputStream iis = ImageIO.createImageInputStream(sourceFile); Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); if (!readers.hasNext()) reader.setInput(iis); inputFormatName = reader.getFormatName(); ImageReadParam irParam = reader.getDefaultReadParam(); int width = reader.getWidth(FIRST_IMAGE_INDEX); int height = reader.getHeight(FIRST_IMAGE_INDEX); Rectangle sourceRegion = region.calculate(width, height); irParam.setSourceRegion(sourceRegion); irParam.setSourceSubsampling(subsampling, subsampling, 0, 0);
Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); while (img==null && iter.hasNext()) { ImageReader reader = iter.next(); ImageReadParam param = reader.getDefaultReadParam(); reader.setInput(iis, false, true); img = reader.read(0, param); break; case 1: { while (imageTypes.hasNext()) { ImageTypeSpecifier imageTypeSpecifier = imageTypes.next(); int bufferedImageType = imageTypeSpecifier.getBufferedImageType(); if (bufferedImageType == BufferedImage.TYPE_BYTE_GRAY) { param.setDestinationType(imageTypeSpecifier); break; if (imageTypes.hasNext()) { ImageTypeSpecifier imageTypeSpecifier = imageTypes.next(); img = imageTypeSpecifier.createBufferedImage(width, height); param.setDestination(img); } else { lastException = new IOException("unable to load even a truncated version of the image.");
private Dimension getSourceRenderSizeFromSubsamping(ImageReadParam pParam, Dimension pOrigSize) { if (pParam.getSourceXSubsampling() > 1 || pParam.getSourceYSubsampling() > 1) { return new Dimension((int) (pOrigSize.width / (float) pParam.getSourceXSubsampling()), (int) (pOrigSize.height / (float) pParam.getSourceYSubsampling())); } return null; }
@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 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 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 testGetDestinationParamGoodDestination() throws IIOException { ImageReadParam param = new ImageReadParam(); param.setDestination(new BufferedImage(21, 1, BufferedImage.TYPE_INT_ARGB)); BufferedImage destination = ImageReaderBase.getDestination(param, TYPES.iterator(), 42, 1); assertEquals(BufferedImage.TYPE_INT_ARGB, destination.getType()); assertEquals(21, destination.getWidth()); assertEquals(1, destination.getHeight()); }
COSDictionary params = getDecodeParams(parameters, index); ImageReadParam irp = reader.getDefaultReadParam(); irp.setSourceSubsampling(options.getSubsamplingX(), options.getSubsamplingY(), options.getSubsamplingOffsetX(), options.getSubsamplingOffsetY()); irp.setSourceRegion(options.getSourceRegion()); options.setFilterSubsampled(true); try (ImageInputStream iis = ImageIO.createImageInputStream(source)) reader.setInput(iis); image = reader.read(0, irp); if (image.getColorModel().getPixelSize() != bits) BufferedImage packedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_BINARY); Graphics graphics = packedImage.getGraphics();
int xOff = param.getSubsamplingXOffset(); int yOff = param.getSubsamplingYOffset(); int xSub = param.getSourceXSubsampling(); int ySub = param.getSourceYSubsampling(); assertEquals("Subsampled image has wrong width: ", (expected.getWidth() - xOff + xSub - 1) / xSub, actual.getWidth()); assertEquals("Subsampled image has wrong height: ", (expected.getHeight() - yOff + ySub - 1) / ySub, actual.getHeight()); assertEquals("Subsampled has different type", expected.getType(), actual.getType()); File tempExpected = File.createTempFile("junit-expected-", ".png"); System.err.println("tempExpected.getAbsolutePath(): " + tempExpected.getAbsolutePath()); ImageIO.write(expected, "PNG", tempExpected); File tempActual = File.createTempFile("junit-actual-", ".png"); System.err.println("tempActual.getAbsolutePath(): " + tempActual.getAbsolutePath()); ImageIO.write(actual, "PNG", tempActual);
ImageTypeSpecifier type = types.next(); int csType = type.getColorModel().getColorSpace().getType(); if (rgbType == null && csType == ColorSpace.TYPE_RGB) { rgbType = type; param.setSourceRegion(new Rectangle(reader.getWidth(0), 8)); // We don't really need to read it all param.setDestinationType(cmykType); BufferedImage imageCMYK = reader.read(0, param); param.setDestinationType(rgbType); BufferedImage imageRGB = reader.read(0, param); assertEquals(ColorSpace.TYPE_CMYK, imageCMYK.getColorModel().getColorSpace().getType()); assertEquals(ColorSpace.TYPE_RGB, imageRGB.getColorModel().getColorSpace().getType()); for (int y = 0; y < imageCMYK.getHeight(); y++) { for (int x = 0; x < imageCMYK.getWidth(); x++) { int cmykAsRGB = imageCMYK.getRGB(x, y);
@Test public void testReadSubsamplingBounds1024() throws IOException { JPEGImageReader reader = createReader(); reader.setInput(ImageIO.createImageInputStream(getClassLoaderResource("/jpeg/read-error1024.jpg"))); ImageReadParam param = reader.getDefaultReadParam(); param.setSourceSubsampling(3, 3, 1, 1); BufferedImage image = reader.read(0, param); assertNotNull(image); // Make sure correct color is actually read, not just left empty assertRGBEquals(0xfffefefd, image.getRGB(0, image.getHeight() - 2)); assertRGBEquals(0xfffefefd, image.getRGB(0, image.getHeight() - 1)); }
Rectangle srcRegion = new Rectangle(); Rectangle destRegion = new Rectangle(); computeRegions(param, width, height, destination, srcRegion, destRegion); WritableRaster destRaster = clipToRect(destination.getRaster(), destRegion, param != null ? param.getDestinationBands() : null); checkReadParamBandSettings(param, rawType.getNumBands(), destRaster.getNumBands()); WritableRaster rowRaster = rawType.createBufferedImage(width, 1).getRaster(); param != null ? param.getSourceBands() : null, param != null ? param.getSourceXSubsampling() : 1); int transferType = rowRaster.getTransferType(); int samplesPerPixel = header.getSamplesPerPixel(); rowDataByte = ((DataBufferByte) rowRaster.getDataBuffer()).getData(); break; case DataBuffer.TYPE_USHORT: if (!destination.getColorModel().isCompatibleRaster(rowRaster)) { colorConvert = new ColorConvertOp(rawType.getColorModel().getColorSpace(), destination.getColorModel().getColorSpace(), null); int xSub = param == null ? 1 : param.getSourceXSubsampling(); int ySub = param == null ? 1 : param.getSourceYSubsampling();
private WritableRaster getDestinationRaster(ImageReadParam param) { if (param == null) throw new IllegalArgumentException(UNKNOWN_IMAGE_TYPE); BufferedImage bi = param.getDestination(); if (bi != null) { width = bi.getWidth(); height = bi.getHeight(); return bi.getRaster(); } ImageTypeSpecifier imageType = param.getDestinationType(); if (imageType != null) { SampleModel sm = imageType.getSampleModel(); width = sm.getWidth(); height = sm.getHeight(); return Raster.createWritableRaster(sm, null); } throw new IllegalArgumentException(UNKNOWN_IMAGE_TYPE); }
private BufferedImage getDestination(ImageReadParam param) { if (param == null) throw new IllegalArgumentException(UNKNOWN_IMAGE_TYPE); BufferedImage bi = param.getDestination(); if (bi != null) { width = bi.getWidth(); height = bi.getHeight(); return bi; } ImageTypeSpecifier imageType = param.getDestinationType(); if (imageType != null) { SampleModel sm = imageType.getSampleModel(); width = sm.getWidth(); height = sm.getHeight(); return imageType.createBufferedImage(width, height); } throw new IllegalArgumentException(UNKNOWN_IMAGE_TYPE); }
Rectangle srcRegion = new Rectangle(); Rectangle destRegion = new Rectangle(); computeRegions(param, width, height, destination, srcRegion, destRegion); WritableRaster destRaster = clipToRect(destination.getRaster(), destRegion, param != null ? param.getDestinationBands() : null); checkReadParamBandSettings(param, rawType.getNumBands(), destRaster.getNumBands()); WritableRaster rowRaster = rawType.createBufferedImage(width, 1).getRaster(); param != null ? param.getSourceBands() : null, param != null ? param.getSourceXSubsampling() : 1); int xSub = param != null ? param.getSourceXSubsampling() : 1; int ySub = param != null ? param.getSourceYSubsampling() : 1; WritableRaster destChannel = destRaster.createWritableChild(destRaster.getMinX(), destRaster.getMinY(), destRaster.getWidth(), destRaster.getHeight(), 0, 0, new int[] {c}); Raster srcChannel = clippedRow.createChild(clippedRow.getMinX(), 0, clippedRow.getWidth(), 1, 0, 0, new int[] {c});
int height = getHeight(imageIndex); Rectangle srcRegion = new Rectangle(); Rectangle destRegion = new Rectangle(); computeRegions(param, width, height, null, srcRegion, destRegion); BufferedImage destination = new BufferedImage(srcRegion.width, srcRegion.height, BufferedImage.TYPE_4BYTE_ABGR); WritableRaster raster = destination.getRaster(); int xSub = param != null ? param.getSourceXSubsampling() : 1; int ySub = param != null ? param.getSourceYSubsampling() : 1; raster.setDataElements(dstX, dstY, pixelRGBE); return destination.getRaster();
checkReadParamBandSettings(param, rawType.getNumBands(), destination.getSampleModel().getNumBands()); int xSub = param != null ? param.getSourceXSubsampling() : 1; int ySub = param != null ? param.getSourceYSubsampling() : 1; WritableRaster destRaster = clipToRect(destination.getRaster(), dstRegion, param != null ? param.getDestinationBands() : null); WritableRaster rowRaster = rawType.createBufferedImage(stripTileWidth, 1).getRaster(); if (rowRaster.getNumBands() != 3) { throw new IIOException("TIFF PhotometricInterpretation YCbCr requires SamplesPerPixel == 3: " + rowRaster.getNumBands()); if (rowRaster.getTransferType() != DataBuffer.TYPE_BYTE && rowRaster.getTransferType() != DataBuffer.TYPE_USHORT) { throw new IIOException("TIFF PhotometricInterpretation YCbCr requires BitsPerSample == [8,8,8] or [16,16,16]"); param != null ? param.getSourceBands() : null, param != null ? param.getSourceXSubsampling() : 1); JPEGImageReadParam jpegParam = (JPEGImageReadParam) jpegReader.getDefaultReadParam(); jpegReader.setInput(new ByteArrayImageInputStream(tablesValue)); jpegReader.getStreamMetadata(); imageInput.seek(stripTileOffsets != null ? stripTileOffsets[i] : realJPEGOffset);
protected void assertReadWithSourceRegionParamEqualImage(final Rectangle r, final TestData data, final int imageIndex) throws IOException { ImageReader reader = createReader(); reader.setInput(data.getInputStream()); ImageReadParam param = reader.getDefaultReadParam(); final BufferedImage roi = reader.read(imageIndex, param).getSubimage(r.x, r.y, r.width, r.height); param.setSourceRegion(r); final BufferedImage image = reader.read(imageIndex, param); assertEquals("Read image has wrong width: " + image.getWidth(), r.width, image.getWidth()); assertEquals("Read image has wrong height: " + image.getHeight(), r.height, image.getHeight()); assertImageDataEquals("Images differ", roi, image);