24-Bit BMP Raster Data Tutorial & Grayscaling(四)

2014-11-24 10:57:02 · 作者: · 浏览: 7
Blue 0
(16 6) = Red 255 Green 255 Blue 0
(16 7) = Red 255 Green 255 Blue 0
(16 8) = Red 255 Green 255 Blue 0
(16 9) = Red 255 Green 255 Blue 0
(16 10) = Red 255 Green 255 Blue 0
(16 11) = Red 255 Green 255 Blue 0
(16 12) = Red 255 Green 255 Blue 255
(16 13) = Red 255 Green 255 Blue 255
(16 14) = Red 0 Green 255 Blue 0
(16 15) = Red 0 Green 255 Blue 0
(16 16) = Red 0 Green 255 Blue 0
(16 17) = Red 0 Green 255 Blue 0
(16 18) = Red 0 Green 255 Blue 0
(16 19) = Red 0 Green 255 Blue 0
:


Notice how entry (16, 0) is (Red = 255, Green = 255, Blue = 0) corresponding to a yellow pixel in TEST24.bmp. And we can see that looking at TEST24.bmp, it matches precisilely. Just remember that in BMPs, the raster data is stored from left to right and bottom to top - so row 16, column 0 is somewhere in the the top left corner!

EXPLANATION
To get preliminary code explanation, please see my raster data tutorial. The difference between this code and my 8-Bit raster code is that instead of 1 byte representing a pixel (8-bit), we now have 3 bytes representing each pixel (24-bit). So we cannot read the value of the pixel intensity and then move on to the next row-column entry. We have to read 3 different bytes! The algorithm below was used to do this:

/*----READ FIRST BYTE TO GET BLUE VALUE-----*/
fread(pChar, sizeof(char), 1, bmpInput);
blueva lue = *pChar;

/*-----READ NEXT BYTE TO GET GREEN VALUE-----*/
fread(pChar, sizeof(char), 1, bmpInput);
greenValue = *pChar;

/*-----READ NEXT BYTE TO GET RED VALUE-----*/
fread(pChar, sizeof(char), 1, bmpInput);
redValue = *pChar;

/*---------PRINT TO TEXT FILE---------*/
fprintf(rasterOutput, "(%d %d) = \tRed \t%d", r, c, redValue);
fprintf(rasterOutput, "\tGreen \t%d \tBlue \t%d\n", greenValue, blueva lue);
CONVERTING A 24-BIT BMP TO GRAY SCALE
We want to take the TEST24.bmp and grayscale it, or convert it from a 24-bit to an 8-bit BMP. We'll call our new grayscaled BMP "gray24.bmp". A formula for converting a RGB pixel value to a grayscale value is shown below:
grayValue = 0.299*redValue + 0.587*greenValue + 0.114*blueva lue
First, I copied the header and info header from the input BMP to the output BMP. This process is described in my raster data tutorial. They are the same with the exception of the file size, bits/pixel value, and the number of colors. We manipulate them by using the following code

/*----CHANGE BIT DEPTH FROM 24 TO 8----*/
fseek(bmpOutput, 28, SEEK_SET);
*pLong = (unsigned long)(8);
fwrite(pLong, sizeof(unsigned long), 1, bmpOutput);
The color tables, however, are not identical. There is no color table in a 24-bit BMP (see above), while there is one in a grayscale image. Therefore, instead of copying the color table like we have been, we are actually going to have to create one. I did this by just copying the color table of another grayscale BMP.

createColorTable(grayBmpInput, bmpOutput);
The following portion of code converts a 24-Bit BMP file to grayscale. The differences from raster24.c are in red.

To be compiled with Turbo C
Note: download gray24.zip rather than cutting and pasting from below.
/*-----READ FIRST BYTE TO GET BLUE VALUE-----*/
fread(pChar, sizeof(char), 1, bmpInput);
blueva lue = *pChar;

/*-----READ NEXT BYTE TO GET GREEN VALUE-----*/
fread(pChar, sizeof(char), 1, bmpInput);
greenValue = *pChar;

/*-----READ NEXT BYTE TO GET RED VALUE-----*/
fread(pChar, sizeof(char), 1, bmpIn