4 #include <bnb/types/config.hpp>
7 #include <bnb/types/base_types.hpp>
8 #include <bnb/types/transformation.hpp>
9 #include <bnb/utils/assert.hpp>
10 #include <bnb/utils/event.hpp>
11 #include <bnb/utils/defs.hpp>
13 #include <type_traits>
42 return static_cast<bool>(
static_cast<int32_t
>(format) & 0x0100);
47 return static_cast<bool>(
static_cast<int32_t
>(format) & 0x0200);
52 return static_cast<bool>(
static_cast<int32_t
>(format) & 0x0400);
57 return static_cast<bool>(
static_cast<int32_t
>(format) & 0x0600);
63 case pixel_buffer_format::bpc8_rgb:
65 case pixel_buffer_format::bpc8_bgr:
67 case pixel_buffer_format::bpc8_rgba:
69 case pixel_buffer_format::bpc8_bgra:
71 case pixel_buffer_format::bpc8_argb:
73 case pixel_buffer_format::nv12:
75 case pixel_buffer_format::i420:
78 BNB_THROW(std::invalid_argument,
"Unknown format.");
92 std::optional<float> fov = std::nullopt;
98 bool require_mirroring =
false;
100 int face_orientation = 0;
105 uint32_t
size() const noexcept;
169 using pixel_format_t = bnb::interfaces::pixel_format;
204 pixel_format_t get_pixel_format()
const noexcept;
209 uint8_t* get_data()
const noexcept;
214 static uint8_t bytes_per_pixel(pixel_format_t fmt);
218 static std::tuple<int, int, int> rgb_offsets(pixel_format_t fmt);
223 std::tuple<uint8_t, uint8_t, uint8_t> rgb_pixel_at(uint32_t x, uint32_t y)
const noexcept;
231 pixel_format_t m_pixel_format;
286 template<
size_t index>
297 switch (m_yuv_format.format) {
298 case yuv_format::yuv_nv12: {
299 BNB_THROW(std::invalid_argument,
"yuv nv12 format has only 2 planes");
301 case yuv_format::yuv_i420: {
305 BNB_THROW(std::invalid_argument,
"Incorrect yuv format");
310 BNB_THROW(std::invalid_argument,
"Incorrect index of plane");
327 m_planes[0] = std::move(y_plane);
328 m_planes[1] = std::move(uv_plane);
344 m_planes[0] = std::move(y_plane);
345 m_planes[1] = std::move(u_plane);
346 m_planes[2] = std::move(v_plane);
360 m_yuv_format.format = yuv_format::yuv_nv12;
361 m_yuv_format.standard = color_std::bt601;
363 m_planes[0] = std::move(y_plane);
364 m_planes[1] = std::move(uv_plane);
370 size_t y_size() const noexcept;
374 size_t uv_size() const noexcept;
376 using conversion_matrix = std::array<
float, 16>;
379 virtual conversion_matrix get_yuv_to_rgb_matrix() const;
380 virtual conversion_matrix get_rgb_to_yuv_matrix() const;
385 uint8_t y_pixel_at(uint32_t xi, uint32_t yi) const noexcept;
389 uint8_t uv_pixel_at(uint32_t xi, uint32_t yi, uint32_t offset) const noexcept;
394 uint8_t u_pixel_at(uint32_t xi, uint32_t yi) const noexcept;
398 uint8_t v_pixel_at(uint32_t xi, uint32_t yi) const noexcept;
442 color_plane m_planes[3];
458 static full_image_t load(
const std::string& path,
bool alpha =
false);
502 bool is_landscape()
const;
515 return bnb::transformation(full_roi, full_roi, rot_t::deg_0, get_format().require_mirroring) >> basis_transform;
522 const T& get_data() const noexcept;
528 bool has_data() const noexcept;
535 void set_fov(
float fov);
540 void normalize_orientation();
542 #if BNB_OS_ANDROID || BNB_OS_EMSCRIPTEN
543 unsigned ext_camera_tex_id = 0;
544 unsigned ext_camera_tex_width = 0;
545 unsigned ext_camera_tex_height = 0;
549 using plane_deleter = std::function<void(uint8_t*)>;
552 const uint8_t* rgb_plane,
559 plane_deleter deleter
563 const uint8_t* y_plane,
565 const uint8_t* uv_plane,
573 plane_deleter y_deleter,
574 plane_deleter uv_deleter
578 const uint8_t* y_plane,
580 const uint8_t* u_plane,
582 const uint8_t* v_plane,
590 plane_deleter y_deleter,
591 plane_deleter u_deleter,
592 plane_deleter v_deleter
605 bool get_mirroring()
const;
628 uint32_t get_number_of_planes()
const;
633 uint8_t* get_base_ptr()
const;
639 uint8_t* get_base_ptr_of_plane(uint32_t plane_num)
const;
644 int32_t get_bytes_per_pixel()
const;
650 int32_t get_bytes_per_pixel_of_plane(uint32_t plane_num)
const;
655 int32_t get_bytes_per_row()
const;
661 int32_t get_bytes_per_row_of_plane(uint32_t plane_num)
const;
666 int32_t get_width()
const;
672 int32_t get_width_of_plane(uint32_t plane_num)
const;
677 int32_t get_height()
const;
683 int32_t get_height_of_plane(uint32_t plane_num)
const;
686 void init_planes_data();
687 void init_plane(uint32_t plane_num, uint8_t* data, int32_t stride, int32_t width, int32_t height, int32_t pixel_size);
688 void validate_plane_number(uint32_t plane_num)
const;
694 int32_t bytes_per_row{0};
697 int32_t pixel_size{0};
700 using image_t = std::variant<yuv_image_t, bpc8_image_t>;
702 plane_data m_planes_info[3];
703 uint32_t m_plane_count{0};
707 const T& _get_data() const noexcept;
710 bool _has_data() const noexcept;
712 void update_basis_transform();
718 static_assert(std::is_base_of<base_image_t, T>::value,
"Type is not image_t");
719 BNB_ASSERT(std::holds_alternative<T>(m_image));
720 return *std::get_if<T>(&m_image);
726 static_assert(std::is_base_of<base_image_t, T>::value,
"Type is not image_t");
727 return std::holds_alternative<T>(m_image);