bmp 是 BitMap(位圖)的簡稱,也是所有 Windows 上圖片顯示的基礎——任何圖片格式都必須先轉換成 bmp 才能最終呈現。正因為如此,bmp 文件的讀寫變得非常重要。
然而很多人是借助 MFC 類、C# 庫函式、OpenCV、OpenGL 等現成工具來讀寫 bmp。試想一下,如果你要在 DSP、FPGA 之類的嵌入式設備上處理 bmp,總不能硬塞一套龐大的 MFC、C# 類庫進去吧?其實我們完全可以拋開這些繁瑣的類庫與 API,僅僅用 C 語言寫幾個函式,就實現 bmp 的讀寫。本文的意圖正在於此。
一個完整的 bmp 位圖文件可分為三部分:
要特別提醒的是,bmp 顏色陣列的像素數據是以左下角為原點儲存的。也就是說,當你把一張圖顯示在螢幕上時,實際寫入文件的卻是這張圖最左下角的像素;接著按照從左到右、從下到上的順序依次存放。若保存的是 3 通道位圖(也就是通常說的彩圖),順序為 B0G0R0 B1G1R1 B2G2R2…,同時還必須處理 4 字節對齊的問題——每行像素的位元組數必須補到 4 的倍數。理解這些基本概念之後,自己動手寫讀寫函式並非難事。
#ifndef CHENLEECV_H
#define CHENLEECV_H
typedef struct
{
//unsigned short bfType;
unsigned long bfSize;
unsigned short bfReserved1;
unsigned short bfReserved2;
unsigned long bfOffBits;
} ClBitMapFileHeader;
typedef struct
{
unsigned long biSize;
long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned long biCompression;
unsigned long biSizeImage;
long biXPelsPerMeter;
long biYPelsPerMeter;
unsigned long biClrUsed;
unsigned long biClrImportant;
} ClBitMapInfoHeader;
typedef struct
{
unsigned char rgbBlue; // 該顏色的藍色分量
unsigned char rgbGreen; // 該顏色的綠色分量
unsigned char rgbRed; // 該顏色的紅色分量
unsigned char rgbReserved; // 保留值
} ClRgbQuad;
typedef struct
{
int width;
int height;
int channels;
unsigned char* imageData;
} ClImage;
ClImage* clLoadImage(char* path);
bool clSaveImage(char* path, ClImage* bmpImg);
#endif
#include "chenLeeCV.h"
#include <stdio.h>
#include <stdlib.h>
ClImage* clLoadImage(char* path)
{
ClImage* bmpImg;
FILE* pFile;
unsigned short fileType;
ClBitMapFileHeader bmpFileHeader;
ClBitMapInfoHeader bmpInfoHeader;
int channels = 1;
int width = 0;
int height = 0;
int step = 0;
int offset = 0;
unsigned char pixVal;
ClRgbQuad* quad;
int i, j, k;
bmpImg = (ClImage*)malloc(sizeof(ClImage));
pFile = fopen(path, "rb");
if (!pFile)
{
free(bmpImg);
return NULL;
}
fread(&fileType, sizeof(unsigned short), 1, pFile);
if (fileType == 0x4D42)
{
// 0x4D42 即 "BM",確認是 bmp 檔
fread(&bmpFileHeader, sizeof(ClBitMapFileHeader), 1, pFile);
fread(&bmpInfoHeader, sizeof(ClBitMapInfoHeader), 1, pFile);
if (bmpInfoHeader.biBitCount == 8)
{
// 該文件有調色板,即該位圖為非真彩色
channels = 1;
width = bmpInfoHeader.biWidth;
height = bmpInfoHeader.biHeight;
offset = (channels*width)%4;
if (offset != 0)
{
offset = 4 - offset;
}
bmpImg->width = width;
bmpImg->height = height;
bmpImg->channels = 1;
bmpImg->imageData = (unsigned char*)malloc(sizeof(unsigned char)*width*height);
step = channels*width;
quad = (ClRgbQuad*)malloc(sizeof(ClRgbQuad)*256);
fread(quad, sizeof(ClRgbQuad), 256, pFile);
free(quad);
for (i=0; i<height; i++)
{
for (j=0; j<width; j++)
{
fread(&pixVal, sizeof(unsigned char), 1, pFile);
bmpImg->imageData[(height-1-i)*step+j] = pixVal;
}
if (offset != 0)
{
for (j=0; j<offset; j++)
{
fread(&pixVal, sizeof(unsigned char), 1, pFile);
}
}
}
}
else if (bmpInfoHeader.biBitCount == 24)
{
// 該位圖為真彩色
channels = 3;
width = bmpInfoHeader.biWidth;
height = bmpInfoHeader.biHeight;
bmpImg->width = width;
bmpImg->height = height;
bmpImg->channels = 3;
bmpImg->imageData = (unsigned char*)malloc(sizeof(unsigned char)*width*3*height);
step = channels*width;
offset = (channels*width)%4;
if (offset != 0)
{
offset = 4 - offset;
}
for (i=0; i<height; i++)
{
for (j=0; j<width; j++)
{
for (k=0; k<3; k++)
{
fread(&pixVal, sizeof(unsigned char), 1, pFile);
bmpImg->imageData[(height-1-i)*step+j*3+k] = pixVal;
}
}
if (offset != 0)
{
for (j=0; j<offset; j++)
{
fread(&pixVal, sizeof(unsigned char), 1, pFile);
}
}
}
}
}
return bmpImg;
}
bool clSaveImage(char* path, ClImage* bmpImg)
{
FILE *pFile;
unsigned short fileType;
ClBitMapFileHeader bmpFileHeader;
ClBitMapInfoHeader bmpInfoHeader;
int step;
int offset;
unsigned char pixVal = '\0';
int i, j;
ClRgbQuad* quad;
pFile = fopen(path, "wb");
if (!pFile)
{
return false;
}
fileType = 0x4D42;
fwrite(&fileType, sizeof(unsigned short), 1, pFile);
if (bmpImg->channels == 3)// 24位,三通道,彩圖
{
step = bmpImg->channels*bmpImg->width;
offset = step%4;
if (offset != 4)
{
step += 4-offset;
}
bmpFileHeader.bfSize = bmpImg->height*step + 54;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = 54;
fwrite(&bmpFileHeader, sizeof(ClBitMapFileHeader), 1, pFile);
bmpInfoHeader.biSize = 40;
bmpInfoHeader.biWidth = bmpImg->width;
bmpInfoHeader.biHeight = bmpImg->height;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = 24;
bmpInfoHeader.biCompression = 0;
bmpInfoHeader.biSizeImage = bmpImg->height*step;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 0;
bmpInfoHeader.biClrImportant = 0;
fwrite(&bmpInfoHeader, sizeof(ClBitMapInfoHeader), 1, pFile);
for (i=bmpImg->height-1; i>-1; i--)
{
for (j=0; j<bmpImg->width; j++)
{
pixVal = bmpImg->imageData[i*bmpImg->width*3+j*3];
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
pixVal = bmpImg->imageData[i*bmpImg->width*3+j*3+1];
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
pixVal = bmpImg->imageData[i*bmpImg->width*3+j*3+2];
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
}
if (offset!=0)
{
for (j=0; j<offset; j++)
{
pixVal = 0;
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
}
}
}
}
else if (bmpImg->channels == 1)// 8位,單通道,灰度圖
{
step = bmpImg->width;
offset = step%4;
if (offset != 4)
{
step += 4-offset;
}
bmpFileHeader.bfSize = 54 + 256*4 + bmpImg->width;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfOffBits = 54 + 256*4;
fwrite(&bmpFileHeader, sizeof(ClBitMapFileHeader), 1, pFile);
bmpInfoHeader.biSize = 40;
bmpInfoHeader.biWidth = bmpImg->width;
bmpInfoHeader.biHeight = bmpImg->height;
bmpInfoHeader.biPlanes = 1;
bmpInfoHeader.biBitCount = 8;
bmpInfoHeader.biCompression = 0;
bmpInfoHeader.biSizeImage = bmpImg->height*step;
bmpInfoHeader.biXPelsPerMeter = 0;
bmpInfoHeader.biYPelsPerMeter = 0;
bmpInfoHeader.biClrUsed = 256;
bmpInfoHeader.biClrImportant = 256;
fwrite(&bmpInfoHeader, sizeof(ClBitMapInfoHeader), 1, pFile);
quad = (ClRgbQuad*)malloc(sizeof(ClRgbQuad)*256);
for (i=0; i<256; i++)
{
quad[i].rgbBlue = i;
quad[i].rgbGreen = i;
quad[i].rgbRed = i;
quad[i].rgbReserved = 0;
}
fwrite(quad, sizeof(ClRgbQuad), 256, pFile);
free(quad);
for (i=bmpImg->height-1; i>-1; i--)
{
for (j=0; j<bmpImg->width; j++)
{
pixVal = bmpImg->imageData[i*bmpImg->width+j];
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
}
if (offset!=0)
{
for (j=0; j<offset; j++)
{
pixVal = 0;
fwrite(&pixVal, sizeof(unsigned char), 1, pFile);
}
}
}
}
fclose(pFile);
return true;
}
#include "stdafx.h"
#include "chenLeeCV.h"
int _tmain(int argc, _TCHAR* argv[])
{
ClImage* img = clLoadImage("c:/test.bmp");
bool flag = clSaveImage("c:/result.bmp", img);
if(flag)
{
printf("save ok... \n");
}
while(1);
return 0;
}
讀取端先用 2 字節的 fileType 比對 0x4D42(也就是 ASCII 的 "BM")確認格式,再依序讀入兩個 header;8 位圖要額外吃掉 256 項調色板,24 位圖則直接逐像素讀 B、G、R 三個分量。兩種情況都要把 (channels*width)%4 算出來的補位字節讀掉,並用 height-1-i 把左下角原點翻成左上角原點,才符合一般影像處理的座標習慣。
寫入端流程相反:先寫 "BM"、header,8 位圖還要補一張灰階調色板,然後從最後一列往前把像素寫回文件,每行結尾補 0 湊足 4 字節對齊。這份程式碼僅供參考,若要移植到嵌入式平台,建議把 long 換成固定寬度型別(如 uint32_t),因為 BMP 的 header 欄位寬度是固定的,而 long 在不同平台並不一致;同時補上 fclose 與 free 的收尾,避免檔案未落盤與記憶體洩漏。如有錯誤,歡迎指正。