用缓冲区读取PNG图像

3

你好,我使用libpng和c语言将灰度PNG图像转换为原始图像。在该库中,函数png_init_io需要文件指针来读取PNG图像。但是我将PNG图像作为缓冲区传递,是否有其他替代函数可以从PNG图像缓冲区中读取原始图像?请帮帮我。

int read_png(char *file_name,int *outWidth,int *outHeight,unsigned char **outRaw)  /* We need to open the file */
{
......
/* Set up the input control if you are using standard C streams */
   png_init_io(png_ptr, fp);
......
}

我需要的是这样的

int read_png(unsigned char *pngbuff, int pngbuffleng, int *outWidth,int *outHeight,unsigned char **outRaw)  /* We need to open the file */
{
}

你的问题不是很清晰(缺少大写字母和标点符号也没有帮助)。你的意思是想从内存中读取PNG图像吗?pngbuff缓冲区包含与PNG文件相同的字节吗? - leonbloy
@leonbloy 是的,你说得对。请帮帮我,还有其他的函数吗? - Siva
那我投票关闭为重复。请参见此处的答案:http://blog.hammerian.net/2009/reading-png-images-from-memory/ - leonbloy
可能是libpng从内存缓冲区加载文件的重复问题。 - leonbloy
@leonbloy 我已经看过这个,但对我来说不是很清楚。我是新手。如果您能提供一些简单的测试程序用于 png_set_read_fn 和 png_set_write_fn,我将非常感激您。 - Siva
你解决了问题并能展示完整的例子吗?我正在尝试解决一个类似的问题,但是使用的是libbpg库(它也使用了png_init_io函数)- https://stackoverflow.com/questions/53315767/libbpg-how-to-pass-bytes-instead-of-file-path - user924
2个回答

2
png_init_io手册中可以看出,你可以使用png_set_read_fn覆盖读取函数。通过这样做,你可以欺骗png_init_io,让它认为正在从文件读取,实际上是从缓冲区读取:
struct fake_file
{
    unsigned int *buf;
    unsigned int size;
    unsigned int cur;
};

static ... fake_read(FILE *fp, ...) /* see input and output from doc */
{
    struct fake_file *f = (struct fake_file *)fp;
    ... /* read a chunk and update f->cur */
}

struct fake_file f = { .buf = pngBuff, .size = pngbuffleng, .cur = 0 };
/* override read function with fake_read */
png_init_io(png_ptr, (FILE *)&f);

请注意,我自己没有使用过libpng,因此我不知道涉及的详细信息。 - Shahbaz

0
尝试使用 png_set_read_fn 替代 png_init_io

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接