1. ## image.getrgb

I try to obtain the rgb data from a png image but Im getting weird values like -1 / 16777215 although I have white pixels in the picture as well as for red and green so something here is a bit off, what am i doing wrong ? doesnt getRGB method return the rgb values of a pixel ?

2. ## Re: image.getrgb

Whats wrong with the values -1 and 1677725?
-1 = 0xFFFFFFFF which is a fully opaque white pixel
16777215 is 0x00FFFFFF which is a fully transparent white pixel.
you should know how to convert the int values of the array to A R G B values (the 8th most left bits are the Alpha value, then the next 8 bits are the Red values, etc)

3. ## Re: image.getrgb

Originally Posted by shai.i
Whats wrong with the values -1 and 1677725?
-1 = 0xFFFFFFFF which is a fully opaque white pixel
16777215 is 0x00FFFFFF which is a fully transparent white pixel.
you should know how to convert the int values of the array to A R G B values (the 8th most left bits are the Alpha value, then the next 8 bits are the Red values, etc)
thanks shai, im not so familliar with bits operation but I saw an example how to convert the int to the different channels
int alpha = (rgbDataMatrix[i][j] >> 24) & 0xff;
int red = (rgbDataMatrix[i][j] >> 16) & 0xff;
int green = (rgbDataMatrix[i][j] >> 8) & 0xff;
int blue = (rgbDataMatrix[i][j]) & 0xff;
now I see that hes shifting the current's channel bits he want to obtain to the first byte for some reason but I still dont understand why hes doing the & 0xff operation, mind making things a bit more clear for me ?

4. ## Re: image.getrgb

because >> bit operator is similar to divide by 2 and as a result it keeps the sign of the number (positive or negative) and thus it will push "1" bits if the number Is negative instead of the bits that are being shifts to the right.
also for the >> operations that are not the >> 24 you are still left with the data of the bits that were not "pushed" out.

I hope this makes sense if not I suggest you google java bitwise operators and read on.

5. ## Re: image.getrgb

The & symbolises a bitwise 'and' operation. It is useful for masking (clearing bits which do not interest you at the moment), as its result is 1 only when both input bits are 1:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
The 0xFF is actually 11111111 in binary, so it is a byte with all bits set to 1.

Here the task is to dissect a 32-bit 'AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB' integer into four 8-bit (or 1-byte) groups, containing the 3 colors (R, G and B) and the alpha channel.
If you & it with 0xFF:
Code:
```AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB
&
00000000000000000000000011111111
=
000000000000000000000000BBBBBBBB, so we have a 32-bit number, containing the Blue channel only, all the other bits are cleared with the mask.```
Shifting operation 'puts' the number some digits left or right, dropping whatever part gets 'out' from the 32-bit. 'New' bits also appear, there are some rules about that as Shai.i briefly mentions.
Here let us pretend that the new bits are going to be 0. For example let us shift right the color 'number' with 1:
Code:
```AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB>>1
=
0AAAAAAAARRRRRRRRGGGGGGGGBBBBBBB, a 0 shifted in from the left, and a B was dropped from the right```
As our task is to extract bytes, shifting by 1 is not too useful, but shifting by 8, 16 or 24 is cool.
Let us check how the "int red = (rgbDataMatrix[i][j] >> 16) & 0xff;" works:
Code:
```AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB>>16
=
0000000000000000AAAAAAAARRRRRRRR, so 16 zeroes appeared on the left, and the GGGGGGGGBBBBBBBB disappeared on the right
Here we have a 32-bit number, which contains a mixture of alpha channel and red channel, so that is why the masking is used in the code:
0000000000000000AAAAAAAARRRRRRRR
&
00000000000000000000000011111111
=
000000000000000000000000RRRRRRRR, the result contains the R bits only```
So something like this is happening in the code.

6. ## Re: image.getrgb

thanks both of you great explanations helped alot

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•