本文介绍了如何释放libjpeg创建的缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用libjpeg从OpenCV Mat转换图像缓冲区并将其写入内存位置

I am using libjpeg to transform image buffer from OpenCV Mat and write it to a memory location

这是代码:

bool mat2jpeg(cv::Mat frame, unsigned char **outbuffer
    , long unsigned int *outlen) {

    unsigned char *outdata = frame.data;

    struct jpeg_compress_struct cinfo = { 0 };
    struct jpeg_error_mgr jerr;
    JSAMPROW row_ptr[1];
    int row_stride;

    *outbuffer = NULL;
    *outlen = 0;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_compress(&cinfo);
    jpeg_mem_dest(&cinfo, outbuffer, outlen);
    jpeg_set_quality(&cinfo, JPEG_QUALITY, TRUE);
    cinfo.image_width = frame.cols;
    cinfo.image_height = frame.rows;
    cinfo.input_components = 1;
    cinfo.in_color_space = JCS_GRAYSCALE;

    jpeg_set_defaults(&cinfo);
    jpeg_start_compress(&cinfo, TRUE);
    row_stride = frame.cols;

    while (cinfo.next_scanline < cinfo.image_height) {
        row_ptr[0] = &outdata[cinfo.next_scanline * row_stride];
        jpeg_write_scanlines(&cinfo, row_ptr, 1);
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);


    return true;

}

问题是我无法在任何地方释放缓冲区.

The thing is I cannot deallocate outbuffer anywhere.

这是我使用该功能的方式:

This is how I am using the function:

long unsigned int * __size__ = nullptr;

unsigned char * _buf = nullptr;

mat2jpeg(_img, &_buf, __size__);

free(_buf)和free(* _ buf)均失败看来我正在尝试这样做,以释放堆头.

both free(_buf) and free(*_buf) failsit seems i am trying to free the head of heap by doing so.

和mat2jpeg将不接受指向缓冲区外指针的指针.有什么主意吗?

and mat2jpeg won't accept a pointer to pointer for outbuffer. any idea?

推荐答案

我认为您的问题可能出在您的__size__变量上.它没有分配到任何地方.根据我对 libjpeg源代码的阅读,这意味着该缓冲区永远不会分配,程序将调用致命错误函数.

I think your problem may be with your __size__ variable. Its not allocated anywhere. According to my reading of the libjpeg source code that means the buffer is never allocated and the program calls a fatal error function.

我认为您需要这样称呼它:

I think you need to call it like this:

long unsigned int __size__ = 0; // not a pointer

unsigned char * _buf = nullptr;

mat2jpeg(_img, &_buf, &__size__); // send address of __size__

然后,您应该可以使用以下方法取消分配缓冲区:

Then you should be able to deallocate the buffer with:

free(_buf);

这篇关于如何释放libjpeg创建的缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:45