RGB转BMP——格式转换

RGB转BMP

void RGB2BMP(unsigned char* data, int w, int h, std::string name)
{
    
    
	FILE* fp = fopen(name.c_str(), "wb");
	typedef struct
	{
    
    
		uint16 bmptype;
		uint bmpsize;
		uint16 bmpre1;
		uint16 bmpre2;
		uint bmpseek;
	}bmphead;
	typedef struct
	{
    
    
		uint infosize;
		uint iw;
		uint ih;
		uint16 iplanes;
		uint16 ibitcount;
		uint iCom;
		uint iimagesize;
		uint ix;
		uint iy;
		uint icolorindex;
		uint icolorimp;
	}infohead;
	bmphead bmph = {
    
     0 };
	bmph.bmpsize = w * h * 4;
	bmph.bmptype = (uint16)0x4d42;
	bmph.bmpre1 = 0;
	bmph.bmpre2 = 0;
	bmph.bmpseek = sizeof(bmphead) + sizeof(infohead) - 2;
	char* tmp = (char*)&bmph;
	fwrite(tmp, 2, 1, fp);
	tmp += 4;
	fwrite(tmp, sizeof(bmphead) - 4, 1, fp);

	infohead ih = {
    
     0 };
	ih.ibitcount = 32;
	ih.icolorimp = 0;
	ih.icolorindex = 0;
	ih.iCom = 0;
	ih.iimagesize = 54 + bmph.bmpsize;
	ih.infosize = sizeof(infohead);
	ih.iplanes = 1;
	ih.iw = w;
	ih.ih = -h;
	ih.ix = 2835;
	ih.iy = 2835;
	fwrite(&ih, sizeof(infohead), 1, fp);
	fwrite(data, bmph.bmpsize, 1, fp);
	fclose(fp);

猜你喜欢

转载自blog.csdn.net/kris_paul/article/details/126826481
今日推荐