Wednesday, August 26, 2015

Conversions between one dimensional pixel arrays and two dimensional coördinates

If you have an image as a one dimensional array of pixels, organised by width, you can calculate the x, y coördinate of a particular index like so:

x = index modulo width
y = index ÷ width

Where the division is an integer division.

To convert to an array index from an x, y coördinate:

index = x + (y × width)