#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 bmpFileTest(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 BmpWidthHeight(FILE* fpbmp);
/* Show bmp file tagBITMAPFILEHEADER info */
void bmpFileHeader(FILE* fpbmp);
/* Show bmp file tagBITMAPINFOHEADER info */
void bmpInfoHeader(FILE* fpbmp);
/* Show the Data Part of bmp file */
void bmpDataPart(FILE* fpbmp);
unsigned int OffSet = 0; // OffSet from Header part to Data Part
long BmpWidth = 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;
}
bmpFileTest(fpbmp); //Test the file is bmp file or not
bmpHeaderPartLength(fpbmp); //Get the length of Header Part
BmpWidthHeight(fpbmp); //Get the width and width of the Data Part
//bmpFileHeader(fpbmp); //Show the FileHeader Information
//bmpInfoHeader(fpbmp); //Show the InfoHeader Information
bmpDataPart(fpbmp); //Reserve the data to file
fclose(fpbmp);
return 0;
}
/* Test the file is bmp file or not */
void bmpFileTest(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 BmpWidthHeight(FILE* fpbmp)
{
fseek(fpbmp, 18L, SEEK_SET);
fread(&BmpWidth, sizeof(char), 4, fpbmp);
fread(&BmpHeight, sizeof(char), 4, fpbmp);
//printf("The Width of the bmp file is %ld.\n", BmpWidth);
//printf("The Height of the bmp file is %ld.\n", BmpHeight);
}
/* Show bmp file tagBITMAPFILEHEADER info */
void bmpFileHeader(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 bmpInfoHeader(FILE* fpbmp)
{
unsigned int biSize; // DWORD biSize;
long biWidth; // LONG biWidth;
long biHeight; // LONG biHeight;
unsigned int biPlanes; // WORD biPlanes;
unsigned int biBitCount; // WORD