歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

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

Copyright © Linux教程網 All Rights Reserved