GraphiteEditor/Graphite · error
Failed to convert bytes to pixel
Error message
Failed to convert bytes to pixel
What it means
Pixel::from_bytes converts a byte slice into a pixel value with bytemuck::try_from_bytes, which succeeds only when the slice length exactly equals size_of::<Self>() (with matching alignment). Any other length — a truncated buffer or a channel count different from the pixel type — fails the check and panics with this message.
Source
Thrown at node-graph/libraries/no-std-types/src/color/color_traits.rs:97
pub trait Rec709Primaries {}
impl<T: Rec709Primaries> RGBPrimaries for T {
const RED: DVec2 = DVec2::new(0.64, 0.33);
const GREEN: DVec2 = DVec2::new(0.3, 0.6);
const BLUE: DVec2 = DVec2::new(0.15, 0.06);
const WHITE: DVec2 = DVec2::new(0.3127, 0.329);
}
pub trait SRGB: Rec709Primaries {}
// TODO: Come up with a better name for this trait
pub trait Pixel: Clone + Pod + Zeroable + Default {
#[cfg(feature = "std")]
fn to_bytes(&self) -> Vec<u8> {
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 byte_size() -> usize {
size_of::<Self>()
}
}
pub trait RGB: Pixel {
type ColorChannel: Channel;
fn red(&self) -> Self::ColorChannel;
fn r(&self) -> Self::ColorChannel {
self.red()
}
fn green(&self) -> Self::ColorChannel;
fn g(&self) -> Self::ColorChannel {
self.green()
}
fn blue(&self) -> Self::ColorChannel;View on GitHub (pinned to c507b35645)
Solutions
- Assert bytes.len() == Self::byte_size() before calling from_bytes
- Choose the Pixel type that matches the source's channel count and order (for example Rgba8 vs Rgb8)
- For bulk buffers, iterate chunks_exact(byte_size()) and convert element by element
Example fix
// before let pixel = P::from_bytes(bytes); // after assert_eq!(bytes.len(), P::byte_size(), "pixel buffer must be exactly one pixel wide"); let pixel = P::from_bytes(bytes);
Defensive patterns
Strategy: validation
Validate before calling
fn is_pixel_sized<P: Pixel>(bytes: &[u8]) -> bool {
bytes.len() == P::byte_size()
} Prevention
- Validate buffer length against the pixel type before every from_bytes call
- Match the pixel type to the source channel count and order exactly
- Use chunks_exact over bulk buffers instead of ad-hoc slicing
When it happens
Trigger: Feeding an RGBA (4-byte) buffer into an RGB pixel type or vice versa; slicing a buffer at a length that is not exactly the pixel size; empty slices produced by failed reads or zero-length image data.
Common situations: Converting between pixel layouts without re-interleaving channels; off-by-one stride handling when slicing image rows; deserialized buffers carrying wrong length fields.
AI-assisted analysis of GraphiteEditor/Graphite@c507b35645 (2026-08-16).
Data as JSON: /api/errors/c59e49d50fa8bf2b.
Report an issue: GitHub.