一、libjpeg-turbo库下载
libjpeg-turbo是libjpeg的升级版,性能有所提升
libpng库链接:https://libpng.sourceforge.io/index.html
二、ubuntu上验证libjpeg-turbo
-
编译libjpeg-turbo库,tar -zxvf libjpeg-turbo-2.1.3.tar.gz && cd libjpeg-turbo-2.1.3 && mkdir build && cd build && cmake .. && make,如下图
-
修改djepg.c
#include "jinclude.h"
#include "jpeglib.h"
#include <setjmp.h>
struct my_error_mgr {
struct jpeg_error_mgr pub; /* "public" fields */
jmp_buf setjmp_buffer; /* for return to caller */
};
typedef struct my_error_mgr * my_error_ptr;
static void my_error_exit (j_common_ptr cinfo)
{
my_error_ptr myerr = (my_error_ptr) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(myerr->setjmp_buffer, 1);
}
int main(int argc, char **argv)
{
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE * infile;
FILE * outfile;
int row_stride;
unsigned char *buffer;
struct timeval start,end;
gettimeofday(&start, NULL);
if (argc != 3)
{
printf("Usage: \n");
printf("%s <jpg_file> <output_file>\n", argv[0]);
return -1;
}
// 指定源文件
if ((infile = fopen(argv[1], "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", argv[1]);
return -1;
}
if ((outfile = fopen(argv[2], "w")) == NULL) {
fprintf(stderr, "can't open %s\n", argv[1]);
return -1;
}
printf("size :%d\n",sizeof(struct jpeg_decompress_struct));
//错误处理 回调函数
cinfo.err = jpeg_std_error(&jerr.pub);
jerr.pub.error_exit = my_error_exit;
if (setjmp(jerr.setjmp_buffer)) {
printf("cinfo->err->msg_code :%d\n",cinfo.err->msg_code);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
fclose(outfile);
return 0;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
// 用jpeg_read_header获得jpg信息
jpeg_read_header(&cinfo, TRUE);
/* 源信息 */
printf("image_width = %d\n", cinfo.image_width);
printf("image_height = %d\n", cinfo.image_height);
printf("num_components = %d\n", cinfo.num_components);
// 设置解压参数,比如放大、缩小
cinfo.scale_num=1;
cinfo.scale_denom=1;
// 启动解压:jpeg_start_decompress
jpeg_start_decompress(&cinfo);
/* 输出的图象的信息 */
printf("output_width = %d\n", cinfo.output_width);
printf("output_height = %d\n", cinfo.output_height);
printf("output_components = %d\n", cinfo.output_components);//解压的是rgb,故为3元素
// 一行的数据长度
row_stride = cinfo.output_width * cinfo.output_components;
buffer = malloc(row_stride);//分配空间用来存储一行数据
// 循环调用jpeg_read_scanlines来一行一行地获得解压的数据
while (cinfo.output_scanline < cinfo.output_height)
{
(void) jpeg_read_scanlines(&cinfo, &buffer, 1);
fwrite(buffer, row_stride, 1, outfile);
}
gettimeofday(&end, NULL);
long timeuse = 1000000*(end.tv_sec - start.tv_sec) + end.tv_usec-start.tv_usec;
printf("time =%ld\n", timeuse);
free(buffer);
fclose(infile);
fclose(outfile);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return 0;
}
3.执行make,mkdir test && cp djpeg test && cd test && ./djpeg test.jpg test.rgb,效果如下
三、向其它平台移植
适配不同交叉编译器,cd build && cmake -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc .. ,其余步骤同上