织梦CMS - 轻松建站从此开始!

罗索

framebuffer下用libjeg库显示jpeg图片

jackyhwei 发布于 2011-11-30 11:25 点击:次 
framebuffer下用libjeg库显示jpeg图片
TAG:

说明:编译时加上-ljpeg

源代码如下:

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

 

(sunsea1026)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201111/15378.html]
本文出处:blog.csdn.net/sunsea1026 作者:sunsea1026
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容