Parse/Read Bitmap file in C

后端 未结 1 956
轮回少年
轮回少年 2020-12-20 03:07

I\'m trying to make a program to read data from a bitmap file (.bmp, Windows file format, 8bit). Right now I\'m stuck on reading the headers before the image data.

I

相关标签:
1条回答
  • 2020-12-20 03:45

    Yup I forgot to pack the structs. This fixes things. Oops:

    typedef struct __attribute__((__packed__)) {                                                                                                                                                                                                                             
        unsigned char fileMarker1;                                                                                                                                                                                              
        unsigned char fileMarker2;                                                                                                                                                                                               
        unsigned int   bfSize;                                                                                                                                                                                                                   
        uint16_t unused1;                                                                                                                                                                                                                        
        uint16_t unused2;                                                                                                                                                                                                                        
        unsigned int   imageDataOffset;                                                                                                                                                            
    } FILEHEADER;                                                                                                                                                                                                                                
    
    typedef struct __attribute__((__packed__)) {                                                                                                                                                                                                                             
        unsigned int   biSize;                                                                                                                                                                                                                   
        int            width;                                                                                                                                                                
        int            height;                                                                                                                                                                     
        uint16_t planes;                                                                                                                                                                                                                         
        uint16_t bitPix;                                                                                                                                                                                                                         
        unsigned int   biCompression;                                                                                                                                                                                                            
        unsigned int   biSizeImage;                                                                                                                                                                                                              
        int            biXPelsPerMeter;                                                                                                                                                                                                          
        int            biYPelsPerMeter;                                                                                                                                                                                                          
        unsigned int   biClrUsed;                                                                                                                                                                                                                
        unsigned int   biClrImportant;                                                                                                                                                                                                           
    } INFOHEADER;                                                                                                                                                                                                                                
    
    typedef struct __attribute__((__packed__)) {                                                                                                                                                                                                                             
        unsigned char  b;                                                                                                                                                                                                                        
        unsigned char  g;                                                                                                                                                                                                                        
        unsigned char  r;                                                                                                                                                                                                                        
    } IMAGE;
    
    0 讨论(0)
提交回复
热议问题