2013-03-14 50 views
1

您好我已经使用libpng将灰度png图像转换为使用c的原始图像。在那个lib中函数png_init_io需要文件指针来读取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 */ 
{ 
} 
+0

你的问题不是很清楚(和缺乏大写和标点符号不起作用)。你的意思是你想从内存中读取PNG图像? “pngbuff”缓冲区包含与PNG文件相同的字节? – leonbloy 2013-03-14 13:35:44

+0

@leonbloy是的绝对你是对的..请帮助我是否有任何其他功能 – Siva 2013-03-14 13:39:34

+0

然后我投票关闭作为重复。请参阅此处的答案:http://blog.hammerian.net/2009/reading-png-images-from-memory/ – leonbloy 2013-03-14 13:40:46

回答

1

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); 
+0

请注意,我自己并没有使用libpng,所以我不知道所涉及的细节。 – Shahbaz 2013-03-14 13:43:31