toeverything/AFFiNE · error · napi::Error

failed to encode webp

Error message

failed to encode webp

What it means

The libwebp lossy encoder (webp_encode wrapper) returned failure or produced a null buffer, so the RGBA image could not be encoded to WebP output bytes; process_image_inner surfaces this as an error to the JS caller.

Source

Thrown at packages/backend/native/src/image.rs:179

      .context("failed to decode image")?,
    _ => ImageReader::with_format(Cursor::new(input), format)
      .into_decoder()
      .context("failed to decode image")?
      .orientation()
      .context("failed to decode image")?,
  })
}

fn encode_webp_lossy(image: &image::RgbaImage) -> AnyResult<Vec<u8>> {
  let width = i32::try_from(image.width()).context("image width is too large")?;
  let height = i32::try_from(image.height()).context("image height is too large")?;
  let stride = width.checked_mul(4).context("image width is too large")?;

  let mut output = std::ptr::null_mut();
  let encoded_len = unsafe { WebPEncodeRGBA(image.as_ptr(), width, height, stride, WEBP_QUALITY, &mut output) };

  if output.is_null() || encoded_len == 0 {
    bail!("failed to encode webp");
  }

  let encoded = unsafe { std::slice::from_raw_parts(output, encoded_len) }.to_vec();
  unsafe {
    WebPFree(output.cast());
  }

  Ok(encoded)
}

fn preserve_exif(input: &[u8], format: ImageFormat, output: &mut Vec<u8>) -> AnyResult<()> {
  let Some(file_type) = map_exif_file_type(format) else {
    return Ok(());
  };

  let input = input.to_vec();
  let Ok(mut metadata) = Metadata::new_from_vec(&input, file_type) else {
    return Ok(());

View on GitHub (pinned to b4c8548c09)

Solutions

  1. Retry encoding; check the input image buffer is valid.
  2. Reduce image size if the encoder runs out of memory.
Defensive patterns

Strategy: fallback

When it happens

Trigger: Raised when WebPEncodeRGBA returns a null pointer or zero length while encoding the resized RGBA image to lossy WebP.

Common situations: libwebp failed to encode the processed buffer, typically due to memory pressure or an invalid intermediate image. Retry or reduce the input size.


AI-assisted analysis of toeverything/AFFiNE@b4c8548c09 (2026-08-18). Data as JSON: /api/errors/c0d62e1f24dc67bb. Report an issue: GitHub.