jpeg图像为解压后即得到位图数据,与上一篇中的bmp图像显示相比,增加的步骤是需要运用jpeg库提供的解码函数,解码得到图像数据,然后再把原始图像显示出来。
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <sys/mman.h>
-
#include <linux/fb.h>
-
#include <unistd.h>
-
#include <jpeglib.h>
-
#define FB_DEV "/dev/fb0"
-
-
static int fb_dev;
-
static struct fb_var_screeninfo fb_var;
-
static int screen_w, screen_h, screen_bits;
-
static void *fbmem, *data;
-
static FILE *infile;
-
//JPEG的内容
-
typedef struct jpeg_color
-
{
-
unsigned char r;
-
unsigned char g;
-
unsigned char b;
-
}COLOR;;
-
typedef struct jpeg_pixel_t
-
{
-
unsigned char r;
-
unsigned char g;
-
unsigned char b;
-
unsigned char a;
-
}PIXEL_T;
-
-
//初始化fb0
-
void fb_init()
-
{
-
//打开fb0设备文件
-
fb_dev = open(FB_DEV, O_RDWR);
-
if(fb_dev < 0)
-
{
-
printf("Error:open %s error\n", FB_DEV);
-
printf("Usage:[ xxx.jpg]\n");
-
exit(1);
-
}
-
//获取fb0参数
-
ioctl(fb_dev, FBIOGET_VSCREENINFO, &fb_var);
-
screen_w = fb_var.xres;
-
screen_h = fb_var.yres;
-
screen_bits = fb_var.bits_per_pixel;
-
printf("Framebuffer:%d * %d\n", screen_w, screen_h);
-
printf("screen_bits:%d\n", screen_bits);
-
fbmem = mmap(0, screen_w * screen_h * screen_bits / 8, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev, 0);
-
-
int screensize=screen_w * screen_h * screen_bits / 8;
-
int i=0,bgcolor=0;
-
for(i=0;i<screensize;i =fb_var.bits_per_pixel/8) //清屏
-
{
-
memcpy(fbmem i,&bgcolor,fb_var.bits_per_pixel/8);
-
}
-
-
return;
-
}
-
-
//显示jpg/jpeg格式图片
-
void display_jpeg(char *jpegfile)
-
{
-
struct jpeg_decompress_struct cinfo;
-
struct jpeg_error_mgr jerr;
-
int picture_w, picture_h, picture_bits;
-
int i, j, x, y;
-
unsigned char *buffer, *tmpbuf;
-
COLOR picture_color;
-
PIXEL_T **fbp;
-
-
infile = fopen(jpegfile, "rb" );
-
-
fbp = (PIXEL_T **)malloc(sizeof(PIXEL_T *) * screen_w);
-
//x, y 代表图片的起始坐标
-
for(i = 0, x = 0 * screen_bits / 8, y = 0; i < screen_w; i )
-
{
-
fbp[i] = (PIXEL_T *)(fbmem (y i) * screen_w * screen_bits / 8 x);
-
}
-
cinfo.err = jpeg_std_error(&jerr);
-
jpeg_create_decompress(&cinfo);
-
//指定要解压缩的图像文件
-
jpeg_stdio_src(&cinfo, infile);
-
//获取图像信息
-
jpeg_read_header(&cinfo, TRUE);
-
//开始解压
-
jpeg_start_decompress(&cinfo);
-
picture_w = cinfo.output_width;
-
picture_h = cinfo.output_height;
-
picture_bits = cinfo.output_components;
-
-
printf("图片信息:\n");
-
printf("宽度 = %d\n", picture_w);
-
printf("高度 = %d\n", picture_h);
-
printf("位数 = %d\n", picture_bits);
-
-
//获取颜色值
-
buffer = (unsigned char *)malloc(picture_w * picture_bits);
-
memset(buffer, 0, picture_w * picture_bits);
-
for(i = 0; i < picture_h; i )
-
{
-
tmpbuf = buffer;
-
jpeg_read_scanlines(&cinfo, &tmpbuf, 1);
-
for(j = 0; j < picture_w; j )
-
{
-
memcpy(&picture_color, tmpbuf, sizeof(picture_color));
-
fbp[i][j].b = picture_color.r;
-
fbp[i][j].g = picture_color.g;
-
fbp[i][j].r = picture_color.b;
-
tmpbuf = picture_bits;
-
}
-
}
-
//完成解压
-
jpeg_finish_decompress(&cinfo);
-
//释放解压对象
-
jpeg_destroy_decompress(&cinfo);
-
free(buffer);
-
return;
-
}
-
-
//chartolong
-
long chartolong(char *string, int length)
-
{
-
long number;
-
if(length <= 4)
-
{
-
memset(&number, 0, sizeof(long));
-
memcpy(&number, string, length);
-
}
-
return number;
-
}
-
-
int main(int argc, char* argv[])
-
{
-
if(argc != 2)
-
{
-
printf("Usage:[ xxx.jpg]\n");
-
exit(1);
-
}
-
//fb0初始化
-
fb_init();
-
-
display_jpeg(argv[1]);
-
fclose(infile);
-
return 0;
-
}
阅读(1325) | 评论(0) | 转发(0) |