Packages
evision
0.2.15
1.0.1-rc.0
1.0.0
1.0.0-rc.0
0.2.17
0.2.17-rc2
0.2.17-rc1
0.2.16
0.2.16-pre.2
0.2.16-pre
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.2-rc2
0.2.1
0.2.0
0.1.39
0.1.38
0.1.37
0.1.36
0.1.35
0.1.34
0.1.33
0.1.32
retired
0.1.31
0.1.30
0.1.29
0.1.28
0.1.27
0.1.26
0.1.26-rc3
0.1.26-rc2
0.1.26-rc1
0.1.26-rc0
0.1.25
0.1.24
0.1.23
0.1.22
0.1.21
0.1.20
0.1.19
0.1.18
0.1.17
0.1.16
0.1.15
0.1.14
0.1.13
0.1.12
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
OpenCV-Erlang/Elixir binding.
Current section
Files
Jump to
Current section
Files
c_src/modules/evision_mat_utils.hpp
#ifndef EVISION_MAT_UTILS_HPP
#define EVISION_MAT_UTILS_HPP
#pragma once
int get_binary_type(const std::string& t, int l, int n, int& type) {
if (t == "u") {
if (l == 8) {
if (n != 0) type = CV_8UC(n);
else type = CV_8U;
return true;
}
if (l == 16) {
if (n != 0) type = CV_16UC(n);
else type = CV_16U;
return true;
}
} else if (t == "s") {
if (l == 8) {
if (n != 0) type = CV_8SC(n);
else type = CV_8S;
return true;
}
if (l == 16) {
if (n != 0) type = CV_16SC(n);
else type = CV_16S;
return true;
}
if (l == 32) {
if (n != 0) type = CV_32SC(n);
else type = CV_32S;
return true;
}
} else if (t == "f") {
if (l == 16) {
if (n != 0) type = CV_16FC(n);
else type = CV_16F;
return true;
}
if (l == 32) {
if (n != 0) type = CV_32FC(n);
else type = CV_32F;
return true;
}
if (l == 64) {
if (n != 0) type = CV_64FC(n);
else type = CV_64F;
return true;
}
}
return false;
}
int get_compact_type(const std::string& compact, int n, int& type) {
if (compact[0] == 'u') {
if (compact == "u8") {
if (n != 0) type = CV_8UC(n);
else type = CV_8U;
return true;
}
if (compact == "u16") {
if (n != 0) type = CV_16UC(n);
else type = CV_16U;
return true;
}
} else if (compact[0] == 's') {
if (compact == "s8") {
if (n != 0) type = CV_8SC(n);
else type = CV_8S;
return true;
}
if (compact == "s16") {
if (n != 0) type = CV_16SC(n);
else type = CV_16S;
return true;
}
if (compact == "s32") {
if (n != 0) type = CV_32SC(n);
else type = CV_32S;
return true;
}
} else if (compact[0] == 'f') {
if (compact == "f16") {
if (n != 0) type = CV_16FC(n);
else type = CV_16F;
return true;
}
if (compact == "f32") {
if (n != 0) type = CV_32FC(n);
else type = CV_32F;
return true;
}
if (compact == "f64") {
if (n != 0) type = CV_64FC(n);
else type = CV_64F;
return true;
}
}
return false;
}
static void _evision_binary_unref(void *buf, ErlNifEnv *env) {
// enif_fprintf(stderr, "freed nif_env\n");
// freeing env frees unref all terms it contains
enif_free_env(env);
return;
}
static ERL_NIF_TERM _evision_binary_ref(ERL_NIF_TERM bin_term, evision_res<cv::Mat *> * zero_copy_mat) {
// adapted from https://github.com/akash-akya/zero_copy/blob/master/c_src/zero_copy.c
ErlNifBinary bin;
ERL_NIF_TERM term;
ErlNifEnv *new_env;
zero_copy_mat->in_buf = nullptr;
zero_copy_mat->in_ref = nullptr;
// keep reference to binary by creating new nif-env and copying
// binary-term reference to it
new_env = enif_alloc_env();
term = enif_make_copy(new_env, bin_term);
if (!enif_inspect_binary(new_env, term, &bin)) {
enif_free_env(new_env);
return 1;
}
// Note that we are *NOT* copying the binary data
zero_copy_mat->in_buf = bin.data;
// input buffer specific opaque data which will be passed as second
// argument to finalizer during unref
zero_copy_mat->in_ref = (void *)new_env;
// function to be called to unref the input data
zero_copy_mat->in_unref = (void (*)(void *, void *))_evision_binary_unref;
return 0;
}
#endif // EVISION_MAT_UTILS_HPP