invert.hpp 1.08 KB
Newer Older
1
2
3
4
5
6
7
#pragma once

#include <py/core/image2d.hpp>
#include <py/core/type_info.hpp>

namespace mln::py
{
8
  namespace impl
9
  {
10
11
12
    template <class T>
    image2d<uint8_t> invert(const image2d<T>& src)
    {
13
14
15
16
17
    auto        res  = image2d<uint8_t>(src.width(), src.height());
    auto        span = src.values();
    const auto& vs   = src.get_value_set();
    std::transform(span.begin(), span.end(), res.values().begin(),
                   [&vs](auto val) -> uint8_t {
18
19
20
                     auto tmp = vs.max();
                     auto new_val = vs.template cast<float>(val) / vs.template cast<float>(tmp);
                     return (1 - new_val) * 255;
21
22
                   });
    return res;
23
    }
24
25
  }

26
  image2d<uint8_t> invert(const image2d<>& src)
27
  {
28
    switch (src.type().tid())
29
    {
30
31
32
33
34
35
36
37
      case (Info::INT8_V):
        return impl::invert(*src.cast_to<int8_t>());
      case (Info::INT32_V):
        return impl::invert(*src.cast_to<int32_t>());
      default:
        return impl::invert(src);
    }
  };
38
39
40

  image2d<> invert_py(const image2d<>& src) { return invert(src); };
} // namespace mln::py