2012-06-20 38 views
8

Tôi đang cố gắng làm việc trên tệp bmp. Đối với một sự khởi đầu tôi đã cố gắng để đọc một header và dữ liệu từ một tập tin bmp và viết nó vào một tập tin mới:đọc/ghi tệp bmp trong c

#pragma pack(push,1) 
/* Windows 3.x bitmap file header */ 
typedef struct { 
    char   filetype[2]; /* magic - always 'B' 'M' */ 
    unsigned int filesize; 
    short  reserved1; 
    short  reserved2; 
    unsigned int dataoffset; /* offset in bytes to actual bitmap data */ 
} file_header; 

/* Windows 3.x bitmap full header, including file header */ 
typedef struct { 
    file_header fileheader; 
    unsigned int headersize; 
    int   width; 
    int   height; 
    short  planes; 
    short  bitsperpixel; /* we only support the value 24 here */ 
    unsigned int compression; /* we do not support compression */ 
    unsigned int bitmapsize; 
    int   horizontalres; 
    int   verticalres; 
    unsigned int numcolors; 
    unsigned int importantcolors; 
} bitmap_header; 
#pragma pack(pop) 

int foo(char* input, char *output) { 

    //variable dec: 
    FILE *fp,*out; 
    bitmap_header* hp; 
    int n; 
    char *data; 

    //Open input file: 
    fp = fopen(input, "r"); 
    if(fp==NULL){ 
     //cleanup 
    } 


    //Read the input file headers: 
    hp=(bitmap_header*)malloc(sizeof(bitmap_header)); 
    if(hp==NULL) 
     return 3; 

    n=fread(hp, sizeof(bitmap_header), 1, fp); 
    if(n<1){ 
     //cleanup 
    } 

    //Read the data of the image: 
    data = (char*)malloc(sizeof(char)*hp->bitmapsize); 
    if(data==NULL){ 
     //cleanup 
    } 

    fseek(fp,sizeof(char)*hp->fileheader.dataoffset,SEEK_SET); 
    n=fread(data,sizeof(char),hp->bitmapsize, fp); 
    if(n<1){ 
     //cleanup 
    } 

     //Open output file: 
    out = fopen(output, "w"); 
    if(out==NULL){ 
     //cleanup 
    } 

    n=fwrite(hp,sizeof(char),sizeof(bitmap_header),out); 
    if(n<1){ 
     //cleanup 
    } 
    fseek(out,sizeof(char)*hp->fileheader.dataoffset,SEEK_SET); 
    n=fwrite(data,sizeof(char),hp->bitmapsize,out); 
    if(n<1){ 
     //cleanup 
    } 

    fclose(fp); 
    fclose(out); 
    free(hp); 
    free(data); 
    return 0; 
} 

Đây là tập tin đầu vào: enter image description here

và đây là sản phẩm : enter image description here

Chúng có cùng kích thước và dường như có cùng tiêu đề. Điều gì có thể sai? Cảm ơn.

+1

Dữ liệu pixel bitmap phải được căn chỉnh 32 bit theo hàng. Bạn có chắc đây không phải là vấn đề? – TheZ

+0

@ TheZ: Dữ liệu hình ảnh bao gồm ba byte cho mỗi pixel theo thứ tự b, g, r. Dữ liệu được lưu trữ theo thứ tự hàng-đầu tiên, hàng sau hàng và mỗi hàng được đệm bằng số không đến độ dài là bội số 4 byte. Tôi chỉ viết lại những gì tôi đã đọc. Đó có phải là vấn đề? – Sanich

+1

Vâng, sau đó mở cả hai tệp trong trình chỉnh sửa hex và so sánh. Một cái nhìn nhanh chóng cho thấy chúng khác nhau về chiều dài và dữ liệu sau tiêu đề. – TheZ

Trả lời

7

Tôi sẽ đoán bạn nên mở tệp của mình ở chế độ nhị phân.

+0

Hoạt động !!! Cảm ơn. – Sanich

Các vấn đề liên quan