supereight
image.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016-2019 Emanuele Vespa
3  * SPDX-FileCopyrightText: 2021 Smart Robotics Lab, Imperial College London, Technical University of Munich
4  * SPDX-FileCopyrightText: 2021 Nils Funk
5  * SPDX-FileCopyrightText: 2021 Sotiris Papatheodorou
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef SE_IMAGE_HPP
10 #define SE_IMAGE_HPP
11 
12 #include <Eigen/StdVector>
13 #include <cassert>
14 
16 
17 namespace se {
18 
19 template<typename T>
20 class Image {
21  public:
22  Image(const unsigned w, const unsigned h) : width_(w), height_(h), data_(width_ * height_)
23  {
24  assert(width_ > 0 && height_ > 0);
25  }
26 
27  Image(const unsigned w, const unsigned h, const T& val) : width_(w), height_(h)
28  {
29  assert(width_ > 0 && height_ > 0);
30  data_.resize(width_ * height_, val);
31  }
32 
33  T& operator[](std::size_t idx)
34  {
35  return data_[idx];
36  }
37  const T& operator[](std::size_t idx) const
38  {
39  return data_[idx];
40  }
41 
42  T& operator()(const int x, const int y)
43  {
44  return data_[x + y * width_];
45  }
46  const T& operator()(const int x, const int y) const
47  {
48  return data_[x + y * width_];
49  }
50 
51  std::size_t size() const
52  {
53  return width_ * height_;
54  };
55  int width() const
56  {
57  return width_;
58  };
59  int height() const
60  {
61  return height_;
62  };
63 
64  T* data()
65  {
66  return data_.data();
67  }
68  const T* data() const
69  {
70  return data_.data();
71  }
72 
73  private:
74  int width_;
75  int height_;
76  std::vector<T, Eigen::aligned_allocator<T>> data_;
77 
78  // std::vector<bool> is specialized for space efficiency which means that element access doesn't
79  // return references to the data as expected, causing compilation issues.
80  static_assert(!std::is_same<T, bool>::value,
81  "Use char/uint8_t instead of bool to avoid the std::vector<bool> specialization");
82 };
83 
84 
85 
86 static void convert_to_output_rgba_img(const se::Image<uint32_t>& input_rgba_img,
87  uint32_t* output_rgba_img_data)
88 {
89  memcpy(output_rgba_img_data,
90  input_rgba_img.data(),
91  input_rgba_img.width() * input_rgba_img.height() * sizeof(uint32_t));
92 }
93 
94 
95 
96 static void convert_to_output_depth_img(const se::Image<float>& input_depth_img,
97  uint32_t* output_depth_img_data)
98 {
99  depth_to_rgba(output_depth_img_data,
100  input_depth_img.data(),
101  Eigen::Vector2i(input_depth_img.width(), input_depth_img.height()),
102  0,
103  std::numeric_limits<float>::max());
104 }
105 
106 
107 
108 static void convert_to_output_depth_img(const se::Image<float>& input_depth_img,
109  const float min_depth,
110  const float max_depth,
111  uint32_t* output_depth_img_data)
112 {
113  depth_to_rgba(output_depth_img_data,
114  input_depth_img.data(),
115  Eigen::Vector2i(input_depth_img.width(), input_depth_img.height()),
116  min_depth,
117  max_depth);
118 }
119 
120 
121 
122 } // end namespace se
123 
124 #endif // SE_IMAGE_HPP
const T & operator()(const int x, const int y) const
Definition: image.hpp:46
int width() const
Definition: image.hpp:55
std::size_t size() const
Definition: image.hpp:51
static void convert_to_output_rgba_img(const se::Image< uint32_t > &input_rgba_img, uint32_t *output_rgba_img_data)
Definition: image.hpp:86
int height() const
Definition: image.hpp:59
Image(const unsigned w, const unsigned h, const T &val)
Definition: image.hpp:27
const T & operator[](std::size_t idx) const
Definition: image.hpp:37
T & operator[](std::size_t idx)
Definition: image.hpp:33
const T * data() const
Definition: image.hpp:68
static void convert_to_output_depth_img(const se::Image< float > &input_depth_img, uint32_t *output_depth_img_data)
Definition: image.hpp:96
void depth_to_rgba(uint32_t *depth_RGBA_image_data, const float *depth_image_data, const Eigen::Vector2i &depth_image_res, const float min_depth, const float max_depth)
Convert a depth image to an RGBA image to allow visualizing it.
Definition: image.hpp:20
Image(const unsigned w, const unsigned h)
Definition: image.hpp:22
T & operator()(const int x, const int y)
Definition: image.hpp:42
Helper wrapper to allocate and de-allocate octants in the octree.
Definition: colour_utils.hpp:17
T * data()
Definition: image.hpp:64