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

罗索

framebuffer下bmp格式图片

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

framebuffer下用libjeg库显示jpeg图片源代码:(包含用libjpeg显示jpeg格式图片代码)

  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 <stdio.h> 
  18. #include <stdlib.h> 
  19. #include <string.h> 
  20. #include <sys/types.h> 
  21. #include <sys/stat.h> 
  22. #include <fcntl.h> 
  23. #include <sys/mman.h> 
  24. #include <linux/fb.h> 
  25. #include <unistd.h> 
  26. #include <jpeglib.h> 
  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. //JPEG的内容 
  38. typedef struct jpeg_color 
  39.     unsigned char r; 
  40.     unsigned char g; 
  41.     unsigned char b; 
  42. }COLOR;; 
  43. typedef struct jpeg_pixel_t 
  44.     unsigned char r; 
  45.     unsigned char g; 
  46.     unsigned char b; 
  47.     unsigned char a; 
  48. }PIXEL_T; 
  49. //BMP的内容 
  50. typedef struct bmp_file_header 
  51.     char cfType[2];             //文件类型,必须为“BM”(0x4D42) 
  52.     char cfSize[4];             //文件的大小(字节) 
  53.     char cfReserved[4];         //保留,必须为0 
  54.     char cfoffBits[4];          //位图阵列相对与文件头的偏移量(字节) 
  55. }__attribute__((packed)) BITMAPFILEHEADER;  //文件头结构 
  56. typedef struct bmp_info_header 
  57.     char ciSize[4];             //sizeof of BITMAPINFOHEADER 
  58.     char ciWidth[4];            //位图宽度(像素) 
  59.     char ciHeight[4];           //位图高度(像素) 
  60.     char ciPlanes[2];           //目标设备的位平面数,必须置为1 
  61.     char ciBitCount[2];         //每个像素的位数,1,4,8或24 
  62.     char ciCompress[4];         //位图阵列的压缩方法,0=不压缩 
  63.     char ciSizeImage[4];            //图像大小(字节) 
  64.     char ciXPelsPerMeter[4];        //目标设备水平每米像素个数 
  65.     char ciYPelsPerMeter[4];        //目标设备垂直每米像素个数 
  66.     char ciClrUsed[4];          //位图实际使用的颜色表的颜色数 
  67.     char ciClrImportant[4];         //重要颜色索引的个数 
  68. }__attribute__((packed)) BITMAPINFOHEADER;  //位图信息头结构 
  69. //初始化fb0 
  70. void fb_init() 
  71.     //打开fb0设备文件 
  72.     fb_dev = open(FB_DEV, O_RDWR); 
  73.     if(fb_dev < 0) 
  74.     { 
  75.         printf("Error:open %s error/n", FB_DEV);  
  76.         printf("Usage:[sudo ./digital_frame xxx.jpg]/n");  
  77.         exit(1); 
  78.     } 
  79.     //获取fb0参数 
  80.     ioctl(fb_dev, FBIOGET_VSCREENINFO, &fb_var); 
  81.     screen_w = fb_var.xres; 
  82.     screen_h = fb_var.yres; 
  83.     screen_bits = fb_var.bits_per_pixel; 
  84.     printf("Framebuffer:%d * %d/n", screen_w, screen_h);  
  85.     printf("screen_bits:%d/n", screen_bits); 
  86.     fbmem = mmap(0, screen_w * screen_h * screen_bits / 8
  87. , PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev, 0); 
  88.     return
  89. //判断图片格式 
  90. int judge_picture_mode(char **argv) 
  91.     char header[8]; 
  92.     printf("picture = %s/n", argv[1]);  
  93.     infile = fopen(argv[1], "rb"); 
  94.     if(infile == NULL) 
  95.     { 
  96.         printf("Error:open %s error!/n", argv[1]);  
  97.         exit(1); 
  98.     } 
  99.     memset(header, 0, sizeof(header)); 
  100.     fread(&header, 8, 1, infile); 
  101.     fseek(infile, -8, 1); 
  102.     if((unsigned char)header[0] == 0xff) 
  103.     { 
  104.         return JPEG; 
  105.     } 
  106.     else if((unsigned char)header[0] == 0x89) 
  107.     { 
  108.         return PNG; 
  109.     } 
  110.     else if(0 == strncmp(header, "BM", 2)) 
  111.     { 
  112.         return BMP; 
  113.     } 
  114.     else 
  115.     { 
  116.         return UNKNOWN; 
  117.     } 
  118. //显示jpg/jpeg格式图片 
  119. void display_jpeg() 
  120.     struct jpeg_decompress_struct cinfo; 
  121.     struct jpeg_error_mgr jerr; 
  122.     int picture_w, picture_h, picture_bits; 
  123.     int i, j, x, y; 
  124.     unsigned char *buffer, *tmpbuf; 
  125.     COLOR picture_color; 
  126.     PIXEL_T **fbp; 
  127.     fbp = (PIXEL_T **)malloc(sizeof(PIXEL_T *) * screen_w); 
  128.     //x, y 代表图片的起始坐标 
  129.     for(i = 0, x = 400 * screen_bits / 8, y = 200; i < screen_w; i++) 
  130.     { 
  131.         fbp[i] = (PIXEL_T *)(fbmem + (y + i) * screen_w * screen_bits / 8 + x); 
  132.     } 
  133.     cinfo.err = jpeg_std_error(&jerr); 
  134.     jpeg_create_decompress(&cinfo); 
  135.     //指定要解压缩的图像文件 
  136.     jpeg_stdio_src(&cinfo, infile); 
  137.     //获取图像信息 
  138.     jpeg_read_header(&cinfo, TRUE); 
  139.     //开始解压 
  140.     jpeg_start_decompress(&cinfo); 
  141.     picture_w = cinfo.output_width; 
  142.     picture_h = cinfo.output_height; 
  143.     picture_bits = cinfo.output_components; 
  144. /*  
  145.     printf("picture info:/n"); 
  146.     printf("width  = %d/n", picture_w); 
  147.     printf("height = %d/n", picture_h);  
  148.     printf("bits   = %d/n", picture_bits);  
  149. */ 
  150.     //获取颜色值 
  151.     buffer = (unsigned char *)malloc(picture_w * picture_bits); 
  152.     memset(buffer, 0, picture_w * picture_bits); 
  153.     for(i = 0; i < picture_h; i++) 
  154.     { 
  155.         tmpbuf = buffer; 
  156.         jpeg_read_scanlines(&cinfo, &tmpbuf, 1); 
  157.         for(j = 0; j < picture_w; j++) 
  158.         { 
  159.             memcpy(&picture_color, tmpbuf, sizeof(picture_color)); 
  160.             fbp[i][j].b = picture_color.r; 
  161.             fbp[i][j].g = picture_color.g; 
  162.             fbp[i][j].r = picture_color.b; 
  163.             tmpbuf += picture_bits; 
  164.         } 
  165.     } 
  166.     //完成解压 
  167.     jpeg_finish_decompress(&cinfo); 
  168.     //释放解压对象 
  169.     jpeg_destroy_decompress(&cinfo); 
  170.     free(buffer); 
  171.     return
  172. //显示png图片 
  173. void display_png() 
  174. {/*  
  175.     png_structp png_ptr; 
  176.     png_infop info_ptr; 
  177.     int picture_w, picture_h, picture_color_type; 
  178.      
  179.     png_ptr = create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0); 
  180.     info_ptr = png_create_info_struct(png_ptr); 
  181.     setjmp(png_jmpbuf(png_ptr)); 
  182.     png_init_io(png_ptr, infile); 
  183.     png_read_png(png_ptr, info_ptr, PNG_TRANSFROM_EXPAND, 0); 
  184.     picture_w = png_get_image_width(PngPtr, InfoPtr); 
  185.     picture_h = png_get_image_height(PngPtr, InfoPtr); 
  186.     picture_color_type = png_get_image_color_type(PngPtr, InfoPtr); 
  187.     printf("picture info:/n");  
  188.     printf("width = %d/n", picture_w); 
  189.     printf("height = %d/n", picture_h); 
  190.     printf("color_type = %d/n", picture_color_type);  
  191. */ 
  192.     return
  193. //chartolong 
  194. long chartolong(char *string, int length) 
  195.     long number; 
  196.     if(length <= 4) 
  197.     { 
  198.         memset(&number, 0, sizeof(long)); 
  199.         memcpy(&number, string, length); 
  200.     } 
  201.     return number; 
  202. //显示bmp格式图片 
  203. void display_bmp() 
  204.     BITMAPFILEHEADER FileHead; 
  205.     BITMAPINFOHEADER InfoHead; 
  206.     int rc ,i ,j, x, y; 
  207.     int ciWidth, ciHeight, ciBitCount; 
  208.     long int BytesPerLine = 0; 
  209.     COLOR picture_color; 
  210.     PIXEL_T **fbp; 
  211.     unsigned char *buffer, *tmpbuf; 
  212.   
  213.     //读位图文件头 
  214.     rc = fread(&FileHead, sizeof(BITMAPFILEHEADER), 1, infile); 
  215.     if(rc != 1) 
  216.     { 
  217.         printf("Error:read bmp header error!/n");  
  218.         fclose(infile); 
  219.         exit(1); 
  220.     } 
  221.    
  222.     //判断位图的类型 
  223.     if(memcmp(FileHead.cfType, "BM", 2) != 0) 
  224.     { 
  225.         printf("This is not a bmp picture!/n");  
  226.         fclose(infile); 
  227.         exit(1); 
  228.     } 
  229.    
  230.     //读取位图信息头 
  231.     rc = fread((char *)&InfoHead, sizeof(BITMAPINFOHEADER), 1, infile); 
  232.     if(rc != 1) 
  233.     { 
  234.         printf("Error:read bmp infoheader error!/n");  
  235.         fclose(infile); 
  236.         exit(1); 
  237.     } 
  238.     ciWidth     = (int)chartolong(InfoHead.ciWidth, 4); 
  239.     ciHeight    = (int)chartolong(InfoHead.ciHeight,    4); 
  240.     ciBitCount  = (int)chartolong(InfoHead.ciBitCount,  4); 
  241.     fseek(infile, (int)chartolong(FileHead.cfoffBits, 4), SEEK_SET); 
  242.     BytesPerLine = (ciWidth * ciBitCount + 31) / 32 * 4; 
  243. /*  printf("bmp info/n");  
  244.     printf("width = %d/n", ciWidth);  
  245.     printf("height = %d/n", ciHeight);  
  246.     printf("bit = %d/n", ciBitCount);  
  247. */ 
  248.     fbp = (PIXEL_T **)malloc(sizeof(PIXEL_T *) * screen_w); 
  249.     for(i = 0, x = 0 * screen_bits / 8, y = 0; i < screen_w; i++) 
  250.     { 
  251.         fbp[i] = (PIXEL_T *)(fbmem + (y + i) * screen_w * screen_bits / 8 + x); 
  252.     } 
  253.     buffer = (unsigned char *)malloc(BytesPerLine); 
  254.     memset(buffer, 0, BytesPerLine); 
  255.     for(i = ciHeight - 1; i >= 0; i--) 
  256.     { 
  257.         tmpbuf = buffer; 
  258.         rc = fread(tmpbuf, BytesPerLine, 1, infile); 
  259.          
  260.         for(j = 0; j < ciWidth; j++) 
  261.         { 
  262.             memcpy(&picture_color, tmpbuf, ciBitCount / 8); 
  263.          
  264.             fbp[i][j].r = picture_color.r; 
  265.             fbp[i][j].g = picture_color.g; 
  266.             fbp[i][j].b = picture_color.b; 
  267.             tmpbuf += ciBitCount / 8; 
  268.         } 
  269.     } 
  270.     return
  271. int main(int argc, char* argv[]) 
  272.     int picture_mode; 
  273.     if(argc != 2) 
  274.     { 
  275.         printf("Usage:[sudo ./digital_frame xxx.jpg]/n");  
  276.         exit(1); 
  277.     } 
  278.     //fb0初始化 
  279.     fb_init(); 
  280.     //判断图片格式 
  281.     picture_mode = judge_picture_mode(argv); 
  282.     switch(picture_mode) 
  283.     { 
  284.         case JPEG: 
  285.             printf("JPEG/JPG/n");  
  286.             display_jpeg(); 
  287.             break
  288.         case PNG: 
  289.             printf("PNG/n");  
  290.             display_png(); 
  291.             break
  292.         case BMP: 
  293.             printf("bmp/n");  
  294.             display_bmp(); 
  295.             break
  296.         case UNKNOWN: 
  297.             printf("UNKNOWN/n");  
  298.             break
  299.         default
  300.             break
  301.     } 
  302.     fclose(infile); 
  303.     return 0;  

 

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