typedef struct /**** BMP file header structure ****/ { unsigned int bfSize; /* Size of file */ unsigned short bfReserved1; /* Reserved */ unsigned short bfReserved2; /* ... */ unsigned int bfOffBits; /* Offset to bitmap data */ } MyBITMAPFILEHEADER;
typedef struct /**** BMP file info structure ****/ { unsigned int biSize; /* Size of info header */ int biWidth; /* Width of image */ int biHeight; /* Height of image */ unsigned short biPlanes; /* Number of color planes */ unsigned short biBitCount; /* Number of bits per pixel */ unsigned int biCompression; /* Type of compression to use */ unsigned int biSizeImage; /* Size of image data */ int biXPelsPerMeter; /* X pixels per meter */ int biYPelsPerMeter; /* Y pixels per meter */ unsigned int biClrUsed; /* Number of colors used */ unsigned int biClrImportant; /* Number of important colors */ } MyBITMAPINFOHEADER;
顏色表用於說明位圖中的顏色,它有若干個表項,每個表項是一個RGBQUAD類型的結構,定義一種顏色。RGBQUAD結構的定義以下:ide
typedef struct tagRGBQUAD{ BYTE rgbBlue;//藍色的亮度(值範圍爲0-255) BYTE rgbGreen;//綠色的亮度(值範圍爲0-255) BYTE rgbRed;//紅色的亮度(值範圍爲0-255) BYTE rgbReserved;//保留,必須爲0 }RGBQUAD;
顏色表中的RGBQUAD結構數據的個數由biBitCount來肯定:當biBitCount=1,4,8時,分別爲2,16,256個表項;當biBitCount=24時,沒有顏色表項。ui
位圖數據記錄了位圖的每個像素值,記錄順序是在掃描行內是從左到右,掃描行之間是從下到上。位圖的一個像素值所佔的字節數:spa
當biBitCount=1時,8個像素佔1個字節;.net
當biBitCount=4時,2個像素佔1個字節;code
當biBitCount=8時,1個像素佔1個字節;htm
當biBitCount=24時,1個像素佔3個字節,按順序分別爲B,G,R;blog
void CDecVideoFilter::MySaveBmp(const char *filename,unsigned char *rgbbuf,int width,int height) { MyBITMAPFILEHEADER bfh; MyBITMAPINFOHEADER bih; /* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */ unsigned short bfType=0x4d42; bfh.bfReserved1 = 0; bfh.bfReserved2 = 0; bfh.bfSize = 2+sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+width*height*3; bfh.bfOffBits = 0x36; bih.biSize = sizeof(BITMAPINFOHEADER); bih.biWidth = width; bih.biHeight = height; bih.biPlanes = 1; bih.biBitCount = 24; bih.biCompression = 0; bih.biSizeImage = 0; bih.biXPelsPerMeter = 5000; bih.biYPelsPerMeter = 5000; bih.biClrUsed = 0; bih.biClrImportant = 0; FILE *file = fopen(filename, "wb"); if (!file) { printf("Could not write file\n"); return; } /*Write headers*/ fwrite(&bfType,sizeof(bfType),1,file); fwrite(&bfh,sizeof(bfh),1, file); fwrite(&bih,sizeof(bih),1, file); fwrite(rgbbuf,width*height*3,1,file); fclose(file); }