This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static BufferedImage convertRenderedImage(RenderedImage img) { | |
if (img instanceof BufferedImage) { | |
return (BufferedImage) img; | |
} | |
ColorModel cm = img.getColorModel(); | |
int width = img.getWidth(); | |
int height = img.getHeight(); | |
WritableRaster raster = cm | |
.createCompatibleWritableRaster(width, height); | |
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); | |
Hashtable properties = new Hashtable(); | |
String[] keys = img.getPropertyNames(); | |
if (keys != null) { | |
for (int i = 0; i < keys.length; i++) { | |
properties.put(keys[i], img.getProperty(keys[i])); | |
} | |
} | |
BufferedImage result = new BufferedImage(cm, raster, | |
isAlphaPremultiplied, properties); | |
img.copyData(raster); | |
return result; | |
} |