Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360897
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: LINUX

2011-12-17 17:29:33

  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <linux/fb.h>
  7. #include <sys/mman.h>
  8. #include <sys/ioctl.h>
  9. #include <arpa/inet.h>

  10. typedef struct {
  11.     char cfType[2];
  12.     long cfSize;
  13.     long cfReserved;
  14.     long cfoffBits;
  15. }__attribute__((packed)) BITMAPFILEHEADER;

  16. typedef struct
  17. {
  18.     char ciSize[4];
  19.     long ciWidth;
  20.     long ciHeight;
  21.     char ciPlanes[2];
  22.     int ciBitCount;
  23.     char ciCompress[4];
  24.     char ciSizeImage[4];
  25.     char ciXPelsPerMeter[4];
  26.     char ciYPelsPerMeter[4];
  27.     char ciClrUsed[4];
  28.     char ciClrImportant[4];
  29. }__attribute__((packed)) BITMAPINFOHEADER;

  30. typedef struct
  31. {
  32.     char r;
  33.     char g;
  34.     char b;
  35. }__attribute__((packed)) PIXEL;

  36. BITMAPFILEHEADER FileHead;
  37. BITMAPINFOHEADER InfoHead;

  38. static char *fbp = 0;
  39. static int xres = 0;
  40. static int yres = 0;
  41. static int bits_per_pixel = 0;

  42. int show_bmp();

  43. int main ( int argc, char *argv[] )
  44. {
  45.     int fbfd = 0;
  46.     struct fb_var_screeninfo vinfo;
  47.     struct fb_fix_screeninfo finfo;
  48.     long int screensize = 0;

  49.     fbfd = open("/dev/graphics/fb0", O_RDWR);
  50.     if (!fbfd)
  51.     {
  52.         printf("Error: cannot open framebuffer device.\n");
  53.         exit(1);
  54.     }
  55.     if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
  56.     {
  57.         printf("Error:reading fixed information.\n");
  58.         exit(2);
  59.     }

  60.     if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
  61.     {
  62.         printf("Error: reading variable information.\n");
  63.         exit(3);
  64.     }

  65.     printf("%dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
  66.     xres = vinfo.xres;
  67.     yres = vinfo.yres;
  68.     bits_per_pixel = vinfo.bits_per_pixel;

  69.     screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  70.     printf("screensize=%d\n",(int)screensize);

  71.     fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
  72.     if ((int)fbp == -1)
  73.     {
  74.         printf("Error: failed to map framebuffer device to memory.\n");
  75.         exit(4);
  76.     }
  77.     printf("sizeof header=%d\n", sizeof(BITMAPFILEHEADER));

  78.     printf("into show_bmp function\n");

  79.     while(1)
  80.     {
  81.         show_bmp();
  82.     }

  83.     munmap(fbp, screensize);
  84.     close(fbfd);
  85.     return 0;
  86. }

  87. int show_bmp()
  88. {
  89.     FILE *fp;
  90.     int rc;
  91.     int line_x, line_y;
  92.     long int location = 0, BytesPerLine = 0;
  93.     char tmp[1024*10];

  94.     fp = fopen( "/data/test.bmp", "rb" );
  95.     if (fp == NULL)
  96.     {
  97.         return( -1 );
  98.     }

  99.     rc = fread( &FileHead, sizeof(BITMAPFILEHEADER),1, fp );
  100.     if ( rc != 1)
  101.     {
  102.         printf("read header error!\n");
  103.         fclose( fp );
  104.         return( -2 );
  105.     }

  106.     if (memcmp(FileHead.cfType, "BM", 2) != 0)
  107.     {
  108.         printf("it's not a BMP file\n");
  109.         fclose( fp );
  110.         return( -3 );
  111.     }

  112.     rc = fread( (char *)&InfoHead, sizeof(BITMAPINFOHEADER),1, fp );
  113.     if ( rc != 1)
  114.     {
  115.         printf("read infoheader error!\n");
  116.         fclose( fp );
  117.         return( -4 );
  118.     }

  119.     else
  120.     {
  121.         int ciWidth = (int)(InfoHead.ciWidth); //the width
  122.         int ciHeight = (int)(InfoHead.ciHeight);//the height
  123.         int ciBitCount = (int)(InfoHead.ciBitCount); //the number of colors
  124.         printf("InfoHead height=%d\n", ciHeight);
  125.         printf("InfoHead wdith=%d\n", ciWidth);
  126.         printf("InfoHead bitCount==%d\n", ciBitCount);
  127.     }

  128.     fseek(fp, FileHead.cfoffBits, SEEK_SET);
  129.     BytesPerLine = (InfoHead.ciWidth * InfoHead.ciBitCount + 31) / 32 * 4;

  130.     line_x = line_y = 0;
  131.     while(!feof(fp))
  132.     {
  133.         PIXEL pix;
  134.         unsigned int tmp;
  135.         rc = fread(&pix, 1, sizeof(pix), fp);
  136.         if (rc != sizeof(pix))
  137.         {
  138.             break;
  139.         }
  140.         tmp=pix.r|pix.g<<8|pix.b<<16;
  141.         //tmp=pix.r<<16|pix.g<<8|pix.b;
  142.         *(fbp + location) = tmp;
  143.         location+=4;
  144.     }
  145.     printf("the location is %d\n",(int)location);
  146.     fclose( fp );
  147.     return( 0 );
  148. }
阅读(1265) | 评论(0) | 转发(0) |
0

上一篇:C位段

下一篇:Makefile中嵌套和其他编译

给主人留下些什么吧!~~