Question

I've got TIFF with 256 byte color palette. In Java I read this TIFF to BufferedImage. This BufferedImage has IndexColorModel. When I iterate through pixels in BufferedImage, I can only get RGB. I want to write method, which for x,y gets original color index from palette using BufferedImage (not RGB color, just original index from TIFFs palette). How can I achieve that?

I know that I could iterate through IndexColorModel and check RBG equality, but it won't work if TIFF has at least 2 indexes with the same colors (e.g. index 0 - black, 132 - black; suppose, that pixel 10x10 has black color [rgb=0,0,0] - then I don't know which index should I take - they have the same RGB value). I could also read raw TIFF and then calculate pixel position in byte array, but I don't want to do this - I would like to use something from JAI.

Is there any way to do this with BufferedImage and JAI without external libraries?

Thanks

Was it helpful?

Solution

OK, then you can obtain

  • the Raster from the BufferedImage
  • the DataBuffer from the Raster
  • the data array from the DataBuffer

and obtain the indices that are actually used from this data array.

This example reads the indices, looks up the corresponding colors in the color model, and writes the result into a "standard" BufferedImage (only as a verification)

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public class IndexedBufferedImage
{
    public static void main(String[] args) throws IOException
    {
        BufferedImage image = ImageIO.read(new File("exampleTiff256.tif"));
        System.out.println(image);
        System.out.println(image.getColorModel());

        ColorModel colorModel = image.getColorModel();
        IndexColorModel indexColorModel = null;
        if (colorModel instanceof IndexColorModel)
        {
            indexColorModel = (IndexColorModel)colorModel;
        }
        else
        {
            System.out.println("No IndexColorModel");
            return;
        }

        DataBuffer dataBuffer = image.getRaster().getDataBuffer();
        DataBufferByte dataBufferByte = null;
        if (dataBuffer instanceof DataBufferByte)
        {
            dataBufferByte = (DataBufferByte)dataBuffer;
        }
        else
        {
            System.out.println("No DataBufferByte");
            return;
        }

        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage test = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        byte data[] = dataBufferByte.getData();
        for (int y=0; y<h; y++)
        {
            for (int x=0; x<w; x++)
            {
                int arrayIndex = x + y * w;
                int colorIndex = data[arrayIndex];
                int color = indexColorModel.getRGB(colorIndex);
                System.out.println("At "+x+" "+y+" index is "+colorIndex+
                    " with color "+Integer.toHexString(color));
                test.setRGB(x, y, color);
            }
        }
        ImageIO.write(test, "PNG", new File("exampleTiff256.png"));
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top