Manipulating BufferedImages with IndexColorModels

Hopefully it’s just because it’s so late, but for the life of me I can’t figure this out. I start with an 8-bit PNG image. I can create a grayscale version of this image with Java2D like so:


   private static final IndexColorModel createGreyscaleModel() {
      int SIZE = 256;
      byte[] r = new byte[SIZE];
      byte[] g = new byte[SIZE];
      byte[] b = new byte[SIZE];
      for (int i=0; i<SIZE; i++) {
         r[i] = g[i] = b[i] = (byte)i;
      }
      return new IndexColorModel(8, SIZE, r, g, b);
   }

   private static final BufferedImage createIndexedCopy(Image orig, IndexColorModel cm, int scale) {
      int w = orig.getWidth(null);
      int h = orig.getHeight(null);
      BufferedImage out = new BufferedImage(w*scale, h*scale, BufferedImage.TYPE_BYTE_INDEXED, cm);
      Graphics2D g2d = out.createGraphics();
//      g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
//      g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
//      g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
//      g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
      g2d.drawImage(orig, 0, 0, w*scale, h*scale, null);
      g2d.dispose();
      return out;
   }

   // ...

   BufferedImage grayscaleImage = createIndexedCopy(otherImage, createGreyscaleModel(), 1);

The end result looks like what I’d expect. So next, I thought I’d try to create a “greenscale” version of the image by creating the color model like so:


   private static final IndexColorModel createGreenModel() {
      int SIZE = 256;
      byte[] r = new byte[SIZE];
      byte[] g = new byte[SIZE];
      byte[] b = new byte[SIZE];
      for (int i=0; i<SIZE; i++) {
         g[i] = (byte)i;
      }
      IndexColorModel model = new IndexColorModel(8, SIZE, r, g, b);
      return model;
   }

But the end result isn’t what I want. It looks as if some kind of interpolation was done, even though the image was never scaled. This happens even when the original image is just a single color. See the example below. The original (blue) image has all pixels a single color, the grayscale version looks good, but the green version doesn’t…

What am I missing?

Very hard to tell from the screenshot that the green is being dithered. Why not try uncommenting line 18 and disabling dithering?

I was reluctant to offer any ideas because I am baffled by IndexColorModel. Are there any tutorials on it that you recommend? I read the header in the JavaDocs and am scratching my head.

Is it possible the problem is arising due to how the signs of the bytes are being interpreted? Casting an int to a byte while running it up into “negative” territory throws a warning flag for me.

System.out.println((byte)128); // displays -128
System.out.println((byte)255); // displays -1

For example, is blue in the “high” position? If so, blue and grey (which includes blue) are possibly having their high bit being interpreted one way (correctly), while the high bit in red or green (when used in isolation) might be interpreted in another.

I can’t figure out IndexedColorModel either, but all I saw was a commented-out hint to disable dithering and one of the swatches being dithered, and I just put the two together. One thing to remember is that in smaller gamuts like 16 bit, green usually gets extra bits, because the eye is more sensitive to green. So it might be interpolating some shades if you don’t take that into account.

Thanks guys. Sorry about the super-small sample image; I was using the simplest test case possible, a single 16x16 tile, all one color, while trying to figure things out.

I had tried all those RenderingHints in the hope they would make a difference (as well as perhaps ward off issues when creating new images with scales > 1) but they seem to make no difference - the result is still the same, apparent dithering.

I found this old thread, in which Abuse gives an approach that does work. If you start with one IndexColorModel’d Image, and create a new BufferedImage with a new ColorModel but the same Raster, things are much happier. I’m still not sure why you get differing results when using a “grayscale” IndexColorModel vs. another IndexColorModel, when using a source image that has all pixels the same color, but whatever.

Glad you found a work-around.

If it is not too much trouble, does the problem appear when you use Red in place of Green?

Interesting, that about green taking more bits sometimes.