使用Boost.Gil将BMP图片保存为JPG

void test_gil(char* lpFileName)
{
	BITMAPFILEHEADER bfh;
	BITMAPINFO BitmapInfo;
	FILE* pFile = fopen( lpFileName, "rb" );
	fread( &bfh, sizeof(bfh), 1, pFile );
	fread( &BitmapInfo, sizeof(BITMAPINFOHEADER), 1, pFile );

	int channels = BitmapInfo.bmiHeader.biBitCount / 8;
	if( channels != 3 )
	{
		fclose(pFile);
		return;
	}
	unsigned char* pData = new unsigned char[BitmapInfo.bmiHeader.biWidth * BitmapInfo.bmiHeader.biHeight * 3];
	fread( pData, sizeof(char), BitmapInfo.bmiHeader.biWidth * BitmapInfo.bmiHeader.biHeight * 3, pFile );

	namespace boost_gil = boost::gil;
	boost_gil::bgr8_image_t img;
	img.recreate(BitmapInfo.bmiHeader.biWidth, BitmapInfo.bmiHeader.biHeight);

	boost_gil::bgr8_view_t vSrc = boost_gil::view(img);

	int dwPitch = BitmapInfo.bmiHeader.biWidth * channels;
	while( dwPitch % 4 != 0 )
		dwPitch++;

	unsigned char* p = pData;
	boost_gil::bgr8_view_t::iterator it = vSrc.begin();
	for( int i = 0; i < BitmapInfo.bmiHeader.biHeight; ++i )
	{
		for( int j = 0; j < BitmapInfo.bmiHeader.biWidth; ++j, p += channels )
		{
			boost_gil::bgr8_pixel_t pix(*p, *(p + 1), *(p + 2));
			*it++ = pix;
		}
	}
	boost_gil::bgr8_view_t v = boost_gil::flipped_up_down_view(vSrc);
	boost_gil::jpeg_write_view("test.jpg", v);
	fclose(pFile);
	delete[] pData;
}

转载于:https://www.cnblogs.com/LinuxHunter/archive/2010/12/19/1910346.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值