farm-ng-core
mut_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 
12 
13 #include <variant>
14 
15 namespace sophus {
16 
17 template <class TPredicate = AnyImagePredicate>
18 class MutDynImageView : public DynImageView<TPredicate> {
19  public:
20  /// Create type-erased image view from ImageView.
21  ///
22  /// By design not "explicit".
23  template <class TPixel>
26  image.layout(), PixelFormat::fromTemplate<TPixel>(), image.ptr()) {
27  static_assert(TPredicate::template isTypeValid<TPixel>());
28  }
29 
30  /// Return true is this contains data of type TPixel.
31  template <class TPixel>
32  [[nodiscard]] auto has() const noexcept -> bool {
33  PixelFormat expected_type = PixelFormat::fromTemplate<TPixel>();
34  return expected_type == this->pixel_format_;
35  }
36 
37  /// Returns v-th row pointer.
38  ///
39  /// Precondition: v must be in [0, height).
40  [[nodiscard]] auto rawMutRowPtr(int v) const -> uint8_t* {
41  return this->rawMutPtr() + v * this->layout_.pitchBytes();
42  }
43 
44  [[nodiscard]] auto rawMutPtr() const -> uint8_t* {
45  return const_cast<uint8_t*>(this->rawPtr());
46  }
47 
48  /// Returns subview with shared ownership semantics of whole image.
49  [[nodiscard]] auto mutSubview(
50  Eigen::Vector2i uv, sophus::ImageSize size) const -> MutDynImageView {
51  SOPHUS_ASSERT(this->imageSize().contains(uv));
52  SOPHUS_ASSERT_LE(uv.x() + size.width, this->layout_.width());
53  SOPHUS_ASSERT_LE(uv.y() + size.height, this->layout_.height());
54 
55  auto const layout = ImageLayout(size, this->pitchBytes());
56  const size_t row_offset =
57  uv.x() * this->numBytesPerPixelChannel() * this->numChannels();
58  uint8_t* ptr = this->rawMutPtr() + uv.y() * this->pitchBytes() + row_offset;
59  return MutDynImageView{layout, this->pixel_format_, ptr};
60  }
61 
62  /// Returns typed image view.
63  ///
64  /// Precondition: this->has<TPixel>()
65  template <class TPixel>
66  [[nodiscard]] auto mutImageView() const noexcept -> MutImageView<TPixel> {
67  if (!this->has<TPixel>()) {
68  PixelFormat expected_type = PixelFormat::fromTemplate<TPixel>();
69 
71  "expected type: {}\n"
72  "actual type: {}",
73  expected_type,
74  this->pixel_format_);
75  }
76 
77  return MutImageView<TPixel>(
78  this->layout_, reinterpret_cast<TPixel const*>(this->ptr_));
79  }
80 
81  /// Copies data from view into this.
82  ///
83  /// Preconditions:
84  /// * this->isEmpty() == view.isEmpty()
85  /// * this->size() == view.size()
86  ///
87  /// No-op if view is empty.
89  SOPHUS_ASSERT_EQ(this->isEmpty(), view.isEmpty());
90 
91  if (this->isEmpty()) {
92  return;
93  }
94  SOPHUS_ASSERT_EQ(this->imageSize(), view.imageSize());
96  details::pitchedCopy(
97  (uint8_t*)this->rawMutPtr(),
98  this->layout().pitchBytes(),
99  (uint8_t const*)view.rawPtr(),
100  view.layout().pitchBytes(),
101  this->imageSize(),
102  this->imageSize().width * this->pixel_format_.numBytesPerPixel());
103  }
104 
105  protected:
107  ImageLayout const& layout,
108  PixelFormat const& pixel_format,
109  void const* ptr)
110  : DynImageView<TPredicate>(layout, pixel_format, ptr) {
111  SOPHUS_ASSERT(TPredicate::isFormatValid(pixel_format));
112  }
113 
114  MutDynImageView() = default;
115 };
116 } // namespace sophus
SOPHUS_PANIC
#define SOPHUS_PANIC(...)
Definition: common.h:50
SOPHUS_ASSERT
#define SOPHUS_ASSERT(...)
Definition: common.h:40
sophus::MutDynImageView::MutDynImageView
MutDynImageView()=default
sophus::DynImageView< AnyImagePredicate >::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< AnyImagePredicate >::layout
auto layout() const -> ImageLayout const &
Definition: dyn_image_view.h:56
sophus::MutDynImageView::has
auto has() const noexcept -> bool
Return true is this contains data of type TPixel.
Definition: mut_dyn_image_view.h:32
sophus::ImageLayout
Layout of the image: width, height and pitch in bytes.
Definition: layout.h:23
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< AnyImagePredicate >::imageSize
auto imageSize() const -> ImageSize const &
Definition: dyn_image_view.h:58
sophus::MutDynImageView::mutSubview
auto mutSubview(Eigen::Vector2i uv, sophus::ImageSize size) const -> MutDynImageView
Returns subview with shared ownership semantics of whole image.
Definition: mut_dyn_image_view.h:49
sophus::DynImageView< AnyImagePredicate >::isEmpty
auto isEmpty() const -> bool
Definition: dyn_image_view.h:71
dyn_image_view.h
sophus::MutDynImageView::MutDynImageView
MutDynImageView(ImageLayout const &layout, PixelFormat const &pixel_format, void const *ptr)
Definition: mut_dyn_image_view.h:106
sophus::MutDynImageView::MutDynImageView
MutDynImageView(MutImageView< TPixel > const &image)
Create type-erased image view from ImageView.
Definition: mut_dyn_image_view.h:24
sophus::DynImageView< AnyImagePredicate >::layout_
ImageLayout layout_
Definition: dyn_image_view.h:146
sophus::DynImageView< AnyImagePredicate >::numChannels
auto numChannels() const -> int
Definition: dyn_image_view.h:78
sophus::DynImageView< AnyImagePredicate >::pixel_format_
PixelFormat pixel_format_
Definition: dyn_image_view.h:147
sophus::DynImageView< AnyImagePredicate >::pitchBytes
auto pitchBytes() const -> size_t
Definition: dyn_image_view.h:65
SOPHUS_ASSERT_EQ
#define SOPHUS_ASSERT_EQ(...)
Definition: common.h:41
SOPHUS_ASSERT_LE
#define SOPHUS_ASSERT_LE(...)
Definition: common.h:44
sophus::MutDynImageView::mutImageView
auto mutImageView() const noexcept -> MutImageView< TPixel >
Returns typed image view.
Definition: mut_dyn_image_view.h:66
sophus::MutDynImageView::copyDataFrom
void copyDataFrom(DynImageView< TPredicate > view) const
Copies data from view into this.
Definition: mut_dyn_image_view.h:88
sophus::MutImageView
View of a mutable image, which does not own the data.
Definition: mut_image_view.h:61
sophus::DynImageView< AnyImagePredicate >::rawPtr
auto rawPtr() const -> uint8_t const *
Definition: dyn_image_view.h:54
sophus::MutDynImageView::rawMutPtr
auto rawMutPtr() const -> uint8_t *
Definition: mut_dyn_image_view.h:44
sophus::ImageSize
Image size, hence its width and height.
Definition: image_size.h:21
sophus::MutDynImageView
Definition: mut_dyn_image_view.h:18
sophus::MutDynImageView::rawMutRowPtr
auto rawMutRowPtr(int v) const -> uint8_t *
Returns v-th row pointer.
Definition: mut_dyn_image_view.h:40
sophus::DynImageView
Definition: dyn_image_view.h:29
sophus::PixelFormat
Definition: pixel_format.h:15