参考libjpeg参考文档 与 disp模块fb.c
main
int main(int argc, char **argv)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE * infile;
//1、分配和初始化一个decompression结构体
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
//2、指定源文件
if ((infile = fopen(argv[1], "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", argv[1]);
return -1;
}
//jpeg_stdio_src使用文件句柄设置解压结构体
jpeg_stdio_src(&cinfo, infile);
//3、用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);
//4、设置解压参数,比如放大、缩小
//缩放因子:scale_num/scale_denom,需要注意的是部分缩放比例不支持
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);
//5、启动解压: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);
//6、循环调用jpeg_read_scanlines来一行一行地获得解压的数据
//6.1、一行的数据长度 width * components, buffer大小
row_stride = cinfo.output_width * cinfo.output_components;
buffer = malloc(row_stride);
//6.2、循环调用jpeg_read_scanlines来一行一行地获得解压的数据
while (cinfo.output_scanline < cinfo.output_height)
{
//6.3、这里buffer取地址,为指针的数组,有row_stride个指针;第三个参数为读取行数
(void) jpeg_read_scanlines(&cinfo, &buffer, 1);
//6.4、arg1:起点;arg2:终点;arg3:显示哪一行;arg4:buffer数据;
FBShowLine(0, cinfo.output_width, cinfo.output_scanline, buffer);
}
//7、jpeg_finish_decompress,完成解压
jpeg_finish_decompress(&cinfo);
//8、释放decompression结构体
jpeg_destroy_decompress(&cinfo);
return 0;
}
FBShowLine显示一行
static int FBShowLine(int iXStart, int iXEnd, int iY, unsigned char *pucRGBArray)
{
int i = iXStart * 3;
int iX;
unsigned int dwColor;
//1、判断参数没有超过范围
if (iY >= g_tFBVar.yres)
return -1;
if (iXStart >= g_tFBVar.xres)
return -1;
if (iXEnd >= g_tFBVar.xres)
{
iXEnd = g_tFBVar.xres;
}
//2、显示这一行
for (iX = iXStart; iX < iXEnd; iX++)
{
/* 0xRRGGBB */
dwColor = (pucRGBArray[i]<<16) + (pucRGBArray[i+1]<<8) + (pucRGBArray[i+2]<<0);
i += 3;//因为显示一个像素用了RGB三种
FBShowPixel(iX, iY, dwColor);
}
return 0;
}
FBCleanScreen、FBShowPixel、FBDeviceInit来自fb.c
省略
fb.c链接:fb.c