Hector Ta
Hector Ta

Reputation: 91

Transform a raw image to BMP color image

Now, I have finished reading the raw image from CMOS camera and calculate the R,G,B Red Green Blue by using bilinear interpolation already. The question is, i've just read a sample code have this part:

image_t = (unsigned char *)malloc((size_t)width * height * 3);

*(image_t + 3 * (width * y + x) + 2) = R;
*(image_t + 3 * (width * y + x) + 1) = G;
*(image_t + 3 * (width * y + x) + 0) = B;

I think image_t is a memory space for pixel to contain RGB value. But i'm not clear why do we need to add image_t with 3 * (width * y + x) + 2 for R?

As I know (**width * y + x**) is the location of pixel in the array. How about 3 and addition 2,1,0 for R,G,B, respectively?

Upvotes: 1

Views: 248

Answers (1)

pmcoltrane
pmcoltrane

Reputation: 3112

image_t is a pointer to an area of memory containing the image data. Its length is width * height * 3: the width of the image in pixels, times the height of the image in pixels, times the number of bytes per pixel.

Each pixel in this image is 3 bytes: one byte each for the R, G, and B values. So the location of the pixel in the array is actually 3 * (width * y + x). That's where the 3 comes from.

The 2, 1, and 0 are the byte offsets of the R, G, and B values within the pixel. R is at the pixel position + 2, G is at the pixel position + 1, and B is at the pixel position + 0.

Upvotes: 1

Related Questions