Reputation: 7550
How can I tell if a binary image that I am generating is 0-indexed or 1-indexed?
I have made a program which reads in an image, generates a binary image and performs some other functions on the image. I would like to know, however, how to tell which 'index' the pixel values in the binary image are?
How is this done?
Is there a simple built-in function (such as image.getRGB();
, for example) which can be called to determine this?
Upvotes: 0
Views: 3193
Reputation: 9541
I don't know what you mean with 0- or 1-indexed, but here are some facts.
BufferedImage
is a generic image, so pixels start at coordinate (0,0)
If you want an array to work on, coming from this image, the upper-left corner will be in index 0 (unless otherwise specified)
image.getRGB(0, 0, image.getWidth(), image.getHeight(), array, 0, image.getWidth());
BufferedImage
doesn't support 1 BPP images natively, but either via a Packed mode with a Colormodel, or a 2-index palette. I can't tell which one you have without examples.
Regardless of the internal format, the different getRGB() methods should always return one value per pixel, and one pixel per value. Note that the full-opacity value (0xFF000000, -16777216) will also be included in results.
eg.
BufferedImage image = new BufferedImage(16, 16, BufferedImage.TYPE_BYTE_BINARY);
image.setRGB(0, 0, 0xFFFFFFFF);
image.setRGB(1, 0, 0xFF000000);
image.setRGB(0, 1, 0xFF000000);
image.setRGB(1, 1, 0xFFFFFFFF);
System.out.println(image.getRGB(0, 0));
System.out.println(image.getRGB(1, 0));
System.out.println(image.getRGB(0, 1));
System.out.println(image.getRGB(1, 1));
int[] array = image.getRGB(0, 0, image.getWidth(), image.getHeight(), null, 0, image.getWidth());
System.out.println(array[0]); // at (0,0)
System.out.println(array[1]); // at (1,0)
System.out.println(array[16]); // at (0,1)
System.out.println(array[17]); // at (1,1)
Upvotes: 3