rgb保存成bmp

二值:
void SaveAsBmp( unsigned char *data, size_t width, size_t height,char *szFileName,RGBQUAD *rgbQuad /*= NULL*/ )
{
	char path[MAX_PATH];
	GetCurrentDirectoryA(MAX_PATH, path);
	strcat(path, szFileName);
	RGBQUAD _rgbquad[256];
 	BYTE *_data = new BYTE[width*height];
	for (int c = 0; c < width * height; c++)
	{
		unsigned char temp = *(data+c);
 		*(_data+c) = (BYTE)(temp);
// 		TRACE1("%d %d\n", *(_data+c), temp);
	}
 	
	if (rgbQuad == NULL)
	{
		rgbQuad = _rgbquad;
	}
	for (int i = 0; i < 256; i++)
	{
		rgbQuad[i].rgbBlue = i;
		rgbQuad[i].rgbGreen = i;
		rgbQuad[i].rgbRed = i;
		rgbQuad[i].rgbReserved = 0;
	}
	BITMAPFILEHEADER bmpfilehead = {0};
	bmpfilehead.bfType = MAKEWORD('B','M');
	bmpfilehead.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*255;
	bmpfilehead.bfSize = bmpfilehead.bfOffBits+(width*height);

	BITMAPINFOHEADER bmpinfoheader = {0};
	bmpinfoheader.biSize = sizeof(BITMAPINFOHEADER);
	bmpinfoheader.biBitCount = 8;//位数
	bmpinfoheader.biClrImportant = 0;
	bmpinfoheader.biClrUsed = 0;
	bmpinfoheader.biCompression = BI_RGB;
	bmpinfoheader.biHeight = height;
	bmpinfoheader.biPlanes = 1;
	bmpinfoheader.biSizeImage = height*width;
	bmpinfoheader.biWidth = width;
	bmpinfoheader.biXPelsPerMeter = 0;
	bmpinfoheader.biYPelsPerMeter = 0;

	FILE *file = NULL;
	__try
	{
		if((file = fopen(path,"wb")) == NULL)
			return;
		size_t w = fwrite(&bmpfilehead,sizeof(BYTE),sizeof(bmpfilehead),file);
		// 	fflush(file);
		w = fwrite(&bmpinfoheader,sizeof(BYTE),sizeof(BITMAPINFOHEADER),file);
		// 	fflush(file);
		w = fwrite(rgbQuad,sizeof(BYTE),sizeof(RGBQUAD)*256,file);
		// 	fflush(file);
		w = fwrite(_data,sizeof(BYTE),width*height,file);
		// 	fflush(file);
	}
	__finally
	{
		if (file)
			fclose(file);
		delete _data;
	}
}


RGB:

	int widthStep = (((iWidth * 24) + 31) & (~31)) / 8 ; //每行实际占用的大小(每行都被填充到一个4字节边界)
	int DIBSize = widthStep * iHeight ;  //buffer的大小 (字节为单位)

	BYTE *data = new BYTE[sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + DIBSize];
	BITMAPFILEHEADER *bhh = (BITMAPFILEHEADER *)data;
	bhh->bfType = ((WORD) ('M' << 8) | 'B');  //'BM'
	bhh->bfSize = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + widthStep * iHeight;
	bhh->bfReserved1 = 0;
	bhh->bfReserved2 = 0;
	bhh->bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);

	BITMAPINFOHEADER *bih = (BITMAPINFOHEADER *)(data + sizeof(BITMAPFILEHEADER));
	bih->biSize=40;       // header size
	bih->biWidth=iWidth;
	bih->biHeight=iHeight;
	bih->biPlanes=1;
	bih->biBitCount=24;     // RGB encoded, 24 bit
	bih->biCompression=BI_RGB;   // no compression 非压缩
	bih->biSizeImage=widthStep*iHeight*3;
	bih->biXPelsPerMeter=0;
	bih->biYPelsPerMeter=0;
	bih->biClrUsed=0;
	bih->biClrImportant=0; 

	memcpy(data + sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER), pBuffer, DIBSize);

	TCHAR *BMPFileName = _T("d:/sss.bmp");
	CFile file;

	if(file.Open(BMPFileName,CFile::modeWrite | CFile::modeCreate))
	{
		file.Write((LPSTR)data, sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER) + DIBSize);
		file.Close();
		return true;
	}
	return false;


你可能感兴趣的:(c++)