5.9k views
4 votes
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)?

User Jorel
by
8.0k points

1 Answer

1 vote

Answer:

2831

Explanation:

You want the index of the pixel at (x, y) = (15, 22) in a 128×128 array, if the pixel at (0, 0) has index 0, and the pixel at (0, 1) has index 128.

Index

The index increase by 128 for each additional row (y-value), so the index of pixel (x, y) is ...

i = x +128y

The index for pixel (15, 22) is ...

i = 15 +128·22 = 15 +2816

i = 2831 . . . . . index of pixel (15, 22)

User Lovely
by
7.5k points