Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > PixelGrabber

Reply
Thread Tools

PixelGrabber

 
 
Riri
Guest
Posts: n/a
 
      01-29-2004
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 >> & 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;
}


 
Reply With Quote
 
 
 
 
Thomas Weidenfeller
Guest
Posts: n/a
 
      01-30-2004
Riri wrote:
> 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 >> & 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

 
Reply With Quote
 
 
 
 
Andrew Hobbs
Guest
Posts: n/a
 
      01-30-2004

"Riri" <(E-Mail Removed)> wrote in message
news:40198dff$0$17138$(E-Mail Removed)...
> 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 >> & 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;
> }
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PixelGrabber question buu Java 4 08-19-2007 07:03 PM
Text to PixelGrabber Luc The Perverse Java 9 03-03-2007 11:27 PM
Uncaught error fetching image with PixelGrabber Alexandre Java 1 04-08-2004 05:41 PM



Advertisments