farm-ng-core
dyn_image_types.h
Go to the documentation of this file.
1 // Copyright (c) 2011, Hauke Strasdat
2 // Copyright (c) 2012, Steven Lovegrove
3 // Copyright (c) 2021, farm-ng, inc.
4 //
5 // Use of this source code is governed by an MIT-style
6 // license that can be found in the LICENSE file or at
7 // https://opensource.org/licenses/MIT.
8 
9 #pragma once
10 
11 #include "sophus/image/dyn_image.h"
13 
14 namespace sophus {
15 
16 /// Image representing any number of channels (>=1) and any floating and
17 /// unsigned integral channel type.
18 template <class TAllocator = Eigen::aligned_allocator<uint8_t>>
20 static_assert(concepts::DynImageView<AnyImage<>>);
22 static_assert(concepts::DynImageView<AnyImageView>);
23 
24 template <class TAllocator = Eigen::aligned_allocator<uint8_t>>
28 static_assert(concepts::DynImageView<MutAnyImageView>);
29 
31  using PixelVariant = std::variant<
32  uint8_t,
33  uint16_t,
34  float,
35  Pixel2U8,
36  Pixel2U16,
37  Pixel2F32,
38  Pixel3U8,
39  Pixel3U16,
40  Pixel3F32,
41  Pixel4U8,
42  Pixel4U16,
44  template <class TPixel>
45  static auto constexpr isTypeValid() -> bool {
46  return has_type_v<TPixel, PixelVariant>;
47  }
48 
49  static auto constexpr isFormatValid(PixelFormat format) -> bool {
50  bool num_components_constraint =
51  (format.num_components == 1 || format.num_components == 2 ||
52  format.num_components == 3 || format.num_components == 4);
53  bool u8_constraint = format.number_type == NumberType::fixed_point &&
54  format.num_bytes_per_component == 1;
55  bool u16_constraint = format.number_type == NumberType::fixed_point &&
56  format.num_bytes_per_component == 2;
57  bool f32_constraint = format.number_type == NumberType::floating_point &&
58  format.num_bytes_per_component == 4;
59  return num_components_constraint &&
60  (u8_constraint || u16_constraint || f32_constraint);
61  }
62 };
63 
64 /// Image to represent intensity image / texture as grayscale (=1 channel),
65 /// RGB (=3 channel ) and RGBA (=4 channel), either uint8_t [0-255],
66 /// uint16 [0-65535] or float [0.0-1.0] channel type.
67 template <class TAllocator = Eigen::aligned_allocator<uint8_t>>
70 template <class TAllocator = Eigen::aligned_allocator<uint8_t>>
73 
74 namespace detail {
75 // Call UserFunc with TDynImage cast to the appropriate concrete type
76 // from the options in pixelFormats...
77 template <class TUserFunc, typename TDynImage, typename... TTpixelFormats>
78 struct VisitImpl;
79 
80 // base case
81 template <class TUserFunc, typename TDynImage, typename TPixelFormat>
82 struct VisitImpl<TUserFunc, TDynImage, std::variant<TPixelFormat>> {
83  static void visit(TUserFunc&& func, TDynImage const& image) {
84  if (image.pixelFormat().template is<TPixelFormat>()) {
85  func(image.template imageView<TPixelFormat>());
86  }
87  }
88 };
89 
90 // inductive case
91 template <
92  typename TUserFunc,
93  typename TDynImage,
94  typename TPixelFormat,
95  typename... TRest>
96 struct VisitImpl<TUserFunc, TDynImage, std::variant<TPixelFormat, TRest...>> {
97  static void visit(TUserFunc&& func, TDynImage const& image) {
98  if (image.pixelFormat().template is<TPixelFormat>()) {
99  func(image.template imageView<TPixelFormat>());
100  } else {
101  VisitImpl<TUserFunc, TDynImage, std::variant<TRest...>>::visit(
102  std::forward<TUserFunc>(func), image);
103  }
104  }
105 };
106 } // namespace detail
107 
108 // DynImage visitor
109 template <
110  typename TUserFunc,
111  class TPredicate = IntensityImagePredicate,
112  class TAllocator = Eigen::aligned_allocator<uint8_t>>
114  TUserFunc&& func, DynImage<TPredicate, TAllocator> const& image) {
115  using TDynImage = DynImage<TPredicate, TAllocator>;
117  visit(std::forward<TUserFunc>(func), image);
118 }
119 
120 // DynImageView visitor - shares same implementation than the one for
121 // DynImage
122 template <class TUserFunc, class TPredicate = IntensityImagePredicate>
123 void visitImage(TUserFunc&& func, DynImageView<TPredicate> const& image) {
125  detail::
126  VisitImpl<TUserFunc, DynImageView, typename TPredicate::PixelVariant>::
127  visit(std::forward<TUserFunc>(func), image);
128 }
129 
130 } // namespace sophus
mut_dyn_image.h
sophus::MutDynImage
Definition: mut_dyn_image.h:23
sophus::Pixel2U16
Pixel2< uint16_t > Pixel2U16
Definition: image_types.h:20
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::IntensityImagePredicate::PixelVariant
std::variant< uint8_t, uint16_t, float, Pixel2U8, Pixel2U16, Pixel2F32, Pixel3U8, Pixel3U16, Pixel3F32, Pixel4U8, Pixel4U16, Pixel4F32 > PixelVariant
Definition: dyn_image_types.h:43
sophus::detail::VisitImpl< TUserFunc, TDynImage, std::variant< TPixelFormat > >::visit
static void visit(TUserFunc &&func, TDynImage const &image)
Definition: dyn_image_types.h:83
sophus::IntensityImagePredicate
Definition: dyn_image_types.h:30
sophus::Pixel3U8
Pixel3< uint8_t > Pixel3U8
Definition: image_types.h:25
sophus::Pixel3F32
Pixel3< float > Pixel3F32
Definition: image_types.h:27
sophus::IntensityImagePredicate::isTypeValid
static constexpr auto isTypeValid() -> bool
Definition: dyn_image_types.h:45
sophus::detail::VisitImpl< TUserFunc, TDynImage, std::variant< TPixelFormat, TRest... > >::visit
static void visit(TUserFunc &&func, TDynImage const &image)
Definition: dyn_image_types.h:97
sophus::Pixel4F32
Pixel4< float > Pixel4F32
Definition: image_types.h:33
sophus::IntensityImagePredicate::isFormatValid
static constexpr auto isFormatValid(PixelFormat format) -> bool
Definition: dyn_image_types.h:49
dyn_image.h
sophus::Pixel2F32
Pixel2< float > Pixel2F32
Definition: image_types.h:21
sophus::concepts::DynImageView
concept DynImageView
Definition: image.h:52
sophus::DynImage
Type-erased image with shared ownership, and read-only access to pixels. Type is nullable.
Definition: dyn_image.h:24
sophus::Pixel2U8
Pixel2< uint8_t > Pixel2U8
Definition: image_types.h:19
sophus::Pixel3U16
Pixel3< uint16_t > Pixel3U16
Definition: image_types.h:26
sophus::Pixel4U8
Pixel4< uint8_t > Pixel4U8
Definition: image_types.h:31
core.event_service_recorder.func
func
Definition: event_service_recorder.py:420
sophus::MutDynImageView< AnyImagePredicate >
sophus::Pixel4U16
Pixel4< uint16_t > Pixel4U16
Definition: image_types.h:32
sophus::detail::VisitImpl
Definition: dyn_image_types.h:78
sophus::DynImageView< AnyImagePredicate >
sophus::visitImage
void visitImage(TUserFunc &&func, DynImage< TPredicate, TAllocator > const &image)
Definition: dyn_image_types.h:113
sophus::PixelFormat
Definition: pixel_format.h:15