Understanding BufferedImage.getRGB output values

77,421

Solution 1

getRGB(int x, int y) return you the value of color pixel at location (x,y).
You are misinterpreting the returned value.
It is in the binary format. like 11...11010101 and that is given to you as int value.
If you want to get RGB (i.e. Red, Green, Blue) components of that value use Color class. e.g.

Color mycolor = new Color(img.getRGB(x, y));

Then you can get the Red, Green, Blue, or Alpha values by using getRed(), getGreen(), getBlue(), getAlpha(). Then an int value will be returned by these methods in familiar format having value 0 < value < 255

int red = mycolor.getRed();

If you don't want to use Color class then you will need to use bitwise operations to get its value.

Solution 2

The RGB int color contains the Red, Green, Blue components of the color in its bits. You have to look at its binary or hexadecimal representation and not look at it as a whole integer number (not look at its decimal representation).

An int has 32 bits, 3x8 = 24 is used to store the RGB components (8 bits for each) in the following format:

               2          1          0
bitpos      32109876 54321098 76543210
------   --+--------+--------+--------+
bits     ..|RRRRRRRR|GGGGGGGG|BBBBBBBB|

You can extract or set the components using bitmasks:

int color = img.getRGB(x, y);

// Components will be in the range of 0..255:
int blue = color & 0xff;
int green = (color & 0xff00) >> 8;
int red = (color & 0xff0000) >> 16;

If the color also has an alpha component (transparency) ARGB, it gets the last remaining 8 bits.

           3          2          1          0
bitpos    10987654 32109876 54321098 76543210
------   +--------+--------+--------+--------+
bits     |AAAAAAAA|RRRRRRRR|GGGGGGGG|BBBBBBBB|

And the value:

int alpha = (color & 0xff000000) >>> 24; // Note the >>> shift
                                         // required due to sign bit

An alpha value of 255 means that a color is completely opaque and a value of 0 means that the color is completely transparent.

Your color:

Your color is color = -16755216 which has:

blue : 240         // Strong blue
green:  85         // A little green mixed in
red  :   0         // No red component at all
alpha: 255         // Completely opaque

Solution 3

See the implementation of ColorModel.getRgb:

589  public int getRGB(int pixel) {
590        return (getAlpha(pixel) << 24)
591             | (getRed(pixel) << 16)
592             | (getGreen(pixel) << 8)
593             | (getBlue(pixel) << 0);
594   }

Solution 4

It's explained in the docs:

Returns an integer pixel in the default RGB color model (TYPE_INT_ARGB) [...]

So you get 8 bits alpha channel, 8 bits red, 8 bits green, 8 bits blue.

A simple (and slow way) to examine the value is to use new java.awt.Color(colour, true); and then call the getters.

Or you can print the value as an unsigned 32bit hex value: Integer.toString(colour, 16). Each two characters of the output will be one part of the ARGB set.

Solution 5

Actually,You can transform the int to binary string by Integer.toBinaryString(-16755216),which is 11111111000000000101010111110000.it made up of 4 bytes: alpha, red, green, blue. The values are unpremultiplied, meaning any transparency is stored solely in the alpha component, and not in the color components. The components are stored as follows (alpha << 24) | (red << 16) | (green << 8) | blue. Each component ranges between 0..255 with 0 meaning no contribution for that component, and 255 meaning 100% contribution. Thus opaque-black would be 0xFF000000 (100% opaque but no contributions from red, green, or blue), and opaque-white would be 0xFFFFFFFF.

Share:
77,421
leonardo
Author by

leonardo

Updated on July 09, 2022

Comments

  • leonardo
    leonardo almost 2 years

    I'm getting an integer value for the pixel in an image using this method:

    int colour = img.getRGB(x, y);
    

    Then I'm printing out the values and I see that black pixels correspond to a value like "-16777216", a kind of blue to something like "-16755216", etc. Can someone please explain me the logic behind this value?

  • juanmf
    juanmf over 8 years
    I voted up as this is the clearest explanation I found, Still, I don't get the meaning of 32222222 22222222 111111 above bitpos Could you clarify?
  • icza
    icza over 8 years
    @juanmf An int has 32 bits, ranging from 0 to 31 (if zero based, that is if the first is 0). I wrote the bitpos vertically: the 33 2222222222 1111111111 are the first digit (the tens) of the bitpos. The rightmost bit has bitposition 0, so the leftmost bit position is 31, next to it is 30, next to it is 29 and so on. You have to read the digit and the one under it together: vertically. This is just a formatting for nice layout.
  • Brick
    Brick over 7 years
    This answer is good but fails to address the alpha channel. See other answers.
  • afzalex
    afzalex about 6 years
    @Brick Color class also have getAlpha() method which will give you alpha value. And I mentioned it in my answer.
  • Admin
    Admin almost 4 years
    I was using >> not >>> which wasn't obvious. Thanks.
  • Lupos
    Lupos over 2 years
    For what do you need the mask can't you directly shift the bits? What does "required due to sign bit" means?
  • icza
    icza over 2 years
    @Lupos It means if the number is negative (highest bit is 1), using >> would bring in ones not zerors. >>> brings in zeros even if the number is negative.