ifm3d
buffer.h
1 // -*- c++ -*-
2 /*
3  * Copyright 2021-present ifm electronic, gmbh
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef IFM3D_FG_Buffer_H
8 #define IFM3D_FG_Buffer_H
9 
10 #include <cstdint>
11 #include <memory>
12 #include <vector>
13 #include <optional>
14 #include <ifm3d/common/json.hpp>
15 #include <limits.h>
16 #include <ifm3d/fg/module_frame_grabber.h>
17 #include <ifm3d/device/device.h>
18 #include <ifm3d/fg/buffer_id.h>
19 
20 namespace ifm3d
21 {
99  class IFM3D_EXPORT Buffer
100  {
101  private:
102  /* @ brief raw pointer to the data*/
103  uint8_t* data_{};
104  /*@brief number of columns in Buffer (width)*/
105  std::uint32_t cols_{};
106  /*@brief number of rows in Buffer (height)*/
107  std::uint32_t rows_{};
108  /*@brief number of channel in Buffer*/
109  std::uint32_t nchannel_{};
110  /* @brief data format or type*/
111  ifm3d::pixel_format data_format_{};
112  /* @brief number of pixel to store one value of data*/
113  std::size_t data_size_in_bytes_{};
114  /* @brief size of the memory allocated*/
115  size_t size_{};
116  /* @brief bytes to store pixel */
117  size_t bytes_per_pixel{};
118  /* @brief bytes per row */
119  size_t bytes_per_row{};
120  /* @brief json formatted metadata of the chunk obtain from device*/
121  json metadata_;
122  /* @brief buffer id of Buffer */
123  ifm3d::buffer_id bufferId_{};
124 
125  class BufferAllocator;
126  std::shared_ptr<BufferAllocator> buffer_allocator_;
127 
128  public:
135  Buffer();
136  /*@overload
137  @param cols Number of columns in a Buffer.
138  @param rows Number of rows in a Buffer.
139  @param nchannel Number of channels in Buffer
140  @param format value from ifm3d::pixel_format releates to data type
141  need to store one value.
142  @param metadata json formatted metadata of the chunk obtain from device
143 
144  @note This internally calls Create Method to allocates Memory
145  */
146  Buffer(const std::uint32_t cols,
147  const std::uint32_t rows,
148  const std::uint32_t nchannel,
149  ifm3d::pixel_format format,
150  const std::optional<ifm3d::json>& metadata = std::nullopt,
151  ifm3d::buffer_id buffer_id = static_cast<ifm3d::buffer_id>(0));
152 
153  virtual ~Buffer() = default;
154 
155  // move semantics
156  Buffer(Buffer&&) = default;
157  Buffer& operator=(Buffer&&) = default;
158 
159  // copy ctor/assignment operator
160  Buffer(const Buffer&) = default;
161  Buffer& operator=(const Buffer&) = default;
162 
163  /*@brief allocates the memory required for storing the image data
164  @param cols Number of columns in a Buffer.
165  @param rows Number of rows in a Buffer.
166  @param nchannel Number of channels in Buffer
167  @param format value from ifm3d::pixel_format releates to data type
168 
169  @Note On repeated calling it will deference the old Memory
170  */
171  void create(const std::uint32_t cols,
172  const std::uint32_t rows,
173  const std::uint32_t nchannel,
174  ifm3d::pixel_format format,
176 
179  Buffer clone() const;
180 
181  /* getters*/
182  std::uint32_t height() const;
183  std::uint32_t width() const;
184  std::uint32_t nchannels() const;
185  ifm3d::pixel_format dataFormat() const;
186  ifm3d::json metadata() const;
187  ifm3d::buffer_id bufferId() const;
188 
192  size_t size() const;
196  template <typename T = std::uint8_t>
197  T* ptr(const std::uint32_t row);
198 
202  template <typename T = std::uint8_t>
203  T const* ptr(const std::uint32_t row) const;
204 
210  template <typename T = std::uint8_t>
211  T* ptr(const std::uint32_t row, const std::uint32_t col);
212 
218  template <typename T = std::uint8_t>
219  T const* ptr(const std::uint32_t row, const std::uint32_t col) const;
220 
221  /*@brief access to the pixel for read and write
222  @param Index of the pixel considering image as 1D array
223  @return refernce of the value at the index
224  */
225  template <typename T>
226  T& at(const std::size_t index);
227  /*@overload considering image as 2D
228  @param row 1st dimension index
229  @param col 2nd dimension index
230  */
231  template <typename T>
232  T& at(const std::uint32_t row, const std::uint32_t col);
233 
234  /*@brief access to the pixel for read and write
235  @param Index of the pixel considering image as 1D array
236  @return refernce of the value at the index
237  */
238  template <typename T>
239  T const& at(const std::size_t index) const;
240  /*@overload considering image as 2D
241  @param row 1st dimension index
242  @param col 2nd dimension index
243  */
244  template <typename T>
245  T const& at(const std::uint32_t row, const std::uint32_t col) const;
246 
247  /*@brief set the value where mask value is 1
248  @param val value to be set
249  @param mask Binary mask
250 
251  @Note mask size must be same as this
252  */
253  template <typename T>
254  void setTo(const T val, const ifm3d::Buffer& mask);
255 
256  /*===========================*/
257  /* Iterators */
258  /*===========================*/
259  template <typename T>
260  struct Iterator
261  {
262  using iterator_category = std::random_access_iterator_tag;
263  using difference_type = std::ptrdiff_t;
264  using value_type = T;
265  using pointer = T*;
266  using reference = T&;
267 
268  Iterator(uint8_t* ptr);
269  reference operator*() const;
270  pointer operator->();
271  Iterator& operator++();
272  Iterator operator++(std::int32_t);
273 
274  friend bool
275  operator==(const Iterator& a, const Iterator& b)
276  {
277  return a.m_ptr == b.m_ptr;
278  }
279 
280  friend bool
281  operator!=(const Iterator& a, const Iterator& b)
282  {
283  return a.m_ptr != b.m_ptr;
284  }
285 
286  bool
287  operator-(const Iterator& rhs) const noexcept
288  {
289  // logic here
290  return this->m_ptr - rhs.m_ptr; // for example
291  }
292 
293  private:
294  pointer m_ptr;
295  };
296 
297  /*@brief Return the Iterator pointing to start of data*/
298  template <typename T>
299  Iterator<T> begin();
300  /*@brief Return the Iterator pointing to end of data*/
301  template <typename T>
302  Iterator<T> end();
303 
304  }; // end Buffer
305 
306  /*@brief IteratorAdapter is adapter and can be used in range based loops
307 
308  @code
309  for (auto value : ifm3d::IteratorAdapter<unsigned short>(image))
310  {
311  // operation on value
312  @endcode
313  */
314  template <typename T>
316  {
317  private:
318  Buffer& it;
319 
320  public:
321  IteratorAdapter(Buffer& it);
322  auto begin();
323  auto end();
324  };
325 
327 
328  template <typename Tp>
329  class Buffer_ : public Buffer
330  {
331  public:
332  /*@brief fefault constructor*/
333  Buffer_();
334 
335  /* Similar to Buffer(cols,rows,ifm3d::formatType<Tp>::nchannel,
336  * ifm3d::formatType<Tp>::format ) */
337  Buffer_(const std::uint32_t cols,
338  const std::uint32_t rows,
339  std::optional<ifm3d::json> metadata = std::nullopt);
340 
341  ~Buffer_() = default;
342 
343  // move semantics
344  Buffer_(Buffer_<Tp>&&) = default;
345  Buffer_& operator=(Buffer_<Tp>&&) = default;
346 
347  // copy ctor/assignment operator
348  Buffer_(const Buffer_<Tp>&) = default;
349  Buffer_& operator=(const Buffer_<Tp>&) = default;
350 
351  Buffer_(const Buffer&);
352  Buffer_& operator=(const Buffer&);
353 
354  /* Similar to Buffer::create(cols,rows,ifm3d::formatType<Tp>::nchannel,
355  * ifm3d::formatType<Tp>::format, ifm3d::buffer_id buffer_id ) */
356  void create(const std::uint32_t cols,
357  const std::uint32_t rows,
359 
362  Buffer_ clone() const;
363 
364  /* getters*/
365  std::uint32_t height() const;
366  std::uint32_t width() const;
367  std::uint32_t nchannels() const;
368  ifm3d::pixel_format dataFormat() const;
369  ifm3d::json metadata() const;
370 
374  Tp* ptr(const std::uint32_t row);
375 
381  Tp* ptr(const std::uint32_t row, const std::uint32_t col);
382 
383  /*@brief access to the pixel for read and write
384  @param Index of the pixel considering image as 1D array
385  @return refernce of the value at the index
386  */
387  Tp& at(const std::size_t index);
388  /*@overload considering image as 2D
389  @param row 1st dimension index
390  @param col 2nd dimension index
391  */
392  Tp& at(const std::uint32_t row, const std::uint32_t col);
393  /*@brief set the value where mask value is 1
394  @param val value to be set
395  @param mask Binary mask
396 
397  @Note mask size must be same as this
398  */
399  void setTo(const Tp val, ifm3d::Buffer& mask);
400 
401  /*@brief Return the Iterator pointing to start of data*/
402  Iterator<Tp> begin();
403  /*@brief Return the Iterator pointing to end of data*/
404  Iterator<Tp> end();
405 
406  }; // end Buffer_<Tp>
407 
411  template <typename T, int n>
412  struct Point
413  {
414  T val[n];
415  using value_type = T;
416  };
417 
418  template <typename T>
419  using Point3D = struct Point<T, 3>;
420 
421  template <typename T>
422  using Point4D = struct Point<T, 4>;
423 
424  // user helper types
427  // checking for 32 bit float support
428  static_assert(CHAR_BIT * sizeof(float) == 32, "32 bit float required");
429  using Point3D_32F = Point3D<float>;
430 
433  using Point4D_32F = Point4D<float>;
434 
435 } // end: namespace ifm3d
436 
437 #include <ifm3d/fg/detail/buffer.hpp>
438 #endif // IFM3D_FG_Buffer_H
ifm3d::json
Definition: json.hpp:128
ifm3d::Buffer_::clone
Buffer_ clone() const
Creates a full copy of the array and the underlying data.
ifm3d::Buffer
The class Buffer represent a STL container to store data from the ifm devices in 2 dimension and supp...
Definition: buffer.h:99
ifm3d::buffer_id
buffer_id
Definition: buffer_id.h:17
ifm3d::Buffer::Iterator
Definition: buffer.h:260
ifm3d::IteratorAdapter
Definition: buffer.h:315
ifm3d::Point
Struct for 3D space point.
Definition: buffer.h:412
ifm3d::Buffer_::ptr
Tp * ptr(const std::uint32_t row)
returns a pointer to the specified Buffer row.
ifm3d::Buffer_
Definition: buffer.h:329