Do you need to free the buffer pointer passed to Halide

148 Views Asked by At

If I create a Halide::Buffer by constructing it with a pointer from an STB_Image function call like so:

inline Halide::Buffer<uint8_t> LoadFromFile(const char* filename)
{
    int w, h, d;
    unsigned char* image_data = stbi_load(filename, &w, &h, &d, 0);
    Halide::Buffer buff =  Halide::Buffer(image_data, std::vector<int>{w, h, d});
    return buff;
}

Who is responsible for freeing the underlying buffer? I assumed it would be me since I allocated the memory so I should free it. If I am responsible, where should it be done?

Thanks

1

There are 1 best solutions below

1
Alex Reinking On

Whenever you pass a raw pointer to the Halide::Buffer constructor, it does not take ownership. It's unfortunate that STBI doesn't include a way to load an image to a pre-allocated buffer.


If the Halide image io library meets your needs, you might find it more convenient.

#include <Halide.h>
#include <halide_image_io.h>

// ...

Halide::Buffer<uint8_t> input = Halide::Tools::load_image("images/rgb.png");

Now input owns the image data and will be freed when the destructor is called. The Halide image io library supports the following formats:

  • PNG (via libpng)
  • JPEG (via libjpeg)
  • TIFF
  • Matlab .mat files