Raster Data Tutorial (24-Bit)
Author: Bill green( 2002)
HOME EMAIL
This tutorial assumes the reader knows:
(1) Data is stored left to right and bottom to top in a BMP.
(2) How to develop source code to read BMP header and info header (I. e. width, height & # of colors ).
(3) How to develop source code to read raster data
INTRODUCTION
There are a possible 256 colors (2 ^ 8) that can be stored in a 8-bit image with 255 being the maximum. likewise, a 24-bit true color BMP has a possible 16 million colors (2 ^ 24 ). in a 24-bit BMP image, a pixel represents a RGB (Red Green Blue) data value. the pixel's RGB data value shows how much Red, Green and Blue are in that Fig. one pixel has 3 8-bit colors in it each having an inten Sity value between 0-255. so a pixel with a data value of (255, 0, 0) is equivalent to (Red = 255, Green = 0, and Blue = 0), or Red! And the composite of the RGB color values produces that pixels actual color. as another example, we know that red and green make yellow. therefore we wowould need all red, all green and no blue. being 255 is the maximum for each color, you wowould need an RGB data value of (255,255, 0) to achieve an accurate representation of yellow.
A 24-Bit BMP file structure is slightly different than an 8-Bit BMP file structure. there is no color table for any BMP with a bits/pixel value> 8. the table below shows how the pixel data is stored from the first byte to the last in a 24-Bit BMP.
TABLE 1: 24-Bit BMP File Structure
Byte # to fseek file pointer Information
0
Signature
2
File size
18
Width (number of columns)
22
Height (number of rows)
28
Bits/pixel
46
Number of colors used
54
Start of raster data in a 24-Bit BMP
The raster data starts at byte 54. the size of the raster data is (width x height) 1 bytes. therefore, a 100 row by 100 column 24-bit image wocould have (100x100) �1 = 9,999 bytes of raster data starting at byte 54 and continuing to the end of the BMP.
READING 24-Bit BMP RASTER DATA
TEST24.bmp is a 20 row by 20 column BMP image which we will use to read raster data from. the top left portion of TEST24.bmp is yellow and has an RGB pixel value of (255,255, 0 ). the bottom right portion is black with an RGB value of (0, 0, 0 ). the top right portion is green with an RGB value of (0,255, 0), and the remainder of TEST24.bmp is white (255,255,255 ).
(TEST24.bmp is scaled up here to a 100 by 100 BMP,
So be sure and download the zip file to test out your raster data program .)
TEST24.bmp contains 20 rows and 20 columns, so we know we will have 400 bytes of raster data. we also know the raster data will start at byte #54. knowing this, let's try our first program to read raster data and print it to a text file.
To be compiled with Turbo C
Note: download raster24.zip rather than cutting and pasting from below.
# Include (stdio. h)
# Include (stdlib. h)
# Include (math. h)
Long getImageInfo (FILE *, long, int );
Typedef struct {int rows; int cols; unsigned char * data;} sImage;
Int main (int argc, char * argv [])
{
FILE * BMP input, * rasterOutput;
SImage originalImage;
Unsigned char someChar;
Unsigned char * pChar;
Long fileSize;
Int vectorSize, nColors;
Int r, c;
/* -------- Initialize pointer ----------*/
SomeChar = '0 ';
PChar = & someChar;
If (argc <2)
{
Printf ("Usage: % s BMP input.bmp \ n", argv [0]);
Exit (0 );
}
Printf ("Reading file % s \ n", argv [1]);
/* ---- Declare input and output files ----*/
BMP input = fopen (argv [1], "rb ");
RasterOutput = fopen ("data24.html", "w ");
Fseek (BMP input, 0L, SEEK_END );
/* ----- Get bmp info -----*/
OriginalImage. cols = (int) getImageInfo (BMP input, 18, 4 );
OriginalImage. rows = (int) getImageInfo (BMP input, 22, 4 );
FileSize = getImageInfo (BMP input, 2, 4 );
NColors = getImageInfo (BMP input, 46, 4 );
/* ---- Print bmp info to screen -----*/
Printf ("Width: % d \ n", originalImage. cols );
Printf ("Height: % d \ n", originalImage. rows );
Printf ("File size: % ld \ n", fileSize );
Printf ("Bits/pixel: % d \ n", getImageInfo (BMP input, 28, 4 ));
Printf ("No. colors: % d \ n", nColors );
/* ---- FOR 24-bit bmp, there is no color table -----*/
Fseek (BMP input, 54, SEEK_SET );
/* ----------- Read raster data -----------*/
For (r = 0; r <= originalImage. rows-1; r ++)
{
For (c = 0; c <= originalImage. cols-1; c ++)
{
/* ---- Read first byte to get blue value -----*/
Fread (pChar, sizeof (char), 1, BMP input );
BlueValue = * pChar;
/* ----- Read next byte to get green value -----*/
Fread (pChar, sizeof (char), 1, BMP input );
GreenValue = * pChar;
/* ----- Read next byte to get red value -----*/
Fread (pChar, sizeof (char), 1, BMP input );
RedValue = * pChar;
/* --------- Print to text file ---------*/
Fprintf (rasterOutput, "(% d) = \ tRed \ t % d", r, c, redValue );
Fprintf (rasterOutput, "\ tGreen \ t % d \ tBlue \ t % d \ n", greenValue, blueValue );
}
}
Fclose (BMP input );
Fclose (rasterOutput );
Return 0;
}
/* -------- SUBPROGRAMS ------------*/
Long getImageInfo (FILE * inputFile, long offset, int numberOfChars)
{
Unsigned char * ptrC;
Long value = 0L;
Int I;
Unsigned char dummy;
Dummy = '0 ';
PtrC = & dummy;
Fseek (inputFile, offset, SEEK_SET );
For (I = 1; I <= numberOfChars; I ++)
{
Fread (ptrC, sizeof (char), 1, inputFile );
/* Calculate value based on adding bytes */
Value = (long) (value + (* ptrC) * (pow (256, (I-1 ))));
}
Return (value );
}
Running your raster data program, you will get an ASCII file called data24.txt with some entries looking like the following:
(0 0) = Red 255 Green 255 Blue 255
(0 1) = Red 255 Green 255 Blue 255
(0 2) = Red 255 Green 255 Blue 255
(0 3) = Red 255 Green 255 Blue 255
(0 4) = Red 255 Green 255 Blue 255
(0 5) = Red 255 Green 255 Blue 255
(0 6) = Red 255 Green 255 Blue 255
(0 7) = Red 255 Green 255 Blue 255
(0 8) = Red 255 Green 255 Blue 255
(0 9) = Red 255 Green 255 Blue 255
(0 10) = Red 255 Green 255 Blue 255
(0 11) = Red 255 Green 255 Blue 255
(0 12) = Red 0 Green 0 Blue 0
(0 13) = Red 0 Green 0 Blue 0
(0 14) = Red 0 Green 0 Blue 0
(0 15) = Red 0 Green 0 Blue 0
(0 16) = Red 0 Green 0 Blue 0
(0 17) = Red 0 Green 0 Blue 0
(0 18) = Red 0 Green 0 Blue 0
(0 19) = Red 0 Green 0 Blue 0
:
(8 0) = Red 255 Green 255 Blue 0
(8 1) = Red 255 Green 255 Blue 0
(8 2) = Red 255 Green 255 Blue 0
(8 3) = Red 255 Green 255 Blue 0
(8 4) = Red 255 Green 255 Blue 0
(8 5) = Red 255 Green 255 Blue 0
(8 6) = Red 255 Green 255 Blue 0
(8 7) = Red 255 Green 255 Blue 0
(8 8) = Red 255 Green 255 Blue 0
(8 9) = Red 255 Green 255 Blue 0
(8 10) = Red 255 Green 255 Blue 0
(8 11) = Red 255 Green 255 Blue 0
(8 12) = Red 255 Green 255 Blue 255
(8 13) = Red 255 Green 255 Blue 255
(8 14) = Red 255 Green 255 Blue 255
(8 15) = Red 255 Green 255 Blue 255
(8 16) = Red 255 Green 255 Blue 255
(8 17) = Red 255 Green 255 Blue 255
(8 18) = Red 255 Green 255 Blue 255
(8 19) = Red 255 Green 255 Blue 255
:
(16 0) = Red 255 Green 255 Blue 0
(16 1) = Red 255 Green 255 Blue 0
(16 2) = Red 255 Green 255 Blue 0
(16 3) = Red 255 Green 255 Blue 0
(16 4) = Red 255 Green 255 Blue 0
(16 5) = Red 255 Green 255 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 BMP, the raster data is stored from left to right and bottom to top-so row 16, column 0 is somewhere in 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, BMP input );
BlueValue = * pChar;
/* ----- Read next byte to get green value -----*/
Fread (pChar, sizeof (char), 1, BMP input );
GreenValue = * pChar;
/* ----- Read next byte to get red value -----*/
Fread (pChar, sizeof (char), 1, BMP input );
RedValue = * pChar;
/* --------- Print to text file ---------*/
Fprintf (rasterOutput, "(% d) = \ tRed \ t % d", r, c, redValue );
Fprintf (rasterOutput, "\ tGreen \ t % d \ tBlue \ t % d \ n", greenValue, blueValue );
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 * blueValue
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 (graybmp input, 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, BMP input );
BlueValue = * pChar;
/* ----- Read next byte to get green value -----*/
Fread (pChar, sizeof (char), 1, BMP input );
GreenValue = * pChar;
/* ----- Read next byte to get red value -----*/
Fread (pChar, sizeof (char), 1, BMP input );
RedValue = * pChar; www.2cto.com
/* ----- Use formula to convert rgb value to grayscale -----*/
GrayValue = (int) (0.299 * redValue + 0.587 * greenValue + 0.114 * blueValue );
/* ----- Print to text file -----*/
Fprintf (rasterOutput, "(% d) = \ tRed \ t % d", r, c, redValue );
Fprintf (rasterOutput, "\ tGreen \ t % d \ tBlue \ t % d \ tGray \ t % d \ n", greenValue, blueValue, grayValue );
/* ----- Write to new bmp file ------*/
* PChar = grayValue;
Fseek (bmpOutput, (54 + 4*256), SEEK_SET );
Fwrite (pChar, sizeof (char), 1, bmpOutput );
Author: caiye917015406