libbpg - how to pass bytes instead of file path

拟墨画扇 提交于 2019-12-11 23:23:57

问题


I want to use libbpg library (written in C) in C++ project (https://github.com/mirrorer/libbpg)

I need to pass image bytes as input instead of the absolute path of the image file and I need the output result in the bytes too (instead of ".png > .bpg" I need "bytes > bytes").

E.g. I would like to capture frames from webcam and convert them to bpg bytes and also pass those bytes over network

main function of libbpg's bpgenc.c takes a string of file path as input parameter

https://github.com/mirrorer/libbpg/blob/master/bpgenc.c#L2909

Any suggestions how to pass bytes instead of file name?

For example If camera returns image bytes in BGR format then how can I convert it to Image of libbpg?


回答1:


Just keep delving into the code, you can see main is calling load_image:

2909: img = load_image(&md, filename, color_space, bit_depth, limited_range,

which, calls either read_png (1456), or read_jpeg (1459). For png for example, the file is used at:

950: png_init_io(png_ptr, f);

and happily, there is a SO question for that: read a png image in buffer

So, as Shahbaz answers, you need to fake the read (taken straight from that answer):

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

and this will allow the original function to work normally.

Of course, if all this is too much hassle, you can just write your bytes to a file and then use the library normally.



来源:https://stackoverflow.com/questions/53315767/libbpg-how-to-pass-bytes-instead-of-file-path

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