#include “myhead.h”//自定义的头文件
int main(int argc,char *argv[])//主函数传参
{
FILE *fp;//
int fd;
//struct fb_var_screeninfo screeninfo;//声明一个与屏幕信息相关的结构体对象
//int screenWidth,screenHeight;
//int bytesperpixel;
unsigned char *pLineColor;//指向开辟的存放一行像素信息的地址
unsigned int *pFrameBuffer;//指向内存映射
int image_width,image_height;//读取图片的高度和宽度
//声明一个jpeg解压对象
struct jpeg_decompress_struct dinfo;
//把jpeg的出错信息关联到进程的标准错误设备上,除非你不关心jpeg库的出错信息
struct jpeg_error_mgr err;
dinfo.err=jpeg_std_error(&err);
//创建一个jpeg解压对象
jpeg_create_decompress(&dinfo);
if(argc<2)
{
printf("Usage:digitalPhoto jpeg_filename.\n");
return -1;
}
fp=fopen(argv[1],"r");//只读方式打开图片
if(fp==NULL)
{
perror("Cannot open jpeg_file");
goto exit;
}
//将jpeg解压对象和jpeg文件流关联起来,意味这jpeg解压对象将从文件流中获取数据
jpeg_stdio_src(&dinfo,fp);
//获取头部信息
jpeg_read_header(&dinfo,TRUE);
image_height=dinfo.image_height;//获取图片高度
image_width=dinfo.image_width;//获取图片宽度
//dinfo.num_components 是解压还原后每个像素点的颜色字节数
printf("w=%d,h=%d,num=%d\n",image_width,image_height,dinfo.num_components);
//打开LCD
fd=open("/dev/fb0",O_RDWR);
//ioctl(fd,FBIOGET_VSCREENINFO,&screeninfo);//获取LCD的宽高等信息
//screenWidth=screeninfo.xres;//获取屏幕的宽
//screenHeight=screeninfo.yres;//获取屏幕的高
//bytesperpixel=screeninfo.bits_per_pixel/8;//获取每个像素颜色所占的字节数
pFrameBuffer=(unsigned int *)mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);//内存映射
dinfo.scale_num = 1; //
dinfo.scale_denom = 1; //显示图片比例为1:1
//开始解压
jpeg_start_decompress(&dinfo);
//申请一个保存jpeg一行颜色数据的缓冲区 图片每个像素点占三个字节
pLineColor=(unsigned char*)malloc(image_width*dinfo.num_components);
//按行读取jpeg图片的数据并处理
while(dinfo.output_scanline<dinfo.output_height && dinfo.output_scanline<480) //output_scanline从0开始,表示读行数 小于图片高度和lcd高度
//每读取一行,output_scanline自动+1
{
int col;
unsigned char *pTmpColor=pLineColor;// 申请一个char指针指向申请的堆空间
unsigned char red,green,blue;//申请三个字节存放每个像素点的RGB信息
//读取一行 申请一个char指针指向申请的堆空间
jpeg_read_scanlines(&dinfo,&pLineColor,1);//每读取一行,output_scanline自动+1
//处理一行
for(col=0;col<800&&col<dinfo.image_width;col++)
{
red=*pTmpColor;
green=*(pTmpColor+1);
blue=*(pTmpColor+2);
pTmpColor+=3;
*(pFrameBuffer+(dinfo.output_scanline-1)*800+col)=(red<<16)|(green<<8)|blue;
}
}
jpeg_finish_decompress(&dinfo);//完成解压
munmap(pFrameBuffer,800*480*4);
free(pLineColor);
fclose(fp);
close(fd);
exit:
jpeg_destroy_decompress(&dinfo);//销毁解压对象,释放资源
return 0;
}