use crate::turbo::Bgra;
pub fn jpeg_orientation(data: &[u8]) -> u8 {
exif_in_jpeg(data).and_then(tiff_orientation).filter(|o| (1..=8).contains(o)).unwrap_or(1)
}
fn exif_in_jpeg(data: &[u8]) -> Option<&[u8]> {
crate::meta::segments(data).find(|(m, body)| *m == 0xE1 && body.starts_with(b"Exif\0\0")).map(|(_, b)| &b[6..])
}
fn tiff_orientation(t: &[u8]) -> Option<u8> {
let t = crate::meta::Tiff::new(t)?;
Some(t.u16(t.entry(0x0112)? + 8)? as u8)
}
pub fn apply(b: Bgra, o: u8) -> Bgra {
if !(2..=8).contains(&o) {
return b;
}
let (w, h) = (b.width as usize, b.height as usize);
let (ow, oh) = if o >= 5 { (h, w) } else { (w, h) };
let src = b.pixels.as_chunks::<4>().0;
let mut out = Vec::with_capacity(ow * oh * 4);
for y in 0..oh {
for x in 0..ow {
let (sx, sy) = match o {
2 => (w - 1 - x, y),
3 => (w - 1 - x, h - 1 - y),
4 => (x, h - 1 - y),
5 => (y, x),
6 => (y, h - 1 - x),
7 => (w - 1 - y, h - 1 - x),
_ => (w - 1 - y, x),
};
out.extend_from_slice(&src[sy * w + sx]);
}
}
Bgra { width: ow as u32, height: oh as u32, pixels: out }
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
pub fn exif_jpeg(orientation: u16, little_endian: bool) -> Vec<u8> {
let u16b = |v: u16| if little_endian { v.to_le_bytes() } else { v.to_be_bytes() };
let u32b = |v: u32| if little_endian { v.to_le_bytes() } else { v.to_be_bytes() };
let mut tiff = if little_endian { b"II*\0".to_vec() } else { b"MM\0*".to_vec() };
tiff.extend(u32b(8));
tiff.extend(u16b(2));
for (tag, value) in [(0x010F_u16, 0u16), (0x0112, orientation)] {
tiff.extend(u16b(tag));
tiff.extend(u16b(3));
tiff.extend(u32b(1));
tiff.extend(u16b(value));
tiff.extend([0, 0]);
}
tiff.extend(u32b(0));
let mut body = b"Exif\0\0".to_vec();
body.extend(tiff);
let mut out = vec![0xFF, 0xD8, 0xFF, 0xE0, 0, 4, 0, 0, 0xFF, 0xE1];
out.extend(((body.len() + 2) as u16).to_be_bytes());
out.extend(body);
out.extend([0xFF, 0xDA, 0, 2, 0xFF, 0xD9]);
out
}
#[test]
fn reads_the_orientation_in_both_byte_orders() {
for o in 1..=8 {
assert_eq!(jpeg_orientation(&exif_jpeg(o, true)), o as u8);
assert_eq!(jpeg_orientation(&exif_jpeg(o, false)), o as u8);
}
assert_eq!(jpeg_orientation(&exif_jpeg(9, true)), 1, "out of range");
assert_eq!(jpeg_orientation(b"\xFF\xD8\xFF\xDA"), 1, "no EXIF");
assert_eq!(jpeg_orientation(b"PNG"), 1);
let mut cut = exif_jpeg(6, true);
cut.truncate(20);
assert_eq!(jpeg_orientation(&cut), 1, "truncated");
}
fn numbered() -> Bgra {
Bgra { width: 3, height: 2, pixels: (0..6u8).flat_map(|n| [n, 0, 0, 255]).collect() }
}
fn ids(b: &Bgra) -> Vec<u8> {
b.pixels.as_chunks::<4>().0.iter().map(|p| p[0]).collect()
}
#[test]
fn turns_pixels_for_every_orientation() {
let cases: [(u8, (u32, u32), &[u8]); 8] = [
(1, (3, 2), &[0, 1, 2, 3, 4, 5]),
(2, (3, 2), &[2, 1, 0, 5, 4, 3]),
(3, (3, 2), &[5, 4, 3, 2, 1, 0]),
(4, (3, 2), &[3, 4, 5, 0, 1, 2]),
(5, (2, 3), &[0, 3, 1, 4, 2, 5]),
(6, (2, 3), &[3, 0, 4, 1, 5, 2]),
(7, (2, 3), &[5, 2, 4, 1, 3, 0]),
(8, (2, 3), &[2, 5, 1, 4, 0, 3]),
];
for (o, size, want) in cases {
let b = apply(numbered(), o);
assert_eq!(((b.width, b.height), ids(&b).as_slice()), (size, want), "orientation {o}");
}
}
}