Search code examples
swiftrgba

What is the index of the pixel in RGBA theory


Say you have an image that is 128 pixels wide, by 128 pixels high, and it is represented by a one-dimensional array of 32-Bit integers.

(Each integer contains the RGBA information for one pixel).

If the pixels are arranged in row-major order

(that is, the top row of pixels takes up array indices 0…127, the second row down is in 128… 255, and so on)

then what is the index of the pixel at x=15, y=22 (y=0 at the top)?


Solution

  • The formula

    index = y * width + x
    

    Example

    Say you have an image that is 128 pixels wide, by 128 pixels high, and it is represented by a one-dimensional array of 32-Bit integers.

    2831 = 128 * 22 + 15