-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New cross-language example snippet better demonstrating image formats (…
…#7785) ### What ![full](https://github.com/user-attachments/assets/70208860-ee0c-4196-aa2c-860e39dbe889) * Fixes #7691 Also unearthed a snippet that showed use with PIL & OpenCV which we didn't reference so far 😱 . Overlapping, but still useful on its own, so I put it into our docs as well! ### Checklist * [x] pass main ci * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using examples from latest `main` build: [rerun.io/viewer](https://rerun.io/viewer/pr/7785?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [rerun.io/viewer](https://rerun.io/viewer/pr/7785?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG * [x] If applicable, add a new check to the [release checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)! * [x] If have noted any breaking changes to the log API in `CHANGELOG.md` and the migration guide - [PR Build Summary](https://build.rerun.io/pr/7785) - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html) To run all checks from `main`, comment on the PR with `@rerun-bot full-check`.
- Loading branch information
Showing
10 changed files
with
261 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <algorithm> | ||
#include <cstdint> | ||
#include <vector> | ||
|
||
#include <rerun.hpp> | ||
|
||
int main() { | ||
const auto rec = rerun::RecordingStream("rerun_example_image_formats"); | ||
rec.spawn().exit_on_failure(); | ||
|
||
// Simple gradient image | ||
std::vector<uint8_t> image(256 * 256 * 3); | ||
for (size_t y = 0; y < 256; ++y) { | ||
for (size_t x = 0; x < 256; ++x) { | ||
image[(y * 256 + x) * 3 + 0] = static_cast<uint8_t>(x); | ||
image[(y * 256 + x) * 3 + 1] = static_cast<uint8_t>(std::min<size_t>(255, x + y)); | ||
image[(y * 256 + x) * 3 + 2] = static_cast<uint8_t>(y); | ||
} | ||
} | ||
|
||
// RGB image | ||
rec.log("image_rgb", rerun::Image::from_rgb24(image, {256, 256})); | ||
|
||
// Green channel only (Luminance) | ||
std::vector<uint8_t> green_channel(256 * 256); | ||
for (size_t i = 0; i < 256 * 256; ++i) { | ||
green_channel[i] = image[i * 3 + 1]; | ||
} | ||
rec.log( | ||
"image_green_only", | ||
rerun::Image(rerun::borrow(green_channel), {256, 256}, rerun::ColorModel::L) | ||
); | ||
|
||
// BGR image | ||
std::vector<uint8_t> bgr_image(256 * 256 * 3); | ||
for (size_t i = 0; i < 256 * 256; ++i) { | ||
bgr_image[i * 3 + 0] = image[i * 3 + 2]; | ||
bgr_image[i * 3 + 1] = image[i * 3 + 1]; | ||
bgr_image[i * 3 + 2] = image[i * 3 + 0]; | ||
} | ||
rec.log( | ||
"image_bgr", | ||
rerun::Image(rerun::borrow(bgr_image), {256, 256}, rerun::ColorModel::BGR) | ||
); | ||
|
||
// New image with Separate Y/U/V planes with 4:2:2 chroma downsampling | ||
std::vector<uint8_t> yuv_bytes(256 * 256 + 128 * 256 * 2); | ||
std::fill_n(yuv_bytes.begin(), 256 * 256, static_cast<uint8_t>(128)); // Fixed value for Y | ||
size_t u_plane_offset = 256 * 256; | ||
size_t v_plane_offset = u_plane_offset + 128 * 256; | ||
for (size_t y = 0; y < 256; ++y) { | ||
for (size_t x = 0; x < 128; ++x) { | ||
auto coord = y * 128 + x; | ||
yuv_bytes[u_plane_offset + coord] = static_cast<uint8_t>(x * 2); // Gradient for U | ||
yuv_bytes[v_plane_offset + coord] = static_cast<uint8_t>(y); // Gradient for V | ||
} | ||
} | ||
rec.log( | ||
"image_yuv422", | ||
rerun::Image(rerun::borrow(yuv_bytes), {256, 256}, rerun::PixelFormat::Y_U_V16_FullRange) | ||
); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
"""Create and log an image with various formats.""" | ||
|
||
import numpy as np | ||
import rerun as rr | ||
|
||
rr.init("rerun_example_image_formats", spawn=True) | ||
|
||
# Simple gradient image, logged in different formats. | ||
image = np.array([[[x, min(255, x + y), y] for x in range(0, 256)] for y in range(0, 256)], dtype=np.uint8) | ||
rr.log("image_rgb", rr.Image(image)) | ||
rr.log("image_green_only", rr.Image(image[:, :, 1], color_model="l")) # Luminance only | ||
rr.log("image_bgr", rr.Image(image[:, :, ::-1], color_model="bgr")) # BGR | ||
|
||
# New image with Separate Y/U/V planes with 4:2:2 chroma downsampling | ||
y = bytes([128 for y in range(0, 256) for x in range(0, 256)]) | ||
u = bytes([x * 2 for y in range(0, 256) for x in range(0, 128)]) # Half horizontal resolution for chroma. | ||
v = bytes([y for y in range(0, 256) for x in range(0, 128)]) | ||
rr.log("image_yuv422", rr.Image(bytes=y + u + v, width=256, height=256, pixel_format=rr.PixelFormat.Y_U_V16_FullRange)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use rerun::external::ndarray; | ||
|
||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let rec = rerun::RecordingStreamBuilder::new("rerun_example_image_formats").spawn()?; | ||
|
||
// Simple gradient image | ||
let image = ndarray::Array3::from_shape_fn((256, 256, 3), |(y, x, c)| match c { | ||
0 => x as u8, | ||
1 => (x + y).min(255) as u8, | ||
2 => y as u8, | ||
_ => unreachable!(), | ||
}); | ||
|
||
// RGB image | ||
rec.log( | ||
"image_rgb", | ||
&rerun::Image::from_color_model_and_tensor(rerun::ColorModel::RGB, image.clone())?, | ||
)?; | ||
|
||
// Green channel only (Luminance) | ||
rec.log( | ||
"image_green_only", | ||
&rerun::Image::from_color_model_and_tensor( | ||
rerun::ColorModel::L, | ||
image.slice(ndarray::s![.., .., 1]).to_owned(), | ||
)?, | ||
)?; | ||
|
||
// BGR image | ||
rec.log( | ||
"image_bgr", | ||
&rerun::Image::from_color_model_and_tensor( | ||
rerun::ColorModel::BGR, | ||
image.slice(ndarray::s![.., .., ..;-1]).to_owned(), | ||
)?, | ||
)?; | ||
|
||
// New image with Separate Y/U/V planes with 4:2:2 chroma downsampling | ||
let mut yuv_bytes = Vec::with_capacity(256 * 256 + 128 * 256 * 2); | ||
yuv_bytes.extend(std::iter::repeat(128).take(256 * 256)); // Fixed value for Y. | ||
yuv_bytes.extend((0..256).flat_map(|_y| (0..128).map(|x| x * 2))); // Gradient for U. | ||
yuv_bytes.extend((0..256).flat_map(|y| std::iter::repeat(y as u8).take(128))); // Gradient for V. | ||
rec.log( | ||
"image_yuv422", | ||
&rerun::Image::from_pixel_format( | ||
[256, 256], | ||
rerun::PixelFormat::Y_U_V16_FullRange, | ||
yuv_bytes, | ||
), | ||
)?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.