Copy codeThe Code is as follows:/* File name: BMP test. c
Author: WanChuan XianSheng
Date: Oct 01,201 1
Description: Show all Info a bmp file has. including
FileHeader Info, InfoHeader Info and Data Part.
Reference: source code for C Language Reading of BMP Image Data
*/
# Include <stdio. h>
# Include <stdlib. h>
# Define BITMAPFILEHEADERLENGTH 14 // The bmp FileHeader length is 14
# Define BM 19778 // The ASCII code for BM
/* Test the file is bmp file or not */
Void BMP filetest (FILE * fpbmp );
/* To get the OffSet of header to data part */
Void bmpHeaderPartLength (FILE * fpbmp );
/* To get the width and height of the bmp file */
Void BMP widthheight (FILE * fpbmp );
/* Show bmp file tagBITMAPFILEHEADER info */
Void BMP fileheader (FILE * fpbmp );
/* Show bmp file tagBITMAPINFOHEADER info */
Void BMP infoheader (FILE * fpbmp );
/* Show the Data Part of bmp file */
Void BMP datapart (FILE * fpbmp );
Unsigned int OffSet = 0; // OffSet from Header part to Data Part
Long BMP Width = 0; // The Width of the Data Part
Long BmpHeight = 0; // The Height of the Data Part
Int main (int argc, char * argv [])
{
/* Open bmp file */
FILE * fpbmp = fopen ("lena.bmp", "r + ");
If (fpbmp = NULL)
{
Fprintf (stderr, "Open lena.bmp failed !!! \ N ");
Return 1;
}
Bmp filetest (fpbmp); // Test the file is bmp file or not
BmpHeaderPartLength (fpbmp); // Get the length of Header Part
BMP widthheight (fpbmp); // Get the width and width of the Data Part
// BMP FileHeader (fpbmp); // Show the FileHeader Information
// BMP InfoHeader (fpbmp); // Show the InfoHeader Information
BMP datapart (fpbmp); // Reserve the data to file
Fclose (fpbmp );
Return 0;
}
/* Test the file is bmp file or not */
Void BMP filetest (FILE * fpbmp)
{
Unsigned short bfType = 0;
Fseek (fpbmp, 0L, SEEK_SET );
Fread (& bfType, sizeof (char), 2, fpbmp );
If (BM! = BfType)
{
Fprintf (stderr, "This file is not bmp file .!!! \ N ");
Exit (1 );
}
}
/* To get the OffSet of header to data part */
Void bmpHeaderPartLength (FILE * fpbmp)
{
Fseek (fpbmp, 10L, SEEK_SET );
Fread (& OffSet, sizeof (char), 4, fpbmp );
// Printf ("The Header Part is of length % d. \ n", OffSet );
}
/* To get the width and height of the bmp file */
Void BMP widthheight (FILE * fpbmp)
{
Fseek (fpbmp, 18L, SEEK_SET );
Fread (& BMP width, sizeof (char), 4, fpbmp );
Fread (& BmpHeight, sizeof (char), 4, fpbmp );
// Printf ("The Width of the bmp file is % ld. \ n", bmp Width );
// Printf ("The Height of the bmp file is % ld. \ n", BmpHeight );
}
/* Show bmp file tagBITMAPFILEHEADER info */
Void BMP fileheader (FILE * fpbmp)
{
Unsigned short bfType; // UNIT bfType;
Unsigned int bfSize; // DWORD bfSize;
Unsigned short bfReserved1; // UINT bfReserved1;
Unsigned short bfReserved2; // UINT bfReserved2;
Unsigned int bfOffBits; // DWORD bfOffBits;
Fseek (fpbmp, 0L, SEEK_SET );
Fread (& bfType, sizeof (char), 2, fpbmp );
Fread (& bfSize, sizeof (char), 4, fpbmp );
Fread (& bfReserved1, sizeof (char), 2, fpbmp );
Fread (& bfReserved2, sizeof (char), 2, fpbmp );
Fread (& bfOffBits, sizeof (char), 4, fpbmp );
Printf ("************************************* * ********** \ n ");
Printf ("************** tagBITMAPFILEHEADER info ************* \ n ");
Printf ("************************************* * ********** \ n ");
Printf ("bfType is % d. \ n", bfType );
Printf ("bfSize is % d. \ n", bfSize );
Printf ("bfReserved1 is % d. \ n", bfReserved1 );
Printf ("bfReserved2 is % d. \ n", bfReserved2 );
Printf ("bfOffBits is % d. \ n", bfOffBits );
}
/* Show bmp file tagBITMAPINFOHEADER info */
Void BMP infoheader (FILE * fpbmp)
{
Unsigned int biSize; // DWORD biSize;
Long biWidth; // LONG biWidth;
Long biHeight; // LONG biHeight;
Unsigned int biPlanes; // WORD biPlanes;
Unsigned int biBitCount; // WORD biBitCount;
Unsigned int biCompression; // DWORD biCompression;
Unsigned int biSizeImage; // DWORD biSizeImage;
Long biXPelsPerMerer; // LONG biXPelsPerMerer;
Long biYPelsPerMerer; // LONG biYPelsPerMerer;
Unsigned int biClrUsed; // DWORD biClrUsed;
Unsigned int biClrImportant; // DWORD biClrImportant;
Fseek (fpbmp, 14L, SEEK_SET );
Fread (& biSize, sizeof (char), 4, fpbmp );
Fread (& biWidth, sizeof (char), 4, fpbmp );
Fread (& biHeight, sizeof (char), 4, fpbmp );
Fread (& biPlanes, sizeof (char), 4, fpbmp );
Fread (& biBitCount, sizeof (char), 4, fpbmp );
Fread (& biCompression, sizeof (char), 4, fpbmp );
Fread (& biSizeImage, sizeof (char), 4, fpbmp );
Fread (& biXPelsPerMerer, sizeof (char), 4, fpbmp );
Fread (& biYPelsPerMerer, sizeof (char), 4, fpbmp );
Fread (& biClrUsed, sizeof (char), 4, fpbmp );
Fread (& biClrImportant, sizeof (char), 4, fpbmp );
Printf ("************************************* * ********** \ n ");
Printf ("************** tagBITMAPINFOHEADER info ************* \ n ");
Printf ("************************************* * ********** \ n ");
Printf ("biSize is % d. \ n", biSize );
Printf ("biWidth is % ld. \ n", biWidth );
Printf ("biHeight is % ld. \ n", biHeight );
Printf ("biPlanes is % d. \ n", biPlanes );
Printf ("biBitCount is % d. \ n", biBitCount );
Printf ("biCompression is % d. \ n", biCompression );
Printf ("biSizeImage is % d. \ n", biSizeImage );
Printf ("biXPelsPerMerer is % ld. \ n", biXPelsPerMerer );
Printf ("biYPelsPerMerer is % ld. \ n", biYPelsPerMerer );
Printf ("biClrUsed is % d. \ n", biClrUsed );
Printf ("biClrImportant is % d. \ n", biClrImportant );
}
/* Show the Data Part of bmp file */
Void BMP datapart (FILE * fpbmp)
{
Int I, j;
Unsigned char BMP pixel [BMP width] [BmpHeight];
Unsigned char * BMP pixeltmp = NULL;
FILE * fpDataBmp;
/* New a file to save the data matrix */
If (fpDataBmp = fopen ("BMP data. dat", "w +") = NULL)
{
Fprintf (stderr, "Failed to construct file BMP data. dat .!!! ");
Exit (1 );
}
Fseek (fpbmp, OffSet, SEEK_SET );
If (BMP pixeltmp = (unsigned char *) malloc (sizeof (char) * BMP width * BmpHeight) = NULL)
{
Fprintf (stderr, "Data allocation failed .!!! \ N ");
Exit (1 );
}
Fread (BMP pixeltmp, sizeof (char), BMP width * BmpHeight, fpbmp );
/* Read the data to Matrix and save it in file BMP data. dat */
For (I = 0; I <BmpHeight; I ++)
{
Fprintf (fpDataBmp, "The data in line %-3d: \ n", I + 1 );
For (j = 0; j <BMP width; j ++)
{
BMP pixel [I] [j] = BMP pixeltmp [BMP width * (BmpHeight-1-i) + j];
// Fwrite (& chartmp, sizeof (char), 1, fpDataBmp );
Fprintf (fpDataBmp, "%-3d", BMP pixel [I] [j]);
If (j + 1) % 32 = 0)
{
Fprintf (fpDataBmp, "\ n ");
}
}
}
/* Used to test the data read is true or false
You can open the file using Matlab to compare the data */
// Printf ("BMP pixel [2] [3] is % d. \ n", BMP pixel [2] [3]);
// Printf ("BMP pixel [20] [30] is % d. \ n", BMP pixel [20] [30]);
Free (BMP pixeltmp );
Fclose (fpDataBmp );
}