libpng 未读数据
看来,当使用 libpng 解码 PNG 文件时,它不会读取最后 16 个字节,因此我向前寻找 16 个字节以到达末尾。我可以假设所有 PNG 文件都是如此吗?
#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
It seems that when decoding PNG files with libpng it doesn't read the last 16 bytes, so I seek 16 bytes forward to get to the end. Can I assume that this is true for all PNG files?
#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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 png_read_image 之后,从技术上讲,您应该有一个 png_read_end 调用:
之后位置应该匹配。
即使是 libpng 文档(第 13.7 节的最后一段)也显得没有必要, 尽管。
After png_read_image you should technically have a png_read_end call:
After that the positions should match.
Even the libpng docs (last paragraphs of section 13.7) make it seem unnecessary, though.
仅当您对 PNG 之外的数据流的其余部分(即 kaykun 就是!)感兴趣时才需要这样做。但是,如果您只想到达 PNG 的末尾而不关心其余 PNG 块的内容,如参考书所述,您可以使用 NULL 而不是 end_info (因此不需要创建end_info 结构)。您不能指望 PNG 文件的剩余部分正好是 16 个字节;如果 PNG 碰巧在最后一个 IDAT 块之后包含文本块,则会有更多。
It is only necessary if you are interested in the rest of the datastream beyond the PNG (which kaykun is!). However, if you just want to get to the end of the PNG and don't care about the contents of the remaining PNG chunks, as the referenced book says, you can use NULL instead of end_info (and therefore don't need to create the end_info structure). You cannot count on the remainder of the PNG file being exactly 16 bytes; there will be more if the PNG happens to contain text chunks after the last IDAT chunk.