24位和8位BMP图片保存纯C代码
BMP图片大家都知道,可以通过查看BMP图片结构使用纯C就可以打开,编辑,处理,保存图片。非常方便使用。
具体BMP结构可以参考:wingdi.h头文件。今天主要在进行删减代码,需要把多余的代码删除,之前写的保存bmp代码就此贴在这里留着以后需要的时候使用:
8位BMP图片保存代码:
1 BOOL SaveMatrixToGrayBmp(char* sBMPFile, //name of BMP file 2 BYTE *pMatrix, // pointer to Matrix Bits array 3 int bmpWidth , 4 int bmpHeight // width and height of the bmp 5 ) 6 { 7 FILE* fp; 8 int bytesofScanLine,i,j; 9 10 DWORD dwFileSize; 11 BITMAPFILEHEADER bmfHeader; 12 BITMAPINFOHEADER bmiHeader; 13 RGBQUAD bmiColors[256]; 14 BYTE tempb=0; 15 16 fp = fopen(sBMPFile, "wb"); 17 bytesofScanLine=(bmpWidth % 4==0) ? bmpWidth: ((bmpWidth+3)/4*4); 18 dwFileSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256+bytesofScanLine*bmpHeight; 19 20 bmfHeader.bfType=((WORD) ('M' << 8) | 'B'); 21 bmfHeader.bfSize=dwFileSize; 22 bmfHeader.bfReserved1=0; 23 bmfHeader.bfReserved2=0; 24 bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256; 25 26 fwrite(&bmfHeader,sizeof(bmfHeader), 1, fp); 27 28 // fill the bmp file Infomation Header. 29 bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 30 bmiHeader.biWidth=bmpWidth; 31 bmiHeader.biHeight=bmpHeight; 32 bmiHeader.biPlanes=1; 33 bmiHeader.biBitCount=8; 34 bmiHeader.biCompression=BI_RGB; 35 bmiHeader.biSizeImage=0; 36 bmiHeader.biXPelsPerMeter=0; 37 bmiHeader.biYPelsPerMeter=0; 38 bmiHeader.biClrUsed=256; 39 bmiHeader.biClrImportant=256; 40 41 fwrite(&bmiHeader,sizeof(bmiHeader), 1, fp); 42 43 // fill the color tables. 44 for (i=0; i<256 ; i++) 45 { 46 bmiColors[i].rgbBlue=(BYTE)(i); 47 bmiColors[i].rgbGreen=(BYTE)(i); 48 bmiColors[i].rgbRed=(BYTE)(i); 49 bmiColors[i].rgbReserved=0; 50 } 51 52 fwrite(&(bmiColors[0]),sizeof(RGBQUAD)*256, 1, fp); 53 54 // fill the BMP data to file 55 for (j=bmpHeight-1 ; j>=0 ; j--) 56 { 57 fwrite((pMatrix+(j*bmpWidth)), 1, bmpWidth, fp); 58 for (i=0; i<(bytesofScanLine-bmpWidth) ; i++) 59 fwrite(&tempb, 1, 1, fp); 60 } 61 62 fclose(fp); 63 return true; 64 }
24位BMP图片保存代码:
1 BOOL SaveRGBMatrixToColorBmp(char* sBMPFile, //name of BMP file 2 BYTE *pRGBMat, // pointer to DIB bits array 3 int bmpWidth ,//width of the bmp 4 int bmpHeight // height of the bmp 5 ) 6 { 7 // create the empty 24bit color BMP file 8 FILE* fp; 9 fp = fopen(sBMPFile, "wb"); 10 DWORD dwFileSize; 11 BITMAPFILEHEADER bmfHeader; 12 BITMAPINFOHEADER bmiHeader; 13 14 int bytesofScanLine,i,j; 15 bytesofScanLine=((bmpWidth*3) % 4==0) ? (bmpWidth*3): (4*int(bmpWidth*3/4)+4) ; 16 dwFileSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) 17 +bytesofScanLine*fabs((double)bmpHeight); 18 19 // fill the bmp file Header; 20 bmfHeader.bfType=((WORD) ('M' << 8) | 'B'); 21 bmfHeader.bfSize=dwFileSize; 22 bmfHeader.bfReserved1=0; 23 bmfHeader.bfReserved2=0; 24 bmfHeader.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER) 25 fwrite(&bmfHeader,sizeof(bmfHeader), 1, fp); 26 27 // fill the bmp file Infomation Header. 28 bmiHeader.biSize=sizeof(BITMAPINFOHEADER); 29 bmiHeader.biWidth=bmpWidth; 30 bmiHeader.biHeight= fabs((double)bmpHeight); 31 bmiHeader.biPlanes=1; 32 bmiHeader.biBitCount=24; 33 bmiHeader.biCompression=BI_RGB; 34 bmiHeader.biSizeImage=0; 35 bmiHeader.biXPelsPerMeter=0; 36 bmiHeader.biYPelsPerMeter=0; 37 bmiHeader.biClrUsed=0; 38 bmiHeader.biClrImportant=0; 39 fwrite(&bmiHeader,sizeof(bmiHeader), 1, fp); 40 41 // fill the BMP data to file 42 BYTE dummy = 0; 43 BYTE tempb=0; 44 for (j=fabs((double)bmpHeight)-1; j>=0 ; j--) 45 { 46 fwrite((pRGBMat+j*3*bmpWidth), bmpWidth*3, 1, fp); 47 if (bmpWidth*3 != bytesofScanLine) 48 { 49 for (i = 0; i<bytesofScanLine - bmpWidth*3; i++) 50 fwrite(&dummy, 1, 1, fp); 51 } 52 53 } 54 fclose(fp); 55 return true; 56 }
使用例子:
SaveMatrixToGrayBmp("./mid/Gray.bmp", Gray, iWidth, iHeight)
SaveRGBMatrixToColorBmp("./mid/Img.bmp", Img, iWidth, iHeight)