Merge pull request #827 from pravdomil/claude/import-exif-orientation

Import: stills and movies respect their orientation

# Conflicts:
#	crates/media/src/pool.rs
This commit is contained in:
Brandon Thomas
2026-10-12 03:23:08 -04:00
8 changed files with 241 additions and 14 deletions
+84
View File
@@ -0,0 +1,84 @@
//! A still's Exif orientation (a camera held sideways, a mirrored scan) is applied on import: the
//! footage has the size the picture is seen at, and its frames are the picture the right way up.
use std::sync::Arc;
use effectcraft_media::{MediaPool, probe_bytes};
use effectcraft_project::FootageKind;
use effectcraft_time::Tick;
const RED: [f32; 3] = [1.0, 0.0, 0.0];
const GREEN: [f32; 3] = [0.0, 1.0, 0.0];
const BLUE: [f32; 3] = [0.0, 0.0, 1.0];
const WHITE: [f32; 3] = [1.0, 1.0, 1.0];
/// A 32×16 JPEG with red, green / blue, white quarters, tagged with Exif orientation `tag`
/// (`None`: no Exif at all).
fn quartered_jpeg(tag: Option<u8>) -> Vec<u8> {
let img = image::RgbImage::from_fn(32, 16, |x, y| {
let c = match (x < 16, y < 8) {
(true, true) => RED,
(false, true) => GREEN,
(true, false) => BLUE,
(false, false) => WHITE,
};
image::Rgb(c.map(|v| (v * 255.0) as u8))
});
let mut jpeg = Vec::new();
image::codecs::jpeg::JpegEncoder::new_with_quality(&mut jpeg, 100).encode_image(&img).expect("encode");
let Some(tag) = tag else { return jpeg };
// APP1 "Exif": a big-endian TIFF header and one IFD holding Orientation (0x0112, SHORT).
let mut app1 = vec![0xFF, 0xE1, 0x00, 0x22];
app1.extend_from_slice(b"Exif\0\0MM\0\x2A\0\0\0\x08");
app1.extend_from_slice(&[0x00, 0x01, 0x01, 0x12, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, tag, 0x00, 0x00]);
app1.extend_from_slice(&[0x00; 4]);
// (Straight after the SOI marker.)
jpeg.splice(2..2, app1);
jpeg
}
/// The footage size and the colours of its four corners (top-left, top-right, bottom-left,
/// bottom-right), as imported.
fn imported(tag: Option<u8>) -> ((u32, u32), [[f32; 3]; 4]) {
let name = format!("oriented-{tag:?}.jpg");
let bytes: Arc<[u8]> = quartered_jpeg(tag).into();
let f = probe_bytes(&name, bytes.clone()).expect("probe");
assert_eq!(f.kind, FootageKind::Still);
let pool = MediaPool::new();
pool.add_bytes(&name, bytes);
let img = pool.frame_at(&f, Tick::ZERO).expect("frame");
assert_eq!((img.width, img.height), (f.width, f.height), "orientation {tag:?}: the frame has the probed size");
let (w, h) = (img.width as usize, img.height as usize);
let at = |x: usize, y: usize| {
let p = img.data[y * w + x];
[p[0], p[1], p[2]]
};
((f.width, f.height), [at(2, 2), at(w - 3, 2), at(2, h - 3), at(w - 3, h - 3)])
}
#[test]
fn exif_orientation_is_applied_to_stills() {
let (landscape, portrait) = ((32, 16), (16, 32));
let cases = [
(None, landscape, [RED, GREEN, BLUE, WHITE]),
(Some(1), landscape, [RED, GREEN, BLUE, WHITE]),
(Some(2), landscape, [GREEN, RED, WHITE, BLUE]),
(Some(3), landscape, [WHITE, BLUE, GREEN, RED]),
(Some(4), landscape, [BLUE, WHITE, RED, GREEN]),
(Some(5), portrait, [RED, BLUE, GREEN, WHITE]),
(Some(6), portrait, [BLUE, RED, WHITE, GREEN]),
(Some(7), portrait, [WHITE, GREEN, BLUE, RED]),
(Some(8), portrait, [GREEN, WHITE, RED, BLUE]),
// (Not an orientation: the picture as stored.)
(Some(0), landscape, [RED, GREEN, BLUE, WHITE]),
(Some(9), landscape, [RED, GREEN, BLUE, WHITE]),
];
for (tag, size, corners) in cases {
let (got_size, got) = imported(tag);
assert_eq!(got_size, size, "orientation {tag:?}: size");
for (i, (g, want)) in got.iter().zip(corners).enumerate() {
let near = g.iter().zip(want).all(|(a, b)| (a - b).abs() < 0.2);
assert!(near, "orientation {tag:?}: corner {i} is {g:?}, expected {want:?}");
}
}
}
+54 -1
View File
@@ -9,7 +9,7 @@ use std::process::Command;
use std::sync::Arc;
use std::time::Instant;
use effectcraft_media::{MediaPool, probe};
use effectcraft_media::{MediaPool, probe, probe_bytes};
use effectcraft_project::{AlphaMode, Footage, FootageKind, ItemId};
use effectcraft_raster::Image;
use effectcraft_render::FootageSource;
@@ -336,6 +336,59 @@ fn resampled_audio_read_in_frame_pieces_matches_ffmpeg() {
}
}
/// The H.264 fixture with the rotation part of its video track header's matrix (`[a b; c d]`,
/// 16.16 fixed point) replaced: what a phone writes for a movie shot with the camera turned.
fn turned_mp4(p: &Path, abcd: [i32; 4]) -> Arc<[u8]> {
let mut bytes = std::fs::read(p).expect("read");
// (The video track comes first; a version 0 `tkhd` has its matrix 44 bytes after the type.)
let at = bytes.windows(4).position(|w| w == b"tkhd").expect("tkhd");
assert_eq!(bytes[at + 4], 0, "a version 0 tkhd");
for (slot, v) in [0, 1, 3, 4].into_iter().zip(abcd) {
let o = at + 44 + slot * 4;
bytes[o..o + 4].copy_from_slice(&v.to_be_bytes());
}
bytes.into()
}
/// A movie whose track header says it is shown rotated (a phone held upright) imports the way it
/// is shown: the footage has the turned size and its frames are turned, at full and reduced size.
#[test]
fn rotated_mp4_imports_upright() {
let Some(p) = h264_mp4() else { return };
let plain_footage = probe(&p).expect("probe");
let pool = MediaPool::new();
let t = t_of(plain_footage.frame_rate, 3);
let plain = pool.frame(ItemId(1), &plain_footage, t).expect("frame");
let (w, h) = (plain.width as usize, plain.height as usize);
let one = 0x1_0000;
// The matrix, the turned size and the plain frame's pixel shown at (x, y).
type From = fn(usize, usize, usize, usize) -> (usize, usize);
let cases: [(&str, [i32; 4], (usize, usize), From); 3] = [
("90", [0, one, -one, 0], (h, w), |x, y, _, h| (y, h - 1 - x)),
("180", [-one, 0, 0, -one], (w, h), |x, y, w, h| (w - 1 - x, h - 1 - y)),
("270", [0, -one, one, 0], (h, w), |x, y, w, _| (w - 1 - y, x)),
];
for (name, matrix, (tw, th), from) in cases {
let path = format!("turned-{name}.mp4");
let bytes = turned_mp4(&p, matrix);
let f = probe_bytes(&path, bytes.clone()).expect("probe");
assert_eq!((f.width as usize, f.height as usize), (tw, th), "{name}°: size");
pool.add_bytes(&path, bytes);
let img = pool.frame(ItemId(2), &f, t).expect("frame");
assert_eq!((img.width as usize, img.height as usize), (tw, th), "{name}°: frame size");
for (x, y) in [(0, 0), (tw - 1, 0), (0, th - 1), (tw - 1, th - 1), (tw / 3, th / 5), (tw / 2, th - 7)] {
let (sx, sy) = from(x, y, w, h);
assert_eq!(img.data[y * tw + x], plain.data[sy * w + sx], "{name}°: pixel ({x}, {y})");
}
// (A reduced-resolution render asks for the turned size.)
let (hw, hh) = (f.width / 2, f.height / 2);
let sized = pool.frame_at_size(ItemId(2), &f, t, hw, hh).expect("frame");
assert_eq!((sized.width, sized.height), (hw, hh), "{name}°: reduced size");
let db = psnr(&sized, &effectcraft_raster::resample(&img, hw, hh));
assert!(db > 45.0, "{name}° at half size: {db:.2} dB");
}
}
#[test]
fn prores_4444_with_alpha() {
let Some(p) = prores_mov() else { return };