bmp格式圖像的讀寫(xiě)函數(shù)(對(duì)一個(gè)開(kāi)源代碼的封裝)
在網(wǎng)上看到一段讀寫(xiě)bmp格式圖像的代碼,本文對(duì)這段代碼分成兩個(gè)函數(shù)封裝起來(lái)方便使用,一個(gè)函數(shù)是讀取bmp格式的圖像,一個(gè)是向指定文件寫(xiě)入bmp格式的圖像。
我們不需要知道這段代碼是如何讀取bmp格式圖像的,不需要知道bmp格式的圖像時(shí)如何存儲(chǔ)的,我們只需要知道有三個(gè)參數(shù)可以確定圖像的尺寸大小,他們是圖像的寬度、高度、通道數(shù)(例如灰度圖像有一個(gè)通道,rgb圖像有三個(gè)通道(rgb))。圖像包含高度X寬度個(gè)像素,每個(gè)像素有相同的通道,他們?cè)趦?nèi)存中按照一定的順序存儲(chǔ),例如三通道bmp圖像,在內(nèi)存中圖像行存儲(chǔ),第一個(gè)像素存儲(chǔ)圖像左下角的像素,第二個(gè)像素存儲(chǔ)圖像左下角向右移動(dòng)一個(gè)單位后的像素,依次類(lèi)推。
函數(shù)定義如下:
bool abReadImage(int &rows, int &cols, int &nChannels, io_byte *&imData, const char *imFileName)
{
imData = NULL;
int err_code=0;
try {
int n;
bmp_in in;
if ((err_code = bmp_in__open(&in,imFileName)) != 0)
throw err_code;
cols = in.cols; rows = in.rows; nChannels = in.num_components;
io_byte *dp;
imData = new io_byte[cols*rows*nChannels];
for (dp=imData, n=rows; n > 0; n--, dp+=cols*nChannels)
if ((err_code = bmp_in__get_line(&in,dp)) != 0)
throw err_code;
bmp_in__close(&in);
}
catch (int exc) {
if (exc == IO_ERR_NO_FILE)
fprintf(stderr,"Cannot open input file <%s>.n", imFileName);
else if (exc == IO_ERR_FILE_HEADER)
fprintf(stderr,"Error encountered while parsing BMP file header.n");
else if (exc == IO_ERR_UNSUPPORTED)
fprintf(stderr,"Input uses an unsupported BMP file format.n Current "
"simple example supports only 8-bit and 24-bit data.n");
else if (exc == IO_ERR_FILE_TRUNC)
fprintf(stderr,"Input file <%s> truncated unexpectedly.n", imFileName);
else if (exc == IO_ERR_FILE_NOT_OPEN)
fprintf(stderr,"Trying to access a file which is not open!(?)n");
return false;
}
return true;
}
使用此函數(shù)必須要包含頭文件:io_bmp.h,這個(gè)頭文件以及他聲明的函數(shù)或者類(lèi)型的實(shí)現(xiàn)可以在這里下載到。
讀圖像函數(shù)輸入:
imFileName:輸入圖像的文件名。
讀圖像函數(shù)輸出:
rows:圖像的行數(shù),或者說(shuō)圖像的高度。
cols:圖像的列數(shù),或者說(shuō)圖像的寬度。
nChannels:圖像的通道數(shù)(1或者3,暫時(shí)不支持其他的通道)。
imData:存儲(chǔ)圖像像素的數(shù)組,注意,這個(gè)數(shù)組的內(nèi)存是在函數(shù)內(nèi)部申請(qǐng)的,在使用完圖像后,記得釋放掉這塊內(nèi)存。
函數(shù)定義如下:
bool abWriteImage(const int rows, const int cols, const int nChannels, io_byte *imData, const char *imFileName)
{
int err_code=0;
try {
bmp_out out;
if ((err_code = bmp_out__open(&out,imFileName,cols,rows,nChannels)) != 0)
throw err_code;
io_byte *dp;
int n;
for (dp=imData, n=rows; n > 0; n--, dp+=cols*nChannels)
bmp_out__put_line(&out,dp);
bmp_out__close(&out);
}
catch (int exc) {
if (exc == IO_ERR_NO_FILE)
fprintf(stderr,"Cannot open the output file <%s>.n", imFileName);
else if (exc == IO_ERR_FILE_HEADER)
fprintf(stderr,"Error encountered while parsing BMP file header.n");
else if (exc == IO_ERR_UNSUPPORTED)
fprintf(stderr,"Input uses an unsupported BMP file format.n Current "
"simple example supports only 8-bit and 24-bit data.n");
else if (exc == IO_ERR_FILE_TRUNC)
fprintf(stderr,"output file <%s> truncated unexpectedly.n", imFileName);
else if (exc == IO_ERR_FILE_NOT_OPEN)
fprintf(stderr,"Trying to access a file which is not open!(?)n");
return false;
}
return true;
}
使用此函數(shù)必須要包含頭文件:io_bmp.h,這個(gè)頭文件以及他聲明的函數(shù)或者類(lèi)型的實(shí)現(xiàn)可以在這里下載到。
寫(xiě)圖像函數(shù)輸入:
imFileName:要寫(xiě)入磁盤(pán)的圖像文件名。
rows:圖像的行數(shù),或者說(shuō)圖像的高度。
cols:圖像的列數(shù),或者說(shuō)圖像的寬度。
nChannels:圖像的通道數(shù)(1或者3,暫時(shí)不支持其他的通道)。
imData:存儲(chǔ)圖像像素的數(shù)組。實(shí)驗(yàn)說(shuō)明
根據(jù)你使用的編譯相關(guān)工具不同,給出幾點(diǎn)說(shuō)明:
1、MSVS。 在你使用這兩個(gè)函數(shù)的項(xiàng)目中要添加你下載的io_bmp.h和io_bmp.cpp。這是比較簡(jiǎn)單的一種使用方法。
2、如果你在linux上編譯。記得將你下載的兩個(gè)文件加入到你的工程中,還要記得對(duì)文件的格式進(jìn)行下轉(zhuǎn)換(可以使用dos2unix這樣的小工具)。