• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LinuxHunter
原创+技巧=无坚不摧
博客园    首页    新随笔    联系   管理    订阅  订阅
使用Boost.Gil将BMP图片保存为JPG
使用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;
}
posted on 2010-12-19 03:02  LinuxHunter  阅读(3235)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3