PixelGrabber

R

Riri

I've found this code on sun web site, but, i don't quite understand what
public void handlesinglepixel(int x, int y, int pixel) does. What is stored
in the table of int[] returned by the method public int[] handlepixels(Image
img, int x, int y, int w, int h)?It seems that the values stored in that
table are the values of the pixels, i thought it would be RGBs values (int
for 0 to 255), but it's not.

Any idea?
Thanks

//Création du tableau pixel image et affichage
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;
}

//Récupération des pixels
public int[] handlepixels(Image img, int x, int y, int w, int h) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return null;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return null;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
handlesinglepixel(x+i, y+j, pixels[j * w + i]);
}
}
return pixels;
}
 
T

Thomas Weidenfeller

Riri said:
I've found this code on sun web site, but, i don't quite understand what
public void handlesinglepixel(int x, int y, int pixel) does. [...]
//Création du tableau pixel image et affichage
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;
}

Unless I have become totally stupid and miss something very obvious, the
code above is just nonsense.

It returns nothing, it changes nothing, it just wastes CPU cycles. It
does isolate the r, g, b and alpha (transparency) components of the
pixel, but throws the result away. It does not use its x and y
parameters at all.

Are you sure you copied it correctly? Or is this code where you are
supposed to complete the method with some own handling code?

/Thomas
 
A

Andrew Hobbs

Riri said:
I've found this code on sun web site, but, i don't quite understand what
public void handlesinglepixel(int x, int y, int pixel) does.

It doesn't actually do anything. It is merely a demonstration of how you
can extract the values for the alpha, red, green and blue color channels.
What is stored
in the table of int[] returned by the method public int[] handlepixels(Image
img, int x, int y, int w, int h)?It seems that the values stored in that
table are the values of the pixels,
Correct.

i thought it would be RGBs values (int
for 0 to 255), but it's not.

If you examine the method carefully you should realize the int array has
just sufficient elements for one int per pixel. So it couldn't possibly
hold an int for each of the Red, Green and Blue values. In fact the
handleSinglePixel() method clearly shows that a single int from a single
pixel can be broken down to its four component bytes, one each for the
alpha, red, green and blue color values.


The documentation for the PixelGrabber class clearly says that the code is
simply a demonstration of how you might get the pixel data and extract the
color values; that it was never intended to be runnable code that actually
achieved anything.

Andrew


--
********************************************************
Andrew Hobbs PhD

MetaSense Pty Ltd - www.metasense.com.au
12 Ashover Grove
Carine W.A.
Australia 6020

61 8 9246 2026
metasens AntiSpam @iinet dot net dot au


*********************************************************
Any idea?
Thanks

//Création du tableau pixel image et affichage
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;
}

//Récupération des pixels
public int[] handlepixels(Image img, int x, int y, int w, int h) {
int[] pixels = new int[w * h];
PixelGrabber pg = new PixelGrabber(img, x, y, w, h, pixels, 0, w);
try {
pg.grabPixels();
} catch (InterruptedException e) {
System.err.println("interrupted waiting for pixels!");
return null;
}
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
System.err.println("image fetch aborted or errored");
return null;
}
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
handlesinglepixel(x+i, y+j, pixels[j * w + i]);
}
}
return pixels;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top