PixelGrabber

I am having trouble understanding what the values of a pixel mean in an array and how to change them. Could someone please explain?

Edit: Also what is the opperation to set a certain pixel color’s alpha to 0? I found this example

for(int ctr = 0; ctr < pixels.length; ctr++)
{
int alpha = (pixels[ctr] >> 24) & 0xff;
if (alpha != 0)
{
// set this pixels transparancy to 25%
pixels[ctr] = 0x4F000000 | (pixels[ctr] & 0x00FFFFFF);
}
}

Can someone explain the
int alpha = (pixels[ctr] >> 24) & 0xff line of code?

Also if I wish to search the array for all the red pixels what value do I look for?

Hey the technique is called bit shifting.

int alpha = (pixels[ctr] >> 24) & 0xff;
you are getting 24th to the 32nd bits of the int.

Check out http://www.javage.net/cgi-bin/showArt.cgi?index=7

Finding all the red pixels is pretty easy.

int width = img.getWidth(null);
int height = img.getHeight(null);
int[] imageData = new int[width * height];
PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, imageData, 0, width);
try
{
pg.grabPixels();

    } catch (InterruptedException e) {            
    }
    
 for(int i=0; i<width; i++)
 {
      for(int j=0; j<height; j++)
         {
            int pixel = imageData[i + (j * width)];
            int red  =  (pixel >> 16) & 0xff;
            int green  =  (pixel >> 8) & 0xff;
            int blue  =  (pixel ) & 0xff;
            if(red==255 && green == 0 && blue == 0){
                 // Found full red pixel.
           }


}

}

ok second question is what do I have to multiply by to set the alpha to 0?

// set this pixels transparancy to 25%
pixels[ctr] = 0x4F000000 | (pixels[ctr] & 0x00FFFFFF);

tis sets them to 25%. How do you figure out by what you need to and the number? In other words how do you get 0x4F000000 and 0x00FFFFFF?

[quote]In other words how do you get 0x4F000000 and 0x00FFFFFF?
[/quote]
Those are number in hex. In hex each digit represents 4 bits, so it takes 2 hex digits for one byte. In java an int is a 32 bit number (4 bytes). When people use an int to represent a color they typically use one byte per component. This works nicely because you can have Red, Green, Blue and a Apha fit nicely in an int. The order is usually ARGB. So, for the number 0x12345678 in hex “12” is the alpha component, “34” is the red, and so on.

To set the apha to 0 (completely transparent) you would set the alpha component to 0x00000000 in hex. The new line would look like:

pixels[ctr] = 0x00000000 | (pixels[ctr] & 0x00FFFFFF);

PS: by my math the 0x4F000000 would be 31% transparent, not 25% like previously claimed.

pixels[ctr] =  0x4F000000 | (pixels[ctr] & 0x00FFFFFF);

Let me explain what is going on.

We have a color: pixels[ctr] . The first thing that happens is the bitwise and operator: “&” is used to effectivly remove the alpha component. It is really setting it to zero by only keeping the RGB components.

So now we have that color without an alpha and we are combining an alpha of 0x4F with the bitwise or operator: “|”.

Finally we put the new color back in to the array.

One more time: We took a color, removed the alpha, and combined that color with a new alpha.

I think you need to study up on bitwise operators. The following link should get you started but if you want a solid understanding you’re gonna need to search around or get a book. http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html

Thanks to everyone fo their responses. I now understand and have implemented my program using the knowledge provided by this community. Now if only I can get some help with my full screen question :slight_smile: