farm-ng-core
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 
13 
14 #include <variant>
15 
16 namespace sophus {
17 
18 /// Type-erased image with shared ownership, and read-only access to pixels.
19 /// Type is nullable.
20 ///
21 template <
22  class TPredicate = AnyImagePredicate,
23  class TAllocator = Eigen::aligned_allocator<uint8_t>>
24 class DynImage : public DynImageView<TPredicate> {
25  public:
26  /// Empty image.
27  DynImage() = default;
28 
29  /// Create type-erased image from Image.
30  ///
31  /// Ownership is shared between DynImage and Image, and hence the
32  /// reference count will be increased by one (unless input is empty).
33  /// By design not "explicit".
34  template <class TPixel>
36  : DynImage(
37  image.layout(),
38  PixelFormat::fromTemplate<TPixel>(),
39  image.shared_) {
40  static_assert(TPredicate::template isTypeValid<TPixel>());
41  }
42 
43  /// Create type-erased image from MutImage.
44  /// By design not "explicit".
45  template <class TPixel>
47  : DynImage(Image<TPixel>(std::move(image))) {
48  static_assert(TPredicate::template isTypeValid<TPixel>());
49  }
50 
51  /// Create type-erased image from MutImage.
52  /// By design not "explicit".
54  : DynImage(
55  image.layout(), image.pixel_format_, std::move(image.unique_)) {
56  image.unique_.reset();
57  image.setViewToEmpty();
58  }
59 
60  /// Tries to create image from provided size and format.
61  /// Returns error if format does not satisfy TPredicate.
62  static Expected<DynImage<TPredicate, TAllocator>> tryFromFormat(
63  ImageSize const& size, PixelFormat const& pixel_format) {
65  FARM_TRY(auto, mut, Mut::tryFromFormat(size, pixel_format));
66 
67  return DynImage(std::move(mut));
68  }
69 
70  // Creates image from provided size and format.
71  //
72  // Panics if format does not satisfy TPredicate.
74  ImageSize const& size, PixelFormat const& pixel_format) {
75  auto maybe = tryFromFormat(size, pixel_format);
76  return SOPHUS_UNWRAP(maybe);
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  auto maybe = tryFromFormat(layout, pixel_format);
85  return SOPHUS_UNWRAP(maybe);
86  }
87 
88  /// Tries to create image from provided size and format.
89  /// Returns error if format does not satisfy TPredicate.
90  static Expected<DynImage<TPredicate, TAllocator>> tryFromFormat(
91  ImageLayout const& layout, PixelFormat const& pixel_format) {
92  if (!TPredicate::isFormatValid(pixel_format)) {
93  return SOPHUS_UNEXPECTED("pixel format does not satisfy predicate");
94  }
96  }
97 
98  template <class TOtherPredicate>
100  -> Expected<DynImage<TPredicate, TAllocator>> {
101  if (!TPredicate::isFormatValid(other_image.pixelFormat())) {
102  return SOPHUS_UNEXPECTED("pixel format does not satisfy predicate");
103  }
105  other_image.layout(), other_image.pixelFormat(), other_image.shared_);
106  }
107 
108  /// Return true is this contains data of type TPixel.
109  template <class TPixel>
110  [[nodiscard]] auto has() const noexcept -> bool {
111  PixelFormat expected_type = PixelFormat::fromTemplate<TPixel>();
112  return expected_type == this->pixel_format_;
113  }
114 
115  /// Returns typed image.
116  ///
117  /// Precondition: this->has<TPixel>()
118  template <class TPixel>
119  [[nodiscard]] auto image() const noexcept -> Image<TPixel, TAllocator> {
120  if (!this->has<TPixel>()) {
121  PixelFormat expected_type = PixelFormat::fromTemplate<TPixel>();
122 
123  SOPHUS_PANIC(
124  "expected type: {}\n"
125  "actual type: {}",
126  expected_type,
127  this->pixel_format_);
128  }
129 
132  this->layout_, reinterpret_cast<TPixel*>(shared_.get())),
133  shared_);
134  }
135 
136  template <class TPixel>
137  auto reinterpretAs(ImageSize reinterpreted_size) const noexcept
140  reinterpreted_size.width * sizeof(TPixel), this->layout().pitch_bytes_);
141  SOPHUS_ASSERT_LE(reinterpreted_size.height, this->height());
142 
144  }
145 
146  [[nodiscard]] auto useCount() const -> size_t { return shared_.use_count(); }
147 
148  protected:
149  template <class TPredicate2, class TAllocator2T>
150  friend class DynImage;
151 
152  // Private constructor mainly available for constructing sub-views
155  PixelFormat pixel_format,
156  std::shared_ptr<uint8_t> shared)
157  : DynImageView<TPredicate>(layout, pixel_format, shared.get()),
158  shared_(shared) {}
159 
160  std::shared_ptr<uint8_t> shared_;
161 };
162 
163 } // namespace sophus
mut_dyn_image.h
sophus::DynImage::reinterpretAs
auto reinterpretAs(ImageSize reinterpreted_size) const noexcept -> Image< TPixel, TAllocator >
Definition: dyn_image.h:137
sophus::DynImage::DynImage
DynImage(MutDynImage< TPredicate, TAllocator > &&image)
Create type-erased image from MutImage. By design not "explicit".
Definition: dyn_image.h:53
SOPHUS_PANIC
#define SOPHUS_PANIC(...)
Definition: common.h:50
sophus::MutDynImage
Definition: mut_dyn_image.h:23
sophus::DynImage::shared_
std::shared_ptr< uint8_t > shared_
Definition: dyn_image.h:160
FARM_TRY
#define FARM_TRY(Type, var, expression)
Assigns *expression to var of Type, but returns error if there is one.
Definition: expected.h:91
sophus::DynImage::has
auto has() const noexcept -> bool
Return true is this contains data of type TPixel.
Definition: dyn_image.h:110
sophus::DynImageView< AnyImagePredicate >::layout
auto layout() const -> ImageLayout const &
Definition: dyn_image_view.h:56
sophus::Image
Image read-only access to pixels and shared ownership, hence cheap to copy. Type is nullable.
Definition: image.h:31
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::DynImage::useCount
auto useCount() const -> size_t
Definition: dyn_image.h:146
sophus::ImageView
A view of an (immutable) image, which does not own the data.
Definition: image_view.h:55
sophus::DynImage::DynImage
DynImage(ImageLayout layout, PixelFormat pixel_format, std::shared_ptr< uint8_t > shared)
Definition: dyn_image.h:153
SOPHUS_UNIMPLEMENTED
#define SOPHUS_UNIMPLEMENTED(...)
Definition: common.h:51
dyn_image_view.h
sophus::DynImage::DynImage
friend class DynImage
Definition: dyn_image.h:150
sophus::DynImage::fromFormat
static DynImage< TPredicate, TAllocator > fromFormat(ImageSize const &size, PixelFormat const &pixel_format)
Definition: dyn_image.h:73
sophus::DynImageView< AnyImagePredicate >::layout_
ImageLayout layout_
Definition: dyn_image_view.h:146
sophus::DynImage::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: dyn_image.h:62
sophus::DynImage::tryFromFormat
static Expected< DynImage< 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: dyn_image.h:90
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 >::pixel_format_
PixelFormat pixel_format_
Definition: dyn_image_view.h:147
SOPHUS_UNEXPECTED
#define SOPHUS_UNEXPECTED(...)
Definition: common.h:57
SOPHUS_ASSERT_LE
#define SOPHUS_ASSERT_LE(...)
Definition: common.h:44
sophus::DynImage::tryFrom
static auto tryFrom(DynImage< TOtherPredicate, TAllocator > other_image) -> Expected< DynImage< TPredicate, TAllocator >>
Definition: dyn_image.h:99
sophus::DynImage::fromFormat
static DynImage< TPredicate, TAllocator > fromFormat(ImageLayout const &layout, PixelFormat const &pixel_format)
Definition: dyn_image.h:82
sophus::DynImage::DynImage
DynImage(MutImage< TPixel > &&image)
Create type-erased image from MutImage. By design not "explicit".
Definition: dyn_image.h:46
sophus::DynImage::DynImage
DynImage(Image< TPixel, TAllocator > const &image)
Create type-erased image from Image.
Definition: dyn_image.h:35
sophus::DynImage
Type-erased image with shared ownership, and read-only access to pixels. Type is nullable.
Definition: dyn_image.h:24
sophus::DynImage::image
auto image() const noexcept -> Image< TPixel, TAllocator >
Returns typed image.
Definition: dyn_image.h:119
sophus::ImageSize
Image size, hence its width and height.
Definition: image_size.h:21
sophus::DynImageView
Definition: dyn_image_view.h:29
sophus::PixelFormat
Definition: pixel_format.h:15