bmp图生成

init

int width = 256;
int height = 256;
std::uint16_t count = 8;

_bmpHead.fileInfo.type[0] = 'B';
_bmpHead.fileInfo.type[1] = 'M';
_bmpHead.fileInfo.reserved1 = 0;
_bmpHead.fileInfo.reserved2 = 0;
_bmpHead.fileInfo.size = sizeof(_bmpHead) + sizeimage;
_bmpHead.fileInfo.offset = sizeof(_bmpHead);

_bmpHead.bitMapInfo.size = 40;
_bmpHead.bitMapInfo.width = width;
_bmpHead.bitMapInfo.height = height;
_bmpHead.bitMapInfo.planes = 1;
_bmpHead.bitMapInfo.count = count;
_bmpHead.bitMapInfo.compression = 0;
_bmpHead.bitMapInfo.sizeimage = sizeimage;
_bmpHead.bitMapInfo.xmeter = 0;
_bmpHead.bitMapInfo.ymeter = 0;
_bmpHead.bitMapInfo.cused = 0;
_bmpHead.bitMapInfo.cimportant = 0;

save

/*
 *      bmp文件头(bmp file header):共14字节;
 *      位图信息头(bitmap information):共40字节;
 *      调色板(color palette):可选;
 *      位图数据;
 */


// 填充计算
int bytesPerLine = ((_bmpHead.bitMapInfo.width * _bmpHead.bitMapInfo.count + 31) >> 5) << 2;

// 重新计算位图的大小
_bmpHead.bitMapInfo.sizeimage = bytesPerLine * _bmpHead.bitMapInfo.height * _bmpHead.bitMapInfo.count / 8;
// 重新计算文件大小
_bmpHead.fileInfo.size = _bmpHead.bitMapInfo.sizeimage + sizeof(BmpHead);

const int colorPalette = 1024;                                               // 调色板大小
if (_bmpHead.bitMapInfo.count == 8) // 灰度图
{
    _bmpHead.fileInfo.size += colorPalette;
    _bmpHead.fileInfo.offset = sizeof(BmpHead) + colorPalette;
}
posted @ 2023-03-16 10:50  thomas_blog  阅读(35)  评论(0编辑  收藏  举报