diff options
| author | PgBiel <9021226+PgBiel@users.noreply.github.com> | 2025-02-10 07:39:04 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-10 10:39:04 +0000 |
| commit | 3fba256405c4aae9f121a07ddaa29cc10b825fc9 (patch) | |
| tree | a6c85d201a84ab13e305dce570e56c6a77546406 /crates/typst-library/src | |
| parent | e4f8e57c534db8a31d51e0342c46b913a7e22422 (diff) | |
Don't crash on image with zero DPI (#5835)
Diffstat (limited to 'crates/typst-library/src')
| -rw-r--r-- | crates/typst-library/src/visualize/image/raster.rs | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/crates/typst-library/src/visualize/image/raster.rs b/crates/typst-library/src/visualize/image/raster.rs index d43b1548..0883fe71 100644 --- a/crates/typst-library/src/visualize/image/raster.rs +++ b/crates/typst-library/src/visualize/image/raster.rs @@ -160,6 +160,8 @@ impl RasterImage { } /// The image's pixel density in pixels per inch, if known. + /// + /// This is guaranteed to be positive. pub fn dpi(&self) -> Option<f64> { self.0.dpi } @@ -334,6 +336,9 @@ fn apply_rotation(image: &mut DynamicImage, rotation: u32) { } /// Try to determine the DPI (dots per inch) of the image. +/// +/// This is guaranteed to be a positive value, or `None` if invalid or +/// unspecified. fn determine_dpi(data: &[u8], exif: Option<&exif::Exif>) -> Option<f64> { // Try to extract the DPI from the EXIF metadata. If that doesn't yield // anything, fall back to specialized procedures for extracting JPEG or PNG @@ -341,6 +346,7 @@ fn determine_dpi(data: &[u8], exif: Option<&exif::Exif>) -> Option<f64> { exif.and_then(exif_dpi) .or_else(|| jpeg_dpi(data)) .or_else(|| png_dpi(data)) + .filter(|&dpi| dpi > 0.0) } /// Try to get the DPI from the EXIF metadata. |
