Rerun C++ SDK
Loading...
Searching...
No Matches
image_utils.hpp
1#pragma once
2
3#include "datatypes/channel_datatype.hpp"
4#include "datatypes/color_model.hpp"
5#include "datatypes/pixel_format.hpp"
6#include "half.hpp"
7
8#include <cassert>
9#include <cstdint>
10
11namespace rerun {
12 /// The width and height of an image.
13 struct WidthHeight {
14 uint32_t width;
15 uint32_t height;
16
17 WidthHeight(uint32_t width_, uint32_t height_) : width{width_}, height{height_} {}
18 };
19
20 /// Number of bits used by this element type
22 switch (value) {
24 return 8;
25 }
27 return 16;
28 }
30 return 32;
31 }
33 return 64;
34 }
36 return 8;
37 }
39 return 16;
40 }
42 return 32;
43 }
45 return 64;
46 }
48 return 16;
49 }
51 return 32;
52 }
54 return 64;
55 }
56 default:
57 assert(false && "unreachable");
58 }
59 return 0;
60 }
61
62 inline size_t num_bytes(WidthHeight resolution, datatypes::ChannelDatatype datatype) {
63 // rounding upwards:
64 return (resolution.width * resolution.height * datatype_bits(datatype) + 7) / 8;
65 }
66
67 template <typename TElement>
68 inline datatypes::ChannelDatatype get_datatype(const TElement* _unused);
69
70 template <>
71 inline datatypes::ChannelDatatype get_datatype(const uint8_t* _unused) {
72 (void)(_unused); // Suppress unused warning.
74 }
75
76 template <>
77 inline datatypes::ChannelDatatype get_datatype(const uint16_t* _unused) {
78 (void)(_unused); // Suppress unused warning.
80 }
81
82 template <>
83 inline datatypes::ChannelDatatype get_datatype(const uint32_t* _unused) {
84 (void)(_unused); // Suppress unused warning.
86 }
87
88 template <>
89 inline datatypes::ChannelDatatype get_datatype(const uint64_t* _unused) {
90 (void)(_unused); // Suppress unused warning.
92 }
93
94 template <>
95 inline datatypes::ChannelDatatype get_datatype(const int8_t* _unused) {
96 (void)(_unused); // Suppress unused warning.
98 }
99
100 template <>
101 inline datatypes::ChannelDatatype get_datatype(const int16_t* _unused) {
102 (void)(_unused); // Suppress unused warning.
104 }
105
106 template <>
107 inline datatypes::ChannelDatatype get_datatype(const int32_t* _unused) {
108 (void)(_unused); // Suppress unused warning.
110 }
111
112 template <>
113 inline datatypes::ChannelDatatype get_datatype(const int64_t* _unused) {
114 (void)(_unused); // Suppress unused warning.
116 }
117
118 template <>
119 inline datatypes::ChannelDatatype get_datatype(const rerun::half* _unused) {
120 (void)(_unused); // Suppress unused warning.
122 }
123
124 template <>
125 inline datatypes::ChannelDatatype get_datatype(const float* _unused) {
126 (void)(_unused); // Suppress unused warning.
128 }
129
130 template <>
131 inline datatypes::ChannelDatatype get_datatype(const double* _unused) {
132 (void)(_unused); // Suppress unused warning.
134 }
135
136 /// Returns the number of channels for a given color model.
137 ///
138 /// This is the number of expected elements per pixel.
140 switch (color_model) {
142 return 1;
145 return 3;
148 return 4;
149 default:
150 assert(false && "unreachable");
151 }
152 return 0;
153 }
154
155 inline size_t pixel_format_num_bytes(
156 WidthHeight resolution, datatypes::PixelFormat pixel_format
157 ) {
158 auto num_pixels = resolution.width * resolution.height;
159 switch (pixel_format) {
160 // 444 formats.
163 return num_pixels * 4;
164
165 // 422 formats.
169 return 16 * num_pixels / 8;
170
171 // 420 formats.
175 return 12 * num_pixels / 8;
176
177 // Monochrome formats.
180 return num_pixels;
181
182 default:
183 assert(false && "unreachable");
184 }
185 return 0;
186 }
187} // namespace rerun
PixelFormat
Datatype: Specifieds a particular format of an archetypes::Image.
Definition pixel_format.hpp:34
@ Y_U_V12_FullRange
Y_U_V12 is a YUV 4:2:0 fully planar YUV format without chroma downsampling, also known as I420.
@ NV12
NV12 (aka Y_UV12) is a YUV 4:2:0 chroma downsampled form at with 12 bits per pixel and 8 bits per cha...
@ Y_U_V12_LimitedRange
Y_U_V12 is a YUV 4:2:0 fully planar YUV format without chroma downsampling, also known as I420.
@ Y_U_V24_FullRange
Y_U_V24 is a YUV 4:4:4 fully planar YUV format without chroma downsampling, also known as I444.
@ Y8_LimitedRange
Monochrome Y plane only, essentially a YUV 4:0:0 planar format.
@ Y_U_V24_LimitedRange
Y_U_V24 is a YUV 4:4:4 fully planar YUV format without chroma downsampling, also known as I444.
@ Y_U_V16_FullRange
Y_U_V16 is a YUV 4:2:2 fully planar YUV format without chroma downsampling, also known as I422.
@ Y_U_V16_LimitedRange
Y_U_V16 is a YUV 4:2:2 fully planar YUV format without chroma downsampling, also known as I422.
@ YUY2
YUY2 (aka YUYV, YUYV16 or NV21), is a YUV 4:2:2 chroma downsampled format with 16 bits per pixel and ...
@ Y8_FullRange
Monochrome Y plane only, essentially a YUV 4:0:0 planar format.
ColorModel
Datatype: Specified what color components are present in an archetypes::Image.
Definition color_model.hpp:26
@ BGR
Blue, Green, Red.
@ BGRA
Blue, Green, Red, Alpha.
@ RGB
Red, Green, Blue.
@ L
Grayscale luminance intencity/brightness/value, sometimes called Y
@ RGBA
Red, Green, Blue, Alpha.
ChannelDatatype
Datatype: The innermost datatype of an image.
Definition channel_datatype.hpp:26
@ F64
64-bit IEEE-754 floating point, also known as double.
@ U64
64-bit unsigned integer.
@ F32
32-bit IEEE-754 floating point, also known as float or single.
@ F16
16-bit IEEE-754 floating point, also known as half.
@ I8
8-bit signed integer.
@ U8
8-bit unsigned integer.
@ I16
16-bit signed integer.
@ U32
32-bit unsigned integer.
@ I32
32-bit signed integer.
@ I64
64-bit signed integer.
@ U16
16-bit unsigned integer.
All Rerun C++ types and functions are in the rerun namespace or one of its nested namespaces.
Definition rerun.hpp:22
size_t datatype_bits(datatypes::ChannelDatatype value)
Number of bits used by this element type.
Definition image_utils.hpp:21
size_t color_model_channel_count(datatypes::ColorModel color_model)
Returns the number of channels for a given color model.
Definition image_utils.hpp:139
The width and height of an image.
Definition image_utils.hpp:13
IEEE 754 16-bit half-precision floating point number.
Definition half.hpp:7