linux C语言读取bmp文件写屏

直接上代码
11 int read_bmp(char* name, unsigned char* buff)
 12 {
 13         Image*                  image;
 14         Rgbquad*                quad;
 15         Bitmapfileheader*       fileheader;
 16         Bitmapinfoheader*       infoheader;
 17         unsigned short          filetype;
 18         int                     num = 0;
 19         FILE*                   pfile = NULL;
 20         unsigned char           pixel;
 21         unsigned int            offset;
 22         image = (Image*)(malloc(sizeof(Image)));
 23         if( !image )
 24         {
 25                 perror("malloc()");
 26                 return -1;
 27         }
 28         fileheader = (Bitmapfileheader*)(malloc(sizeof(Bitmapfileheader)));
 29         memset(fileheader, 0, sizeof(Bitmapfileheader));
 30         infoheader = (Bitmapinfoheader*)(malloc(sizeof(Bitmapinfoheader)));
 31         memset(infoheader, 0, sizeof(Bitmapinfoheader));
 32         pfile = fopen(name, "rb");
 33         if( !pfile )
 34         {
 35                 perror("fopen()");
 36                 goto out;
 37         }
 38 
 39         num = fread( filetype, sizeof(unsigned short), 1, pfile);
 40         if( num )
 41         {
 42         //      printf("filetype =%#x\n",filetype);
 43         }else
 44                 goto out;
 45 
 46         if( filetype == 0x4D42 )
 47         {
 48                 num = fread( fileheader, sizeof(Bitmapfileheader), 1, pfile);
 49                 if( num  0 ) goto out;
 50         //      printf("fileheader-bfsize=%u\n",fileheader-bfsize);
 51                 printf("fileheader-bfoffbits=%u\n",fileheader-bfoffbits);
 52                 offset = fileheader-bfoffbits;
 53                 num = fread( infoheader, sizeof(Bitmapinfoheader), 1, pfile);
 54                 if( num  0) goto out;
55         //      printf("infoheader-bisize=%u\n", infoheader-bisize);
 56                 image-width = infoheader-bitwidth;
 57                 image-height = infoheader-bitheight;
 58         //      printf("infoheader-bitwidth=%u\n",infoheader-bitwidth);
 59         //      printf("infoheader-bitheight=%u\n",infoheader-bitheight);
 60         //      printf("infoheader-biplanes=%u\n",infoheader-biplanes);
 61         //      printf("infoheader-bibitcount=%u\n",infoheader-bibitcount);
 62         //      printf("infoheader-bicompression=%u\n",infoheader-bicompression);
 63         //      printf("infoheader-biclrimportant=%#x\n",infoheader-biclrimportant);
 64         }
 65 //      fseek(pfile, SEEK_SET, offset);
 66         fseek(pfile, offset+1, SEEK_SET );
 67         num = fread(buff, sizeof(unsigned char), (image-width)*(image-height)*2, pfile);
 68         printf("num=%d\n",num);
 69         goto out;
 70 out:
 71         free(image);
 72         free(fileheader);
 73         free(infoheader);
 74         fclose(pfile);
 75         return num;
 76 }

image.h定义bmp图片头数据结构

  1 #ifndef __IMAGE_BPEER__
  2 #define __IMAGE_BPEER__
  3 
  4 typedef struct {
  5 //      unsigned short  bftype;
  6         unsigned int    bfsize;
  7         unsigned short  bfreserved1;
  8         unsigned short  bfreserved2;
  9         unsigned int    bfoffbits;
 10 }Bitmapfileheader;
 11 
 12 typedef struct {
 13         unsigned int    bisize;
 14         unsigned int    bitwidth;
 15         unsigned int    bitheight;
 16         unsigned short  biplanes;
 17         unsigned short  bibitcount;
 18         unsigned int    bicompression;
 19         unsigned int    bisizeimage;
 20         unsigned int    bixpelspermeter;
 21         unsigned int    biypelspermeter;
 22         unsigned int    biclrused;
 23         unsigned int    biclrimportant;
 24 }Bitmapinfoheader;
 25 
 26 typedef struct {
 27         unsigned char   rgbblue;
 28         unsigned char   rgbgreen;
 29         unsigned char   rgbred;
 30         unsigned char   rgbreserved;
 31 }Rgbquad;
 32 
 33 typedef struct {
 34         int     width;
 35         int     height;
 36         int     channel;
 37 }Image;
 38 
 39 
 40 int read_bmp(char* name, unsigned char* buff);
 41 
 42 
 43 #endif


最新回复(0)
/jishuMJzBWd0bVF_2BeXchnsAnYjPSsjDLxjP8bTdsyIg_3D_3D4794609
8 简首页