将jpeg图片显示在framebuffer上

本文介绍了一个使用C语言编写的程序,该程序能够读取JPEG格式的图片文件,并将其解码后绘制到帧缓冲区上显示。程序首先打开指定的JPEG文件和帧缓冲设备,获取帧缓冲区的相关信息,然后利用libjpeg库进行JPEG解码,并将RGB数据转换为帧缓冲区支持的颜色格式。
  1. /**************************************************
  2.  * example5.c
  3.  * Author: T-bagwell
  4.  *
  5.  * Compile:gcc -Wall example5.-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) (| G | B);
  33. }

  34. int draw_pixel(void *fbmem, int width, int height, int x, int y, unsigned short color)
  35. {
  36.         if ((> width) || (> 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 (= 0; x < cinfo.output_width; x++)
  97.                         {
  98.                                 color = PIXELRGB888TO565(buffer[* 3], buffer[* 3 + 1], buffer[* 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值