如何缩短程序运行时间

qyl_qiyalu 2015-04-23 10:20:33
在linux下,调用jpeg库来对bmp图像压缩成jpg格式,bmp文件为1024*768的8位灰度图,但发现程序压缩耗时太多,一张图片压缩下来需要40ms以上,因为我后期需要将压缩好的多张jpg图像实时轮流显示来达到视频播放的效果,现在的压缩耗时明显是不可以的。请问大家代码有没有可以优化的地方,另外,还有没有其他方法
  gettimeofday(&tp1, NULL); 

//Convert BMP to JPG
cinfo.err = jpeg_std_error(&jerr);
//* Now we can initialize the JPEG compression object.
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, output_file);

cinfo.image_width = bih.biWidth; //* image width and height, in pixels
cinfo.image_height = bih.biHeight;
cinfo.input_components = depth; //* # of color components per pixel
if(depth==3)
{cinfo.in_color_space = JCS_RGB;} //* colorspace of input image
else if(depth==1)
{cinfo.in_color_space = JCS_GRAYSCALE;}
jpeg_set_defaults(&cinfo);

//Now you can set any non-default parameters you wish to.
//Here we just illustrate the use of quality (quantization table) scaling:
jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE ); //* limit to baseline-JPEG values
jpeg_start_compress(&cinfo, TRUE);

point=dst_buffer; //获取BMP像素数据

while (cinfo.next_scanline<bih.biHeight)
{
row_pointer[0]=&point[0];
jpeg_write_scanlines(&cinfo,row_pointer,1);
point+=bih.biWidth*depth;
}
gettimeofday(&tp2, NULL);

jpeg_finish_compress(&cinfo);

printf("cost time=%ld us\n", (tp2.tv_sec - tp1.tv_sec)*1000000 + tp2.tv_usec - tp1.tv_usec) ;
...全文
1010 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
e_feeling 2015-05-12
  • 打赏
  • 举报
回复
学习.......
qyl_qiyalu 2015-05-04
  • 打赏
  • 举报
回复
自己顶下,别沉了
qyl_qiyalu 2015-04-30
  • 打赏
  • 举报
回复
或者在jpeglib库下,有没有加快压缩速度的参数选择?
qyl_qiyalu 2015-04-30
  • 打赏
  • 举报
回复
请问有没有其他的库,可以在linux下将bmp图片压缩成jpg图片,谢谢各位
赵4老师 2015-04-23
  • 打赏
  • 举报
回复
既然提到视频,为什么不用ffmpeg库呢?
shenyi0106 2015-04-23
  • 打赏
  • 举报
回复
恩,是的,没看到你的平台,不好意思 libjpeg的速度确实很慢
qyl_qiyalu 2015-04-23
  • 打赏
  • 举报
回复
IJL的全称是Intel® JPEG Library,是由Intel公司提供的JEPG解码/编码库,专门针对Intel处理器进行了彻底的优化,因此在Win-tel环境下速度一流。这个工具最大的特点就是简单,一共只有六个函数(ijlInit、ijlFree、ijlRead、ijlWrite、ijlGetLibVersion、ijlErrorStr),而且随机文档中附有完整的VC++实例,因此对于VC程序员来说不用费什么脑子就可以对JPEG应用自如。 从网上找到以上信息,这是否意味着IJL是针对Intel处理器做了相关优化,对arm-linux来说没效果了?
qyl_qiyalu 2015-04-23
  • 打赏
  • 举报
回复
引用 3 楼 shenyi0106 的回复:
刚才我又测试了一下libjpeg,的确比ijl慢了不少,慢了一倍还转弯
引用 3 楼 shenyi0106 的回复:
刚才我又测试了一下libjpeg,的确比ijl慢了不少,慢了一倍还转弯
我没用过ijl,请问ijl能否将bmp灰度图转化为jpg?能否设置压缩率?另外ijl在linux下能用吗,能否移植到arm板子上?谢谢
shenyi0106 2015-04-23
  • 打赏
  • 举报
回复
刚才我又测试了一下libjpeg,的确比ijl慢了不少,慢了一倍还转弯
shenyi0106 2015-04-23
  • 打赏
  • 举报
回复
用ijl试试呢? 我用的是ijl,压缩1920*1080的24位彩色图,也才是16ms一帧(由于用的是GetTickCount来计时的,受操作系统线程调度影响,其计时时间不太准确,16ms是个平均数字),不知道你的为啥要40ms? 还有就是因为你是直接写文件的,所以IO这块应该也多少有些影响,建议你修改成内存压缩在看看时间。 以下是我的IJL压缩代码:

int  ConvertToJpg(BYTE *pBitmap, const RECT& rect, int nQuality, float fScale, BYTE *pJpgBuf, int &nJpgSize)
{
	int nRet = 0, nWidth = rect.right - rect.left, nHeight = rect.bottom - rect.top;

	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;
}

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧