2013-03-14 56 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