read a png image in buffer

此生再无相见时 提交于 2019-12-08 05:06:21

问题


hi i have used libpng to convert grayscale png image to raw image using c. in that lib the function png_init_io needs file pointer to read the png. but i pass the png image as buffer is there any other alternative functions to to read png image buffer to raw image. please help me

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);
......
}

instead i need this like

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

回答1:


From the manual of png_init_io, it is apparent that you can override the read function with png_set_read_fn.

Doing this, you can fool png_init_io into thinking that it is reading from a file, while in reality you're reading from a buffer:

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


来源:https://stackoverflow.com/questions/15410177/read-a-png-image-in-buffer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!