fix: don't strip ICC from exif (#735)
* fix: don't strip ICC from exif Signed-off-by: ispik <ispik@ispik.dev> * fix: refactor ICC profile handling in image processing Signed-off-by: ispik <ispik@ispik.dev> --------- Signed-off-by: ispik <ispik@ispik.dev>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
use std::io::Cursor;
|
||||
|
||||
use crate::utils::apply_icc_profile;
|
||||
use image::{GenericImageView, ImageError, ImageReader};
|
||||
use revolt_database::Metadata;
|
||||
use revolt_files::{image_size, is_animated, video_size};
|
||||
@@ -27,16 +28,26 @@ pub fn generate_metadata(f: &NamedTempFile, mime_type: &str) -> Metadata {
|
||||
.map(|(width, height)| Metadata::Image {
|
||||
width: width as isize,
|
||||
height: height as isize,
|
||||
thumbhash: ImageReader::open(f)
|
||||
.and_then(|r| r.with_guessed_format())
|
||||
.map_err(ImageError::from)
|
||||
.and_then(|r| r.decode())
|
||||
.map(|img| img.thumbnail(100, 100))
|
||||
.map(|img| (img.dimensions(), img.to_rgba8().into_raw()))
|
||||
.map(|((width, height), rgba)| {
|
||||
thumbhash::rgba_to_thumb_hash(width as usize, height as usize, &rgba)
|
||||
})
|
||||
.ok(),
|
||||
thumbhash: (|| {
|
||||
let reader = ImageReader::open(f).ok()?.with_guessed_format().ok()?;
|
||||
let mut decoder = reader.into_decoder().ok()?;
|
||||
let icc_profile = image::ImageDecoder::icc_profile(&mut decoder)
|
||||
.ok()
|
||||
.flatten();
|
||||
let mut img = image::DynamicImage::from_decoder(decoder).ok()?;
|
||||
|
||||
if let Some(icc) = icc_profile {
|
||||
img = apply_icc_profile(img, &icc);
|
||||
}
|
||||
|
||||
let img = img.thumbnail(100, 100);
|
||||
let (width, height) = img.dimensions();
|
||||
Some(thumbhash::rgba_to_thumb_hash(
|
||||
width as usize,
|
||||
height as usize,
|
||||
&img.into_rgba8().into_raw(),
|
||||
))
|
||||
})(),
|
||||
animated: is_animated(f, mime_type).or(Some(false)),
|
||||
})
|
||||
.unwrap_or_default()
|
||||
|
||||
Reference in New Issue
Block a user