2012-10-23 57 views
0

我試圖創建一個應用程序,它顛倒了位圖文件的顏色,但在實際收集數據和位圖時遇到了一些麻煩。我正在使用結構來保存位圖的數據和它的頭部。現在,我有:讀取和寫入位圖c

struct 
{ 
    uint16_t type; 
    uint32_t size; 
    uint32_t offset; 
    uint32_t header_size; 
    int32_t width; 
    int32_t height; 
    uint16_t planes; 
    uint16_t bits; 
    uint32_t compression; 
    uint32_t imagesize; 
    int32_t xresolution; 
    int32_t yresolution; 
    uint32_t ncolours; 
    uint32_t importantcolours; 
} header_bmp 

struct { 
    header_bmp header; 
    int data_size; 
    int width; 
    int height; 
    int bytes_per_pixel; 
    char *data; 
} image_bmp; 

現在的實際讀取和寫入的位圖我有以下幾點:

image_bmp* startImage(FILE* fp) 
{ 
header_bmp* bmp_h = (struct header_bmp*)malloc(sizeof(struct header_bmp)); 
ReadHeader(fp, bmp_h, 54); 
} 

void ReadHeader(FILE* fp, char* header, int dataSize) 
{ 
fread(header, dataSize, 1, fp); 
} 

從這裏我怎麼提取頭信息到我的頭結構?

此外,如果任何人有任何超過閱讀和寫作位圖的好資源,請讓我知道。我一直在尋找幾個小時,無法找到關於這個話題的很多有用的信息。

回答

0

您實際上應該已經擁有了正確位置的所有數據。唯一可能出錯的問題可能是排序。例如是以「short」表示的數字256,如 0x01 0x00或0x00 0x01。

編輯:有錯與結構的語法東西...

struct name_of_definition { int a; int b; short c; short d; }; 
struct name_of_def_2 { struct name_of_definition instance; int a; int b; } 
    *ptr_to_instance; // or one can directly allocate the instance it self by 
         // by omitting the * mark. 
struct { int b; int c; } instance_of_anonymous_struct; 

ptr_to_instance = malloc(sizeof(struct name_of_def_2)); 

也:

ReadHeader(fp, (char*)&ptr_to_instance->header, sizeof(struct definition)); 
      // ^don't forget to cast to the type accepted by ReadHeader 

通過這種方式,你可以直接將數據讀入該結構的中間,但是可能存在的字節碼問題仍然存在。