Packages
evision
1.0.0
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_backend/bit_unary.h
#ifndef EVISION_BACKEND_BIT_UNARY_H
#define EVISION_BACKEND_BIT_UNARY_H
#include <cstdint>
#include <type_traits>
#include <erl_nif.h>
#include "../../ArgInfo.hpp"
#include "../evision_mat_utils.hpp"
static inline int evision_popcount_u64(uint64_t x) {
int c = 0;
while (x) { c += (int)(x & 1); x >>= 1; }
return c;
}
static inline int evision_clz_width(uint64_t x, int width) {
int c = 0;
for (int b = width - 1; b >= 0; b--) {
if (x & ((uint64_t)1 << b)) break;
c++;
}
return c;
}
// Per-element bit op preserving the integer type (Nx.count_leading_zeros /
// population_count). op 0 = clz (within the type's bit width), 1 = popcount. The
// element's raw bits are read as unsigned, matching Nx's unsigned read; the count
// is written back in the same type.
template <typename T>
static void evision_bit_unary(cv::Mat &m, int op) {
typedef typename std::make_unsigned<T>::type U;
T *p = (T *)m.data;
size_t n = m.total();
int width = (int)(sizeof(T) * 8);
for (size_t i = 0; i < n; i++) {
uint64_t x = (uint64_t)(U)p[i];
int r = (op == 0) ? evision_clz_width(x, width) : evision_popcount_u64(x);
p[i] = (T)r;
}
}
// @evision c: mat_bit_unary, evision_cv_mat_bit_unary, 1
// @evision nif: def mat_bit_unary(_opts \\ []), do: :erlang.nif_error(:undefined)
static ERL_NIF_TERM evision_cv_mat_bit_unary(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
using namespace cv;
using namespace evision::nif;
std::map<std::string, ERL_NIF_TERM> erl_terms;
int nif_opts_index = 0;
evision::nif::parse_arg(env, nif_opts_index, argv, erl_terms);
{
Mat src;
int op = 0;
if (evision_to_safe(env, evision_get_kw(env, erl_terms, "src"), src, ArgInfo("src", 0)) &&
evision_to_safe(env, evision_get_kw(env, erl_terms, "op"), op, ArgInfo("op", 0))) {
Mat dst = src.clone();
switch (dst.depth()) {
case CV_8U: evision_bit_unary<uint8_t>(dst, op); break;
case CV_8S: evision_bit_unary<int8_t>(dst, op); break;
case CV_16U: evision_bit_unary<uint16_t>(dst, op); break;
case CV_16S: evision_bit_unary<int16_t>(dst, op); break;
case CV_32S: evision_bit_unary<int32_t>(dst, op); break;
case CV_32U: evision_bit_unary<uint32_t>(dst, op); break;
case CV_64S: evision_bit_unary<int64_t>(dst, op); break;
case CV_64U: evision_bit_unary<uint64_t>(dst, op); break;
default: return evision::nif::error(env, "mat_bit_unary: integer types only");
}
return evision_from(env, dst);
}
}
return enif_make_badarg(env);
}
#endif // EVISION_BACKEND_BIT_UNARY_H