BufferedImage bufferedImage = new BufferedImage(
image.getWidth(observer),
image.getHeight(observer),
(bkg == null) ? BufferedImage.TYPE_INT_ARGB : BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
if (bkg == null) {
g.drawImage(image, 0, 0, observer);
} else {
g.drawImage(image, 0, 0, bkg, observer);
}
return bufferedImage;
}