(OK works, but colors are ugly) ---
BufferedImage rgbImage = sv.getBufferedImage(); // is of TYPE_INT_RGB
BufferedImage indexedImage = new BufferedImage(rgbImage.getWidth(),
rgbImage.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g = indexedImage.createGraphics();
RenderingHints hints = new RenderingHints(RenderingHints.KEY_DITHERING,
RenderingHints.VALUE_DITHER_ENABLE);
RescaleOp op = new RescaleOp(1.0f, 0.0f, hints); // do nothing!
g.drawImage(rgbImage,op,1,1);
---- 2 ----
BufferedImage rgbImage = sv.getBufferedImage(); // is of TYPE_INT_RGB
BufferedImage indexedImage = new BufferedImage(rgbImage.getHeight(),
rgbImage.getWidth(), BufferedImage.TYPE_BYTE_INDEXED);
ColorConvertOp op = new ColorConvertOp(null);
indexedImage = op.filter(rgbImage, indexedImage);
ImageIO.write(indexedImage, "gif", file);
This throws:
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: Destination ColorSpace is undefined
at java.awt.image.ColorConvertOp.ICCBIFilter(ColorConvertOp.java:290)
at java.awt.image.ColorConvertOp.filter(ColorConvertOp.java:262)
at io.BitmapRecorder.writeBitmapFile(BitmapRecorder.java:58)
---- 3 ----
BufferedImage rgbImage = sv.getBufferedImage(); // is of TYPE_INT_RGB
PlanarImage indexedImage =
ColorQuantizerDescriptor.create(rgbImage,ColorQuantizerDescriptor.MEDIANCUT,256,32768,null,1,1,null);
ImageIO.write(indexedImage, "gif", file);
This throws:
Exception in thread "AWT-EventQueue-0"
java.lang.IllegalArgumentException: The specified ColorModel is
incompatible with the image SampleModel.
at javax.media.jai.PlanarImage.setImageLayout(PlanarImage.java:541)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
at javax.media.jai.RenderedOp.getColorModel(RenderedOp.java:2253)
at javax.imageio.ImageTypeSpecifier.<init>(ImageTypeSpecifier.java:226)
at
javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:1031)
at javax.imageio.ImageIO.write(ImageIO.java:1439)
at javax.imageio.ImageIO.write(ImageIO.java:1488)
at io.BitmapRecorder.writeBitmapFile(BitmapRecorder.java:65)