2012-10-23 5 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 

, 그러나 엔디안 니스의 문제는 여전히 주위에 숨어 있습니다.