farm-ng-core
dyn_image_view.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/image.h"
14 
15 #include <variant>
16 
17 namespace sophus {
18 
20  template <class TPixel>
21  static auto constexpr isTypeValid() -> bool {
22  return true;
23  }
24 
25  static auto constexpr isFormatValid(PixelFormat) -> bool { return true; }
26 };
27 
28 template <class TPredicate = AnyImagePredicate>
29 class DynImageView {
30  public:
31  /// Create type-erased image view from ImageView.
32  ///
33  /// By design not "explicit".
34  template <class TPixel>
36  : DynImageView(
37  image.layout(), PixelFormat::fromTemplate<TPixel>(), image.ptr()) {
38  static_assert(TPredicate::template isTypeValid<TPixel>());
39  }
40 
41  template <class TPixel>
42  [[nodiscard]] auto has() const noexcept -> bool {
43  PixelFormat expected_type = PixelFormat::fromTemplate<TPixel>();
44  return expected_type == pixel_format_;
45  }
46 
47  /// Returns v-th row pointer.
48  ///
49  /// Precondition: v must be in [0, height).
50  [[nodiscard]] auto rawRowPtr(int v) const -> uint8_t const* {
51  return ((uint8_t*)(rawPtr()) + v * layout_.pitchBytes());
52  }
53 
54  [[nodiscard]] auto rawPtr() const -> uint8_t const* { return ptr_; }
55 
56  [[nodiscard]] auto layout() const -> ImageLayout const& { return layout_; }
57 
58  [[nodiscard]] auto imageSize() const -> ImageSize const& {
59  return layout_.imageSize();
60  }
61 
62  [[nodiscard]] auto area() const -> size_t { return layout().area(); }
63  [[nodiscard]] auto width() const -> int { return layout().width(); }
64  [[nodiscard]] auto height() const -> int { return layout().height(); }
65  [[nodiscard]] auto pitchBytes() const -> size_t {
66  return layout().pitchBytes();
67  }
68  [[nodiscard]] auto sizeBytes() const -> size_t {
69  return height() * pitchBytes();
70  }
71  [[nodiscard]] auto isEmpty() const -> bool {
72  return this->rawPtr() == nullptr;
73  }
74 
75  [[nodiscard]] auto pixelFormat() const -> PixelFormat {
76  return pixel_format_;
77  }
78  [[nodiscard]] auto numChannels() const -> int {
79  return this->pixel_format_.num_components;
80  }
81 
82  /// Returns subview with shared ownership semantics of whole image.
83  [[nodiscard]] auto subview(Eigen::Vector2i uv, sophus::ImageSize size) const
84  -> DynImageView {
85  SOPHUS_ASSERT(imageSize().contains(uv));
86  SOPHUS_ASSERT_LE(uv.x() + size.width, this->layout_.width());
87  SOPHUS_ASSERT_LE(uv.y() + size.height, this->layout_.height());
88 
89  auto const layout = ImageLayout(size, pitchBytes());
90  const size_t row_offset = uv.x() * this->pixelFormat().numBytesPerPixel();
91  uint8_t const* ptr = this->rawPtr() + uv.y() * pitchBytes() + row_offset;
92  return DynImageView{layout, this->pixel_format_, ptr};
93  }
94 
95  /// Returns typed image.
96  ///
97  /// Precondition: this->has<TPixel>()
98  template <class TPixel>
99  [[nodiscard]] auto imageView() const noexcept -> ImageView<TPixel> {
100  if (!this->has<TPixel>()) {
101  PixelFormat expected_type = PixelFormat::fromTemplate<TPixel>();
102 
103  SOPHUS_PANIC(
104  "expected type: {}\n"
105  "actual type: {}",
106  expected_type,
107  this->pixel_format_);
108  }
109 
110  return ImageView<TPixel>(
111  this->layout_, reinterpret_cast<TPixel const*>(ptr_));
112  }
113 
114  void setViewToEmpty() {
115  this->layout_ = {};
116  this->ptr_ = nullptr;
117  }
118 
119  // Marked as unsafe until this is fully understood.
120  //
121  // In particular, the user need to make sure that the memory block ptr
122  // is pointing too is properly aligned, such that calls as follows won't
123  // cause UB:
124  //
125  // this->imageView<Eigen::Vector4f>()
127  ImageLayout const& layout,
128  PixelFormat const& pixel_format,
129  void const* ptr) {
130  return DynImageView(layout, pixel_format, ptr);
131  }
132 
133  protected:
134  DynImageView() = default;
135 
137  ImageLayout const& layout,
138  PixelFormat const& pixel_format,
139  void const* ptr)
140  : layout_(layout),
141  pixel_format_(pixel_format),
142  ptr_(reinterpret_cast<uint8_t const*>(ptr)) {
143  SOPHUS_ASSERT(TPredicate::isFormatValid(pixel_format));
144  }
145 
148  uint8_t const* ptr_ = nullptr;
149 };
150 } // namespace sophus
SOPHUS_PANIC
#define SOPHUS_PANIC(...)
Definition: common.h:50
SOPHUS_ASSERT
#define SOPHUS_ASSERT(...)
Definition: common.h:40
sophus::DynImageView::ptr_
uint8_t const * ptr_
Definition: dyn_image_view.h:148
sophus::ImageLayout::pitchBytes
auto pitchBytes() const -> size_t
Definition: layout.h:57
sophus::DynImageView::layout
auto layout() const -> ImageLayout const &
Definition: dyn_image_view.h:56
sophus::ImageLayout
Layout of the image: width, height and pitch in bytes.
Definition: layout.h:23
image.h
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::DynImageView::pixelFormat
auto pixelFormat() const -> PixelFormat
Definition: dyn_image_view.h:75
sophus::DynImageView::imageSize
auto imageSize() const -> ImageSize const &
Definition: dyn_image_view.h:58
image_types.h
sophus::ImageView
A view of an (immutable) image, which does not own the data.
Definition: image_view.h:55
sophus::DynImageView::height
auto height() const -> int
Definition: dyn_image_view.h:64
sophus::DynImageView::isEmpty
auto isEmpty() const -> bool
Definition: dyn_image_view.h:71
sophus::DynImageView::setViewToEmpty
void setViewToEmpty()
Definition: dyn_image_view.h:114
pixel_format.h
sophus::DynImageView::layout_
ImageLayout layout_
Definition: dyn_image_view.h:146
sophus::DynImageView::numChannels
auto numChannels() const -> int
Definition: dyn_image_view.h:78
sophus::DynImageView::rawRowPtr
auto rawRowPtr(int v) const -> uint8_t const *
Returns v-th row pointer.
Definition: dyn_image_view.h:50
sophus::DynImageView::width
auto width() const -> int
Definition: dyn_image_view.h:63
sophus::DynImageView::pixel_format_
PixelFormat pixel_format_
Definition: dyn_image_view.h:147
sophus::PixelFormat::num_components
uint8_t num_components
Definition: pixel_format.h:38
sophus::DynImageView::pitchBytes
auto pitchBytes() const -> size_t
Definition: dyn_image_view.h:65
SOPHUS_ASSERT_LE
#define SOPHUS_ASSERT_LE(...)
Definition: common.h:44
sophus::DynImageView::has
auto has() const noexcept -> bool
Definition: dyn_image_view.h:42
sophus::AnyImagePredicate::isTypeValid
static constexpr auto isTypeValid() -> bool
Definition: dyn_image_view.h:21
sophus::DynImageView::DynImageView
DynImageView(ImageView< TPixel > const &image)
Create type-erased image view from ImageView.
Definition: dyn_image_view.h:35
sophus::DynImageView::sizeBytes
auto sizeBytes() const -> size_t
Definition: dyn_image_view.h:68
sophus::DynImageView::subview
auto subview(Eigen::Vector2i uv, sophus::ImageSize size) const -> DynImageView
Returns subview with shared ownership semantics of whole image.
Definition: dyn_image_view.h:83
sophus::DynImageView::unsafeWrapAndPromiseProperAlignment
static DynImageView unsafeWrapAndPromiseProperAlignment(ImageLayout const &layout, PixelFormat const &pixel_format, void const *ptr)
Definition: dyn_image_view.h:126
sophus::DynImageView::DynImageView
DynImageView(ImageLayout const &layout, PixelFormat const &pixel_format, void const *ptr)
Definition: dyn_image_view.h:136
sophus::AnyImagePredicate
Definition: dyn_image_view.h:19
sophus::DynImageView::DynImageView
DynImageView()=default
sophus::DynImageView::rawPtr
auto rawPtr() const -> uint8_t const *
Definition: dyn_image_view.h:54
sophus::DynImageView::imageView
auto imageView() const noexcept -> ImageView< TPixel >
Returns typed image.
Definition: dyn_image_view.h:99
sophus::ImageLayout::imageSize
auto imageSize() const -> sophus::ImageSize const &
Definition: layout.h:51
sophus::ImageSize
Image size, hence its width and height.
Definition: image_size.h:21
sophus::DynImageView::area
auto area() const -> size_t
Definition: dyn_image_view.h:62
sophus::DynImageView
Definition: dyn_image_view.h:29
sophus::PixelFormat
Definition: pixel_format.h:15
sophus::AnyImagePredicate::isFormatValid
static constexpr auto isFormatValid(PixelFormat) -> bool
Definition: dyn_image_view.h:25