您的位置:首页 > 其它

Windows下使用IJL编解码图片

2015-08-27 10:31 260 查看
IJL的全称是Intel JPEG Library,是Intel专门针对它自己的CPU优化过的JPEG编码库,速度非常之快,编码一张1080p的BMP图片,只需要十多毫秒的时间,这要比libjpeg这个开源库要块了2.5倍~3倍。

它的优点是:

1. 效率高——快到不可思议

2. 使用简单——就三个函数主要函数,可以完成BMP转JPEG,JPEG转BMP

它的缺点是:

1. 目标单一 —— 专门针对Intel的CPU做了优化,在其他平台上无法使用(或者表现平平)

它的主要函数就以下四个:

IJLERR ijlInit (JPEG_CORE_PROPERTIES *jcprops); 

IJLERR ijlFree (JPEG_CORE_PROPERTIES *jcprops); 

IJLERR ijlRead (JPEG_CORE_PROPERTIES *jcprops, IJLIOTYPE iotype);

IJLERR ijlWrite (JPEG_CORE_PROPERTIES *jcprops, IJLIOTYPE iotype);
编解码过程中,只需要使用这四个函数即可完成JPEG的编解码。

下面,将给出实际的编解码例子,这些例子都是从实际的项目中扣下来的,可以完成每秒30+帧的编解码转换;

IJL库下载地址:点这里

《一》 BMP编码JPEG:

// 包含IJL的头文件和静态链接库
#include "ijl.h"
#pragma comment(lib, "ijl15l.lib")

// RGB24转JPG
// pBitmap  : RGB数据
// w, h     : 原始RGB图像的宽度和高度
// fScale   : 如果需要缩放图像,则指定缩放比率;如果不需要缩放图像,请设定为1.0
// nQuality : 编码JPEG的质量(100最高,0最低)
// pJpgBuf  : 输出参数,存储JPEG数据的缓冲区
// nJpgSize : 输入输出参数,用作输入时,表示pJpgBuf大小;用作输出时,表示编码后的JPEG图像大小
int IJL_RGB24ToJpg(BYTE *pBitmap, int w, int h, int nQuality, float fScale, BYTE *pJpgBuf, int &nJpgSize)
{
int nRet = 0, nWidth = w, nHeight = h;

JPEG_CORE_PROPERTIES jcprops;
IJLERR jerr = ijlInit(&jcprops);
if (IJL_OK != jerr)
{
("ijlInit failed! jerr=%d\n", jerr);
return -1;
}

do
{
//保证缓冲区够大
if (nWidth * nHeight * 2 > nJpgSize)
{
("buffer too small\n");
nRet = -1;
break;
}

jcprops.DIBWidth       = nWidth;
jcprops.DIBHeight      = nHeight;
jcprops.DIBBytes       = pBitmap;
jcprops.DIBPadBytes    = IJL_DIB_PAD_BYTES(nWidth, 3);
jcprops.DIBChannels    = 3;
jcprops.DIBColor       = IJL_BGR;

jcprops.JPGWidth       = (int)(nWidth * fScale);
jcprops.JPGHeight      = (int)(nHeight * fScale);
jcprops.JPGFile        = NULL;
jcprops.JPGBytes       = pJpgBuf;
jcprops.JPGSizeBytes   = nJpgSize;
jcprops.JPGChannels    = 3;
jcprops.JPGColor       = IJL_YCBCR;
jcprops.JPGSubsampling = IJL_411;       //4:1:1 subsampling.
jcprops.jquality       = nQuality;      //Write the actual JPEG image from the pixel buffer.

jerr = ijlWrite(&jcprops, IJL_JBUFF_WRITEWHOLEIMAGE);
if (IJL_OK != jerr)
{
("ijlWrite failed! jerr=%d\n", jerr);
nRet = -1;
break;
}

//JPG图像大小
nJpgSize = jcprops.JPGSizeBytes;
("JPG Image Size=%d\n", nJpgSize);
}while(FALSE);

ijlFree(&jcprops);
return nRet;//


《二》JPEG解码成RGB24

// 获得JPEG图像的宽度高度
int IJL_JpgSize(BYTE *pJpgBuf, int nJpgSize, INT *pnWidth, INT *pnHeight)
{
JPEG_CORE_PROPERTIES jcprops = {0};
if (IJL_OK != ijlInit(&jcprops))
{
return FALSE;
}

IJLERR result        = IJL_OK;
jcprops.JPGBytes     = pJpgBuf;
jcprops.JPGSizeBytes = nJpgSize;
result = ijlRead(&jcprops, IJL_JBUFF_READPARAMS);
if (IJL_OK == result)
{
*pnWidth  = jcprops.JPGWidth;
*pnHeight = jcprops.JPGHeight;
}

ijlFree(&jcprops);
return (IJL_OK == result);
}

// 解码JPEG成RGB24
int IJL_JpgToRGB24(BYTE *pJpgBuf, int nJpgSize, BYTE *pBitmap, INT *pnWidth, INT *pnHeight)
{
JPEG_CORE_PROPERTIES jcprops = {0};
if (IJL_OK != ijlInit(&jcprops))
{
return FALSE;
}

IJLERR result        = IJL_OK;
jcprops.JPGBytes     = pJpgBuf;
jcprops.JPGSizeBytes = nJpgSize;
if (IJL_OK == ijlRead(&jcprops, IJL_JBUFF_READPARAMS))
{
*pnWidth  = jcprops.JPGWidth;
*pnHeight = jcprops.JPGHeight;

jcprops.DIBBytes    = pBitmap;
jcprops.DIBWidth    = *pnWidth;
jcprops.DIBHeight   = *pnHeight;
jcprops.DIBColor    = IJL_RGB;
jcprops.DIBChannels = 3;
result = ijlRead(&jcprops, IJL_JBUFF_READWHOLEIMAGE);
}
ijlFree(&jcprops);
return (IJL_OK == result);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: