Import: stills respect their Exif orientation
A photo taken with the camera held sideways (or a mirrored scan) imported as stored, lying on its side. Probing now reports the size the picture is seen at, and the still and smart-object decodes apply the rotation or mirroring to the pixels. A missing or invalid tag decodes as stored. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
9a7d3e7699
commit
683ec372d9
@@ -81,7 +81,7 @@ pub(crate) fn decode(path: &str, bytes: &[u8], footage: &Footage, op: AlphaOp) -
|
||||
let inner = effectcraft_psd::Psd::parse(data.to_vec()).map_err(|e| MediaError::Decode(format!("{path} (smart object): {e}")))?;
|
||||
inner.composite().map_err(|e| MediaError::Decode(format!("{path} (smart object): {e}")))?
|
||||
} else {
|
||||
let img = image::load_from_memory(data).map_err(|e| MediaError::Decode(format!("{path} (smart object): {e}")))?;
|
||||
let img = crate::probe::load_oriented(data).map_err(|e| MediaError::Decode(format!("{path} (smart object): {e}")))?;
|
||||
let rgba = img.to_rgba32f();
|
||||
effectcraft_psd::Pixels { width: rgba.width(), height: rgba.height(), data: rgba.pixels().map(|p| p.0).collect() }
|
||||
};
|
||||
|
||||
@@ -738,7 +738,7 @@ impl Inner {
|
||||
{
|
||||
return Err(MediaError::Decode(format!("{path}: {why}")));
|
||||
}
|
||||
let img = match image::load_from_memory(&bytes) {
|
||||
let img = match crate::probe::load_oriented(&bytes) {
|
||||
Ok(img) => img,
|
||||
// A multi-layer OpenEXR file without an unnamed RGB layer: its colour layer.
|
||||
Err(e) => crate::exr_channels::cached_layered_image(path, bytes).ok_or_else(|| MediaError::Decode(format!("{path}: {e}")))?,
|
||||
|
||||
@@ -5,6 +5,7 @@ use std::sync::Arc;
|
||||
|
||||
use effectcraft_project::{AlphaMode, Footage, FootageKind};
|
||||
use effectcraft_time::{FrameRate, Tick};
|
||||
use image::metadata::Orientation;
|
||||
|
||||
use crate::{MediaError, Result, ext_of, rate_from_fc};
|
||||
|
||||
@@ -165,7 +166,12 @@ fn probe_still_bytes(path: &str, bytes: &[u8], fmt: image::ImageFormat) -> Resul
|
||||
}
|
||||
let reader = image::ImageReader::with_format(std::io::Cursor::new(bytes), fmt);
|
||||
let (w, h, has_alpha) = match reader.into_decoder() {
|
||||
Ok(dec) => (dec.dimensions().0, dec.dimensions().1, dec.color_type().has_alpha()),
|
||||
Ok(mut dec) => {
|
||||
// The size as displayed: an Exif orientation of a quarter turn swaps the sides.
|
||||
let (w, h) = dec.dimensions();
|
||||
let (w, h) = if swaps_sides(dec.orientation().unwrap_or(Orientation::NoTransforms)) { (h, w) } else { (w, h) };
|
||||
(w, h, dec.color_type().has_alpha())
|
||||
}
|
||||
Err(e) => {
|
||||
// A multi-layer OpenEXR file without an unnamed RGB layer.
|
||||
let img = (fmt == image::ImageFormat::OpenExr).then(|| crate::exr_channels::layered_image(bytes)).flatten();
|
||||
@@ -175,3 +181,20 @@ fn probe_still_bytes(path: &str, bytes: &[u8], fmt: image::ImageFormat) -> Resul
|
||||
};
|
||||
Ok(still_footage(path, w, h, fmt, has_alpha))
|
||||
}
|
||||
|
||||
/// Whether an Exif orientation turns the picture a quarter turn (its width and height trade places).
|
||||
fn swaps_sides(o: Orientation) -> bool {
|
||||
matches!(o, Orientation::Rotate90 | Orientation::Rotate270 | Orientation::Rotate90FlipH | Orientation::Rotate270FlipH)
|
||||
}
|
||||
|
||||
/// Decode a still the way it is meant to be seen: its Exif orientation (a camera held sideways, a
|
||||
/// mirrored scan) is applied to the pixels, as image viewers do. A file without
|
||||
/// the tag, or with a broken one, decodes as stored.
|
||||
pub(crate) fn load_oriented(bytes: &[u8]) -> image::ImageResult<image::DynamicImage> {
|
||||
use image::ImageDecoder;
|
||||
let mut dec = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format()?.into_decoder()?;
|
||||
let orientation = dec.orientation().unwrap_or(Orientation::NoTransforms);
|
||||
let mut img = image::DynamicImage::from_decoder(dec)?;
|
||||
img.apply_orientation(orientation);
|
||||
Ok(img)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user