Astra SDK  v2.1.3
Image.hpp
1 // This file is part of the Orbbec Astra SDK [https://orbbec3d.com]
2 // Copyright (c) 2015-2017 Orbbec 3D
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // Be excellent to each other.
17 #ifndef ASTRA_IMAGE_HPP
18 #define ASTRA_IMAGE_HPP
19 
20 #include <stdexcept>
21 #include <iostream>
22 #include <cstdint>
23 #include <astra_core/astra_core.hpp>
24 #include <astra/capi/astra_ctypes.h>
25 #include <astra/capi/streams/image_capi.h>
26 #include <astra/capi/streams/stream_types.h>
27 
28 namespace astra {
29 
39  struct RgbPixel : public astra_rgb_pixel_t
40  {
45  : RgbPixel(0, 0, 0)
46  {}
47 
55  RgbPixel(std::uint8_t r, std::uint8_t g, std::uint8_t b)
56  {
57  ::astra_rgb_pixel_t::r = r;
58  ::astra_rgb_pixel_t::g = g;
59  ::astra_rgb_pixel_t::b = b;
60  }
61  };
62 
66  struct RgbaPixel : public astra_rgba_pixel_t
67  {
72  : RgbaPixel(0, 0,0,0)
73  {}
74 
83  RgbaPixel(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t alpha)
84  {
85  ::astra_rgba_pixel_t::r = r;
86  ::astra_rgba_pixel_t::g = g;
87  ::astra_rgba_pixel_t::b = b;
88  ::astra_rgba_pixel_t::alpha = alpha;
89  }
90  };
91 
92  // DEPRECATED
93  typedef RgbaPixel RGBAPixel;
94 
99  {
100  public:
105  : ImageStreamMode(0, 0, 0, ASTRA_PIXEL_FORMAT_UNKNOWN)
106  {}
107 
116  ImageStreamMode(std::uint32_t width, std::uint32_t height, std::uint8_t fps, astra_pixel_format_t format)
117  {
118  set_width(width);
120  set_fps(fps);
121  set_pixel_format(format);
122  }
123 
129  ImageStreamMode(const ::astra_imagestream_mode_t& mode)
130  {
131  *this = mode;
132  }
133 
139  ImageStreamMode& operator=(const ::astra_imagestream_mode_t& mode)
140  {
141  set_width(mode.width);
142  set_height(mode.height);
143  set_fps(mode.fps);
144  set_pixel_format(mode.pixelFormat);
145 
146  return *this;
147  }
148 
149  operator ::astra_imagestream_mode_t*() { return this; }
150  operator const ::astra_imagestream_mode_t*() const { return this; }
151 
157  std::uint8_t fps() const { return astra_imagestream_mode_t::fps; }
158 
164  void set_fps(std::uint8_t fps) { astra_imagestream_mode_t::fps = fps; }
165 
171  const std::uint8_t bytes_per_pixel() const
172  {
173  std::uint8_t bpp;
174  astra_pixelformat_get_bytes_per_pixel(pixel_format(), &bpp);
175  return bpp;
176  }
177 
183  std::uint32_t width() const { return astra_imagestream_mode_t::width; }
184 
190  void set_width(std::uint32_t width) { astra_imagestream_mode_t::width = width; }
191 
197  std::uint32_t height() const { return astra_imagestream_mode_t::height; }
198 
204  void set_height(std::uint32_t height) { astra_imagestream_mode_t::height = height; }
205 
211  astra_pixel_format_t pixel_format() const { return astra_imagestream_mode_t::pixelFormat; }
212 
218  void set_pixel_format(astra_pixel_format_t format) { astra_imagestream_mode_t::pixelFormat = format; }
219 
220  friend std::ostream& operator<<(std::ostream& os, const ImageStreamMode& ism);
221  };
222 
230  inline bool operator==(const ImageStreamMode& lhs, const ImageStreamMode& rhs)
231  {
232  return
233  lhs.width() == rhs.width() &&
234  lhs.height() == rhs.height() &&
235  lhs.pixel_format() == rhs.pixel_format() &&
236  lhs.fps() == rhs.fps() &&
237  lhs.bytes_per_pixel() && rhs.bytes_per_pixel();
238  }
239 
247  inline bool operator!=(const ImageStreamMode& lhs, const ImageStreamMode& rhs)
248  {
249  return !(lhs == rhs);
250  }
251 
252  inline std::ostream& operator<<(std::ostream& os, const ImageStreamMode& ism)
253  {
254  os << ism.width()
255  << "x"
256  << ism.height()
257  << "x"
258  << static_cast<int>(ism.bytes_per_pixel())
259  << "@"
260  << static_cast<int>(ism.fps())
261  << " pf:"
262  << ism.pixel_format();
263 
264  return os;
265  }
266 
270  class ImageStream : public DataStream
271  {
272  public:
273  explicit ImageStream(astra_streamconnection_t connection)
274  : DataStream(connection),
275  imageStream_(reinterpret_cast<astra_imagestream_t>(connection))
276  {}
277 
283  float hFov() const
284  {
285  float hFov = 0.0f;
286  astra_imagestream_get_hfov(imageStream_, &hFov);
287 
288  return hFov;
289  }
290 
296  float vFov() const
297  {
298  float vFov = 0.0f;
299  astra_imagestream_get_vfov(imageStream_, &vFov);
300 
301  return vFov;
302  }
303 
310  {
311  astra_usb_info_t usbInfo;
312  astra_imagestream_get_usb_info(imageStream_, &usbInfo);
313  return usbInfo;
314  }
315 
321  bool mirroring_enabled() const
322  {
323  bool mirroring = false;
324  astra_imagestream_get_mirroring(imageStream_, &mirroring);
325 
326  return mirroring;
327  }
328 
334  void enable_mirroring(bool mirroring)
335  {
336  astra_imagestream_set_mirroring(imageStream_, mirroring);
337  }
338 
344  std::vector<ImageStreamMode> available_modes() const
345  {
346  astra_result_token_t token;
347  std::uint32_t count = 0;
348  astra_status_t ret = astra_imagestream_request_modes(imageStream_, &token, &count);
349  if (ret != ASTRA_STATUS_SUCCESS)
350  return std::vector<ImageStreamMode>();
351 
352  std::vector<ImageStreamMode> result;
353  result.resize(count);
354 
355  astra_imagestream_get_modes_result(imageStream_,
356  token,
357  reinterpret_cast<astra_imagestream_mode_t*>(&result[0]),
358  count);
359 
360  return result;
361  }
362 
369  {
372  astra_imagestream_get_mode(imageStream_, cMode);
373  return mode;
374  }
375 
382  {
383  const astra_imagestream_mode_t* cMode = mode;
384  astra_imagestream_set_mode(imageStream_, cMode);
385  }
386 
387  private:
388  astra_imagestream_t imageStream_;
389  };
390 
391 
395  template<typename TDataType, astra_stream_type_t TStreamType>
397  {
398  public:
405  ImageFrame(astra_imageframe_t frame, astra_pixel_format_t expected_format)
406  {
407  imageFrame_ = frame;
408  if (imageFrame_)
409  {
410  if (imageFrame_->metadata.pixelFormat != expected_format)
411  {
412  //wrong format -- this must be an invalid frame
413  imageFrame_ = nullptr;
414  return;
415  }
416 
417  astra_imageframe_get_frameindex(imageFrame_, &frameIndex_);
418 
419  void* voidData = nullptr;
420  astra_imageframe_get_data_ptr(imageFrame_, &voidData, &byteLength_);
421  dataPtr_ = static_cast<TDataType*>(voidData);
422  }
423  }
424 
430  const bool is_valid() const { return imageFrame_ != nullptr; }
431 
437  const int width() const { throw_if_invalid_frame(); return imageFrame_->metadata.width; }
438 
444  const int height() const { throw_if_invalid_frame(); return imageFrame_->metadata.height; }
445 
451  const std::uint8_t bytes_per_pixel() const
452  {
453  throw_if_invalid_frame();
454 
455  std::uint8_t bpp;
456  astra_pixelformat_get_bytes_per_pixel(imageFrame_->metadata.pixelFormat, &bpp);
457  return bpp;
458  }
459 
465  const astra_frame_index_t frame_index() const { throw_if_invalid_frame(); return frameIndex_; }
466 
472  const astra_imageframe_t handle() const { return imageFrame_; }
473 
479  static astra_stream_type_t stream_type() { return TStreamType; }
480 
486  const TDataType* data() const { throw_if_invalid_frame(); return dataPtr_; }
487 
493  const size_t byte_length() const { throw_if_invalid_frame(); return byteLength_; }
494 
500  const size_t length() const { throw_if_invalid_frame(); return imageFrame_->metadata.width * imageFrame_->metadata.height; }
501 
507  void copy_to(TDataType* buffer) const
508  {
509  throw_if_invalid_frame();
510  astra_imageframe_copy_data(imageFrame_, buffer);
511  }
512 
518  template<typename TFrameType>
519  static TFrameType acquire(astra_reader_frame_t readerFrame,
520  astra_stream_subtype_t subtype)
521  {
522  if (readerFrame != nullptr)
523  {
524  astra_imageframe_t imageFrame;
525  astra_reader_get_imageframe(readerFrame, TStreamType, subtype, &imageFrame);
526 
527  return TFrameType(imageFrame);
528  }
529 
530  return TFrameType(nullptr);
531  }
532 
533 
538  void rotate(int angle)
539  {
540  astra_imageframe_rotate(imageFrame_, angle);
541  }
542 
543  private:
544  void throw_if_invalid_frame() const
545  {
546  if (!imageFrame_) { throw std::logic_error("Cannot operate on an invalid frame"); }
547  }
548 
549  astra_imageframe_t imageFrame_{nullptr};
550  astra_frame_index_t frameIndex_;
551 
552  TDataType* dataPtr_;
553  std::uint32_t byteLength_;
554  };
556 }
557 
558 #endif // ASTRA_IMAGE_HPP
Data Stream
Definition: DataStream.hpp:33
Basic Image Frame
Definition: Image.hpp:397
static TFrameType acquire(astra_reader_frame_t readerFrame, astra_stream_subtype_t subtype)
acquire frame
Definition: Image.hpp:519
void rotate(int angle)
Definition: Image.hpp:538
const int height() const
get height
Definition: Image.hpp:444
const std::uint8_t bytes_per_pixel() const
get bytes per pixel
Definition: Image.hpp:451
void copy_to(TDataType *buffer) const
copy to buffer
Definition: Image.hpp:507
const size_t byte_length() const
get byte length
Definition: Image.hpp:493
const astra_frame_index_t frame_index() const
get frame index
Definition: Image.hpp:465
const size_t length() const
get length
Definition: Image.hpp:500
const int width() const
get width
Definition: Image.hpp:437
const astra_imageframe_t handle() const
get handle
Definition: Image.hpp:472
const bool is_valid() const
get is vaild
Definition: Image.hpp:430
const TDataType * data() const
get data pointer
Definition: Image.hpp:486
ImageFrame(astra_imageframe_t frame, astra_pixel_format_t expected_format)
Default constructs
Definition: Image.hpp:405
static astra_stream_type_t stream_type()
get stream type
Definition: Image.hpp:479
basic Image Stream
Definition: Image.hpp:271
ImageStreamMode mode() const
get current image mode
Definition: Image.hpp:368
void enable_mirroring(bool mirroring)
enable mirroring
Definition: Image.hpp:334
bool mirroring_enabled() const
get is mirroring
Definition: Image.hpp:321
float vFov() const
get vertical fov
Definition: Image.hpp:296
astra_usb_info_t usb_info() const
get usb_info
Definition: Image.hpp:309
float hFov() const
get horizontal fov
Definition: Image.hpp:283
void set_mode(const ImageStreamMode &mode)
set current image mode
Definition: Image.hpp:381
std::vector< ImageStreamMode > available_modes() const
get available_modes
Definition: Image.hpp:344
Image Stream Mode
Definition: Image.hpp:99
void set_width(std::uint32_t width)
set width
Definition: Image.hpp:190
std::uint8_t fps() const
get fps
Definition: Image.hpp:157
std::uint32_t width() const
get width
Definition: Image.hpp:183
ImageStreamMode(std::uint32_t width, std::uint32_t height, std::uint8_t fps, astra_pixel_format_t format)
constructs
Definition: Image.hpp:116
ImageStreamMode(const ::astra_imagestream_mode_t &mode)
copy constructs
Definition: Image.hpp:129
void set_height(std::uint32_t height)
set height
Definition: Image.hpp:204
const std::uint8_t bytes_per_pixel() const
get bytes per pixel
Definition: Image.hpp:171
astra_pixel_format_t pixel_format() const
get height
Definition: Image.hpp:211
ImageStreamMode()
Default constructs
Definition: Image.hpp:104
std::uint32_t height() const
get height
Definition: Image.hpp:197
void set_pixel_format(astra_pixel_format_t format)
set fps
Definition: Image.hpp:218
void set_fps(std::uint8_t fps)
set fps
Definition: Image.hpp:164
ImageStreamMode & operator=(const ::astra_imagestream_mode_t &mode)
copy constructs
Definition: Image.hpp:139
bool operator!=(const ImageStreamMode &lhs, const ImageStreamMode &rhs)
compare is ImageStreamMode not equal
Definition: Image.hpp:247
bool operator==(const ImageStreamMode &lhs, const ImageStreamMode &rhs)
compare is ImageStreamMode equal
Definition: Image.hpp:230
ASTRA_API_EX astra_status_t astra_imageframe_rotate(astra_imageframe_t imageFrame, int angle)
Definition: stream_types.h:37
Definition: astra_plugin.h:24
rgb pixel struct
Definition: Image.hpp:40
RgbPixel(std::uint8_t r, std::uint8_t g, std::uint8_t b)
constructs
Definition: Image.hpp:55
RgbPixel()
Default constructs all zero
Definition: Image.hpp:44
rgba pixel struct
Definition: Image.hpp:67
RgbaPixel()
Default constructs all zero
Definition: Image.hpp:71
RgbaPixel(std::uint8_t r, std::uint8_t g, std::uint8_t b, std::uint8_t alpha)
constructs
Definition: Image.hpp:83
Definition: image_types.h:62
Definition: image_types.h:42
Definition: image_types.h:48
Definition: astra_ctypes.h:44