使用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;

}

你可能感兴趣的:(boost)