flxzt/rnote · error

Creating RgbaImage from data failed for image with…

Error message

Creating RgbaImage from data failed for image with memory-format {:?}.

What it means

image::RgbaImage::from_vec returned None when wrapping the image's raw bytes into an RGBA buffer, meaning the byte vector length does not exactly match width*height*4 for the given memory format. Raised inside into_imgbuf after assert_valid's format handling.

Solutions

  1. Ensure data.len() == pixel_width*pixel_height*4 before constructing the Image (see assert_valid).
  2. If the texture has a row stride larger than width*4, strip padding row by row instead of using the raw bytes.
  3. Re-encode the image to RGBA8 with no padding upstream.
  4. Check the memory_format field matches the actual layout of data (premultiplied vs straight).

Example fix

// before
let img = image.into_imgbuf()?;
// after
let image = image.crop_to_exact_stride(); // removes row padding
let img = image.into_imgbuf()?;
Defensive patterns

Strategy: validation

Validate before calling

// rust
fn exact_rgba8_size(w: u32, h: u32, data: &[u8]) -> bool {
    (w as usize) * (h as usize) * 4 == data.len()
}

Type guard

fn has_no_row_padding(stride: usize, width: u32) -> bool {
    stride == width as usize * 4
}

Try / catch

let imgbuf = match image.into_imgbuf() {
    Ok(b) => b,
    Err(e) => { log::error!("{e}"); return Err(anyhow!("image buffer conversion failed")); }
};

Prevention

When it happens

Trigger: Calling into_imgbuf (directly or via into_encoded_bytes) on an Image whose data buffer size disagrees with pixel_width*pixel_height*4, bypassing or racing the earlier assert.

Common situations: Images constructed from GdkTexture bytes with unaligned row stride; data mutated after construction; images saved/loaded with dimension metadata mismatch.

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


AI-assisted analysis of flxzt/rnote@bbc5354502 (2026-09-08). Data as JSON: /api/errors/3ecf9238b11a82b5. Report an issue: GitHub.

Appendix: source

Thrown at crates/rnote-engine/src/image.rs:233

            pixel_height: height,
            // cairo renders to bgra8-premultiplied, but we convert it to rgba8-premultiplied
            memory_format: ImageMemoryFormat::R8g8b8a8Premultiplied,
        })
    }

    pub fn into_imgbuf(
        self,
    ) -> Result<image::ImageBuffer<image::Rgba<u8>, Vec<u8>>, anyhow::Error> {
        self.assert_valid()?;

        match self.memory_format {
            ImageMemoryFormat::R8g8b8a8Premultiplied => image::RgbaImage::from_vec(
                self.pixel_width,
                self.pixel_height,
                self.data.to_vec(),
            )
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "Creating RgbaImage from data failed for image with memory-format {:?}.",
                    self.memory_format
                )
            }),
        }
    }

    /// Encodes the image into the provided format.
    ///
    /// When the format is `Jpeg`, the quality should be provided, but falls back to 93 if it is None.
    pub fn into_encoded_bytes(
        self,
        format: image::ImageFormat,
        quality: Option<u8>,
    ) -> Result<Vec<u8>, anyhow::Error> {
        const QUALITY_FALLBACK: u8 = 93;

        self.assert_valid()?;

View on GitHub (pinned to bbc5354502)