代码之家  ›  专栏  ›  技术社区  ›  user924

libbpg-如何传递字节而不是文件路径

  •  0
  • user924  · 技术社区  · 6 年前

    我想用 伦敦银行同业拆借利率 C++项目中的库(C语言编写) https://github.com/mirrorer/libbpg )

    我需要传递图像字节作为输入,而不是图像文件的绝对路径,我还需要输出结果的字节数(而不是“.png>.bpg”I need“bytes>bytes”)。

    E、 g.我想从网络摄像头捕获帧,并将其转换为bpg字节,还可以通过网络传递这些字节

    main libbpg的功能 bpgenc.c 以文件路径字符串作为输入参数

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

    有什么建议是如何传递字节而不是文件名吗?

    例如,如果camera返回BGR格式的图像字节,则如何将其转换为 Image 关于libbpg?

    1 回复  |  直到 6 年前
        1
  •  0
  •   kabanus    6 年前

    继续深入研究代码,你可以看到 main 正在呼叫 load_image :

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

    不管是哪一个 read_png (1456年),或 read_jpeg (1459年)。为了 png 例如,文件用于:

    950: png_init_io(png_ptr, f);
    

    令人高兴的是,有一个这样的问题: read a png image in buffer

    所以,作为 Shahbaz 答案,你需要假装阅读(直接从答案中获得):

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

    这将允许原始函数正常工作。

    当然,如果这一切太麻烦了,您可以将字节写入文件,然后正常使用库。