2011-04-20 29 views
0

看來,當使用libpng解碼PNG文件時,它不會讀取最後的16個字節,所以我尋找16個字節進行到底。我能否假設所有PNG文件都是如此?libpng未讀數據

#include<sys/stat.h> 
#include<unistd.h> 
#include<fcntl.h> 
#include<stdlib.h> 
#include<stdio.h> 
#include<png.h> 
int fd; 
void png_read(png_struct *png,png_byte *data,png_size_t len){ 
    read(fd,data,len); 
} 
int main(void){ 
    fd=open("foo.png",O_RDONLY); 
    png_struct *png=png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0); 
    png_info *png_info=png_create_info_struct(png); 
    png_set_read_fn(png,0,png_read); 
    struct stat s; 
    fstat(fd,&s); 
    printf("File Size: %d\n",s.st_size); 
    png_read_info(png,png_info); 
    int x=png_get_image_width(png,png_info); 
    int y=png_get_image_height(png,png_info); 
    int c=png_get_channels(png,png_info); 
    char *buf=malloc(x*y*c); 
    char **row=malloc(sizeof(*row)*y); 
    { 
    int i=0; 
    while(i<y){ 
     row[i]=buf+x*i*c; 
     i++; 
    } 
    } 
    png_read_image(png,(png_byte**)row); 
    printf("Ending File Position: %d\n",lseek(fd,0,SEEK_CUR)); 
    return(0); 
} 

File Size: 20279 
Ending File Position: 20263 

回答

4

png_read_image後,您應該在技術上有一個png_read_end電話:

// ... 
png_read_image(png,(png_byte**)row); 

png_infop end_info = png_create_info_struct(png); 
png_read_end(png, end_info); 

後,該位置應該匹配。儘管如此,即使是libpng docs(第13.7節的最後幾段)也沒有必要。

1

只有在您對PNG以外的其他數據流感興趣時才需要這樣(kaykun是!)。但是,如果您只想到達PNG的結尾,並且不關心其餘PNG塊的內容,如參考書所述,則可以使用NULL而不是end_info(因此不需要創建end_info結構)。你不能指望PNG文件的其餘部分正好是16個字節;如果PNG恰好在最後一個IDAT塊之後包含文本塊,則會有更多。