Don't panic with EXIF, resize and corrupted JPEG

This commit is contained in:
Matteo Paonessa 2024-01-25 19:02:48 +01:00
parent 59e54e508d
commit eee774f93c
1 changed files with 11 additions and 16 deletions

View File

@ -5,7 +5,8 @@ use std::panic::catch_unwind;
use std::{fs, ptr}; use std::{fs, ptr};
use image::ImageOutputFormat::Jpeg; use image::ImageOutputFormat::Jpeg;
use img_parts::{DynImage, ImageEXIF, ImageICC}; use img_parts::jpeg::Jpeg as PartsJpeg;
use img_parts::{ImageEXIF, ImageICC};
use libc::free; use libc::free;
use mozjpeg_sys::*; use mozjpeg_sys::*;
@ -236,13 +237,10 @@ unsafe fn lossy(in_file: Vec<u8>, parameters: &CSParameters) -> Result<Vec<u8>,
} }
fn extract_metadata(image: Vec<u8>) -> (Option<img_parts::Bytes>, Option<img_parts::Bytes>) { fn extract_metadata(image: Vec<u8>) -> (Option<img_parts::Bytes>, Option<img_parts::Bytes>) {
let (iccp, exif) = DynImage::from_bytes(image.into()) match PartsJpeg::from_bytes(image.into()) {
.expect("image loaded") Ok(d) => (d.icc_profile(), d.exif()),
.map_or((None, None), |dyn_image| { Err(_) => (None, None),
(dyn_image.icc_profile(), dyn_image.exif()) }
});
(iccp, exif)
} }
//TODO if image is resized, change "PixelXDimension" and "PixelYDimension" //TODO if image is resized, change "PixelXDimension" and "PixelYDimension"
@ -252,12 +250,9 @@ fn save_metadata(
exif: Option<img_parts::Bytes>, exif: Option<img_parts::Bytes>,
) -> Vec<u8> { ) -> Vec<u8> {
if iccp.is_some() || exif.is_some() { if iccp.is_some() || exif.is_some() {
let mut dyn_image = match DynImage::from_bytes(img_parts::Bytes::from(image_buffer.clone())) let mut dyn_image =
{ match PartsJpeg::from_bytes(img_parts::Bytes::from(image_buffer.clone())) {
Ok(o) => match o { Ok(d) => d,
None => return image_buffer,
Some(d) => d,
},
Err(_) => return image_buffer, Err(_) => return image_buffer,
}; };