1

I have a BufferedImage with alpha transparency, which I need to save as GIF with index transparency. There are no semi-opague pixels, therefore a conversion should be possible.

Using the code found under http://gman.eichberger.de/2007/07/transparent-gifs-in-java.html, I define a transparency color (green, for instance, which is not part of the current image) and make it transparent. Works fine, BUT it mixes up the color table and all the colors look awful (although I only use 3 different colors).

Is there a way to adjust this or else another way of converting such an ARGB-Image to an indexed one without significant quality loss?

The way my image gets constructed:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D graphics = (Graphics2D)image.getGraphics();
graphics.setColor(backgroundColor);
graphics.fillRect(0, 0, width, height);
// Some more painting here
graphics.dispose();

Thanks for any help!

sudoremo
  • 2,274
  • 2
  • 22
  • 39
  • Color conversion from (A)RGB to indexed is not trivial, if you know your are only going to use 3 colors you should create the image as indexed in the first place. If you need the conversion you should look at an image library (or implement an algorithm like error diffusion by yourself, but that wouldn't be very smart). – Viruzzo Dec 20 '11 at 09:28
  • Thanks for your answer! Well, the thing is that I also need to save a PNG (having alpha transparency) from the same graphics and I do not want to render it twice (too expensive). – sudoremo Dec 20 '11 at 09:35

1 Answers1

0

Using ImageIO to handle transparency especially when saving as a GIF image is a nightmare. Transparency support in ImageIO is incomplete for different image formats and even same image format but different variations.

If you already has a binary transparent BufferedImage, I've got a color quantization tool and a GIF encoder to save it as GIF with transparency. If the actual colors of the image are less than 256, no quantization will be applied. Otherwise, colors will be reduced to less than 256 but the transparent color is retained. Dithering can be applied during quantization process, so the resulting image size might be larger than usual.

The following screenshot shows two images: the one to the left is the original PNG image with alpha channel transparency while the right hand side one is a transparent GIF image converted from the same PNG image. The GIF image is actually reduced to 128 colors but the quality is great. (PNG: 49K, GIF: 17K)

enter image description here

Image source: http://svg2rlg.googlecode.com/svn-history/r2/trunk/test-suite/png/butterfly.png

dragon66
  • 2,645
  • 2
  • 21
  • 43
  • Just a reminder: PNG has color palette modes, and quantizing it like you would with a GIF makes a big difference. Consider [this version](http://i.stack.imgur.com/85H8e.png) of that PNG which looks the same but also weights 17k, with the added bonus that the borders don't look like a jaggy mess (unlike in your gif example when viewed non-resized). I quantized the RGB colors, but the alpha channel is still full - not just one level of transparency like gif has. – Camilo Martin Feb 05 '16 at 10:03
  • (also; just for the sake of completeness - the original SVG is smaller than both the GIF and the PNG after passing it through [an optimizer](https://github.com/svg/svgo)). – Camilo Martin Feb 05 '16 at 10:06