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 /// The raw image data is stored as a single buffer of bytes in a `components::Blob`.
28 /// The meaning of these bytes is determined by the `components::ImageFormat` which specifies the resolution
29 /// and the pixel format (e.g. RGB, RGBA, …).
30 ///
31 /// The order of dimensions in the underlying `components::Blob` follows the typical
32 /// row-major, interleaved-pixel image format.
33 ///
34 /// Rerun also supports compressed images (JPEG, PNG, …), using `archetypes::EncodedImage`.
35 /// Compressing images can save a lot of bandwidth and memory.
36 ///
37 /// Since the underlying [rerun::components::Blob] uses `rerun::Collection` internally,
38 /// data can be passed in without a copy from raw pointers or by reference from `std::vector`/`std::array`/c-arrays.
39 /// If needed, this "borrow-behavior" can be extended by defining your own `rerun::CollectionAdapter`.
40 ///
41 /// ## Examples
42 ///
43 /// ### image_simple:
44 /// ![image](https://static.rerun.io/image_simple/06ba7f8582acc1ffb42a7fd0006fad7816f3e4e4/full.png)
45 ///
46 /// ```cpp
47 /// #include <rerun.hpp>
48 ///
49 /// #include <vector>
50 ///
51 /// int main() {
52 /// const auto rec = rerun::RecordingStream("rerun_example_image");
53 /// rec.spawn().exit_on_failure();
54 ///
55 /// // Create a synthetic image.
56 /// const int HEIGHT = 200;
57 /// const int WIDTH = 300;
58 /// std::vector<uint8_t> data(WIDTH * HEIGHT * 3, 0);
59 /// for (size_t i = 0; i <data.size(); i += 3) {
60 /// data[i] = 255;
61 /// }
62 /// for (size_t y = 50; y <150; ++y) {
63 /// for (size_t x = 50; x <150; ++x) {
64 /// data[(y * WIDTH + x) * 3 + 0] = 0;
65 /// data[(y * WIDTH + x) * 3 + 1] = 255;
66 /// data[(y * WIDTH + x) * 3 + 2] = 0;
67 /// }
68 /// }
69 ///
70 /// rec.log("image", rerun::Image::from_rgb24(data, {WIDTH, HEIGHT}));
71 /// }
72 /// ```
73 ///
74 /// ### Advanced usage of `send_columns` to send multiple images at once
75 /// ![image](https://static.rerun.io/image_send_columns/321455161d79e2c45d6f5a6f175d6f765f418897/full.png)
76 ///
77 /// ```cpp
78 /// #include <numeric>
79 /// #include <rerun.hpp>
80 ///
81 /// int main() {
82 /// auto rec = rerun::RecordingStream("rerun_example_image_send_columns");
83 /// rec.spawn().exit_on_failure();
84 ///
85 /// // Timeline on which the images are distributed.
86 /// std::vector<int64_t> times(20);
87 /// std::iota(times.begin(), times.end(), 0);
88 ///
89 /// // Create a batch of images with a moving rectangle.
90 /// const size_t width = 300, height = 200;
91 /// std::vector<uint8_t> images(times.size() * height * width * 3, 0);
92 /// for (size_t t = 0; t <times.size(); ++t) {
93 /// for (size_t y = 0; y <height; ++y) {
94 /// for (size_t x = 0; x <width; ++x) {
95 /// size_t idx = (t * height * width + y * width + x) * 3;
96 /// images[idx + 2] = 255; // Blue background
97 /// if (y>= 50 && y <150 && x>= t * 10 && x <t * 10 + 100) {
98 /// images[idx + 1] = 255; // Turquoise rectangle
99 /// }
100 /// }
101 /// }
102 /// }
103 ///
104 /// // Log the ImageFormat and indicator once, as static.
105 /// auto format = rerun::components::ImageFormat(
106 /// {width, height},
107 /// rerun::ColorModel::RGB,
108 /// rerun::ChannelDatatype::U8
109 /// );
110 /// rec.log_static("images", rerun::borrow(&format), rerun::Image::IndicatorComponent());
111 ///
112 /// // Split up the image data into several components referencing the underlying data.
113 /// const size_t image_size_in_bytes = width * height * 3;
114 /// std::vector<rerun::components::ImageBuffer> image_data(times.size());
115 /// for (size_t i = 0; i <times.size(); ++i) {
116 /// image_data[i] = rerun::borrow(images.data() + i * image_size_in_bytes, image_size_in_bytes);
117 /// }
118 ///
119 /// // Send all images at once.
120 /// rec.send_columns(
121 /// "images",
122 /// rerun::TimeColumn::from_sequence_points("step", std::move(times)),
123 /// rerun::borrow(image_data)
124 /// );
125 /// }
126 /// ```
127 struct Image {
128 /// The raw image data.
130
131 /// The format of the image.
133
134 /// Opacity of the image, useful for layering several images.
135 ///
136 /// Defaults to 1.0 (fully opaque).
137 std::optional<rerun::components::Opacity> opacity;
138
139 /// An optional floating point value that specifies the 2D drawing order.
140 ///
141 /// Objects with higher values are drawn on top of those with lower values.
142 std::optional<rerun::components::DrawOrder> draw_order;
143
144 public:
145 static constexpr const char IndicatorComponentName[] = "rerun.components.ImageIndicator";
146
147 /// Indicator component, used to identify the archetype when converting to a list of components.
149
150 public: // START of extensions from image_ext.cpp:
151 /// Construct an image from bytes and image format.
152 ///
153 /// @param bytes The raw image data as bytes.
154 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
155 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
156 /// The length of the data should be `W * H * image_format.bytes_per_pixel`.
157 /// @param format_ How the data should be interpreted.
159 : buffer(std::move(bytes)), format(format_) {
160 if (buffer.size() != format.image_format.num_bytes()) {
161 Error(
162 ErrorCode::InvalidTensorDimension,
163 "Image buffer has the wrong size. Got " + std::to_string(buffer.size()) +
164 " bytes, expected " + std::to_string(format.image_format.num_bytes())
165 )
166 .handle();
167 }
168 }
169
170 /// Construct an image from resolution, pixel format and bytes.
171 ///
172 /// @param bytes The raw image data as bytes.
173 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
174 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
175 /// The length of the data should be `W * H * pixel_format.bytes_per_pixel`.
176 /// @param resolution The resolution of the image as {width, height}.
177 /// @param pixel_format How the data should be interpreted.
179 Collection<uint8_t> bytes, WidthHeight resolution, datatypes::PixelFormat pixel_format
180 )
181 : Image{std::move(bytes), datatypes::ImageFormat{resolution, pixel_format}} {}
182
183 /// Construct an image from resolution, color model, channel datatype and bytes.
184 ///
185 /// @param bytes The raw image data.
186 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
187 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
188 /// The length of the data should be `W * H * datatype.bytes * color_model.num_channels`.
189 /// @param resolution The resolution of the image as {width, height}.
190 /// @param color_model The color model of the pixel data.
191 /// @param datatype Datatype of the individual channels of the color model.
193 Collection<uint8_t> bytes, WidthHeight resolution, datatypes::ColorModel color_model,
195 )
196 : Image(std::move(bytes), datatypes::ImageFormat(resolution, color_model, datatype)) {}
197
198 /// Construct an image from resolution, color model and elements,
199 /// inferring the channel datatype from the element type.
200 ///
201 /// @param elements Pixel data as a `rerun::Collection`.
202 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
203 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
204 /// The length of the data should be `W * H * color_model.num_channels`.
205 /// @param resolution The resolution of the image as {width, height}.
206 /// @param color_model The color model of the pixel data.
207 /// Each element in elements is interpreted as a single channel of the color model.
208 template <typename T>
209 Image(Collection<T> elements, WidthHeight resolution, datatypes::ColorModel color_model)
210 : Image(elements.to_uint8(), resolution, color_model, get_datatype(elements.data())) {}
211
212 /// Construct an image from resolution, color model and element pointer,
213 /// inferring the channel datatype from the element type.
214 ///
215 /// @param elements The raw image data.
216 /// ⚠️ Does not take ownership of the data, the caller must ensure the data outlives the image.
217 /// The number of elements is assumed to be `W * H * color_model.num_channels`.
218 /// @param resolution The resolution of the image as {width, height}.
219 /// @param color_model The color model of the pixel data.
220 /// Each element in elements is interpreted as a single channel of the color model.
221 template <typename T>
222 Image(const T* elements, WidthHeight resolution, datatypes::ColorModel color_model)
223 : Image(
224 rerun::Collection<uint8_t>::borrow(
225 reinterpret_cast<const uint8_t*>(elements),
226 resolution.width * resolution.height * color_model_channel_count(color_model)
227 ),
228 resolution, color_model, get_datatype(elements)
229 ) {}
230
231 /// Assumes single channel greyscale/luminance with 8-bit per value.
232 ///
233 /// @param bytes Pixel data as a `rerun::Collection`.
234 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
235 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
236 /// The length of the data should be `W * H`.
237 /// @param resolution The resolution of the image as {width, height}.
239 return Image(
240 bytes,
241 resolution,
244 );
245 }
246
247 /// Assumes RGB, 8-bit per channel, packed as `RGBRGBRGB…`.
248 ///
249 /// @param bytes Pixel data as a `rerun::Collection`.
250 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
251 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
252 /// The length of the data should be `W * H * 3`.
253 /// @param resolution The resolution of the image as {width, height}.
255 return Image(
256 bytes,
257 resolution,
260 );
261 }
262
263 /// Assumes RGBA, 8-bit per channel, with separate alpha.
264 ///
265 /// @param bytes Pixel data as a `rerun::Collection`.
266 /// If the data does not outlive the image, use `std::move` or create the `rerun::Collection`
267 /// explicitly ahead of time with `rerun::Collection::take_ownership`.
268 /// The length of the data should be `W * H * 4`.
269 /// @param resolution The resolution of the image as {width, height}.
271 return Image(
272 bytes,
273 resolution,
276 );
277 }
278
279 // END of extensions from image_ext.cpp, start of generated code:
280
281 public:
282 Image() = default;
283 Image(Image&& other) = default;
284
285 /// Opacity of the image, useful for layering several images.
286 ///
287 /// Defaults to 1.0 (fully opaque).
289 opacity = std::move(_opacity);
290 // See: https://github.com/rerun-io/rerun/issues/4027
291 RR_WITH_MAYBE_UNINITIALIZED_DISABLED(return std::move(*this);)
292 }
293
294 /// An optional floating point value that specifies the 2D drawing order.
295 ///
296 /// Objects with higher values are drawn on top of those with lower values.
298 draw_order = std::move(_draw_order);
299 // See: https://github.com/rerun-io/rerun/issues/4027
300 RR_WITH_MAYBE_UNINITIALIZED_DISABLED(return std::move(*this);)
301 }
302 };
303
304} // namespace rerun::archetypes
305
306namespace rerun {
307 /// \private
308 template <typename T>
309 struct AsComponents;
310
311 /// \private
312 template <>
313 struct AsComponents<archetypes::Image> {
314 /// Serialize all set component batches.
315 static Result<std::vector<ComponentBatch>> serialize(const archetypes::Image& archetype);
316 };
317} // 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:91
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:72
PixelFormat
Datatype: Specifieds a particular format of an archetypes::Image.
Definition pixel_format.hpp:34
ColorModel
Datatype: Specified what color components are present in an archetypes::Image.
Definition color_model.hpp:26
@ 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
@ 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:22
size_t color_model_channel_count(datatypes::ColorModel color_model)
Returns the number of channels for a given color model.
Definition image_utils.hpp:136
Collection< TElement > borrow(const TElement *data, size_t num_instances=1)
Borrows binary data into a Collection from a pointer.
Definition collection.hpp:425
The width and height of an image.
Definition image_utils.hpp:12
Archetype: A monochrome or color image.
Definition image.hpp:127
static Image from_greyscale8(Collection< uint8_t > bytes, WidthHeight resolution)
Assumes single channel greyscale/luminance with 8-bit per value.
Definition image.hpp:238
Image(Collection< uint8_t > bytes, components::ImageFormat format_)
Construct an image from bytes and image format.
Definition image.hpp:158
Image with_opacity(rerun::components::Opacity _opacity) &&
Opacity of the image, useful for layering several images.
Definition image.hpp:288
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:209
rerun::components::ImageFormat format
The format of the image.
Definition image.hpp:132
Image(Collection< uint8_t > bytes, WidthHeight resolution, datatypes::PixelFormat pixel_format)
Construct an image from resolution, pixel format and bytes.
Definition image.hpp:178
std::optional< rerun::components::DrawOrder > draw_order
An optional floating point value that specifies the 2D drawing order.
Definition image.hpp:142
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:222
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:192
rerun::components::ImageBuffer buffer
The raw image data.
Definition image.hpp:129
std::optional< rerun::components::Opacity > opacity
Opacity of the image, useful for layering several images.
Definition image.hpp:137
static Image from_rgba32(Collection< uint8_t > bytes, WidthHeight resolution)
Assumes RGBA, 8-bit per channel, with separate alpha.
Definition image.hpp:270
Image with_draw_order(rerun::components::DrawOrder _draw_order) &&
An optional floating point value that specifies the 2D drawing order.
Definition image.hpp:297
static Image from_rgb24(Collection< uint8_t > bytes, WidthHeight resolution)
Assumes RGB, 8-bit per channel, packed as RGBRGBRGB….
Definition image.hpp:254
Component: Draw order of 2D elements.
Definition draw_order.hpp:19
Component: A buffer that is known to store image data.
Definition image_buffer.hpp:18
size_t size() const
Number of bytes.
Definition image_buffer.hpp:23
Component: The metadata describing the contents of a components::ImageBuffer.
Definition image_format.hpp:14
Indicator component used by archetypes when converting them to component lists.
Definition indicator_component.hpp:30
Component: Degree of transparency ranging from 0.0 (fully transparent) to 1.0 (fully opaque).
Definition opacity.hpp:17
size_t num_bytes() const
How many bytes will this image occupy?
Definition image_format.hpp:65