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 // Widen first: `uint32_t` overflows here, and the result is a buffer length.
64 const auto num_pixels =
65 static_cast<size_t>(resolution.width) * static_cast<size_t>(resolution.height);
66 // rounding upwards:
67 return (num_pixels * datatype_bits(datatype) + 7) / 8;
68 }
69
70 template <typename TElement>
71 inline datatypes::ChannelDatatype get_datatype(const TElement* _unused);
72
73 template <>
74 inline datatypes::ChannelDatatype get_datatype(const uint8_t* _unused) {
75 (void)(_unused); // Suppress unused warning.
77 }
78
79 template <>
80 inline datatypes::ChannelDatatype get_datatype(const uint16_t* _unused) {
81 (void)(_unused); // Suppress unused warning.
83 }
84
85 template <>
86 inline datatypes::ChannelDatatype get_datatype(const uint32_t* _unused) {
87 (void)(_unused); // Suppress unused warning.
89 }
90
91 template <>
92 inline datatypes::ChannelDatatype get_datatype(const uint64_t* _unused) {
93 (void)(_unused); // Suppress unused warning.
95 }
96
97 template <>
98 inline datatypes::ChannelDatatype get_datatype(const int8_t* _unused) {
99 (void)(_unused); // Suppress unused warning.
101 }
102
103 template <>
104 inline datatypes::ChannelDatatype get_datatype(const int16_t* _unused) {
105 (void)(_unused); // Suppress unused warning.
107 }
108
109 template <>
110 inline datatypes::ChannelDatatype get_datatype(const int32_t* _unused) {
111 (void)(_unused); // Suppress unused warning.
113 }
114
115 template <>
116 inline datatypes::ChannelDatatype get_datatype(const int64_t* _unused) {
117 (void)(_unused); // Suppress unused warning.
119 }
120
121 template <>
122 inline datatypes::ChannelDatatype get_datatype(const rerun::half* _unused) {
123 (void)(_unused); // Suppress unused warning.
125 }
126
127 template <>
128 inline datatypes::ChannelDatatype get_datatype(const float* _unused) {
129 (void)(_unused); // Suppress unused warning.
131 }
132
133 template <>
134 inline datatypes::ChannelDatatype get_datatype(const double* _unused) {
135 (void)(_unused); // Suppress unused warning.
137 }
138
139 /// Returns the number of channels for a given color model.
140 ///
141 /// This is the number of expected elements per pixel.
143 switch (color_model) {
145 return 1;
148 return 3;
151 return 4;
152 default:
153 assert(false && "unreachable");
154 }
155 return 0;
156 }
157
158 inline size_t pixel_format_num_bytes(
159 WidthHeight resolution, datatypes::PixelFormat pixel_format
160 ) {
161 // Widen before multiplying — see `num_bytes` above.
162 const auto num_pixels =
163 static_cast<size_t>(resolution.width) * static_cast<size_t>(resolution.height);
164 switch (pixel_format) {
165 // 444 formats.
168 return num_pixels * 4;
169
170 // 422 formats.
174 return 16 * num_pixels / 8;
175
176 // 420 formats.
180 return 12 * num_pixels / 8;
181
182 // Monochrome formats.
185 return num_pixels;
186
187 default:
188 assert(false && "unreachable");
189 }
190 return 0;
191 }
192} // 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 pixe...
@ 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:23
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:142
The width and height of an image.
Definition image_utils.hpp:13
IEEE 754 16-bit half-precision floating point number.
Definition half.hpp:11