需要將攝像頭採集到的圖像數據(純淨的RGB,或BGR數據)加上文件頭後暫存在內存中
如若寫入文件僅需將memcpy換成fwrite即可。
#ifndef RGB2BMP_H
#define RGB2BMP_H
///爲拍攝的RGB數據加上BMP文件頭
///作者:
/// 2014.6.25
///
typedef unsigned char BYTE;
typedef unsigned short WORD;
// BMP圖像各部分說明如下
/***********
第一部分 位圖文件頭
該結構的長度是固定的,爲14個字節,各個域的依次如下:
2byte :文件類型,必須是0x4d42,即字符串"BM"。
4byte :整個文件大小
4byte :保留字,爲0
4byte :從文件頭到實際的位圖圖像數據的偏移字節數。
*************/
typedef struct
{ long imageSize;
long blank;
long startPosition;
}BmpHead;
/*********************
/*********************
第二部分 位圖信息頭
該結構的長度也是固定的,爲40個字節,各個域的依次說明如下:
4byte :本結構的長度,值爲40
4byte :圖像的寬度是多少象素。
4byte :圖像的高度是多少象素。
2Byte :必須是1。
2Byte :表示顏色時用到的位數,常用的值爲1(黑白二色圖)、4(16色圖)、8(256色圖)、24(真彩色圖)。
4byte :指定位圖是否壓縮,有效值爲BI_RGB,BI_RLE8,BI_RLE4,BI_BITFIELDS。Windows位圖可採用RLE4和RLE8的壓縮格式,BI_RGB表示不壓縮。
4byte :指定實際的位圖圖像數據佔用的字節數,可用以下的公式計算出來:
圖像數據 = Width' * Height * 表示每個象素顏色佔用的byte數(即顏色位數/8,24bit圖爲3,256色爲1)
要注意的是:上述公式中的biWidth'必須是4的整數倍(不是biWidth,而是大於或等於biWidth的最小4的整數倍)。
如果biCompression爲BI_RGB,則該項可能爲0。
4byte :目標設備的水平分辨率。
4byte :目標設備的垂直分辨率。
4byte :本圖像實際用到的顏色數,如果該值爲0,則用到的顏色數爲2的(顏色位數)次冪,如顏色位數爲8,2^8=256,即256色的位圖
4byte :指定本圖像中重要的顏色數,如果該值爲0,則認爲所有的顏色都是重要的。
***********************************/
typedef struct
{
long Length;
long width;
long height;
WORD colorPlane;
WORD bitColor;
long zipFormat;
long realSize;
long xPels;
long yPels;
long colorUse;
long colorImportant;
}InfoHead;
/***************************
/***************************
第三部分 調色盤結構 顏色表
對於256色BMP位圖,顏色位數爲8,需要2^8 = 256個調色盤;
對於24bitBMP位圖,各象素RGB值直接保存在圖像數據區,不需要調色盤,不存在調色盤區
rgbBlue: 該顏色的藍色分量。
rgbGreen: 該顏色的綠色分量。
rgbRed: 該顏色的紅色分量。
rgbReserved:保留值。
*****************************/
typedef struct
{ BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
}RGBMixPlate;
/*********************
/*********************
//輸入
// rgb_buffer: RGB24緩衝區指針
// nWidth : 圖片寬度
// nHeight : 圖片高度
// fp2 : 指向加bmp頭後的數據
**************************/
int RGB2BMP1(char *rgb_buffer,int nWidth,int nHeight,char*fp2)
{
BmpHead m_BMPHeader;
char bfType[2]={'B','M'};
m_BMPHeader.imageSize=3*nWidth*nHeight+54;
m_BMPHeader.blank=0;
m_BMPHeader.startPosition=54;
char* temp=fp2;
memcpy(fp2,bfType,2);
fp2+=sizeof(bfType);
memcpy(fp2,&m_BMPHeader,sizeof(m_BMPHeader.imageSize));
fp2+=sizeof(m_BMPHeader.imageSize);
memcpy(fp2,&m_BMPHeader.blank,sizeof(m_BMPHeader.blank));
fp2+=sizeof(m_BMPHeader.blank);
memcpy(fp2,&m_BMPHeader.startPosition,sizeof(m_BMPHeader.startPosition));
fp2+=sizeof(m_BMPHeader.startPosition);
InfoHead m_BMPInfoHeader;
m_BMPInfoHeader.Length=40;
m_BMPInfoHeader.width=nWidth;
m_BMPInfoHeader.height=-nHeight;
m_BMPInfoHeader.colorPlane=1;
m_BMPInfoHeader.bitColor=24;
m_BMPInfoHeader.zipFormat=0;
m_BMPInfoHeader.realSize=3*nWidth*nHeight;
m_BMPInfoHeader.xPels=0;
m_BMPInfoHeader.yPels=0;
m_BMPInfoHeader.colorUse=0;
m_BMPInfoHeader.colorImportant=0;
memcpy(fp2,&m_BMPInfoHeader.Length,sizeof(m_BMPInfoHeader.Length));
fp2+=sizeof(m_BMPInfoHeader.Length);
memcpy(fp2,&m_BMPInfoHeader.width,sizeof(m_BMPInfoHeader.width));
fp2+=sizeof(m_BMPInfoHeader.width);
memcpy(fp2,&m_BMPInfoHeader.height,sizeof(m_BMPInfoHeader.height));
fp2+=sizeof(m_BMPInfoHeader.height);
memcpy(fp2,&m_BMPInfoHeader.colorPlane,sizeof(m_BMPInfoHeader.colorPlane));
fp2+=sizeof(m_BMPInfoHeader.colorPlane);
memcpy(fp2,&m_BMPInfoHeader.bitColor,sizeof(m_BMPInfoHeader.bitColor));
fp2+=sizeof(m_BMPInfoHeader.bitColor);
memcpy(fp2,&m_BMPInfoHeader.zipFormat,sizeof(m_BMPInfoHeader.zipFormat));
fp2+=sizeof(m_BMPInfoHeader.zipFormat);
memcpy(fp2,&m_BMPInfoHeader.realSize,sizeof(m_BMPInfoHeader.realSize));
fp2+=sizeof(m_BMPInfoHeader.realSize);
memcpy(fp2,&m_BMPInfoHeader.xPels,sizeof(m_BMPInfoHeader.xPels));
fp2+=sizeof(m_BMPInfoHeader.xPels);
memcpy(fp2,&m_BMPInfoHeader.yPels,sizeof(m_BMPInfoHeader.yPels));
fp2+=sizeof(m_BMPInfoHeader.yPels);
memcpy(fp2,&m_BMPInfoHeader.colorUse,sizeof(m_BMPInfoHeader.colorUse));
fp2+=sizeof(m_BMPInfoHeader.colorUse);
memcpy(fp2,&m_BMPInfoHeader.colorImportant,sizeof(m_BMPInfoHeader.colorImportant));
fp2+=sizeof(m_BMPInfoHeader.colorImportant);
memcpy(fp2,rgb_buffer,3*nWidth*nHeight);
fp2=temp;
return 1;
}
#endif // RGB2BMP_H