第7课.libjpeg的使用

1.简介

通过libjpeg把jpg图片给解压出RGB的原始数据,放入显存进行显示

2.安装libjpeg库

tar xzf libjpeg-turbo-1.2.1.tar.gz
cd libjpeg-turbo-1.2.1
./configure --prefix=/work/projects/13.libjpeg/libjpeg-turbo-1.2.1/tmp/ --host=arm-linux
// 设置安装目录(/work/projects/13.libjpeg/libjpeg-turbo-1.2.1/tmp/)和使用范围(arm-linux)
make
make install

把库文件和头文件放到交叉编译工具链中去(参考freetype)

把编译出来的头文件应该放入:
/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include
:
cd  /work/projects/13.libjpeg/libjpeg-turbo-1.2.1/tmp/include
cp * /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/usr/include

把编译出来的库文件应该放入:
/usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib
:
cd  /work/projects/13.libjpeg/libjpeg-turbo-1.2.1/tmp/lib
cp *so* -d /usr/local/arm/4.3.2/arm-none-linux-gnueabi/libc/armv4t/lib

3.使用步骤

1.分配和初始化一个decompression结构体

struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);

2.指定源文件

FILE * infile;

// 指定源文件, 这里argv[1]为输入的文件名
if ((infile = fopen(argv[1], "rb")) == NULL) {
	fprintf(stderr, "can't open %s
", argv[1]);
	return -1;
}
jpeg_stdio_src(&cinfo, infile);

知识点补充:
    FILE * = fopen(文件名,使用文件方式)
        r(read): 只读
        w(write): 只写
        a(append): 追加
        t(text): 文本文件,可省略不写
        b(binary): 二进制文件
        +: 读和写

    fprintf中:
        stderr:标准错误输出设备
        stdout:标准输出设备

3.用jpeg_read_header获得jpg的信息
此时得到的是图片最原始的信息

// 用jpeg_read_header获得jpg信息
jpeg_read_header(&cinfo, TRUE);
/* 源信息 */
printf("image_width = %d
", cinfo.image_width);
printf("image_height = %d
", cinfo.image_height);
printf("num_components = %d
", cinfo.num_components);

4.设置解压参数,比如放大,缩小

// 设置解压参数,比如放大、缩小
printf("enter M/N:
");
scanf("%d/%d", &cinfo.scale_num, &cinfo.scale_denom);
printf("scale to : %d/%d
", cinfo.scale_num, cinfo.scale_denom);

5.启动解压jpeg_start_decompress

// 启动解压:jpeg_start_decompress	
jpeg_start_decompress(&cinfo);

/* 输出的图象的信息 */
printf("output_width = %d
", cinfo.output_width);
printf("output_height = %d
", cinfo.output_height);
printf("output_components = %d
", cinfo.output_components);

6.循环调用jpeg_read_scanflines来一行一行的获取解压的数据

eg:
    / 循环调用jpeg_read_scanlines来一行一行地获得解压的数据
while (cinfo.output_scanline < cinfo.output_height) 
{
	(void) jpeg_read_scanlines(&cinfo, &buffer, 1);

	// 写到LCD去
	FBShowLine(0, cinfo.output_width, cinfo.output_scanline, buffer);
}

7.jpeg_finsh_decompress

jpeg_finish_decompress(&cinfo);

8.释放decompress结构体

jpeg_destroy_decompress(&cinfo);
原文地址:https://www.cnblogs.com/huangdengtao/p/12378525.html