PixelGrabber

Discussion in 'Java' started by Riri, Jan 29, 2004.

  1. Riri

    Riri Guest

    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;
    }
     
    Riri, Jan 29, 2004
    #1
    1. Advertising

  2. 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 >> 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
     
    Thomas Weidenfeller, Jan 30, 2004
    #2
    1. Advertising

  3. Riri

    Andrew Hobbs Guest

    "Riri" <> wrote in message
    news:40198dff$0$17138$...
    > 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;
    > }
    >
    >
     
    Andrew Hobbs, Jan 30, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alexandre
    Replies:
    1
    Views:
    2,469
    Knute Johnson
    Apr 8, 2004
  2. Luc The Perverse

    Text to PixelGrabber

    Luc The Perverse, Mar 3, 2007, in forum: Java
    Replies:
    9
    Views:
    650
  3. buu

    PixelGrabber question

    buu, Aug 19, 2007, in forum: Java
    Replies:
    4
    Views:
    400
    Thomas Fritsch
    Aug 19, 2007
Loading...

Share This Page