if (obj instanceof RenderedImage) {
writer.write((RenderedImage) obj);
} else if (obj instanceof BufferedImage) {
BufferedImage buffered = (BufferedImage) obj;
writer.write(new IIOImage(buffered.getRaster(), null, null));
} else if (obj instanceof Image) {
Image image = (Image) obj;
BufferedImage buffered = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = buffered.createGraphics();
graphics.drawImage(image, 0, 0, null, null);
writer.write(new IIOImage(buffered.getRaster(), null, null));
} else {
throw new UnsupportedDataTypeException("Unknown image type " + obj.getClass().getName());
}
os.flush();
}