Skip to content

Commit

Permalink
Use an sRGB-correct gray gradient when displaying grayscale images (#…
Browse files Browse the repository at this point in the history
…2014)

* Use an sRGB-correct gray gradient when displaying grayscale images

This also means using the grayscale colormap for tensors result
in a more perceptually even gradient, which is good.

* cleanup: import the correct shaders in the correct shader files

* Add explanatory comment

* Be explicit with clamp-to-edge address mode

* py-format
  • Loading branch information
emilk authored May 2, 2023
1 parent a4c7096 commit 3212976
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/re_data_store/src/entity_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl ExtraQueryHistory {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Colormap {
/// Perceptually even
/// sRGB gray gradient = perceptually even
Grayscale,

Inferno,
Expand Down
4 changes: 2 additions & 2 deletions crates/re_renderer/shader/colormap.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#import <./utils/srgb.wgsl>

// NOTE: Keep in sync with `colormap.rs`!
const COLORMAP_GRAYSCALE: u32 = 1u;
const COLORMAP_GRAYSCALE: u32 = 1u; // sRGB gradient = perceptually even
const COLORMAP_INFERNO: u32 = 2u;
const COLORMAP_MAGMA: u32 = 3u;
const COLORMAP_PLASMA: u32 = 4u;
Expand All @@ -15,7 +15,7 @@ const COLORMAP_VIRIDIS: u32 = 6u;
fn colormap_srgb(which: u32, t_unsaturated: f32) -> Vec3 {
let t = saturate(t_unsaturated);
if which == COLORMAP_GRAYSCALE {
return linear_from_srgb(Vec3(t));
return Vec3(t);
} else if which == COLORMAP_INFERNO {
return colormap_inferno_srgb(t);
} else if which == COLORMAP_MAGMA {
Expand Down
3 changes: 0 additions & 3 deletions crates/re_renderer/shader/rectangle.wgsl
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
#import <./types.wgsl>
#import <./colormap.wgsl>
#import <./global_bindings.wgsl>
#import <./utils/depth_offset.wgsl>

// Keep in sync with mirror in rectangle.rs

Expand Down
2 changes: 2 additions & 0 deletions crates/re_renderer/shader/rectangle_fs.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#import <./colormap.wgsl>
#import <./rectangle.wgsl>
#import <./utils/srgb.wgsl>

fn is_magnifying(pixel_coord: Vec2) -> bool {
return fwidth(pixel_coord.x) < 1.0;
Expand Down
1 change: 1 addition & 0 deletions crates/re_renderer/shader/rectangle_vs.wgsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#import <./rectangle.wgsl>
#import <./utils/depth_offset.wgsl>

@vertex
fn vs_main(@builtin(vertex_index) v_idx: u32) -> VertexOut {
Expand Down
3 changes: 1 addition & 2 deletions crates/re_renderer/src/colormap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use glam::{Vec2, Vec3A, Vec4, Vec4Swizzles};
#[repr(u32)]
pub enum Colormap {
// Reserve 0 for "disabled"
/// Perceptually even
/// sRGB gray gradient = perceptually even
#[default]
Grayscale = 1,
Inferno = 2,
Expand Down Expand Up @@ -59,7 +59,6 @@ pub fn colormap_srgb(which: Colormap, t: f32) -> [u8; 4] {
pub fn grayscale_srgb(t: f32) -> [u8; 4] {
debug_assert!((0.0..=1.0).contains(&t));

let t = t.powf(2.2);
let t = ((t * u8::MAX as f32) + 0.5) as u8;

[t, t, t, 255]
Expand Down
2 changes: 2 additions & 0 deletions crates/re_renderer/src/renderer/rectangles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ impl RectangleDrawData {
TextureFilterMin::Nearest => wgpu::FilterMode::Nearest,
},
mipmap_filter: wgpu::FilterMode::Nearest,
address_mode_u: wgpu::AddressMode::ClampToEdge,
address_mode_v: wgpu::AddressMode::ClampToEdge,
..Default::default()
},
);
Expand Down
2 changes: 1 addition & 1 deletion examples/python/api_demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def run_image_tensors() -> None:
"uint16",
"uint32",
"uint64",
"int8",
"int8", # produces wrap-around when casting, producing ugly images, but clipping which is not useful as a test
"int16",
"int32",
"int64",
Expand Down

0 comments on commit 3212976

Please sign in to comment.