libjpeg-turbo使用实例(编解码jpeg、jpg转bmp、bmp转jpg代码)

libjpeg-turbo库用于jpeg图像编解码,上一节说了编译过程:编译libjpeg-turbo 。现在说说jpeg的编码、解码使用方法。

Windows上GDI接口支持的都是位图格式(DDB\DIB)图像,这里只说bmp编码成jpeg格式图片并保存到本地和jpeg解码成bmp格式并保存到本地。

bmp转jpeg

int Bmp2Jpeg_Compress(void* lpBmpBuffer, int nWidth, int nHeight, OUT void** ppJpegBuffer, OUT unsigned long* pOutSize)
{
	jpeg_compress_struct toWriteInfo;
	jpeg_error_mgr errorMgr;
	toWriteInfo.err = jpeg_std_error(&errorMgr);
	//注册失败的回调函数
	toWriteInfo.err->error_exit = error_exit;
	jpeg_create_compress(&toWriteInfo);
	//保存压缩后的图片
	//FILE* fp = NULL;
	//_wfopen_s(&fp, L"c:\\output.jpg", L"wb+");
	//jpeg_stdio_dest(&toWriteInfo, fp);
	//确定要用于输出压缩的jpeg的数据空间
	jpeg_mem_dest(&toWriteInfo, (unsigned char**)ppJpegBuffer, pOutSize);
	toWriteInfo.image_width = nWidth;
	toWriteInfo.image_height = nHeight;
	toWriteInfo.jpeg_width = nWidth / 2;
	toWriteInfo.jpeg_height = nHeight / 2;
	toWriteInfo.input_components = 4;// 在此为1,表示灰度图, 如果是彩色位图,则为4
	toWriteInfo.in_color_space = JCS_EXT_BGRA; //JCS_GRAYSCALE表示灰度图,JCS_RGB表示彩色图像 
	jpeg_set_defaults(&toWriteInfo);
	jpeg_set_quality(&toWriteInfo, 100, TRUE);	//设置压缩质量100表示100%
	jpeg_start_compress(&toWriteInfo, TRUE);
	int nRowStride = nWidth*4;	// 如果不是索引图,此处需要乘以4
	JSAMPROW row_pointer[1];	// 一行位图
	while (toWriteInfo.next_scanline < toWriteInfo.image_height)
	{
		row_pointer[0] = (JSAMPROW)((unsigned char*)lpBmpBuffer + toWriteInfo.next_scanline*nRowStride);
		jpeg_write_scanlines(&toWriteInfo, row_pointer, 1);
	}
	jpeg_finish_compress(&toWriteInfo);
	jpeg_destroy_compress(&toWriteInfo);
	return 0;
}
传入读取的bmp文件二进制数据,输出编码后的jpeg流和大小。

input_components指定为4个字节,目前的Windows支持的都是32位位图;nRowStride = nWidth*4 表示每一行位图数据的字节数,每个像素4字节;还有一个关键的地方in_color_space = JCS_EXT_BGRA,涉及到Windows中数据在内存中的排列方式(little-endian, 低字节存放在内存的低位)ARGB在内存中为BGRA(PS 我尝试过使用JCS_EXT_ARGB,结果颜色全部取反了)。

编码完成后,直接写入文件即可保存为jpeg文件:

//libjpeg为我们压缩好了jpeg数据,只需要往文件里面写入即可
		FILE* fpOut = NULL;
		fopen_s(&fpOut, "c:\\out.jpg", "wb+");
		if (fp)
		{
			fwrite(pOutBuffer, 1, lOutSize, fpOut);
			fclose(fpOut);
		}


jpeg转DIB

int Jpeg2DIB_DeCompress(void* lpJpegBuffer, unsigned long nInSize, OUT void** ppDibBuffer, OUT unsigned long* pOutSize, OUT int* pWidth, OUT int* pHeight)
{
	jpeg_decompress_struct cInfo;
	jpeg_create_decompress(&cInfo);
	jpeg_error_mgr errorMgr;
	cInfo.err = jpeg_std_error(&errorMgr);
	cInfo.err->error_exit = error_exit;
	jpeg_mem_src(&cInfo, (const unsigned char*)lpJpegBuffer, nInSize);
	jpeg_read_header(&cInfo, TRUE);
	jpeg_start_decompress(&cInfo);
	JSAMPROW row_pointer[1];
	int nBitCounts = cInfo.num_components * 8;
	int nWidthBits = cInfo.image_width*cInfo.num_components;// ((cInfo.image_width*nBitCounts + 31) >> 5) << 2;
	unsigned long lOutSize = nWidthBits*cInfo.image_height;
	unsigned char* pOutBuffer = (unsigned char*)malloc(lOutSize);
	row_pointer[0] = pOutBuffer;
	while (cInfo.output_scanline
unsigned long lOutSize = nWidthBits*cInfo.image_height 通过jpeg图片的尺寸计算DIB数据区大小,通常是高度*宽度*像素字节数。

和编码一样,解码时jpeg库也是一行一行进行的循环调用 jpeg_read_scanlines。

这样解码后,并不是真正的DIB数据,因为cInfo.num_components=3,也就是说解码的数据是RGB的,每个像素占用3个字节。我们需要再次转换成ARGB的Windows上支持的DIB格式。

//RGB to ARGB
		unsigned long nDestSize = nWidth * 4 * nHeight;
		DWORD* pArgbData = (DWORD*)malloc(nDestSize);
		DWORD* pArgbDataTemp = pArgbData;
		unsigned char* pRgbData = (unsigned char*)lpOutData;
		int nOffset = nOutSize-3, i = 0;
		while (nOffset>=0)
		{
			/*注意,在window系统中内存以little-endian存储,即低字节存放在内存的低位 0xARGB -- 0xBGRA
			/除去忽略的A 即alpha通道位  读取内存中的数据为 BGR 需要转换成 RGB
			/bmp位图会忽略掉alpha通道位,设置成任意数值都可以以
			*/
			DWORD dwColor = 0x00000000 + RGB(pRgbData[nOffset+2], pRgbData[nOffset + 1], pRgbData[nOffset]);
			*pArgbDataTemp = dwColor;
			pArgbDataTemp++;
			nOffset -= 3;
		}
循环转换后,得到的就是标准的DIB数据了,可以调用GDI API创建与之关联的位图句柄,然后贴图绘制到界面上。

BITMAP bmp = { 0 };
		bmp.bmWidth = nWidth;
		bmp.bmHeight = nHeight;
		bmp.bmWidthBytes = nWidth * 4;
		bmp.bmPlanes = 1;
		bmp.bmBitsPixel = 32;
		bmp.bmBits = pArgbData;

		HBITMAP hBitmap = CreateBitmapIndirect(&bmp);

还可以把DIB数据保存到本地,bmp位图图像。


BID保存为位图文件

int SaveDIBToBmpFile(const char* pFile, void* pDibBuffer, unsigned long nBufferSize, int nWidth, int nHeight)
{
	BITMAPFILEHEADER fHeader;
	int nStructSize1 = sizeof(BITMAPFILEHEADER);
	int nStructSize2 = sizeof(BITMAPINFO)-sizeof(RGBQUAD);
	memset(&fHeader, 0, nStructSize1);
	memcpy(&fHeader, "BM", 2);
	fHeader.bfSize = nStructSize1 + nStructSize2 + nBufferSize;
	fHeader.bfOffBits = nStructSize1 + nStructSize2;
	BITMAPINFO bmpInfo = { nStructSize2 };
	bmpInfo.bmiHeader.biWidth = nWidth;
	bmpInfo.bmiHeader.biHeight = nHeight;
	bmpInfo.bmiHeader.biPlanes = 1;
	bmpInfo.bmiHeader.biBitCount = 32;
	bmpInfo.bmiHeader.biCompression = BI_RGB;
	bmpInfo.bmiHeader.biSizeImage = 0;
	FILE* fp = NULL;
	fopen_s(&fp, pFile, "wb+");
	if (NULL == fp)
		return -1;
	/*	写入 bmp位图文件,
	*	文件格式:位图文件头+位图信息头+位图数据
	*	写入数据头前,需要填充相应的字段
	*	详细结构说明,见 http://blog.csdn.net/mfcing/article/details/7451670
	*/
	fwrite(&fHeader, 1, nStructSize1, fp);
	fwrite(&bmpInfo, 1, nStructSize2, fp);
	fwrite(pDibBuffer, 1, nBufferSize, fp);
	fclose(fp);
	return 0;
}

程序运行图



libjpeg-turbo使用实例(编解码jpeg、jpg转bmp、bmp转jpg代码)_第1张图片

jpeg图片转换成bmp图片后,保存成功

libjpeg-turbo使用实例(编解码jpeg、jpg转bmp、bmp转jpg代码)_第2张图片


本实例说的不够清楚,完整源码下载:http://download.csdn.net/detail/mfcing/9638178



 
  

你可能感兴趣的:(C/C++基础,开源)