BMP Format Image reading and storage, bmp Format Image
Global variables:
1 # include "stdafx. h "2 # include <windows. h> 3 4/* BMP bitmap data is 4-byte aligned */5 # define WIDTHBYTES (bits) (DWORD) (bits) + 31 )&(~ 31)/8) 6 # define WIDTHBYTES (bits) (DWORD) (bits) + 31)/32) * 4) 7 8 unsigned char * gPBmpBuf; // pointer to the image data 9 int gBmpWidth; // The Image Width 10 int gBmpHeight; // The Image Height 11 RGBQUAD * gPColorTable; // The color table pointer 12 int gBiBitCount; // image type, the number of digits of each pixel 13 char bmp [100] = "E:/test_pic.bmp"; 14 char saveBmp [100] = "E:/save_pic.bmp ";
Read bmp files:
1 bool loadBmp (char * bmp name) 2 {3/* Open the bmp FILE in binary mode */4 FILE * fp = fopen (bmp name, "rb "); 5 if (NULL = fp) 6 {7 printf ("open file \" % s \ "failed \ n", BMP name); 8 return false; 9} 10 11/* Skip bmp file structure */12 fseek (fp, sizeof (BITMAPFILEHEADER), 0); 13 14/* read bmp file information header */15 BITMAPINFOHEADER infoHead; 16 fread (& infoHead, sizeof (BITMAPINFOHEADER), 1, fp); 17 18/* Get the image width, height, pixel digits */19 gBmpWidth = infoHead. biWidth; 20 gBmpHeight = infoHead. biHeight; 21 gBiBitCount = infoHead. biBitCount; 22 23/* get the memory size occupied by pixels in each row (must be a multiple of 4) */24 int lineByte = (gBmpWidth * gBiBitCount/8 + 3)/4*4; 25 26/* grayscale images have a color table, and the color table items are, 24-bit true color images do not use color palette */27 if (8 = gBiBitCount) 28 {29 gPColorTable = (RGBQUAD *) malloc (sizeof (RGBQUAD) * 256); 30 fread (gPColorTable, sizeof (RGBQUAD), 1, fp ); 31} 32 33/* apply for a bitmap data space and store the bitmap data in the memory */34 gPBmpBuf = (unsigned char *) malloc (sizeof (unsigned char) * gBmpHeight * lineByte); 35 fread (gPBmpBuf, 1, gBmpHeight * lineByte, fp); 36 37 fclose (fp); 38 39 return true; 40}
Save the bmp file format:
1 bool storeBmp (char * BMP name, unsigned char * imgBuf, int width, int height, 2 int gBiBitCount, RGBQUAD * gPColorTable) 3 {4/* empty bitmap data operation */5 if (NULL = imgBuf) 6 {7 return false; 8} 9 10/* based on the number of pixels, judge the current color table size */11 int colorTableSize = 0; 12 if (gBiBitCount = 8) 13 {14/* 15 1 byte for blue weight 16 1 byte for green weight 17 1 byte for red weight 18 1 byte for fill character (set to 0) 19 */20 colorTableSize = 1024;/* 4*256 */21} 22 23/* size of each row of image data to be stored (multiples of 4) */24 int lineByte = (gBmpWidth * gBiBitCount/8 + 3)/4*4; 25 26 FILE * fp = fopen (BMP name, "wb "); 27 if (NULL = fp) 28 {29 printf ("creat file failed! \ N "); 30 return false; 31} 32 33/* fill in the bitmap file header structure */34 BITMAPFILEHEADER fileHead; 35 fileHead. bfType = 0x4D42; // bmp Type 36 fileHead. bfSize = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 37 colorTableSize + lineByte * height; 38 fileHead. bfReserved1 = 0; 39 fileHead. bfReserved2 = 0; 40 fileHead. bfOffBits = sizeof (BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + 41 colorTableSize; 42 fwrite (& fileHead, sizeof (BITMAPFILEHEADER), 1, fp ); 43 44/* fill in the bitmap information structure */45 BITMAPINFOHEADER infoHead; 46 infoHead. biBitCount = gBiBitCount; 47 infoHead. biClrImportant = 0; 48 infoHead. biClrUsed = 0; 49 infoHead. biCompression = 0; 50 infoHead. biHeight = height; 51 infoHead. biPlanes = 1; 52 infoHead. biSize = 40; 53 infoHead. biSizeImage = lineByte * height; 54 infoHead. biWidth = width; 55 infoHead. biXPelsPerMeter = 0; 56 infoHead. biYPelsPerMeter = 0; 57 fwrite (& infoHead, sizeof (BITMAPINFOHEADER), 1, fp); 58 59/* fill in the color table */60 if (gBiBitCount = 8) 61 {62 fwrite (& gPColorTable, sizeof (RGBQUAD), 256, fp); 63} 64 65/* write bitmap data into the file */66 fwrite (imgBuf, height * lineByte, 1, fp); 67 68 fclose (fp); 69 70 return true; 71}
Main entry test:
1 int _tmain(int argc, _TCHAR* argv[]) 2 { 3 4 bool ret = false ; 5 6 ret |= loadBmp(bmp); 7 ret |= storeBmp(saveBmp,gPBmpBuf,gBmpWidth,gBmpHeight,gBiBitCount,gPColorTable); 8 9 if(false == ret)10 {11 printf("excut fail! \n");12 }13 else if(true == ret)14 {15 printf("excut success!\n");16 }17 18 if(gBiBitCount == 8)19 {20 free(gPColorTable);21 }22 23 free(gPBmpBuf);24 25 return 0;26 }