diff --git a/node-graph/gcore/src/raster.rs b/node-graph/gcore/src/raster.rs index 00085f767..3dac13181 100644 --- a/node-graph/gcore/src/raster.rs +++ b/node-graph/gcore/src/raster.rs @@ -102,16 +102,14 @@ pub trait Pixel: Clone + Pod + Zeroable { bytemuck::bytes_of(self).to_vec() } // TODO: use u8 for Color - fn from_bytes(bytes: &[u8]) -> &Self { - bytemuck::try_from_bytes(bytes).expect("Failed to convert bytes to pixel") + fn from_bytes(bytes: &[u8]) -> Self { + *bytemuck::try_from_bytes(bytes).expect("Failed to convert bytes to pixel") } fn byte_size() -> usize { std::mem::size_of::() } } -impl Pixel for T {} - pub trait RGB: Pixel { type ColorChannel: Channel; fn red(&self) -> Self::ColorChannel; diff --git a/node-graph/gcore/src/raster/color.rs b/node-graph/gcore/src/raster/color.rs index 9c4ea848e..01367be59 100644 --- a/node-graph/gcore/src/raster/color.rs +++ b/node-graph/gcore/src/raster/color.rs @@ -12,7 +12,7 @@ use spirv_std::num_traits::Euclid; use bytemuck::{Pod, Zeroable}; -use super::{Alpha, AssociatedAlpha, Luminance, Rec709Primaries, RGB, SRGB}; +use super::{Alpha, AssociatedAlpha, Luminance, Pixel, Rec709Primaries, RGB, SRGB}; /// Structure that represents a color. /// Internally alpha is stored as `f32` that ranges from `0.0` (transparent) to `1.0` (opaque). @@ -52,6 +52,16 @@ impl RGB for Color { } } +impl Pixel for Color { + fn to_bytes(&self) -> Vec { + self.to_rgba8_srgb().to_vec() + } + + fn from_bytes(bytes: &[u8]) -> Self { + Color::from_rgba8_srgb(bytes[0], bytes[1], bytes[2], bytes[3]) + } +} + impl Alpha for Color { type AlphaChannel = f32; fn alpha(&self) -> f32 { @@ -514,8 +524,9 @@ impl Color { /// let color = Color::from_rgbaf32(0.114, 0.103, 0.98, 0.97).unwrap(); /// //TODO: Add test /// ``` - pub fn to_rgba8(&self) -> [u8; 4] { - [(self.red * 255.) as u8, (self.green * 255.) as u8, (self.blue * 255.) as u8, (self.alpha * 255.) as u8] + pub fn to_rgba8_srgb(&self) -> [u8; 4] { + let gamma = self.to_gamma_srgb(); + [(gamma.red * 255.) as u8, (gamma.green * 255.) as u8, (gamma.blue * 255.) as u8, (gamma.alpha * 255.) as u8] } // https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/ diff --git a/node-graph/gstd/src/image_segmentation.rs b/node-graph/gstd/src/image_segmentation.rs index b0f3e2774..a75474731 100644 --- a/node-graph/gstd/src/image_segmentation.rs +++ b/node-graph/gstd/src/image_segmentation.rs @@ -5,7 +5,7 @@ use graphene_core::Node; fn apply_mask(image_frame: &mut ImageFrame, x: usize, y: usize, multiplier: u8) { let color = &mut image_frame.image.data[y * image_frame.image.width as usize + x]; - let color8 = color.to_rgba8(); + let color8 = color.to_rgba8_srgb(); *color = Color::from_rgba8_srgb(color8[0] * multiplier, color8[1] * multiplier, color8[2] * multiplier, color8[3] * multiplier); } @@ -90,7 +90,7 @@ fn convert_image_to_mask(input: &ImageFrame) -> Vec { let mut last_value = 0_usize; for (color, result) in input.image.data.iter().zip(result.iter_mut()) { - let color = color.to_rgba8(); + let color = color.to_rgba8_srgb(); if let Some(value) = colors.get(&color) { *result = *value as u8; } else {