libjpeg使用



     要实现图片在lcd上的显示,可以使用libjpeg库的解压功能,使用libjpeg库将jpg图片解压成RGB数据。
在lcd上描绘RGB数据即可显示图片

在libjpeg库的阅读文档libjpeg.txt中有解压的一般步骤:

   

Allocate and initialize a JPEG decompression object    // 分配和初始化一个decompression结构体
Specify the source of the compressed data (eg, a file) // 指定源文件
Call jpeg_read_header() to obtain image info           // 用jpeg_read_header获得jpg信息
Set parameters for decompression                       // 设置解压参数,比如放大、缩小
jpeg_start_decompress(...);                            // 启动解压:jpeg_start_decompress
while (scan lines remain to be read)
    jpeg_read_scanlines(...);                           // 循环调用jpeg_read_scanlines
jpeg_finish_decompress(...);                           // jpeg_finish_decompress
Release the JPEG decompression object                   // 释放decompression结构体
#include <stdio.h>
#include "jpeglib.h"
#include <setjmp.h>
int main(int argc, char **argv)
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
	FILE * infile;
	int row_stride;
	unsigned char *buffer;

	// 分配和初始化一个decompression结构体
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&cinfo);

	// 指定源文件
	if ((infile = fopen(argv[1], "rb")) == NULL) {
		fprintf(stderr, "can't open %s\n", argv[1]);
		return -1;
	}
	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);

	// 设置解压参数,比如放大、缩小。并不是所有的缩放比例都可以实现。
	//1/2, 1/4, 1/8可以实现,其他的可以测试。
	printf("enter M/N:\n");
	scanf("%d/%d", &cinfo.scale_num, &cinfo.scale_denom);
	printf("scale to : %d/%d\n", cinfo.scale_num, cinfo.scale_denom);

	// 启动解压: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);
	
	// 一行的数据长度
	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);
		//在这里实现在LCD上描绘一行数据的函数。
	}
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	return 0;
}

libjpeg库的功能很强大,在这里我们仅用到了它的解压功能。如果想更深入的了解它的功能,可以
阅读libjpeg.txt.


     要实现图片在lcd上的显示,可以使用libjpeg库的解压功能,使用libjpeg库将jpg图片解压成RGB数据。
在lcd上描绘RGB数据即可显示图片

在libjpeg库的阅读文档libjpeg.txt中有解压的一般步骤:

   

Allocate and initialize a JPEG decompression object    // 分配和初始化一个decompression结构体
Specify the source of the compressed data (eg, a file) // 指定源文件
Call jpeg_read_header() to obtain image info           // 用jpeg_read_header获得jpg信息
Set parameters for decompression                       // 设置解压参数,比如放大、缩小
jpeg_start_decompress(...);                            // 启动解压:jpeg_start_decompress
while (scan lines remain to be read)
    jpeg_read_scanlines(...);                           // 循环调用jpeg_read_scanlines
jpeg_finish_decompress(...);                           // jpeg_finish_decompress
Release the JPEG decompression object                   // 释放decompression结构体
#include <stdio.h>
#include "jpeglib.h"
#include <setjmp.h>
int main(int argc, char **argv)
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
	FILE * infile;
	int row_stride;
	unsigned char *buffer;

	// 分配和初始化一个decompression结构体
	cinfo.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&cinfo);

	// 指定源文件
	if ((infile = fopen(argv[1], "rb")) == NULL) {
		fprintf(stderr, "can't open %s\n", argv[1]);
		return -1;
	}
	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);

	// 设置解压参数,比如放大、缩小。并不是所有的缩放比例都可以实现。
	//1/2, 1/4, 1/8可以实现,其他的可以测试。
	printf("enter M/N:\n");
	scanf("%d/%d", &cinfo.scale_num, &cinfo.scale_denom);
	printf("scale to : %d/%d\n", cinfo.scale_num, cinfo.scale_denom);

	// 启动解压: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);
	
	// 一行的数据长度
	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);
		//在这里实现在LCD上描绘一行数据的函数。
	}
	jpeg_finish_decompress(&cinfo);
	jpeg_destroy_decompress(&cinfo);

	return 0;
}

libjpeg库的功能很强大,在这里我们仅用到了它的解压功能。如果想更深入的了解它的功能,可以
阅读libjpeg.txt.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值