@Test public void testPNGTransparency() throws Exception { try (InputStream is = ImageWorker.class.getResourceAsStream("test-data/rgb_trns.png")) { RenderedImage image = ImageIOExt.read(is); ColorModel colorModel = image.getColorModel(); assertThat(colorModel, instanceOf(ComponentColorModel.class)); assertEquals(4, colorModel.getNumComponents()); assertEquals(ColorModel.TRANSLUCENT, colorModel.getTransparency()); int[] pixel = new int[4]; image.getData().getPixel(32, 32, pixel); assertEquals(0, pixel[0]); assertEquals(255, pixel[1]); assertEquals(52, pixel[2]); assertEquals(0, pixel[3]); } }
public static RenderedImage read(Object input) throws IOException { if (input == null) { throw new IllegalArgumentException("input == null!"); } try (ImageInputStream stream = getImageInputStream(input)) { Iterator iter = ImageIO.getImageReaders(stream); if (!iter.hasNext()) { return null; } ImageReader reader = (ImageReader) iter.next(); boolean isJdkPNGReader = "com.sun.imageio.plugins.png.PNGImageReader" .equals(reader.getClass().getName()); reader.setInput(stream, true, !isJdkPNGReader); BufferedImage bi; try { ImageReadParam param = reader.getDefaultReadParam(); bi = reader.read(0, param); } finally { reader.dispose(); } if (isJdkPNGReader && bi.getColorModel() instanceof ComponentColorModel && !bi.getColorModel().hasAlpha() && bi.getColorModel().getNumComponents() == 3) { IIOMetadata imageMetadata = reader.getImageMetadata(0); Node tree = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName()); Node trns_rgb = getNodeFromPath(tree, Arrays.asList("tRNS", "tRNS_RGB")); if (trns_rgb != null) { NamedNodeMap attributes = trns_rgb.getAttributes(); Integer red = getIntegerAttribute(attributes, "red"); Integer green = getIntegerAttribute(attributes, "green"); Integer blue = getIntegerAttribute(attributes, "blue"); if (red != null && green != null && blue != null) { Color color = new Color(red, green, blue); ImageWorker iw = new ImageWorker(bi); iw.makeColorTransparent(color); return iw.getRenderedImage(); } } } return bi; } }
ImageIOExt { public static RenderedImage read(Object input) throws IOException { if (input == null) { throw new IllegalArgumentException("input == null!"); } try (ImageInputStream stream = getImageInputStream(input)) { Iterator iter = ImageIO.getImageReaders(stream); if (!iter.hasNext()) { return null; } ImageReader reader = (ImageReader) iter.next(); boolean isJdkPNGReader = "com.sun.imageio.plugins.png.PNGImageReader" .equals(reader.getClass().getName()); reader.setInput(stream, true, !isJdkPNGReader); BufferedImage bi; try { ImageReadParam param = reader.getDefaultReadParam(); bi = reader.read(0, param); } finally { reader.dispose(); } if (isJdkPNGReader && bi.getColorModel() instanceof ComponentColorModel && !bi.getColorModel().hasAlpha() && bi.getColorModel().getNumComponents() == 3) { IIOMetadata imageMetadata = reader.getImageMetadata(0); Node tree = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName()); Node trns_rgb = getNodeFromPath(tree, Arrays.asList("tRNS", "tRNS_RGB")); if (trns_rgb != null) { NamedNodeMap attributes = trns_rgb.getAttributes(); Integer red = getIntegerAttribute(attributes, "red"); Integer green = getIntegerAttribute(attributes, "green"); Integer blue = getIntegerAttribute(attributes, "blue"); if (red != null && green != null && blue != null) { Color color = new Color(red, green, blue); ImageWorker iw = new ImageWorker(bi); iw.makeColorTransparent(color); return iw.getRenderedImage(); } } } return bi; } } }
ImageIOExt { public static RenderedImage read(Object input) throws IOException { if (input == null) { throw new IllegalArgumentException("input == null!"); } try (ImageInputStream stream = getImageInputStream(input)) { Iterator iter = ImageIO.getImageReaders(stream); if (!iter.hasNext()) { return null; } ImageReader reader = (ImageReader) iter.next(); boolean isJdkPNGReader = "com.sun.imageio.plugins.png.PNGImageReader" .equals(reader.getClass().getName()); reader.setInput(stream, true, !isJdkPNGReader); BufferedImage bi; try { ImageReadParam param = reader.getDefaultReadParam(); bi = reader.read(0, param); } finally { reader.dispose(); } if (isJdkPNGReader && bi.getColorModel() instanceof ComponentColorModel && !bi.getColorModel().hasAlpha() && bi.getColorModel().getNumComponents() == 3) { IIOMetadata imageMetadata = reader.getImageMetadata(0); Node tree = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName()); Node trns_rgb = getNodeFromPath(tree, Arrays.asList("tRNS", "tRNS_RGB")); if (trns_rgb != null) { NamedNodeMap attributes = trns_rgb.getAttributes(); Integer red = getIntegerAttribute(attributes, "red"); Integer green = getIntegerAttribute(attributes, "green"); Integer blue = getIntegerAttribute(attributes, "blue"); if (red != null && green != null && blue != null) { Color color = new Color(red, green, blue); ImageWorker iw = new ImageWorker(bi); iw.makeColorTransparent(color); return iw.getRenderedImage(); } } } return bi; } }  }
ImageIOExt { public static RenderedImage read(Object input) throws IOException { if (input == null) { throw new IllegalArgumentException("input == null!"); } try (ImageInputStream stream = getImageInputStream(input)) { Iterator iter = ImageIO.getImageReaders(stream); if (!iter.hasNext()) { return null; } ImageReader reader = (ImageReader) iter.next(); boolean isJdkPNGReader = "com.sun.imageio.plugins.png.PNGImageReader" .equals(reader.getClass().getName()); reader.setInput(stream, true, !isJdkPNGReader); BufferedImage bi; try { ImageReadParam param = reader.getDefaultReadParam(); bi = reader.read(0, param); } finally { reader.dispose(); } if (isJdkPNGReader && bi.getColorModel() instanceof ComponentColorModel && !bi.getColorModel().hasAlpha() && bi.getColorModel().getNumComponents() == 3) { IIOMetadata imageMetadata = reader.getImageMetadata(0); Node tree = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName()); Node trns_rgb = getNodeFromPath(tree, Arrays.asList("tRNS", "tRNS_RGB")); if (trns_rgb != null) { NamedNodeMap attributes = trns_rgb.getAttributes(); Integer red = getIntegerAttribute(attributes, "red"); Integer green = getIntegerAttribute(attributes, "green"); Integer blue = getIntegerAttribute(attributes, "blue"); if (red != null && green != null && blue != null) { Color color = new Color(red, green, blue); ImageWorker iw = new ImageWorker(bi); iw.makeColorTransparent(color); return iw.getRenderedImage(); } } } return bi; } }  static ImageOutputStream createImageOutputStream(RenderedImage image, Object destination); static ImageInputStream createImageInputStream(Object input); static File getCacheDirectory(); static void setCacheDirectory(File cache); static Long getFilesystemThreshold(); static void setFilesystemThreshold(Long filesystemThreshold); static synchronized void allowNativeCodec(
            final String format, final Class<T> category, final boolean allowed); static final ImageInputStreamSpi getImageInputStreamSPI(final Object input); static final ImageInputStreamSpi getImageInputStreamSPI(
            final Object input, final boolean streamCreationCheck); static boolean isCLibAvailable(); static ImageReader getImageioReader(final ImageInputStream inStream); static RenderedImage read(Object input); static BufferedImage readBufferedImage(Object input); }
ImageIOExt { public static RenderedImage read(Object input) throws IOException { if (input == null) { throw new IllegalArgumentException("input == null!"); } try (ImageInputStream stream = getImageInputStream(input)) { Iterator iter = ImageIO.getImageReaders(stream); if (!iter.hasNext()) { return null; } ImageReader reader = (ImageReader) iter.next(); boolean isJdkPNGReader = "com.sun.imageio.plugins.png.PNGImageReader" .equals(reader.getClass().getName()); reader.setInput(stream, true, !isJdkPNGReader); BufferedImage bi; try { ImageReadParam param = reader.getDefaultReadParam(); bi = reader.read(0, param); } finally { reader.dispose(); } if (isJdkPNGReader && bi.getColorModel() instanceof ComponentColorModel && !bi.getColorModel().hasAlpha() && bi.getColorModel().getNumComponents() == 3) { IIOMetadata imageMetadata = reader.getImageMetadata(0); Node tree = imageMetadata.getAsTree(imageMetadata.getNativeMetadataFormatName()); Node trns_rgb = getNodeFromPath(tree, Arrays.asList("tRNS", "tRNS_RGB")); if (trns_rgb != null) { NamedNodeMap attributes = trns_rgb.getAttributes(); Integer red = getIntegerAttribute(attributes, "red"); Integer green = getIntegerAttribute(attributes, "green"); Integer blue = getIntegerAttribute(attributes, "blue"); if (red != null && green != null && blue != null) { Color color = new Color(red, green, blue); ImageWorker iw = new ImageWorker(bi); iw.makeColorTransparent(color); return iw.getRenderedImage(); } } } return bi; } }  static ImageOutputStream createImageOutputStream(RenderedImage image, Object destination); static ImageInputStream createImageInputStream(Object input); static File getCacheDirectory(); static void setCacheDirectory(File cache); static Long getFilesystemThreshold(); static void setFilesystemThreshold(Long filesystemThreshold); static synchronized void allowNativeCodec(
            final String format, final Class<T> category, final boolean allowed); static final ImageInputStreamSpi getImageInputStreamSPI(final Object input); static final ImageInputStreamSpi getImageInputStreamSPI(
            final Object input, final boolean streamCreationCheck); static boolean isCLibAvailable(); static ImageReader getImageioReader(final ImageInputStream inStream); static RenderedImage read(Object input); static BufferedImage readBufferedImage(Object input);  }