farm-ng-core
mut_dyn_image.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, class TAllocator>
18 class DynImage;
19 
20 template <
21  class TPredicate = AnyImagePredicate,
22  class TAllocator = Eigen::aligned_allocator<uint8_t>>
23 class MutDynImage : public MutDynImageView<TPredicate> {
24  public:
25  /// Empty image.
26  MutDynImage() = default;
27 
28  /// Not copy constructable
29  MutDynImage(MutDynImage const& other) = delete;
30  /// Not copy assignable
31  auto operator=(MutDynImage const&) -> MutDynImage& = delete;
32 
33  /// Nothrow move constructable
34  MutDynImage(MutDynImage&& other) noexcept = default;
35  /// Nothrow move assignable
36  auto operator=(MutDynImage&&) noexcept -> MutDynImage& = default;
37 
38  /// Create type-erased image from MutImage.
39  ///
40  /// By design not "explicit".
41  template <class TPixel>
43  : MutDynImage(
44  image.layout(),
45  PixelFormat::fromTemplate<TPixel>(),
46  std::move(image.unique_)) {
47  image.reset();
48  static_assert(TPredicate::template isTypeValid<TPixel>());
49  }
50 
51  /// Tries to create image from provided size and format.
52  /// Returns error if format does not satisfy TPredicate.
53  static Expected<DynImage<TPredicate, TAllocator>> tryFromFormat(
54  ImageSize const& size, PixelFormat const& pixel_format) {
55  if (!TPredicate::isFormatValid(pixel_format)) {
56  return SOPHUS_UNEXPECTED("pixel format does not satisfy predicate");
57  }
58  return MutDynImage<TPredicate, TAllocator>(size, pixel_format);
59  }
60 
61  /// Tries to create image from provided size and format.
62  /// Returns error if format does not satisfy TPredicate.
63  static Expected<MutDynImage<TPredicate, TAllocator>> tryFromFormat(
64  ImageLayout const& layout, PixelFormat const& pixel_format) {
65  if (!TPredicate::isFormatValid(pixel_format)) {
66  return SOPHUS_UNEXPECTED("pixel format does not satisfy predicate");
67  }
68  return MutDynImage<TPredicate, TAllocator>(layout, pixel_format);
69  }
70 
71  // Creates image from provided size and format.
72  //
73  // Panics if format does not satisfy TPredicate.
75  ImageSize const& size, PixelFormat const& pixel_format) {
76  return SOPHUS_UNWRAP(tryFromFormat(size, pixel_format));
77  }
78 
79  // Creates image from provided size and format.
80  //
81  // Panics if format does not satisfy TPredicate.
83  ImageLayout const& layout, PixelFormat const& pixel_format) {
84  return SOPHUS_UNWRAP(tryFromFormat(layout, pixel_format));
85  }
86 
87  template <class TT>
88  static auto makeCopyFrom(ImageView<TT> image_view) -> MutDynImage {
89  return MutImage<TT>::makeCopyFrom(image_view);
90  }
91 
92  static auto makeCopyFrom(DynImageView<TPredicate> image_view) -> MutDynImage {
93  MutDynImage image(image_view.imageSize(), image_view.pixelFormat());
94  image.copyDataFrom(image_view);
95  return image;
96  }
97 
98  /// Return true is this contains data of type TPixel.
99  template <class TPixel>
100  [[nodiscard]] auto has() const noexcept -> bool {
101  PixelFormat expected_type = PixelFormat::fromTemplate<TPixel>();
102  return expected_type == this->pixel_format_;
103  }
104 
105  /// Returns typed MutImage.
106  ///
107  /// Precondition: this->has<TPixel>()
108  template <class TPixel>
109  [[nodiscard]] auto moveOutAs() noexcept -> MutImage<TPixel, TAllocator> {
110  SOPHUS_ASSERT(this->has<TPixel>());
112  mut_image.layout_ = this->layout_;
113  mut_image.unique_ = std::move(unique_);
114  mut_image.ptr_ = (TPixel*)mut_image.unique_.get();
115  this->unique_.reset();
116  this->setViewToEmpty();
117  return mut_image;
118  }
119 
120  protected:
121  template <class TPredicate2, class TAllocator2T>
122  friend class DynImage;
123 
124  // Private constructor mainly available for constructing sub-views
127  PixelFormat pixel_format,
129  : MutDynImageView<TPredicate>(layout, pixel_format, unique.get()),
130  unique_(std::move(unique)) {}
131 
132  MutDynImage(ImageSize const& size, PixelFormat const& pixel_format)
133  : MutDynImage(
134  ImageLayout::makeFromSizeAndPitch<uint8_t>(
135  size, size.width * pixel_format.numBytesPerPixel()),
136  pixel_format) {
138  TPredicate::isFormatValid(pixel_format), "Internal logic error");
139  }
140 
141  MutDynImage(ImageLayout const& layout, PixelFormat const& pixel_format)
142  : MutDynImage(layout, pixel_format, nullptr) {
144  TPredicate::isFormatValid(pixel_format), "Internal logic error");
145  if (this->layout_.sizeBytes() != 0u) {
147  TAllocator().allocate(this->layout_.sizeBytes()),
149  UniqueDataAreaDeleter<TAllocator>(this->layout_.sizeBytes())));
150  }
151  this->ptr_ = this->unique_.get();
152  }
153 
155 };
156 
157 } // namespace sophus
sophus::MutDynImage::tryFromFormat
static Expected< MutDynImage< TPredicate, TAllocator > > tryFromFormat(ImageLayout const &layout, PixelFormat const &pixel_format)
Tries to create image from provided size and format. Returns error if format does not satisfy TPredic...
Definition: mut_dyn_image.h:63
sophus::MutDynImage::makeCopyFrom
static auto makeCopyFrom(ImageView< TT > image_view) -> MutDynImage
Definition: mut_dyn_image.h:88
sophus::MutDynImage
Definition: mut_dyn_image.h:23
SOPHUS_ASSERT
#define SOPHUS_ASSERT(...)
Definition: common.h:40
mut_dyn_image_view.h
sophus::MutDynImage::MutDynImage
MutDynImage(ImageSize const &size, PixelFormat const &pixel_format)
Definition: mut_dyn_image.h:132
sophus::MutDynImage::tryFromFormat
static Expected< DynImage< TPredicate, TAllocator > > tryFromFormat(ImageSize const &size, PixelFormat const &pixel_format)
Tries to create image from provided size and format. Returns error if format does not satisfy TPredic...
Definition: mut_dyn_image.h:53
sophus::DynImageView< AnyImagePredicate >::ptr_
uint8_t const * ptr_
Definition: dyn_image_view.h:148
sophus::DynImageView< AnyImagePredicate >::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
SOPHUS_UNWRAP
#define SOPHUS_UNWRAP(...)
Definition: common.h:52
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::MutDynImage::MutDynImage
MutDynImage()=default
Empty image.
sophus::MutDynImage::has
auto has() const noexcept -> bool
Return true is this contains data of type TPixel.
Definition: mut_dyn_image.h:100
sophus::UniqueDataArea
std::unique_ptr< uint8_t, MaybeLeakingUniqueDataAreaDeleter< TAllocator > > UniqueDataArea
Definition: mut_image.h:79
sophus::ImageView
A view of an (immutable) image, which does not own the data.
Definition: image_view.h:55
sophus::MutDynImage::moveOutAs
auto moveOutAs() noexcept -> MutImage< TPixel, TAllocator >
Returns typed MutImage.
Definition: mut_dyn_image.h:109
sophus::DynImageView< AnyImagePredicate >::setViewToEmpty
void setViewToEmpty()
Definition: dyn_image_view.h:114
sophus::MaybeLeakingUniqueDataAreaDeleter
Definition: mut_image.h:53
sophus::DynImageView< AnyImagePredicate >::layout_
ImageLayout layout_
Definition: dyn_image_view.h:146
sophus::MutImage
A image with write access to pixels and exclusive ownership. There is no copy constr / copy assignmen...
Definition: image_view.h:32
sophus::DynImageView< AnyImagePredicate >::width
auto width() const -> int
Definition: dyn_image_view.h:63
sophus::DynImageView< AnyImagePredicate >::pixel_format_
PixelFormat pixel_format_
Definition: dyn_image_view.h:147
SOPHUS_UNEXPECTED
#define SOPHUS_UNEXPECTED(...)
Definition: common.h:57
sophus::MutDynImage::makeCopyFrom
static auto makeCopyFrom(DynImageView< TPredicate > image_view) -> MutDynImage
Definition: mut_dyn_image.h:92
sophus::MutDynImage::operator=
auto operator=(MutDynImage const &) -> MutDynImage &=delete
Not copy assignable.
sophus::MutImage::unique_
UniqueDataArea< TAllocator > unique_
MutImage has unique ownership.
Definition: mut_image.h:218
sophus::MutDynImage::MutDynImage
MutDynImage(MutImage< TPixel, TAllocator > &&image)
Create type-erased image from MutImage.
Definition: mut_dyn_image.h:42
sophus::MutDynImage::fromFormat
static MutDynImage< TPredicate, TAllocator > fromFormat(ImageSize const &size, PixelFormat const &pixel_format)
Definition: mut_dyn_image.h:74
sophus::MutDynImage::MutDynImage
MutDynImage(ImageLayout const &layout, PixelFormat const &pixel_format)
Definition: mut_dyn_image.h:141
sophus::UniqueDataAreaDeleter
Definition: mut_image.h:32
sophus::MutDynImageView::copyDataFrom
void copyDataFrom(DynImageView< TPredicate > view) const
Copies data from view into this.
Definition: mut_dyn_image_view.h:88
sophus::MutImage::makeCopyFrom
static auto makeCopyFrom(ImageView< TPixel > const &view) -> MutImage
Creates contiguous copy from view.
Definition: mut_image.h:124
sophus::MutDynImage::unique_
UniqueDataArea< TAllocator > unique_
Definition: mut_dyn_image.h:154
sophus::DynImage
Type-erased image with shared ownership, and read-only access to pixels. Type is nullable.
Definition: dyn_image.h:24
sophus::ImageLayout::sizeBytes
auto sizeBytes() const -> size_t
Definition: layout.h:62
sophus::MutDynImage::MutDynImage
MutDynImage(ImageLayout layout, PixelFormat pixel_format, UniqueDataArea< TAllocator > unique)
Definition: mut_dyn_image.h:125
sophus::MutDynImage::fromFormat
static DynImage< TPredicate, TAllocator > fromFormat(ImageLayout const &layout, PixelFormat const &pixel_format)
Definition: mut_dyn_image.h:82
sophus::ImageSize
Image size, hence its width and height.
Definition: image_size.h:21
sophus::MutDynImageView
Definition: mut_dyn_image_view.h:18
sophus::DynImageView
Definition: dyn_image_view.h:29
sophus::PixelFormat
Definition: pixel_format.h:15