0
votes

1) I want to know the value of red color in 24 bitmap image.

pixel is represented by 24bits, where there are 8 bits for each of the red, green, and blue (RGB) values

Suppose I open the 24 bitmap image, and save the data in unsigned char Array A.

i,e) ( A[0],A[1],A[2] ) is the first pixel value of the 24 bitmap image.

Then what is the value of Red color?

I think 24 bitmap image color order(?) is 'RGB', So A[0] is the red color value. Am I right?

2) In 16 bitmap image, pixel is represented by 16 bits, where there are 5 bits for each of the red, green, and blue (RGB) values .

Suppose I open the 16 bitmap image, and save the data in unsigned short Array B.

i,e) B[0] is the first pixel value of the 16 bitmap image.

In this situation, I want to know the value of red color.

I think the last 5 bit, i,e) 'A[0] & 31' is the value of blue color since the color order(?) of 16 bitmap image is 'RGB'

Am I right?

1
Why are you guessing and not using a library?stark
Blue are the least-significant bits, red in the most. The endian-ness of the machine plays a role, if the image is generated on a big-endian machine, increasingly less likely, then it is convenient to talk about BGR instead. So A[0] normally has blue and for 16bpp you have to and with 0x1f. Do note that 565 is not uncommon for 16bpp. It is wise to ignore all this and only use 32bpp, the endianness detail disappears by addressing the pixels with int*.Hans Passant

1 Answers

0
votes

Bitmaps can be tough to work with in source code to retrieve its data. You first have to parse the file for its header and from its header you have to parse it and its data to know what type of bitmap image you are working with. Bitmaps have a lot of information stored. Logically speaking if the bitmap image is 24bits without transparency values for specifically a RGB type you then need to know if the image is compressed and by what compression method as well as other properties of a the file such as palette information, icon information, etc. so you know where to find the actual pixel data within that specific file. After you extract the raw data you then need to know how the information is stored. Here is were it can get tricky: it might be a RGB image but the data could be stored in the file in a direct RGB order or it could be reversed BGR. After that then it is a matter of knowing which orientation the image data is stored and if you need to flip it vertically, horizontally or both. Other than that if the color data is in the correct order and the image is in the proper orientation without compression then it should be without a doubt structured as this for unsigned char values:

Bitmap 24bits - 24/3 for RGB gives 8bits per channel or 1byte per channel. Since an unsigned char is 1 byte in memory width, then each byte in the appropriate order would represent each color channel. Consider the following:


// Considering data is in RGB order:
unsigned char colorData[3];      // Integer Base Range
colorData[0] = red byte          [0, 255]
colorData[1] = green byte        [0, 255]
colorData[2] = blue byte         [0, 255]

As for the bitmap internal file structure(s) here is a good source for parsing its data: bmp