Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2705583
  • 博文数量: 505
  • 博客积分: 1552
  • 博客等级: 上尉
  • 技术积分: 2514
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-23 18:24
文章分类

全部博文(505)

文章存档

2019年(12)

2018年(15)

2017年(1)

2016年(17)

2015年(14)

2014年(93)

2013年(233)

2012年(108)

2011年(1)

2009年(11)

分类: LINUX

2013-03-12 20:34:16

说明:编译时加上-ljpeg

源代码如下:

  1. /* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  digital_frame.c 
  5.  * 
  6.  *    Description:  framebuffer下用libjeg库显示jpeg图片 
  7.  * 
  8.  *        Version:  1.0 
  9.  *        Created:  2011年03月21日 14时01分21秒 
  10.  *       Revision:  none 
  11.  *       Compiler:  gcc 
  12.  * 
  13.  *         Author:  sunsea1026@gmail.com   
  14.  * 
  15.  * ===================================================================================== 
  16.  */  
  17. #include   
  18. #include   
  19. #include   
  20. #include   
  21. #include   
  22. #include   
  23. #include   
  24. #include   
  25. #include   
  26. #include   
  27. #define FB_DEV      "/dev/fb0"  
  28. #define JPEG        0x001  
  29. #define PNG     0x010  
  30. #define BMP     0x100  
  31. #define UNKNOWN     0x111  
  32. static int  fb_dev;  
  33. static struct   fb_var_screeninfo fb_var;  
  34. static int  screen_w, screen_h, screen_bits;  
  35. static void *fbmem, *data;  
  36. static FILE     *infile;  
  37. typedef struct color  
  38. {  
  39.     unsigned char r;  
  40.     unsigned char g;  
  41.     unsigned char b;  
  42. }COLOR;;  
  43. typedef struct pixel_t  
  44. {  
  45.     unsigned char r;  
  46.     unsigned char g;  
  47.     unsigned char b;  
  48.     unsigned char a;  
  49. }PIXEL_T;  
  50. //初始化fb0  
  51. void fb_init()  
  52. {  
  53.     //打开fb0设备文件  
  54.     fb_dev = open(FB_DEV, O_RDWR);  
  55.     if(fb_dev < 0)  
  56.     {  
  57.         printf("Error:open %s error/n", FB_DEV);   
  58.         printf("Usage:[sudo ./digital_frame xxx.jpg]/n");   
  59.         exit(1);  
  60.     }  
  61.     //获取fb0参数  
  62.     ioctl(fb_dev, FBIOGET_VSCREENINFO, &fb_var);  
  63.     screen_w = fb_var.xres;  
  64.     screen_h = fb_var.yres;  
  65.     screen_bits = fb_var.bits_per_pixel;  
  66.     printf("Framebuffer:%d * %d/n", screen_w, screen_h);   
  67.     printf("screen_bits:%d/n", screen_bits);  
  68.     fbmem = mmap(0, screen_w * screen_h * screen_bits / 8, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev, 0);  
  69.     return;  
  70. }  
  71. //判断图片格式  
  72. int judge_picture_mode(char **argv)  
  73. {  
  74.     char header[8];  
  75.     printf("picture = %s/n", argv[1]);   
  76.     infile = fopen(argv[1], "rb");  
  77.     if(infile == NULL)  
  78.     {  
  79.         printf("Error:open %s error!/n", argv[1]);   
  80.         exit(1);  
  81.     }  
  82.     memset(header, 0, sizeof(header));  
  83.     fread(&header, 8, 1, infile);  
  84.     fseek(infile, -8, 1);  
  85.     if((unsigned char)header[0] == 0xff)  
  86.     {  
  87.         return JPEG;  
  88.     }  
  89.     else if((unsigned char)header[0] == 0x89)  
  90.     {  
  91.         return PNG;  
  92.     }  
  93.     else if(0 == strncmp(header, "BM", 2))  
  94.     {  
  95.         return BMP;  
  96.     }  
  97.     else  
  98.     {  
  99.         return UNKNOWN;  
  100.     }  
  101. }  
  102. //显示jpg/jpeg格式图片  
  103. void display_jpeg()  
  104. {  
  105.     struct jpeg_decompress_struct cinfo;  
  106.     struct jpeg_error_mgr jerr;  
  107.     int picture_w, picture_h, picture_bits;  
  108.     int i, j, x, y;  
  109.     unsigned char *buffer, *tmpbuf;  
  110.     COLOR picture_color;  
  111.     PIXEL_T **fbp;  
  112.     fbp = (PIXEL_T **)malloc(sizeof(PIXEL_T *) * screen_w);  
  113.     //x, y 代表图片的起始坐标  
  114.     for(i = 0, x = 0 * screen_bits / 8, y = 0; i < screen_w; i++)  
  115.     {  
  116.         fbp[i] = (PIXEL_T *)(fbmem + (y + i) * screen_w * screen_bits / 8 + x);  
  117.     }  
  118.     cinfo.err = jpeg_std_error(&jerr);  
  119.     jpeg_create_decompress(&cinfo);  
  120.     //指定要解压缩的图像文件  
  121.     jpeg_stdio_src(&cinfo, infile);  
  122.     //获取图像信息  
  123.     jpeg_read_header(&cinfo, TRUE);  
  124.     //开始解压  
  125.     jpeg_start_decompress(&cinfo);  
  126.     picture_w = cinfo.output_width;  
  127.     picture_h = cinfo.output_height;  
  128.     picture_bits = cinfo.output_components;  
  129. /*  
  130.     printf("picture info:/n"); 
  131.     printf("width  = %d/n", picture_w); 
  132.     printf("height = %d/n", picture_h);  
  133.     printf("bits   = %d/n", picture_bits);  
  134. */  
  135.     //获取颜色值  
  136.     buffer = (unsigned char *)malloc(picture_w * picture_bits);  
  137.     memset(buffer, 0, picture_w * picture_bits);  
  138.     for(i = 0; i < picture_h; i++)  
  139.     {  
  140.         tmpbuf = buffer;  
  141.         jpeg_read_scanlines(&cinfo, &tmpbuf, 1);  
  142.         for(j = 0; j < picture_w; j++)  
  143.         {  
  144.             memcpy(&picture_color, tmpbuf, sizeof(picture_color));  
  145.             fbp[i][j].b = picture_color.r;  
  146.             fbp[i][j].g = picture_color.g;  
  147.             fbp[i][j].r = picture_color.b;  
  148.             tmpbuf += picture_bits;  
  149.         }  
  150.     }  
  151.     //完成解压  
  152.     jpeg_finish_decompress(&cinfo);  
  153.     //释放解压对象  
  154.     jpeg_destroy_decompress(&cinfo);  
  155.     free(buffer);  
  156.     return;  
  157. }  
  158. int main(int argc, char* argv[])  
  159. {  
  160.     int picture_mode;  
  161.     if(argc != 2)  
  162.     {  
  163.         printf("Usage:[sudo ./digital_frame xxx.jpg]/n");   
  164.         exit(1);  
  165.     }  
  166.     //fb0初始化  
  167.     fb_init();  
  168.     //判断图片格式  
  169.     picture_mode = judge_picture_mode(argv);  
  170.     switch(picture_mode)  
  171.     {  
  172.         case JPEG:  
  173.             printf("JPEG/JPG/n");   
  174.             display_jpeg();  
  175.             break;  
  176.         case PNG:  
  177.             printf("PNG/n");   
  178.             break;  
  179.         case BMP:  
  180.             printf("bmp/n");   
  181.             break;  
  182.         case UNKNOWN:  
  183.             printf("UNKNOWN/n");   
  184.             break;  
  185.         default:  
  186.             break;  
  187.     }  
  188.     return 0;   
  189. }  
 
阅读(1499) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~