天天看點

将jpeg圖檔顯示在framebuffer上

點選(此處)折疊或打開

  1. /**************************************************
  2. * example5.c
  3. * Author: T-bagwell
  4. *
  5. * Compile:gcc -Wall example5.c -o example5
  6. *************************************************/
  7. #include <stdio.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <linux/fb.h>
  13. #include <errno.h>
  14. #include <sys/mman.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/ioctl.h>
  18. #include <jpeglib.h>
  19. #include <jerror.h>
  20. int print_screen(short *buf,int width,int height);
  21. void draw_hline(short *buffer,int x,int y,int maxy,int maxx);
  22. char *fb_addr;
  23. unsigned int fb_width;
  24. unsigned int fb_height;
  25. unsigned int fb_depth;
  26. unsigned fb_size;
  27. unsigned short PIXELRGB888TO565(unsigned char red, unsigned char green, unsigned char blue)
  28. {
  29.         unsigned short B = (blue >> 3) & 0x001F;
  30.         unsigned short G = ((green >> 3) << 5) & 0x07E0;
  31.         unsigned short R = ((red >> 3) << 11) & 0xF800;
  32.         return (unsigned short) (R | G | B);
  33. }
  34. int draw_pixel(void *fbmem, int width, int height, int x, int y, unsigned short color)
  35. {
  36. if ((x > width) || (y > height))
  37.                 return -1;
  38.         unsigned short *dst = ((unsigned short *) fbmem + y * width + x);
  39. *dst = color;
  40.         return (0);
  41. }
  42. int main(int argc, char *argv[])
  43. {
  44. int screen_fbd=0;
  45.         struct jpeg_decompress_struct cinfo;
  46.         struct jpeg_error_mgr jerr;
  47.         FILE *infile;
  48.         struct fb_fix_screeninfo fb_fix;
  49.         struct fb_var_screeninfo fb_var;
  50.         char *env=NULL;
  51.         unsigned char *buffer;
  52.         unsigned char *fbmem;
  53.         short *color_white;
  54.         unsigned short color;
  55.         unsigned int x;
  56.         unsigned int y;
  57. if ((infile = fopen(argv[1], "rb")) == NULL)
  58. {
  59.                 fprintf(stderr, "open bbs.chinaffmpeg.com 孫悟空提醒您 %s failed\n", argv[1]);
  60. exit(-1);
  61. }
  62. if(!(env = getenv("FRAMEBUFFER")))
  63. {
  64.                 env = "/dev/fb0";
  65. }
  66.         screen_fbd = open(env, O_RDWR);
  67. if(screen_fbd < 0)
  68. {
  69.                 return 0;
  70. }
  71. if(ioctl(screen_fbd, FBIOGET_VSCREENINFO, &fb_var) == -1)
  72. {
  73.                 close(screen_fbd);
  74.                 return 0;
  75. }
  76.         fb_width = fb_var.xres;
  77.         fb_height = fb_var.yres;
  78.         fb_depth = fb_var.bits_per_pixel;
  79.         cinfo.err = jpeg_std_error(&jerr);
  80.         jpeg_create_decompress(&cinfo);
  81.         jpeg_stdio_src(&cinfo, infile);
  82.         jpeg_read_header(&cinfo, TRUE);
  83.         jpeg_start_decompress(&cinfo);
  84. if ((cinfo.output_width > fb_width) ||(cinfo.output_height > fb_height))
  85. {
  86.                 return (-1);
  87. }
  88.         fbmem = mmap(0, fb_width * fb_height, PROT_READ | PROT_WRITE, MAP_SHARED, screen_fbd, 0);
  89.         buffer = (unsigned char *) malloc(cinfo.output_width * cinfo.output_components);
  90.         y = 0;
  91. while (cinfo.output_scanline < cinfo.output_height)
  92. {
  93.                 jpeg_read_scanlines(&cinfo, &buffer, 1);
  94. if (fb_depth == 16)
  95. {
  96. for (x = 0; x < cinfo.output_width; x++)
  97. {
  98.                                 color = PIXELRGB888TO565(buffer[x * 3], buffer[x * 3 + 1], buffer[x * 3 + 2]);
  99.                                 draw_pixel(fbmem, fb_width, fb_height, x, y, color);
  100. }
  101. }
  102.         y++;
  103. }
  104.         jpeg_finish_decompress(&cinfo);
  105.         jpeg_destroy_decompress(&cinfo);
  106.         free(buffer);
  107.         return 0;
  108. }

QQ群裡突然有人問了這個問題,希望得到開源的程式,正好以前寫過一個,找的挺費勁的,就貼到這裡吧

http://bbs.chinaunix.net/forum.php?mod=redirect&goto=findpost&ptid=2021099&pid=14296454&fromuid=11344913