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 "../component_batch.hpp"
8#include "../component_column.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/182a233fb4d0680eb31912a82f328ddaaa66324e/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.
146 std::optional<ComponentBatch> buffer;
147
148 /// The format of the image.
149 std::optional<ComponentBatch> format;
150
151 /// Opacity of the image, useful for layering several images.
152 ///
153 /// Defaults to 1.0 (fully opaque).
154 std::optional<ComponentBatch> 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<ComponentBatch> 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 /// The name of the archetype as used in `ComponentDescriptor`s.
167 static constexpr const char ArchetypeName[] = "rerun.archetypes.Image";
168
169 /// `ComponentDescriptor` for the `buffer` field.
170 static constexpr auto Descriptor_buffer = ComponentDescriptor(
171 ArchetypeName, "buffer",
173 );
174 /// `ComponentDescriptor` for the `format` field.
175 static constexpr auto Descriptor_format = ComponentDescriptor(
176 ArchetypeName, "format",
178 );
179 /// `ComponentDescriptor` for the `opacity` field.
181 ArchetypeName, "opacity",
183 );
184 /// `ComponentDescriptor` for the `draw_order` field.
186 ArchetypeName, "draw_order",
188 );
189
190 public: // START of extensions from image_ext.cpp:
191 /// Construct an image from bytes and image format.
192 ///
193 /// @param bytes The raw image data as bytes.
194 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
195 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
196 /// The length of the data should be `W * H * image_format.bytes_per_pixel`.
197 /// @param format_ How the data should be interpreted.
199 if (bytes.size() != format_.image_format.num_bytes()) {
200 Error(
201 ErrorCode::InvalidTensorDimension,
202 "Image buffer has the wrong size. Got " + std::to_string(bytes.size()) +
203 " bytes, expected " + std::to_string(format_.image_format.num_bytes())
204 )
205 .handle();
206 }
207 *this = std::move(*this).with_buffer(bytes).with_format(format_);
208 }
209
210 /// Construct an image from resolution, pixel format and bytes.
211 ///
212 /// @param bytes The raw image data as bytes.
213 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
214 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
215 /// The length of the data should be `W * H * pixel_format.bytes_per_pixel`.
216 /// @param resolution The resolution of the image as {width, height}.
217 /// @param pixel_format How the data should be interpreted.
219 Collection<uint8_t> bytes, WidthHeight resolution, datatypes::PixelFormat pixel_format
220 )
221 : Image{std::move(bytes), datatypes::ImageFormat{resolution, pixel_format}} {}
222
223 /// Construct an image from resolution, color model, channel datatype and bytes.
224 ///
225 /// @param bytes The raw image data.
226 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
227 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
228 /// The length of the data should be `W * H * datatype.bytes * color_model.num_channels`.
229 /// @param resolution The resolution of the image as {width, height}.
230 /// @param color_model The color model of the pixel data.
231 /// @param datatype Datatype of the individual channels of the color model.
233 Collection<uint8_t> bytes, WidthHeight resolution, datatypes::ColorModel color_model,
235 )
236 : Image(std::move(bytes), datatypes::ImageFormat(resolution, color_model, datatype)) {}
237
238 /// Construct an image from resolution, color model and elements,
239 /// inferring the channel datatype from the element type.
240 ///
241 /// @param elements Pixel data as a `rerun::Collection`.
242 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
243 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
244 /// The length of the data should be `W * H * color_model.num_channels`.
245 /// @param resolution The resolution of the image as {width, height}.
246 /// @param color_model The color model of the pixel data.
247 /// Each element in elements is interpreted as a single channel of the color model.
248 template <typename T>
249 Image(Collection<T> elements, WidthHeight resolution, datatypes::ColorModel color_model)
250 : Image(elements.to_uint8(), resolution, color_model, get_datatype(elements.data())) {}
251
252 /// Construct an image from resolution, color model and element pointer,
253 /// inferring the channel datatype from the element type.
254 ///
255 /// @param elements The raw image data.
256 /// ⚠️ Does not take ownership of the data, the caller must ensure the data outlives the image.
257 /// The number of elements is assumed to be `W * H * color_model.num_channels`.
258 /// @param resolution The resolution of the image as {width, height}.
259 /// @param color_model The color model of the pixel data.
260 /// Each element in elements is interpreted as a single channel of the color model.
261 template <typename T>
262 Image(const T* elements, WidthHeight resolution, datatypes::ColorModel color_model)
263 : Image(
264 rerun::Collection<uint8_t>::borrow(
265 reinterpret_cast<const uint8_t*>(elements),
266 resolution.width * resolution.height * color_model_channel_count(color_model)
267 ),
268 resolution, color_model, get_datatype(elements)
269 ) {}
270
271 /// Assumes single channel greyscale/luminance with 8-bit per value.
272 ///
273 /// @param bytes Pixel data as a `rerun::Collection`.
274 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
275 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
276 /// The length of the data should be `W * H`.
277 /// @param resolution The resolution of the image as {width, height}.
279 return Image(
280 bytes,
281 resolution,
284 );
285 }
286
287 /// Assumes RGB, 8-bit per channel, packed as `RGBRGBRGB…`.
288 ///
289 /// @param bytes Pixel data as a `rerun::Collection`.
290 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
291 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
292 /// The length of the data should be `W * H * 3`.
293 /// @param resolution The resolution of the image as {width, height}.
295 return Image(
296 bytes,
297 resolution,
300 );
301 }
302
303 /// Assumes RGBA, 8-bit per channel, with separate alpha.
304 ///
305 /// @param bytes Pixel data as a `rerun::Collection`.
306 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
307 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
308 /// The length of the data should be `W * H * 4`.
309 /// @param resolution The resolution of the image as {width, height}.
311 return Image(
312 bytes,
313 resolution,
316 );
317 }
318
319 // END of extensions from image_ext.cpp, start of generated code:
320
321 public:
322 Image() = default;
323 Image(Image&& other) = default;
324 Image(const Image& other) = default;
325 Image& operator=(const Image& other) = default;
326 Image& operator=(Image&& other) = default;
327
328 /// Update only some specific fields of a `Image`.
330 return Image();
331 }
332
333 /// Clear all the fields of a `Image`.
335
336 /// The raw image data.
338 buffer = ComponentBatch::from_loggable(_buffer, Descriptor_buffer).value_or_throw();
339 return std::move(*this);
340 }
341
342 /// This method makes it possible to pack multiple `buffer` in a single component batch.
343 ///
344 /// This only makes sense when used in conjunction with `columns`. `with_buffer` should
345 /// be used when logging a single row's worth of data.
347 buffer = ComponentBatch::from_loggable(_buffer, Descriptor_buffer).value_or_throw();
348 return std::move(*this);
349 }
350
351 /// The format of the image.
353 format = ComponentBatch::from_loggable(_format, Descriptor_format).value_or_throw();
354 return std::move(*this);
355 }
356
357 /// This method makes it possible to pack multiple `format` in a single component batch.
358 ///
359 /// This only makes sense when used in conjunction with `columns`. `with_format` should
360 /// be used when logging a single row's worth of data.
362 format = ComponentBatch::from_loggable(_format, Descriptor_format).value_or_throw();
363 return std::move(*this);
364 }
365
366 /// Opacity of the image, useful for layering several images.
367 ///
368 /// Defaults to 1.0 (fully opaque).
370 opacity = ComponentBatch::from_loggable(_opacity, Descriptor_opacity).value_or_throw();
371 return std::move(*this);
372 }
373
374 /// This method makes it possible to pack multiple `opacity` in a single component batch.
375 ///
376 /// This only makes sense when used in conjunction with `columns`. `with_opacity` should
377 /// be used when logging a single row's worth of data.
379 opacity = ComponentBatch::from_loggable(_opacity, Descriptor_opacity).value_or_throw();
380 return std::move(*this);
381 }
382
383 /// An optional floating point value that specifies the 2D drawing order.
384 ///
385 /// Objects with higher values are drawn on top of those with lower values.
387 draw_order =
388 ComponentBatch::from_loggable(_draw_order, Descriptor_draw_order).value_or_throw();
389 return std::move(*this);
390 }
391
392 /// This method makes it possible to pack multiple `draw_order` in a single component batch.
393 ///
394 /// This only makes sense when used in conjunction with `columns`. `with_draw_order` should
395 /// be used when logging a single row's worth of data.
397 draw_order =
398 ComponentBatch::from_loggable(_draw_order, Descriptor_draw_order).value_or_throw();
399 return std::move(*this);
400 }
401
402 /// Partitions the component data into multiple sub-batches.
403 ///
404 /// Specifically, this transforms the existing `ComponentBatch` data into `ComponentColumn`s
405 /// instead, via `ComponentBatch::partitioned`.
406 ///
407 /// This makes it possible to use `RecordingStream::send_columns` to send columnar data directly into Rerun.
408 ///
409 /// The specified `lengths` must sum to the total length of the component batch.
411
412 /// Partitions the component data into unit-length sub-batches.
413 ///
414 /// This is semantically similar to calling `columns` with `std::vector<uint32_t>(n, 1)`,
415 /// where `n` is automatically guessed.
417 };
418
419} // namespace rerun::archetypes
420
421namespace rerun {
422 /// \private
423 template <typename T>
424 struct AsComponents;
425
426 /// \private
427 template <>
428 struct AsComponents<archetypes::Image> {
429 /// Serialize all set component batches.
430 static Result<Collection<ComponentBatch>> as_batches(const archetypes::Image& archetype);
431 };
432} // namespace rerun
Generic collection of elements that are roughly contiguous in memory.
Definition collection.hpp:49
size_t size() const
Returns the number of instances in this collection.
Definition collection.hpp:291
Status outcome object (success or error) returned for fallible operations.
Definition error.hpp:96
void handle() const
Handle this error based on the set log handler.
A class for representing either a usable value, or an error.
Definition result.hpp:14
All built-in archetypes. See Types in the Rerun manual.
Definition rerun.hpp:76
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:462
static Result< ComponentBatch > from_loggable(const rerun::Collection< T > &components, const ComponentDescriptor &descriptor=rerun::Loggable< T >::Descriptor)
Creates a new component batch from a collection of component instances.
Definition component_batch.hpp:46
A ComponentDescriptor fully describes the semantics of a column of data.
Definition component_descriptor.hpp:14
The Loggable trait is used by all built-in implementation of rerun::AsComponents to serialize a colle...
Definition loggable.hpp:11
The width and height of an image.
Definition image_utils.hpp:13
Archetype: A monochrome or color image.
Definition image.hpp:144
Collection< ComponentColumn > columns(const Collection< uint32_t > &lengths_)
Partitions the component data into multiple sub-batches.
Image with_draw_order(const rerun::components::DrawOrder &_draw_order) &&
An optional floating point value that specifies the 2D drawing order.
Definition image.hpp:386
static constexpr auto Descriptor_opacity
ComponentDescriptor for the opacity field.
Definition image.hpp:180
Image with_many_format(const Collection< rerun::components::ImageFormat > &_format) &&
This method makes it possible to pack multiple format in a single component batch.
Definition image.hpp:361
Image with_format(const rerun::components::ImageFormat &_format) &&
The format of the image.
Definition image.hpp:352
Image with_buffer(const rerun::components::ImageBuffer &_buffer) &&
The raw image data.
Definition image.hpp:337
static constexpr auto Descriptor_buffer
ComponentDescriptor for the buffer field.
Definition image.hpp:170
static Image from_greyscale8(Collection< uint8_t > bytes, WidthHeight resolution)
Assumes single channel greyscale/luminance with 8-bit per value.
Definition image.hpp:278
Image(Collection< uint8_t > bytes, components::ImageFormat format_)
Construct an image from bytes and image format.
Definition image.hpp:198
Image with_many_opacity(const Collection< rerun::components::Opacity > &_opacity) &&
This method makes it possible to pack multiple opacity in a single component batch.
Definition image.hpp:378
static constexpr auto Descriptor_draw_order
ComponentDescriptor for the draw_order field.
Definition image.hpp:185
Collection< ComponentColumn > columns()
Partitions the component data into unit-length sub-batches.
static constexpr auto Descriptor_format
ComponentDescriptor for the format field.
Definition image.hpp:175
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:249
static Image clear_fields()
Clear all the fields of a Image.
Image(Collection< uint8_t > bytes, WidthHeight resolution, datatypes::PixelFormat pixel_format)
Construct an image from resolution, pixel format and bytes.
Definition image.hpp:218
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:262
std::optional< ComponentBatch > format
The format of the image.
Definition image.hpp:149
Image with_opacity(const rerun::components::Opacity &_opacity) &&
Opacity of the image, useful for layering several images.
Definition image.hpp:369
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:232
Image with_many_draw_order(const Collection< rerun::components::DrawOrder > &_draw_order) &&
This method makes it possible to pack multiple draw_order in a single component batch.
Definition image.hpp:396
static Image update_fields()
Update only some specific fields of a Image.
Definition image.hpp:329
std::optional< ComponentBatch > draw_order
An optional floating point value that specifies the 2D drawing order.
Definition image.hpp:159
std::optional< ComponentBatch > buffer
The raw image data.
Definition image.hpp:146
static constexpr const char ArchetypeName[]
The name of the archetype as used in ComponentDescriptors.
Definition image.hpp:167
std::optional< ComponentBatch > 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:310
static Image from_rgb24(Collection< uint8_t > bytes, WidthHeight resolution)
Assumes RGB, 8-bit per channel, packed as RGBRGBRGB….
Definition image.hpp:294
Image with_many_buffer(const Collection< rerun::components::ImageBuffer > &_buffer) &&
This method makes it possible to pack multiple buffer in a single component batch.
Definition image.hpp:346
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
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