Skip to content

Commit

Permalink
Validate provided info in Encoder::with_info (image-rs#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
fintelia authored Dec 25, 2023
1 parent 8a499d9 commit d761f16
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/change-png-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn main() -> BoxResult<()> {

// Edit previous info
info_out.interlaced = info_default.interlaced;
let mut encoder = png::Encoder::with_info(w, info_out);
let mut encoder = png::Encoder::with_info(w, info_out)?;
encoder.set_depth(png_info.bit_depth);

// Edit some attribute
Expand Down
16 changes: 13 additions & 3 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,22 @@ impl<'a, W: Write> Encoder<'a, W> {
}
}

pub fn with_info(w: W, info: Info<'a>) -> Encoder<'a, W> {
Encoder {
pub fn with_info(w: W, info: Info<'a>) -> Result<Encoder<'a, W>> {
if info.animation_control.is_some() != info.frame_control.is_some() {
return Err(EncodingError::Format(FormatErrorKind::NotAnimated.into()));
}

if let Some(actl) = info.animation_control {
if actl.num_frames == 0 {
return Err(EncodingError::Format(FormatErrorKind::ZeroFrames.into()));
}
}

Ok(Encoder {
w,
info,
options: Options::default(),
}
})
}

/// Specify that the image is animated.
Expand Down

0 comments on commit d761f16

Please sign in to comment.