Discussion:
How to translate X,Y coordinates to a pixel index?
(too old to reply)
a***@broadpark.no
2006-03-13 02:53:38 UTC
Permalink
Hi,

I am currently working with some BufferedImages and I need to translate
a given pixel index in the image to X,Y coordinates... I tried one
approach but that seems obviously wrong, Im sure lots of people has
done this before, although I was not able to search up any code that
did this, so I would like to see some other approaches to do this.

I need both:
pixel index to X,Y
and
X,Y to pixel index

Thanks for any responses
opalpa@gmail.com opalinski from opalpaweb
2006-03-13 03:26:19 UTC
Permalink
BufferedImage's multi-pixel setRGB and multi-pixel getRGB have general
conversion encoded with details in documentation:

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/BufferedImage.html#setRGB(int,%20int,%20int,%20int,%20int[],%20int,%20int)

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/image/BufferedImage.html#getRGB(int,%20int,%20int,%20int,%20int[],%20int,%20int)

Opalinski
***@gmail.com
http://www.geocities.com/opalpaweb/
a***@broadpark.no
2006-03-13 03:45:28 UTC
Permalink
I am not sure if this will help me... I will use the getRGB function,
but I have only a pixel index and the method takes X,Y coordinates, so
I gathered I will have to convert my pixel index to be able to call
this function.

I will list my problem in a bigger context:
I have a buffered image and an interger that specifies the index of a
certain pixel in this image.

I have to retrieve the color of this pixel, and then go through the
other pixels to find the next pixel that has the same color.

So my idea was to take my pixel index, translate to X,Y, use the getRGB
function to get the color, then go through the pixels, calling the
getRGB to get their color values, untill I find a pixel that has the
same color, then translate the X,Y I used to get that pixel from
getRGB, into a pixel index and thus have the new pixel index.

Does the methods you mention allow me to do this in an easier way?
opalpa@gmail.com opalinski from opalpaweb
2006-03-13 12:25:04 UTC
Permalink
There is "int getRGB(int x, int y)" and there is "int[] getRGB(int
startX, int startY, int w, int h, int[] rgbArray, int offset, int
scansize)"

The documentation for the latter has the relationship between one
integer indexes and (x,y) indexes.

It says: "With a specified coordinate (x, y) in the image, the ARGB
pixel can be accessed in this way:

pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)];"

So that means the integer pixel index, from (x,y) is:
offset + (y-startY)*scansize + (x-startX)]


Cheers
Opalinski
***@gmail.com
http://www.geocities.com/opalpaweb/
a***@broadpark.no
2006-03-13 14:47:17 UTC
Permalink
Im sorry, but I still do not get it.
Post by ***@gmail.com opalinski from opalpaweb
offset + (y-startY)*scansize + (x-startX)]
I have no offset, no start y no start x and I believe the scnasize is
the width,
thus I get the following formula:

pixelIndex = ( y * width ) + x

which would indicate that to go the other way:

y = pixelIndex / width
x = pixelIndex % width

(assuming integer division)

And that is what I tried myself before posting here. And it doesnt
work...

Am I looking at the formula the wrong way?
Post by ***@gmail.com opalinski from opalpaweb
There is "int getRGB(int x, int y)" and there is "int[] getRGB(int
startX, int startY, int w, int h, int[] rgbArray, int offset, int
scansize)"
The documentation for the latter has the relationship between one
integer indexes and (x,y) indexes.
It says: "With a specified coordinate (x, y) in the image, the ARGB
pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)];"
offset + (y-startY)*scansize + (x-startX)]
Cheers
Opalinski
http://www.geocities.com/opalpaweb/
opalpa@gmail.com opalinski from opalpaweb
2006-03-13 15:48:58 UTC
Permalink
I believe your analysis good, inclduing the scansize==width and
starty=0startx=0.

Perhaps post code, after reducing it to something folks could take,
compile, execute.

Cheers
Opalinski
***@gmail.com
http://www.geocities.com/opalpaweb/

Loading...