Search code examples
playn

PlayN image.getRgb() usage


I'm trying to figure out how to use the 'image.getRgb()' API. for testing, I just copy and show an exact image, but could not get it working with the code:

    int picWidth = (int) image.width();
    int picHeight = (int) image.height();
    buf = new int[picWidth * picHeight];

    image.getRgb(0, 0, picWidth, picHeight, buf, 0, picWidth);
    int color = 0;
    for(int y=0; y < picHeight; y ++) {
        for(int x=0; x < picWidth; x++) {
            color = buf[x*y];
            layer.surface().setFillColor(color);
            layer.surface().fillRect(x,y, 1, 1);
        }
    }

What I expect the above code is copy pixel-by-pixel to buffer, then display the exact same image on the surface. However, here is my original pic and result pic:

Original image enter image description here

Result image enter image description here

I guess I mis-understand the getRgb() API, what is the correct way to use it? Thanks.


Solution

  • You sure you don't mean:

    color = buf[x + (y * width)];
    

    That should get the right x, y coord from the buffer.