Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1746046
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类:

2012-09-18 14:15:41

原文地址:BMP图像解析显示 作者:东方钰

首先获取bmp文件信息头,根据得到的图像长,宽,以及存储位数信息,设置输出,将其输出到屏幕上。

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <sys/mman.h>
  8. #include <linux/fb.h>
  9. #include <unistd.h>
  10. #include <jpeglib.h>
  11. #define FB_DEV "/dev/fb0"
  12.  
  13. static int fb_dev;
  14. static struct fb_var_screeninfo fb_var;
  15. static int screen_w, screen_h, screen_bits;
  16. static void *fbmem, *data;
  17. static FILE *infile;

  18.  
  19. typedef struct
  20. {
  21.     unsigned char r;
  22.     unsigned char g;
  23.     unsigned char b;
  24. }COLOR;;
  25. typedef struct
  26. {
  27.     unsigned char r;
  28.     unsigned char g;
  29.     unsigned char b;
  30.     unsigned char a;
  31. }PIXEL_T;
  32. //BMP的内容
  33. typedef struct bmp_file_header
  34. {
  35.     char cfType[2]; //文件类型,必须为“BM”(0x4D42)
  36.     char cfSize[4]; //文件的大小(字节)
  37.     char cfReserved[4]; //保留,必须为0
  38.     char cfoffBits[4]; //位图阵列相对与文件头的偏移量(字节)
  39. }__attribute__((packed)) BITMAPFILEHEADER; //文件头结构
  40. typedef struct bmp_info_header
  41. {
  42.     char ciSize[4]; //sizeof of BITMAPINFOHEADER
  43.     char ciWidth[4]; //位图宽度(像素)
  44.     char ciHeight[4]; //位图高度(像素)
  45.     char ciPlanes[2]; //目标设备的位平面数,必须置为1
  46.     char ciBitCount[2]; //每个像素的位数,1,4,8或24
  47.     char ciCompress[4]; //位图阵列的压缩方法,0=不压缩
  48.     char ciSizeImage[4]; //图像大小(字节)
  49.     char ciXPelsPerMeter[4]; //目标设备水平每米像素个数
  50.     char ciYPelsPerMeter[4]; //目标设备垂直每米像素个数
  51.     char ciClrUsed[4]; //位图实际使用的颜色表的颜色数
  52.     char ciClrImportant[4]; //重要颜色索引的个数
  53. }__attribute__((packed)) BITMAPINFOHEADER; //位图信息头结构

  54. unsigned char *fbp;
  55. struct fb_var_screeninfo vinfo;
  56. struct fb_fix_screeninfo finfo;

  57. long int screensize;
  58. int xres = 0;
  59. int yres = 0;
  60. int bits_per_pixel = 0;

  61. //初始化fb0
  62. void fb_init()
  63. {
  64.     //打开fb0设备文件
  65.     fb_dev = open(FB_DEV, O_RDWR);
  66.     if(fb_dev < 0)
  67.     {
  68.         printf("Error:open %s error/n", FB_DEV);
  69.         printf("Usage:[sudo ./digital_frame xxx.jpg]/n");
  70.         exit(1);
  71.     }
  72.     //获取fb0参数
  73.     ioctl(fb_dev, FBIOGET_VSCREENINFO, &fb_var);
  74.     screen_w = fb_var.xres;
  75.     screen_h = fb_var.yres;
  76.     screen_bits = fb_var.bits_per_pixel;
  77.     printf("Framebuffer:%d * %d/n", screen_w, screen_h);
  78.     printf("screen_bits:%d/n", screen_bits);
  79.     
  80.     int screensize=screen_w * screen_h * screen_bits / 8;
  81.     fbmem = mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fb_dev, 0);
  82.     
  83.     
  84.     int i=0,bgcolor=0;
  85.     for(i=0;i<screensize;i+=fb_var.bits_per_pixel/8)
  86.         {
  87.             memcpy(fbmem+i,&bgcolor,fb_var.bits_per_pixel/8);
  88.         }
  89.     
  90.     return;
  91. }
  92.  
  93. //chartolong
  94. long chartolong(char *string, int length)
  95. {
  96.     long number;
  97.     if(length <= 4)
  98.     {
  99.         memset(&number, 0, sizeof(long));
  100.         memcpy(&number, string, length);
  101.     }
  102.     return number;
  103. }
  104. //显示bmp格式图片
  105. void display_bmp(char *bmpfile)
  106. {
  107.     BITMAPFILEHEADER FileHead;
  108.     BITMAPINFOHEADER InfoHead;
  109.     int rc ,i ,j, x, y;
  110.     int ciWidth, ciHeight, ciBitCount;
  111.     long int BytesPerLine = 0;
  112.     COLOR picture_color;
  113.     PIXEL_T **fbp;
  114.     unsigned char *buffer, *tmpbuf;
  115.    
  116.     infile = fopen( bmpfile, "rb" );
  117.     //读位图文件头
  118.     rc = fread(&FileHead, sizeof(BITMAPFILEHEADER), 1, infile);
  119.     if(rc != 1)
  120.     {
  121.         printf("Error:read bmp header error!\n");
  122.         fclose(infile);
  123.         exit(1);
  124.     }
  125.     
  126.     //判断位图的类型
  127.     if(memcmp(FileHead.cfType, "BM", 2) != 0)
  128.     {
  129.         printf("This is not a bmp picture!\n");
  130.         fclose(infile);
  131.         exit(1);
  132.     }
  133.     
  134.     //读取位图信息头
  135.     rc = fread((char *)&InfoHead, sizeof(BITMAPINFOHEADER), 1, infile);
  136.     if(rc != 1)
  137.     {
  138.         printf("Error:read bmp infoheader error!\n");
  139.         fclose(infile);
  140.         exit(1);
  141.     }
  142.     ciWidth = (int)chartolong(InfoHead.ciWidth, 4);
  143.     ciHeight = (int)chartolong(InfoHead.ciHeight, 4);
  144.     ciBitCount = (int)chartolong(InfoHead.ciBitCount, 4);
  145.     fseek(infile, (int)chartolong(FileHead.cfoffBits, 4), SEEK_SET);
  146.     BytesPerLine = (ciWidth * ciBitCount + 31) / 32 * 4;
  147.     
  148.     printf("图像宽度:%d\n",ciWidth);
  149.     printf("图像高度:%d\n",ciHeight);
  150.     printf("每个像素位数:%d\n",ciBitCount);
  151.     
  152.     
  153.     fbp = (PIXEL_T **)malloc(sizeof(PIXEL_T *) * screen_w);
  154.     for(i = 0, x = 0 * screen_bits / 8, y = 0; i < screen_w; i++)
  155.     {
  156.         fbp[i] = (PIXEL_T *)(fbmem + (y + i) * screen_w * screen_bits / 8 + x);
  157.     }
  158.     buffer = (unsigned char *)malloc(BytesPerLine);
  159.     memset(buffer, 0, BytesPerLine);
  160.     for(i = ciHeight - 1; i >= 0; i--)
  161.     {
  162.         tmpbuf = buffer;
  163.         rc = fread(tmpbuf, BytesPerLine, 1, infile);
  164.           
  165.         for(j = 0; j < ciWidth; j++)
  166.         {
  167.             memcpy(&picture_color, tmpbuf, ciBitCount / 8);
  168.           
  169.             fbp[i][j].r = picture_color.r;
  170.             fbp[i][j].g = picture_color.g;
  171.             fbp[i][j].b = picture_color.b;
  172.             tmpbuf += ciBitCount / 8;
  173.         }
  174.     }
  175.     
  176.     fclose(infile);
  177.     return;
  178. }


  179. int main(int argc, char* argv[])
  180. {
  181.    
  182.     if(argc != 2)
  183.     {
  184.         printf("Usage:[ + xxx.bmp]\n");
  185.         exit(1);
  186.     }
  187.     //fb0初始化
  188.     fb_init();
  189.     
  190.     display_bmp(argv[1]);
  191.     return 0;
  192. }

阅读(268) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~