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

罗索

Framebuffer 在LCD上显示JPEG图像

落鹤生 发布于 2010-05-04 23:27 点击:次 
编译时需要jpeglib库支持
TAG:

///////////////framebuffer.h

  1. #include <stdio.h> 
  2. #include <fcntl.h> 
  3. #include <stdlib.h> 
  4. #include <errno.h> 
  5. #include <string.h> 
  6. #include <linux/fb.h> 
  7. #include <sys/mman.h> 
  8. #include <unistd.h> 
  9. #include <sys/ioctl.h> 
  10. #include <string.h> 
  11. #include <math.h> 
  12. #define closegr closegraph 
  13. #define FBDEV   "/dev/fb0" 
  14. static char *default_framebuffer = FBDEV; 
  15.  
  16. struct fb_dev 
  17.    int fb; 
  18.    void * fb_mem; 
  19.    int fb_width, fb_height, fb_line_len, fb_size; 
  20.    int fb_bpp; 
  21. }; 
  22.  
  23. static struct fb_dev fbdev; 
  24. int framebuffer_open () 
  25.                 int fb; 
  26.    struct fb_var_screeninfo fb_vinfo; 
  27.    struct fb_fix_screeninfo fb_finfo; 
  28.    char * fb_dev_name = NULL; 
  29.    
  30.    if (!(fb_dev_name = getenv("FRAMEBUFFER"))) 
  31.     fb_dev_name = default_framebuffer; 
  32.  
  33.    fb = open (fb_dev_name, O_RDWR); 
  34.    if (fb < 0 ) 
  35.     { 
  36.      printf("device %s open failed.\n", fb_dev_name); 
  37.      return -1; 
  38.     } 
  39.     
  40.    
  41.    if (ioctl(fb, FBIOGET_VSCREENINFO, &fb_vinfo)) { 
  42.      printf("Can't get VSCREENINFO: %s\n", strerror(errno)); 
  43.      close(fb); 
  44.      return -1; 
  45.     } 
  46.     
  47.    if (ioctl(fb, FBIOGET_FSCREENINFO, &fb_finfo)) { 
  48.      printf("Can't get FSCREENINFO: %s\n", strerror(errno)); 
  49.      return 1; 
  50.     } 
  51.  
  52.    fbdev.fb_bpp = fb_vinfo.red.length + fb_vinfo.green.length
  53.  + fb_vinfo.blue.length + fb_vinfo.transp.length; 
  54.  
  55.    fbdev.fb_width = fb_vinfo.xres; 
  56.    fbdev.fb_height = fb_vinfo.yres; 
  57.    fbdev.fb_line_len = fb_finfo.line_length; 
  58.    fbdev.fb_size = fb_finfo.smem_len; 
  59.  
  60.    printf("frame buffer : %d(%d)x%d, %dbpp, 0x%xbyte\n", fbdev.fb_width, 
  61. fbdev.fb_line_len, fbdev.fb_height, fbdev.fb_bpp, fbdev.fb_size); 
  62.  
  63.  
  64. /* if (fbdev.fb_bpp != 16) { 
  65.     printf ("frame buffer must be 16bpp mode. \n"); 
  66.     exit(0); 
  67.     } 
  68.          */ 
  69.    fbdev.fb_mem = mmap (NULL, fbdev.fb_size, PROT_READ | PROT_WRITE, MAP_SHARED, fb, 0); 
  70.    if (fbdev.fb_mem == NULL || (int) fbdev.fb_mem == -1) { 
  71.     fbdev.fb_mem = NULL; 
  72.     printf("mmap failed.\n"); 
  73.     close(fb); 
  74.     return -1; 
  75.     } 
  76.  
  77.    fbdev.fb = fb; 
  78.    memset (fbdev.fb_mem, 0x0, fbdev.fb_size); 
  79.  
  80.    return 0; 
  81.  
  82. void framebuffer_close() 
  83.    if (fbdev.fb_mem) { 
  84.     munmap (fbdev.fb_mem, fbdev.fb_size); 
  85.     fbdev.fb_mem = NULL; 
  86.     } 
  87.  
  88.    if (fbdev.fb) { 
  89.     close (fbdev.fb); 
  90.     fbdev.fb = 0; 
  91.     } 

/////////jpeg2LCD.c

  1. #include "framebuffer.h" 
  2. #include "jpeglib.h" 
  3.  
  4. #include <setjmp.h> 
  5.  
  6. // error handler, to avoid those pesky exit(0)'s 
  7.  
  8. struct my_error_mgr { 
  9. struct jpeg_error_mgr pub; /* "public" fields */ 
  10.  
  11. jmp_buf setjmp_buffer; /* for return to caller */ 
  12. }; 
  13.  
  14. typedef struct my_error_mgr * my_error_ptr; 
  15.  
  16. // 
  17. // 
  18. // 
  19.  
  20. METHODDEF(void) my_error_exit (j_common_ptr cinfo); 
  21.  
  22. // 
  23. // to handle fatal errors. 
  24. // the original JPEG code will just exit(0). can't really 
  25. // do that in Windows.... 
  26. // 
  27.  
  28. METHODDEF(void) my_error_exit (j_common_ptr cinfo) 
  29. /* cinfo->err really points to a my_error_mgr struct, so coerce pointer */ 
  30. my_error_ptr myerr = (my_error_ptr) cinfo->err; 
  31.  
  32. char buffer[JMSG_LENGTH_MAX]; 
  33.  
  34. /* Create the message */ 
  35. (*cinfo->err->format_message) (cinfo, buffer); 
  36.  
  37. /* Always display the message. */ 
  38. //MessageBox(NULL,buffer,"JPEG Fatal Error",MB_ICONSTOP); 
  39.  
  40.  
  41. /* Return control to the setjmp point */ 
  42. longjmp(myerr->setjmp_buffer, 1); 
  43.  
  44. int Jpeg2LCD(char * filename, int x,int y) 
  45. // get our buffer set to hold data 
  46. unsigned short *p = (unsigned short *)fbdev.fb_mem;//定义framebuffer大的内存空间 
  47. int width=0; 
  48. int height=0; 
  49.  
  50. struct jpeg_decompress_struct cinfo; 
  51. struct my_error_mgr jerr; 
  52. FILE * infile=NULL;   /* source file */ 
  53.  
  54. JSAMPARRAY buffer;   /* Output row buffer */ 
  55. int row_stride;   /* physical row width in output buffer */ 
  56.  
  57. if ((infile = fopen(filename, "rb")) == NULL) { 
  58.    printf("JPEG :\nCan't open %s\n", filename); 
  59.    return -1; 
  60.  
  61. /* Step 1: allocate and initialize JPEG decompression object */ 
  62.  
  63. /* We set up the normal JPEG error routines, then override error_exit. */ 
  64. cinfo.err = jpeg_std_error(&jerr.pub); 
  65. jerr.pub.error_exit = my_error_exit; 
  66.  
  67. /* Establish the setjmp return context for my_error_exit to use. */ 
  68. if (setjmp(jerr.setjmp_buffer)) 
  69.    jpeg_destroy_decompress(&cinfo); 
  70.    if (infile!=NULL) 
  71.     fclose(infile); 
  72.    return -1; 
  73.  
  74. /* Now we can initialize the JPEG decompression object. */ 
  75. jpeg_create_decompress(&cinfo); 
  76.  
  77. /* Step 2: specify data source (eg, a file) */ 
  78. jpeg_stdio_src(&cinfo, infile); 
  79.  
  80. /* Step 3: read file parameters with jpeg_read_header() */ 
  81. (void) jpeg_read_header(&cinfo, TRUE); 
  82.  
  83. /* Step 4: set parameters for decompression */ 
  84. /* In this example, we don't need to change any of the defaults set by 
  85. * jpeg_read_header(), so we do nothing here. 
  86. */ 
  87.  
  88. /* Step 5: Start decompressor */ 
  89.  
  90. (void) jpeg_start_decompress(&cinfo); 
  91. //////////////////////////////////////////////////////////// 
  92.  
  93. // how big is this thing gonna be? 
  94. width = cinfo.output_width; 
  95. height = cinfo.output_height; 
  96.  
  97. /* JSAMPLEs per row in output buffer */ 
  98. row_stride = cinfo.output_width * cinfo.output_components; 
  99.  
  100. /* Make a one-row-high sample array that will go away when done with image */ 
  101. buffer = (*cinfo.mem->alloc_sarray) 
  102.    ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); 
  103.  
  104. /* Here we use the library's state variable cinfo.output_scanline as the 
  105. * loop counter, so that we don't have to keep track ourselves. 
  106. */ 
  107. p+=(y)*fbdev.fb_line_len/2;//文件指针指向开始 
  108. while (cinfo.output_scanline < cinfo.output_height) 
  109.    int count; 
  110.    unsigned char * buf; 
  111.    (void) jpeg_read_scanlines(&cinfo, buffer, 1); 
  112.    buf=buffer[0]; 
  113.    if (cinfo.out_color_components==3) 
  114.    { 
  115.     for (count=0;count<width;count++) 
  116.     { 
  117.      //buffer[count*3]; R 
  118.      //buffer[count*3+1]; G 
  119.      //buffer[count*3+2]; B 
  120.      p[x+count]=((buf[count*3]>>3)<<11)|((buf[count*3+1]>>2)<<5)|(buf[count*3+2]>>3); 
  121.     } 
  122.    } 
  123.    else if (cinfo.out_color_components==1) 
  124.    { 
  125.     for (count=0;count<width;count++) 
  126.     { 
  127.      p[x+count]=((buf[count]>>3)<<11)|((buf[count]>>2)<<5)|(buf[count]>>3); 
  128.  
  129.     } 
  130.    } 
  131.    p+=fbdev.fb_line_len/2; 
  132.  
  133. (void) jpeg_finish_decompress(&cinfo); 
  134. jpeg_destroy_decompress(&cinfo); 
  135.  
  136. fclose(infile); 
  137.  
  138. return 0; 
  139. int main (void
  140. framebuffer_open(); 
  141.         Jpeg2LCD("image.jpg",0,0); 
  142.    usleep(1000*100); 
  143.  
  144. framebuffer_close(); 
  145. return 0; 

编译时需要jpeglib库支持

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