Skip to content

Instantly share code, notes, and snippets.

@brotherofken
Last active August 30, 2019 15:36
Show Gist options
  • Save brotherofken/8cd6b5d8bc209372c212a79866f5f128 to your computer and use it in GitHub Desktop.
Save brotherofken/8cd6b5d8bc209372c212a79866f5f128 to your computer and use it in GitHub Desktop.
extern "C" int runOpenCV(halide_buffer_t *im1, halide_buffer_t *im2, halide_buffer_t *im3, halide_buffer_t *out) {
// width
const int min0 = out->dim[0].min;
const int max0 = out->dim[0].min + out->dim[0].extent - 1;
const int width = out->dim[0].extent;
// height
const int min1 = out->dim[1].min;
const int max1 = out->dim[1].min + out->dim[1].extent - 1;
const int height = out->dim[1].extent;
if (im1->host == nullptr || im2->host == nullptr || im3->host == nullptr) {
// If any of the inputs have a null host pointer, we're in
// bounds inference mode, and should mutate those input
// buffers that have a null host pointer.
if (im1->is_bounds_query()) {
SetBufferBounds(im1, min0, out->dim[0].extent, min1, out->dim[1].extent);
}
if (im2->is_bounds_query()) {
SetBufferBounds(im2, min0, out->dim[0].extent, min1, out->dim[1].extent);
}
if (im3->is_bounds_query()) {
SetBufferBounds(im3, min0, out->dim[0].extent, min1, out->dim[1].extent);
}
// We don't mutate the output buffer, because we can handle
// any size output.
return 0;
} else {
// Configure input
assert(in->type == halide_type_of<uint16_t>());
assert(out->type == halide_type_of<uint16_t>());
assert(in->dim[0].stride == 1 && in->dim[1].stride == 1);
assert(out->dim[0].stride == 1 && out->dim[1].stride == 1);
Halide::Runtime::Buffer<uint16_t> im1Buffer(*im1);
Halide::Runtime::Buffer<uint16_t> im2Buffer(*im2);
Halide::Runtime::Buffer<uint16_t> im3Buffer(*im3);
cv::Mat im1Mat(cv::Size(im1Buffer.width(), im1Buffer.height()), CV_16U, im1Buffer.data());
cv::Mat im2Mat(cv::Size(im2Buffer.width(), im2Buffer.height()), CV_16U, im2Buffer.data());
cv::Mat im3Mat(cv::Size(im3Buffer.width(), im3Buffer.height()), CV_16U, im3Buffer.data());
std::vector<cv::Mat> images{im1Mat, im2Mat, im3Mat};
cv::Mat stuffResult;
DoStuff(images, stuffResult);
Halide::Runtime::Buffer<uint16_t> outputBuffer(*out);
cv::Mat result(cv::Size(outputBuffer.width(), outputBuffer.height()), CV_16U, outputBuffer.data());
stuffResult.copyTo(result);
}
return 0;
}
int main() {
std::vector<ExternFuncArgument> args;
args.push_back(im1);
args.push_back(im2);
args.push_back(im3);
Func f;
f.define_extern(
std::string("runOpenCV"),
args,
Halide::UInt(16),
2,
NameMangling::C
);
f.compute_root();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment