farm-ng-core
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 /// Image MutImage, owning images types.
10 ///
11 /// Note that it is a conscious API decision to follow "shallow-compare" type
12 /// semantic for ImageView, MutImageView, Image and MutImage. See image_view.h
13 /// for details.
14 #pragma once
15 
16 #include "sophus/common/enum.h"
17 #include "sophus/image/mut_image.h"
18 
19 #include <optional>
20 
21 namespace sophus {
22 
23 template <class TPredicate, class TAllocator>
24 class DynImage;
25 
26 /// Image read-only access to pixels and shared ownership, hence cheap to copy.
27 /// Type is nullable.
28 ///
29 /// Image has close interop with DynImage (see below).
30 template <class TPixel, class TAllocator = Eigen::aligned_allocator<uint8_t>>
31 class Image : public ImageView<TPixel> {
32  public:
33  /// Constructs empty image.
34  Image() = default;
35 
36  /// Moves MutImage into this.
37  /// By design not "explicit".
39  : ImageView<TPixel>(image.view()) {
40  if (!image.isEmpty()) {
41  this->shared_ = std::move(image.unique_);
42  this->ptr_ = reinterpret_cast<TPixel*>(this->shared_.get());
43  image.setViewToEmpty();
44  }
45  }
46 
47  /// Creates contiguous copy from view.
48  ///
49  /// If view is not empty, memory allocation will happen.
50  [[nodiscard]] static auto makeCopyFrom(ImageView<TPixel> const& view)
51  -> Image {
53  }
54 
55  /// Allocated and generates image from provided function taking u,v indices
56  ///
57  /// Memory allocation will happen.
58  template <class TUVOperation>
59  [[nodiscard]] static auto makeGenerative(
60  ImageSize size, TUVOperation const& uv_op) -> Image {
61  MutImage<TPixel> mut_image(size);
62  mut_image.generate(uv_op);
63  return mut_image;
64  }
65 
66  /// Creates new Image given view and unary transform function.
67  ///
68  /// image(u, v) = unary_op(view(u, v));
69  template <class TOtherPixel, class TUnaryOperation>
70  static auto makeFromTransform(
71  ImageView<TOtherPixel> view, TUnaryOperation const& unary_op) -> Image {
72  return MutImage<TPixel>::makeFromTransform(view, unary_op);
73  }
74 
75  /// Creates new Image given two views and binary transform function.
76  ///
77  /// image(u, v) = binary_op(lhs(u, v), rhs(u, v));
78  template <class TLhsPixel, class TRhsPixel, class TBinaryOperation>
79  static auto makeFromTransform(
82  TBinaryOperation const& binary_op) -> Image {
83  return MutImage<TPixel>::makeFromTransform(lhs, rhs, binary_op);
84  }
85 
86  [[nodiscard]] auto useCount() const -> size_t { return shared_.use_count(); }
87 
88  /// Sets Image instance to empty. Reduced use count by one.
89  ///
90  /// If use count goes to zero, deallocation happens.
91  ///
92  /// No-op if empty.
93  void reset() {
94  shared_.reset();
95  this->setViewToEmpty();
96  }
97 
98  private:
99  template <class TT, class TAllocator2T>
100  friend class MutImage;
101 
102  template <class TPredicate, class TAllocator2T>
103  friend class DynImage;
104 
105  explicit Image(ImageView<TPixel> view) : ImageView<TPixel>(view) {}
106 
107  Image(ImageView<TPixel> view, std::shared_ptr<uint8_t> const& shared)
108  : ImageView<TPixel>(view), shared_(shared) {}
109 
110  std::shared_ptr<uint8_t> shared_;
111 };
112 
113 } // namespace sophus
sophus::ImageView::setViewToEmpty
void setViewToEmpty()
Resets view such that it is empty.
Definition: image_view.h:239
sophus::Image
Image read-only access to pixels and shared ownership, hence cheap to copy. Type is nullable.
Definition: image.h:31
sophus
Image MutImage, owning images types.
Definition: num_diff.h:20
sophus::Image::makeFromTransform
static auto makeFromTransform(ImageView< TLhsPixel > lhs, ImageView< TRhsPixel > rhs, TBinaryOperation const &binary_op) -> Image
Creates new Image given two views and binary transform function.
Definition: image.h:79
sophus::ImageView
A view of an (immutable) image, which does not own the data.
Definition: image_view.h:55
sophus::Image::Image
Image()=default
Constructs empty image.
sophus::MutImage::makeFromTransform
static auto makeFromTransform(ImageView< TOtherPixel > view, TUnaryOperation const &unary_op) -> MutImage
Creates new MutImage given view and unary transform function.
Definition: mut_image.h:135
sophus::Image::makeFromTransform
static auto makeFromTransform(ImageView< TOtherPixel > view, TUnaryOperation const &unary_op) -> Image
Creates new Image given view and unary transform function.
Definition: image.h:70
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::Image::Image
Image(MutImage< TPixel, TAllocator > &&image) noexcept
Moves MutImage into this. By design not "explicit".
Definition: image.h:38
sophus::Image::useCount
auto useCount() const -> size_t
Definition: image.h:86
mut_image.h
sophus::Image::makeCopyFrom
static auto makeCopyFrom(ImageView< TPixel > const &view) -> Image
Creates contiguous copy from view.
Definition: image.h:50
sophus::DynImage
Type-erased image with shared ownership, and read-only access to pixels. Type is nullable.
Definition: dyn_image.h:24
sophus::Image::reset
void reset()
Sets Image instance to empty. Reduced use count by one.
Definition: image.h:93
enum.h
sophus::ImageView::ptr_
TPixel const * ptr_
Definition: image_view.h:242
sophus::ImageSize
Image size, hence its width and height.
Definition: image_size.h:21
sophus::Image::makeGenerative
static auto makeGenerative(ImageSize size, TUVOperation const &uv_op) -> Image
Allocated and generates image from provided function taking u,v indices.
Definition: image.h:59