Rerun C++ SDK
Loading...
Searching...
No Matches
image.hpp
1// DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/cpp/mod.rs
2// Based on "crates/store/re_types/definitions/rerun/archetypes/image.fbs".
3
4#pragma once
5
6#include "../collection.hpp"
7#include "../compiler_utils.hpp"
8#include "../component_batch.hpp"
9#include "../components/draw_order.hpp"
10#include "../components/image_buffer.hpp"
11#include "../components/image_format.hpp"
12#include "../components/opacity.hpp"
13#include "../image_utils.hpp"
14#include "../indicator_component.hpp"
15#include "../result.hpp"
16
17#include <cstdint>
18#include <optional>
19#include <utility>
20#include <vector>
21
22namespace rerun::archetypes {
23 /// **Archetype**: A monochrome or color image.
24 ///
25 /// See also `archetypes::DepthImage` and `archetypes::SegmentationImage`.
26 ///
27 /// Rerun also supports compressed images (JPEG, PNG, …), using `archetypes::EncodedImage`.
28 /// For images that refer to video frames see `archetypes::VideoFrameReference`.
29 /// Compressing images or using video data instead can save a lot of bandwidth and memory.
30 ///
31 /// The raw image data is stored as a single buffer of bytes in a `components::Blob`.
32 /// The meaning of these bytes is determined by the `components::ImageFormat` which specifies the resolution
33 /// and the pixel format (e.g. RGB, RGBA, …).
34 ///
35 /// The order of dimensions in the underlying `components::Blob` follows the typical
36 /// row-major, interleaved-pixel image format.
37 ///
38 /// Since the underlying [rerun::components::Blob] uses `rerun::Collection` internally,
39 /// data can be passed in without a copy from raw pointers or by reference from `std::vector`/`std::array`/c-arrays.
40 /// If needed, this "borrow-behavior" can be extended by defining your own `rerun::CollectionAdapter`.
41 ///
42 /// ## Examples
43 ///
44 /// ### image_simple:
45 /// ![image](https://static.rerun.io/image_simple/06ba7f8582acc1ffb42a7fd0006fad7816f3e4e4/full.png)
46 ///
47 /// ```cpp
48 /// #include <rerun.hpp>
49 ///
50 /// #include <vector>
51 ///
52 /// int main() {
53 /// const auto rec = rerun::RecordingStream("rerun_example_image");
54 /// rec.spawn().exit_on_failure();
55 ///
56 /// // Create a synthetic image.
57 /// const int HEIGHT = 200;
58 /// const int WIDTH = 300;
59 /// std::vector<uint8_t> data(WIDTH * HEIGHT * 3, 0);
60 /// for (size_t i = 0; i <data.size(); i += 3) {
61 /// data[i] = 255;
62 /// }
63 /// for (size_t y = 50; y <150; ++y) {
64 /// for (size_t x = 50; x <150; ++x) {
65 /// data[(y * WIDTH + x) * 3 + 0] = 0;
66 /// data[(y * WIDTH + x) * 3 + 1] = 255;
67 /// data[(y * WIDTH + x) * 3 + 2] = 0;
68 /// }
69 /// }
70 ///
71 /// rec.log("image", rerun::Image::from_rgb24(data, {WIDTH, HEIGHT}));
72 /// }
73 /// ```
74 ///
75 /// ### Logging images with various formats
76 /// ![image](https://static.rerun.io/image_formats/7b8a162fcfd266f303980439beea997dc8544c24/full.png)
77 ///
78 /// ```cpp
79 /// #include <algorithm>
80 /// #include <cstdint>
81 /// #include <vector>
82 ///
83 /// #include <rerun.hpp>
84 ///
85 /// int main() {
86 /// const auto rec = rerun::RecordingStream("rerun_example_image_formats");
87 /// rec.spawn().exit_on_failure();
88 ///
89 /// // Simple gradient image
90 /// std::vector<uint8_t> image(256 * 256 * 3);
91 /// for (size_t y = 0; y <256; ++y) {
92 /// for (size_t x = 0; x <256; ++x) {
93 /// image[(y * 256 + x) * 3 + 0] = static_cast<uint8_t>(x);
94 /// image[(y * 256 + x) * 3 + 1] = static_cast<uint8_t>(std::min<size_t>(255, x + y));
95 /// image[(y * 256 + x) * 3 + 2] = static_cast<uint8_t>(y);
96 /// }
97 /// }
98 ///
99 /// // RGB image
100 /// rec.log("image_rgb", rerun::Image::from_rgb24(image, {256, 256}));
101 ///
102 /// // Green channel only (Luminance)
103 /// std::vector<uint8_t> green_channel(256 * 256);
104 /// for (size_t i = 0; i <256 * 256; ++i) {
105 /// green_channel[i] = image[i * 3 + 1];
106 /// }
107 /// rec.log(
108 /// "image_green_only",
109 /// rerun::Image(rerun::borrow(green_channel), {256, 256}, rerun::ColorModel::L)
110 /// );
111 ///
112 /// // BGR image
113 /// std::vector<uint8_t> bgr_image(256 * 256 * 3);
114 /// for (size_t i = 0; i <256 * 256; ++i) {
115 /// bgr_image[i * 3 + 0] = image[i * 3 + 2];
116 /// bgr_image[i * 3 + 1] = image[i * 3 + 1];
117 /// bgr_image[i * 3 + 2] = image[i * 3 + 0];
118 /// }
119 /// rec.log(
120 /// "image_bgr",
121 /// rerun::Image(rerun::borrow(bgr_image), {256, 256}, rerun::ColorModel::BGR)
122 /// );
123 ///
124 /// // New image with Separate Y/U/V planes with 4:2:2 chroma downsampling
125 /// std::vector<uint8_t> yuv_bytes(256 * 256 + 128 * 256 * 2);
126 /// std::fill_n(yuv_bytes.begin(), 256 * 256, static_cast<uint8_t>(128)); // Fixed value for Y
127 /// size_t u_plane_offset = 256 * 256;
128 /// size_t v_plane_offset = u_plane_offset + 128 * 256;
129 /// for (size_t y = 0; y <256; ++y) {
130 /// for (size_t x = 0; x <128; ++x) {
131 /// auto coord = y * 128 + x;
132 /// yuv_bytes[u_plane_offset + coord] = static_cast<uint8_t>(x * 2); // Gradient for U
133 /// yuv_bytes[v_plane_offset + coord] = static_cast<uint8_t>(y); // Gradient for V
134 /// }
135 /// }
136 /// rec.log(
137 /// "image_yuv422",
138 /// rerun::Image(rerun::borrow(yuv_bytes), {256, 256}, rerun::PixelFormat::Y_U_V16_FullRange)
139 /// );
140 ///
141 /// return 0;
142 /// }
143 /// ```
144 struct Image {
145 /// The raw image data.
147
148 /// The format of the image.
150
151 /// Opacity of the image, useful for layering several images.
152 ///
153 /// Defaults to 1.0 (fully opaque).
154 std::optional<rerun::components::Opacity> opacity;
155
156 /// An optional floating point value that specifies the 2D drawing order.
157 ///
158 /// Objects with higher values are drawn on top of those with lower values.
159 std::optional<rerun::components::DrawOrder> draw_order;
160
161 public:
162 static constexpr const char IndicatorComponentName[] = "rerun.components.ImageIndicator";
163
164 /// Indicator component, used to identify the archetype when converting to a list of components.
166
167 public: // START of extensions from image_ext.cpp:
168 /// Construct an image from bytes and image format.
169 ///
170 /// @param bytes The raw image data as bytes.
171 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
172 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
173 /// The length of the data should be `W * H * image_format.bytes_per_pixel`.
174 /// @param format_ How the data should be interpreted.
176 : buffer(std::move(bytes)), format(format_) {
177 if (buffer.size() != format.image_format.num_bytes()) {
178 Error(
179 ErrorCode::InvalidTensorDimension,
180 "Image buffer has the wrong size. Got " + std::to_string(buffer.size()) +
181 " bytes, expected " + std::to_string(format.image_format.num_bytes())
182 )
183 .handle();
184 }
185 }
186
187 /// Construct an image from resolution, pixel format and bytes.
188 ///
189 /// @param bytes The raw image data as bytes.
190 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
191 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
192 /// The length of the data should be `W * H * pixel_format.bytes_per_pixel`.
193 /// @param resolution The resolution of the image as {width, height}.
194 /// @param pixel_format How the data should be interpreted.
196 Collection<uint8_t> bytes, WidthHeight resolution, datatypes::PixelFormat pixel_format
197 )
198 : Image{std::move(bytes), datatypes::ImageFormat{resolution, pixel_format}} {}
199
200 /// Construct an image from resolution, color model, channel datatype and bytes.
201 ///
202 /// @param bytes The raw image data.
203 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
204 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
205 /// The length of the data should be `W * H * datatype.bytes * color_model.num_channels`.
206 /// @param resolution The resolution of the image as {width, height}.
207 /// @param color_model The color model of the pixel data.
208 /// @param datatype Datatype of the individual channels of the color model.
210 Collection<uint8_t> bytes, WidthHeight resolution, datatypes::ColorModel color_model,
212 )
213 : Image(std::move(bytes), datatypes::ImageFormat(resolution, color_model, datatype)) {}
214
215 /// Construct an image from resolution, color model and elements,
216 /// inferring the channel datatype from the element type.
217 ///
218 /// @param elements Pixel data as a `rerun::Collection`.
219 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
220 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
221 /// The length of the data should be `W * H * color_model.num_channels`.
222 /// @param resolution The resolution of the image as {width, height}.
223 /// @param color_model The color model of the pixel data.
224 /// Each element in elements is interpreted as a single channel of the color model.
225 template <typename T>
226 Image(Collection<T> elements, WidthHeight resolution, datatypes::ColorModel color_model)
227 : Image(elements.to_uint8(), resolution, color_model, get_datatype(elements.data())) {}
228
229 /// Construct an image from resolution, color model and element pointer,
230 /// inferring the channel datatype from the element type.
231 ///
232 /// @param elements The raw image data.
233 /// ⚠️ Does not take ownership of the data, the caller must ensure the data outlives the image.
234 /// The number of elements is assumed to be `W * H * color_model.num_channels`.
235 /// @param resolution The resolution of the image as {width, height}.
236 /// @param color_model The color model of the pixel data.
237 /// Each element in elements is interpreted as a single channel of the color model.
238 template <typename T>
239 Image(const T* elements, WidthHeight resolution, datatypes::ColorModel color_model)
240 : Image(
241 rerun::Collection<uint8_t>::borrow(
242 reinterpret_cast<const uint8_t*>(elements),
243 resolution.width * resolution.height * color_model_channel_count(color_model)
244 ),
245 resolution, color_model, get_datatype(elements)
246 ) {}
247
248 /// Assumes single channel greyscale/luminance with 8-bit per value.
249 ///
250 /// @param bytes Pixel data as a `rerun::Collection`.
251 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
252 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
253 /// The length of the data should be `W * H`.
254 /// @param resolution The resolution of the image as {width, height}.
256 return Image(
257 bytes,
258 resolution,
261 );
262 }
263
264 /// Assumes RGB, 8-bit per channel, packed as `RGBRGBRGB…`.
265 ///
266 /// @param bytes Pixel data as a `rerun::Collection`.
267 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
268 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
269 /// The length of the data should be `W * H * 3`.
270 /// @param resolution The resolution of the image as {width, height}.
272 return Image(
273 bytes,
274 resolution,
277 );
278 }
279
280 /// Assumes RGBA, 8-bit per channel, with separate alpha.
281 ///
282 /// @param bytes Pixel data as a `rerun::Collection`.
283 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
284 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
285 /// The length of the data should be `W * H * 4`.
286 /// @param resolution The resolution of the image as {width, height}.
288 return Image(
289 bytes,
290 resolution,
293 );
294 }
295
296 // END of extensions from image_ext.cpp, start of generated code:
297
298 public:
299 Image() = default;
300 Image(Image&& other) = default;
301
302 /// Opacity of the image, useful for layering several images.
303 ///
304 /// Defaults to 1.0 (fully opaque).
306 opacity = std::move(_opacity);
307 // See: https://github.com/rerun-io/rerun/issues/4027
308 RR_WITH_MAYBE_UNINITIALIZED_DISABLED(return std::move(*this);)
309 }
310
311 /// An optional floating point value that specifies the 2D drawing order.
312 ///
313 /// Objects with higher values are drawn on top of those with lower values.
315 draw_order = std::move(_draw_order);
316 // See: https://github.com/rerun-io/rerun/issues/4027
317 RR_WITH_MAYBE_UNINITIALIZED_DISABLED(return std::move(*this);)
318 }
319 };
320
321} // namespace rerun::archetypes
322
323namespace rerun {
324 /// \private
325 template <typename T>
326 struct AsComponents;
327
328 /// \private
329 template <>
330 struct AsComponents<archetypes::Image> {
331 /// Serialize all set component batches.
332 static Result<std::vector<ComponentBatch>> serialize(const archetypes::Image& archetype);
333 };
334} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:95
void handle() const
Handle this error based on the set log handler.
All built-in archetypes. See Types in the Rerun manual.
Definition rerun.hpp:77
PixelFormat
Datatype: Specifieds a particular format of an archetypes::Image.
Definition pixel_format.hpp:35
ColorModel
Datatype: Specified what color components are present in an archetypes::Image.
Definition color_model.hpp:27
@ 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:27
@ U8
8-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 color_model_channel_count(datatypes::ColorModel color_model)
Returns the number of channels for a given color model.
Definition image_utils.hpp:139
Collection< TElement > borrow(const TElement *data, size_t num_instances=1)
Borrows binary data into a Collection from a pointer.
Definition collection.hpp:461
The width and height of an image.
Definition image_utils.hpp:13
Archetype: A monochrome or color image.
Definition image.hpp:144
static Image from_greyscale8(Collection< uint8_t > bytes, WidthHeight resolution)
Assumes single channel greyscale/luminance with 8-bit per value.
Definition image.hpp:255
Image(Collection< uint8_t > bytes, components::ImageFormat format_)
Construct an image from bytes and image format.
Definition image.hpp:175
Image with_opacity(rerun::components::Opacity _opacity) &&
Opacity of the image, useful for layering several images.
Definition image.hpp:305
Image(Collection< T > elements, WidthHeight resolution, datatypes::ColorModel color_model)
Construct an image from resolution, color model and elements, inferring the channel datatype from the...
Definition image.hpp:226
rerun::components::ImageFormat format
The format of the image.
Definition image.hpp:149
Image(Collection< uint8_t > bytes, WidthHeight resolution, datatypes::PixelFormat pixel_format)
Construct an image from resolution, pixel format and bytes.
Definition image.hpp:195
std::optional< rerun::components::DrawOrder > draw_order
An optional floating point value that specifies the 2D drawing order.
Definition image.hpp:159
Image(const T *elements, WidthHeight resolution, datatypes::ColorModel color_model)
Construct an image from resolution, color model and element pointer, inferring the channel datatype f...
Definition image.hpp:239
Image(Collection< uint8_t > bytes, WidthHeight resolution, datatypes::ColorModel color_model, datatypes::ChannelDatatype datatype)
Construct an image from resolution, color model, channel datatype and bytes.
Definition image.hpp:209
rerun::components::ImageBuffer buffer
The raw image data.
Definition image.hpp:146
std::optional< rerun::components::Opacity > opacity
Opacity of the image, useful for layering several images.
Definition image.hpp:154
static Image from_rgba32(Collection< uint8_t > bytes, WidthHeight resolution)
Assumes RGBA, 8-bit per channel, with separate alpha.
Definition image.hpp:287
Image with_draw_order(rerun::components::DrawOrder _draw_order) &&
An optional floating point value that specifies the 2D drawing order.
Definition image.hpp:314
static Image from_rgb24(Collection< uint8_t > bytes, WidthHeight resolution)
Assumes RGB, 8-bit per channel, packed as RGBRGBRGB….
Definition image.hpp:271
Component: Draw order of 2D elements.
Definition draw_order.hpp:20
Component: A buffer that is known to store image data.
Definition image_buffer.hpp:19
size_t size() const
Number of bytes.
Definition image_buffer.hpp:24
Component: The metadata describing the contents of a components::ImageBuffer.
Definition image_format.hpp:15
Indicator component used by archetypes when converting them to component lists.
Definition indicator_component.hpp:32
Component: Degree of transparency ranging from 0.0 (fully transparent) to 1.0 (fully opaque).
Definition opacity.hpp:18
size_t num_bytes() const
How many bytes will this image occupy?
Definition image_format.hpp:66