what does pixelgrabber return?

Newb question #3
Right now I have a 2d array of booleans for obstructions, grid[][]. Every x/y that’s true is obstructed (can’t walk there), and so far it’s working great, but I have to manually set it each time. I’m trying to figure out how to use pixelgrabber, but the tutorials expect you to know bitwise operations. I read some tutorials on bitwise operations, and still couldn’t understand it.
Anyway, what I want to do is draw a 2nd layer, on that has trees, walls, tables, etc… obstructable tiles. Then I want pixelgrabber to tell me which pixels have a color (anything other than translucent) and set that pixel in grid[][] to true.

after I do this jazz:
public void handlesinglepixel(int x, int y, int pixel) {
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel ) & 0xff;

i’m lost. How, for example, can I test if the present pixel has alpha? Am I dealing with ints that have separated the byte value for each pixel int? wh… i… i guess i just don’t… confused!

Hi!

PixelGrabber is out of fashion since java-1.2 and is very slow since 1.4. When using the OGL-Pipeline with 1.5 its unuseable slow, so I would suggest to use BufferedImage instead with INT_ARGB as type.

Anyway you’ll still need bitwise operations and its easier than you think:
int alpha = (pixel >> 24) & 0xff;
int red = (pixel >> 16) & 0xff;
int green = (pixel >> & 0xff;
int blue = (pixel ) & 0xff;

The pixel-value is stored in an “int” which has 32 bit.
bit 24-32 are alpha
bit 16-24 are red

so, the pixel has alpha if alpha is !=255 I think. maybeit has alpha if its !=0, just try it out!

I would really recommend to use BufferedImage, its WAY faster and much more efficient!

lg Clemens

i’m lost. How, for example, can I test if the present pixel has alpha? Am I dealing with ints that have separated the byte value for each pixel int? wh… i… i guess i just don’t… confused!

Your code snippet is wonkey.


public void handlesinglepixel(int x, int y, int pixel) {
   int alpha = (pixel >> 24);
   int red   = (pixel >> 16) & 0x000000ff;
   int green = (pixel >>  8.) & 0x000000ff;
   int blue  = (pixel ) & 0x000000ff;
   // now alpha contains a value 0-255 that is the alpha value of the pixel.
 

ANDing with zeros makes all bits zero. ‘ff’ is all ones in the least significant byte so the ones will be preserved.

‘>>’ means shift right ie

0100 >> 2 -> 0001

AND means:

x & y gives
0 0 0
0 1 0
1 0 0
1 1 1

0100 & 1100 = 0100

OR means:

0100 | 1100 = 1100

Your code example is wonkey too nonnus29 =)

This is wrong :-

int alpha = (pixel >> 24); 

It should be either this :-

int alpha = (pixel >>> 24);

or this :-

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

:wink:

D’oh! To much C lately…

I know that this post is old but I was wondering what is best between the two code snippets:
int alpha = (pixel >> 24) & 0xFF;

and

int alpha = pixel & 0xFF000000;

The second seems less costly.

Also, apart BufferedImage.getRGB(), is there a better or faster way to read pixels? getRGB() performs pixel conversions if the color model is not ARGB.

Thanks!

EDIT:
int alpha = pixel & 0xFF000000;
Oop! ::slight_smile: The above instruction doesn’t have the same result as the first one. Forget the first question. Any answer about the last question?