Need help understanding the following Java syntax
public void printPixelARGB(int pixel){
public void printPixelARGB(int pixel){ int alpha = (pixel >> 24) & 0xff; int red = (pixel >> 16) & 0xff; int green = (pixel >> 8) & 0xff; int blue = (pixel) & 0xff; System.out.println("ARGB : " + alpha + "," + red + "," + green + "," + blue); }
I found that the Java syntax displays the RGB value of each pixel of the image, but I was a little confused about this method Can you help me by explaining this method?
0xff? What's that? In addition, (pixels > > 24) & what does 0xff mean? Thank you in advance
Solution
A pixel is an int
An int has 4 bytes, and each byte has 8 bits
for instance
0xA3 0x41 0x28 0x76
Is an example pixel (1 int, containing 4 bytes, 32 bits)
Now packed in this pixel is information about transparency (a) red, green and blue components
Transparency is the first byte (0xa3), then red (0x41), green (0x28), and then blue (0x76)
So to get the red part, you move 16 bits to the right, which gives you
0x00 0x00 0xA3 0x41
Now red is the most correct position, but you have that transparency byte, which is not good, so you must delete it
Do and do 0xff, really
0xa341 & set to 0x00ff
This means that you and each bit, so the bits of A3 are given by 00, and the bits of 0 and 41 are represented by FF, because FF is all 1, which gives you the original value, that is, 41
It's too red
0x00 0x00 0x00 0x41
So this code is to extract each component from an integer