Read Pixel in image

Hi, could anyone help me, i'm really upset in here..

i try to read every pixel in my image, but what i got some number like this (-946523, -964323, and so on...) this is hexa right? and then i read some article, i got the answer like this :

int pixel = -946523 & 0xff, what does it means?

how can -946523 become 165? i understand that 165 is the value of the pixel, right? but how can? how about the process? can anyone explain to me...

Thanks a lot...

[497 byte] By [2Puluh11Lapan3a] at [2007-10-2 21:10:26]
# 1

> Hi, could anyone help me, i'm really upset in here..

>

> i try to read every pixel in my image, but what i got

> some number like this (-946523, -964323, and so

> on...) this is hexa right? and then i read some

> article, i got the answer like this :

> int pixel = -946523 & 0xff, what does it

> means?

> how can -946523 become 165? i understand that 165 is

> the value of the pixel, right? but how can? how about

> the process? can anyone explain to me...

Pixels stored as integers have the following format:

ARGB

One byte of alpha information, a byte for red, a byte for green and a byte for blue.

The byte represent a value from 0 to 255, where 0 means none of the component is present, and 255 means the full amount is present. For a fully opaque pixel (no tranparency), alpha is 255, which in hex is 0xFF. An integer in Java is a 32 bit signed value. The sign is carried in the high order bit. So, if you have a pixel packed as 4 bytes in an integer, and the pixel has an alpha value of 0xFF, the high order bit of the integer will alway be set, and the integer will be negative.

A pixel value of -946523 in hex is 0xFFF18EA5. so, the pixel has an alpha value of 0xFF or 255, a red component of 0xF1 or 241; a blue component of 0x8E or 142; and a blue component of 0xA5 or 165.

Your pixel in binary looks like this:

1 1 1 1 1 1 1 11 1 1 1 0 0 0 11 0 0 0 1 1 1 01 0 1 0 0 1 0 1

ALPHA 24-31RED 16-23 GREEN 8-15BLUE 0-7

So, say you want to get the alpha value, which is stored in the highest 8 bits, or bits 24 through 31. First, you need to shift those bits to the right until they are in the lowest 8 bits (0-7). This is done with the shift operator (>>).

int pixel = -964323;

int aShift = pixel >> 24;

After this operation, the integer now looks like this:

1 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 1

Note that when you shift right in Java, the empty bits to the left are filled with ones. This is called sign extension, and it is a problem for us, because we want all zeros in the left bits, leaving just the alpha component bits unchanged. To do this, you mask, which is done use the AND operator (&). This operator takes two numbers and compares each bit. If the bits are both 1, a 1 is placed in the result, otherwise a zero is placed in the result. So, if we take the shifted integer above and mask it with 0xFF, it looks like this:

1 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 1 (shifted integer)

0 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 01 1 1 1 1 1 1 1 (mask 0xFF)

--

0 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 01 1 1 1 1 1 1 1 (result)

The result is 0xFF, which is 255, which is the correct value for alpha.

In code, this looks like:

int a = aShift & 0xFF;

This can all be done in one line as follows:

int a = (pixel >> 24) & 0xFF;

For the red component, we first shift 16 bits to the right, which gives us

1 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 1 1 1 11 1 1 1 0 0 0 1

Once again, you mask with 0xFF and the result is:

0 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 01 1 1 1 0 0 0 1

which is 0xF1, or 241.

In code:

int r = (pixel >> 16) & 0xFF;

Green is handled the same way, with a shift of 8 bits:

int g = (pixel >> 8) & 0xFF;

Blue is next, but what should be used for the shift value? Alpha was 24, red was 16, greeb was 8, which indicates that blue should be zero. In fact, if you use zero you will get the correct answer:

int b = (pixel >> 0) & 0xFF

but not that the shift with 0 really doesn't do anything, so you can just use the mask, and get the right answer:

1 1 1 1 1 1 1 11 1 1 1 0 0 0 11 0 0 0 1 1 1 01 0 1 0 0 1 0 1 (pixel)

0 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 01 1 1 1 1 1 1 1 (mask)

-

0 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 01 0 1 0 0 1 0 1 (result)

So, the blue pixel is 0xA5, or 165.

In summary:

int pixel = -964323;

int a = (pixel >> 24) & 0xFF;

int r = (pixel >> 16) & 0xFF;

int g = (pixel >> 8) & 0xFF;

int b = pixel & 0xFF;

Jim S.

Niceguy1a at 2007-7-13 23:56:25 > top of Java-index,Security,Cryptography...
# 2

WOW, thats very very good explaination,i understood now..Thanks a lot.

can i ask one more? i got something in here, u describe something like this :

int b = pixel & 0xFF;

This is my code ,which i used to get pixel value :

int w = bufImage.getWidth(null);

int h = bufImage.getHeight(null);

for (int j = 0 ; j < h; j++)

{

for (int i = 0; i < w; i++)

{

System.out.println(bufImage.getRGB(i, j) & 0xff);

}

}

does my code only got the blue pixel? cos i just use this :

System.out.println(bufImage.getRGB(i, j) & 0xff);

it same with ur code :

int b = pixel & 0xFF;

So, is there anyway to get right just the value of the pixels? ( i mean if i got some hex number like this -4623289 and i dont want to know about A result, R Result, etc..can i just use my code?)

2Puluh11Lapan3a at 2007-7-13 23:56:25 > top of Java-index,Security,Cryptography...
# 3

>> So, is there anyway to get right just the value of the pixels? ( i mean if i got some hex number like this -4623289 and i dont want to know about A result, R Result, etc..can i just use my code?)

Sorry, what i mean :

So, is there anyway to get just the value of the pixels? ( i mean if i got some hex number like this -4623289 and i dont want to know about A result, R Result, etc..can i just use my code?)

2Puluh11Lapan3a at 2007-7-13 23:56:25 > top of Java-index,Security,Cryptography...
# 4

> >> So, is there anyway to get right just the value of

> the pixels? ( i mean if i got some hex number like

> this -4623289 and i dont want to know about A result,

> R Result, etc..can i just use my code?)

>

> Sorry, what i mean :

> So, is there anyway to get just the value of the

> pixels? ( i mean if i got some hex number like this

> -4623289 and i dont want to know about A result, R

> Result, etc..can i just use my code?)

Your code will only retrieve the blue value from the pixel. If you just want the Color of the pixel, in a Color object, you can simply use the Color constructor that takes an integer a an argument:

Color

public Color(int rgba,

boolean hasalpha)

Creates an sRGB color with the specified combined RGBA value consisting of the alpha component in bits 24-31, the red component in bits 16-23, the green component in bits 8-15, and the blue component in bits 0-7. If the hasalpha argument is false, alpha is defaulted to 255.

Parameters:

rgba - the combined RGBA components

hasalpha - true if the alpha bits are valid; false otherwise

See Also:

ColorModel.getRGBdefault(), getRed(), getGreen(), getBlue(), getAlpha(), getRGB()

Niceguy1a at 2007-7-13 23:56:25 > top of Java-index,Security,Cryptography...
# 5

what do you mean about combine? how to work with that? cos i really dont know..

i found this : public void readPix()

{

IndexColorModel colorModel = (IndexColorModel)getColorModel(image);

int numColors = colorModel.getMapSize();

System.out.println(numColors);

}

public ColorModel getColorModel(Image image)

{

// If buffered image, the color model is readily available

if (image instanceof BufferedImage) {

BufferedImage bimage = (BufferedImage)image;

System.out.println("Halo");

return bimage.getColorModel();

}

// Use a pixel grabber to retrieve the image's color model;

// grabbing a single pixel is usually sufficient

PixelGrabber pg = new PixelGrabber(image, 5, 5, 1, 1, false);

try {

pg.grabPixels();

} catch (InterruptedException e) {

}

ColorModel cm = pg.getColorModel();

cm.get

return cm;

}

but why that code only work with *.gif image..

2Puluh11Lapan3a at 2007-7-13 23:56:25 > top of Java-index,Security,Cryptography...