flxzt/rnote · error
Asserting image validity failed, invalid size or data.
Error message
Asserting image validity failed, invalid size or data.
What it means
Image::assert_valid failed because the image has zero width/height or its data buffer length does not equal 4 * pixel_width * pixel_height (RGBA8 bytes per pixel). Called before any consumer (into_imgbuf, into_encoded_bytes, to_memtexture, to_rendernode) touches the data.
Solutions
- Fix the producer of the Image so pixel_width/pixel_height match data.len()/4 and are > 0.
- Check where dimensions are computed (e.g. width_scaled as u32) and guard against 0 via max(1).
- Re-encode/re-download the source image if data arrived truncated.
- Call assert_valid() right after constructing Image to fail fast near the real bug.
Example fix
// before
let image = Image { pixel_width: w, pixel_height: h, data };
// after
assert!(w > 0 && h > 0 && data.len() as u32 == 4 * w * h, "image size/data mismatch");
let image = Image { pixel_width: w, pixel_height: h, data }; Defensive patterns
Strategy: validation
Validate before calling
// rust
fn image_dims_match(img: &Image) -> bool {
img.pixel_width > 0 && img.pixel_height > 0
&& img.data.len() as u32 == 4 * img.pixel_width * img.pixel_height
} Type guard
fn is_valid_image(img: &Image) -> bool {
img.pixel_width.checked_mul(img.pixel_height)
.map(|px| px > 0 && img.data.len() as u32 == 4 * px)
.unwrap_or(false)
} Try / catch
if let Err(e) = image.assert_valid() {
eprintln!("discarding invalid image: {e}");
return Ok(None); // or rebuild the image
} Prevention
- Always build Image from dimensions and data produced by the same source call.
- Use checked arithmetic for width*height.
- Call assert_valid immediately after construction to localize bugs.
When it happens
Trigger: Creating an Image with pixel_width or pixel_height == 0, or with a `data` byte buffer whose length mismatches width*height*4; then calling any of the into_*/to_* accessors.
Common situations: Off-by-one scaling math when generating surfaces (integer truncation to 0); clipboard/paste handlers storing partial data; serialization bugs where the byte count drifted from the declared dimensions.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Creating RgbaImage from data failed for image with…
- on-conflict behaviour is still
- Expected directory, found file
- Expected file, found directory
- Expected file with extension
AI-assisted analysis of flxzt/rnote@bbc5354502 (2026-09-08).
Data as JSON: /api/errors/470241bedc77eff3.
Report an issue: GitHub.
Appendix: source
Thrown at crates/rnote-engine/src/image.rs:190
fn rotate(&mut self, angle: f64, center: Vector2) {
self.rectangle.rotate(angle, center)
}
fn scale(&mut self, scale: Vector2) {
self.rectangle.scale(scale)
}
}
impl Image {
pub fn assert_valid(&self) -> anyhow::Result<()> {
self.rectangle.bounds().assert_valid()?;
if self.pixel_width == 0
|| self.pixel_height == 0
|| self.data.len() as u32 != 4 * self.pixel_width * self.pixel_height
{
Err(anyhow::anyhow!(
"Asserting image validity failed, invalid size or data."
))
} else {
Ok(())
}
}
pub fn try_from_encoded_bytes(bytes: &[u8]) -> Result<Self, anyhow::Error> {
let reader = ImageReader::new(io::Cursor::new(bytes)).with_guessed_format()?;
Ok(Image::from(reader.decode()?))
}
pub fn try_from_cairo_surface(
mut surface: cairo::ImageSurface,
bounds: Aabb,
) -> anyhow::Result<Self> {
let width = surface.width() as u32;
let height = surface.height() as u32;View on GitHub (pinned to bbc5354502)