Skip to content

Commit

Permalink
Fix "from_rgba returned None" panic (#95)
Browse files Browse the repository at this point in the history
* clamp rgba with alpha to avoid `from_rgba` from returning None
  • Loading branch information
jonmmease authored Aug 30, 2023
1 parent 24ebf65 commit 65b342e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions vl-convert-rs/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,8 +860,18 @@ pub fn encode_png(pixmap: Pixmap, ppi: f32) -> Result<Vec<u8>, AnyError> {
// due to rounding. So we stick with this method for now.
for pixel in pixmap.pixels_mut() {
let c = pixel.demultiply();
*pixel = PremultipliedColorU8::from_rgba(c.red(), c.green(), c.blue(), c.alpha())
.expect("from_rgba returned None");
let alpha = c.alpha();

// jonmmease: tiny-skia uses the private PremultipliedColorU8::from_rgba_unchecked here,
// but we need to use from_rgba, which checks to make sure r/g/b are less then or equal
// to alpha. Use min to ensure we don't trigger the check
*pixel = PremultipliedColorU8::from_rgba(
c.red().min(alpha),
c.green().min(alpha),
c.blue().min(alpha),
alpha,
)
.expect("Failed to construct PremultipliedColorU8 from rgba");
}

let mut data = Vec::new();
Expand Down

0 comments on commit 65b342e

Please sign in to comment.