Search code examples
imageinitializationhalide

Create a Halide::Image object from an existing double C++ array


I have a question about wrapping a Halide::Image object around an already allocated C++ array that defines a 2-D double precision floating point image.

I've already looked at:

C++ array to Halide Image (and back)

This is close to what I want to do, but I'm confused by uint8_t type of the host member of buffer_t in halide and how you work with existing images that are not uint8_t.

I see that in the blur app which uses aot, the example allocates a Halide Image, and then copies elements into this halide image. I would like to do this, but without paying for the copy.

It's not an option for me to use load_image, I need to work with existing, already allocated memory defined by a double *.

Image<uint16_t> input(6408, 4802);

for (int y = 0; y < input.height(); y++) {
    for (int x = 0; x < input.width(); x++) {
        input(x, y) = rand() & 0xfff;
    }
}

Solution

  • The uint8_t* type of the host field of buffer_t is just a pointer to any array of data. You are free to point it to a float or double array. It's basically a void* pointer that gets reinterpreted by the actual pipeline code. Its interpretation is determined by the combination of the Halide program that loads from it, and the elem_size field (which should be, e.g., sizeof(double) or sizeof(float) for floating point data of different kinds).