Merge pull request #2513 from Terra854/fix/stored-raster-orientation

Preserve stored raster orientation in FigCraft
This commit is contained in:
Dylan M. Taylor
2026-11-19 19:44:17 -05:00
committed by GitHub
2 changed files with 59 additions and 2 deletions
+55
View File
@@ -16,6 +16,61 @@ fn png(w: u32, h: u32) -> Vec<u8> {
out
}
fn asymmetric_jpeg() -> Vec<u8> {
let img = image::RgbImage::from_fn(24, 16, |x, y| {
image::Rgb(match (x < 12, y < 8) {
(true, true) => [220, 30, 40],
(false, true) => [20, 190, 50],
(true, false) => [30, 60, 210],
(false, false) => [230, 200, 20],
})
});
let mut out = Vec::new();
img.write_to(&mut std::io::Cursor::new(&mut out), image::ImageFormat::Jpeg).unwrap();
out
}
/// Add one EXIF orientation tag without changing the JPEG's compressed pixels.
fn jpeg_with_orientation(jpeg: &[u8], orientation: u16) -> Vec<u8> {
assert_eq!(&jpeg[..2], &[0xff, 0xd8]);
// EXIF header, little-endian TIFF header, and an IFD containing one SHORT orientation.
let mut exif = b"Exif\0\0II\x2a\0\x08\0\0\0\x01\0\x12\x01\x03\0\x01\0\0\0".to_vec();
exif.extend_from_slice(&orientation.to_le_bytes());
exif.extend_from_slice(&[0; 6]); // Inline value padding, then no next IFD.
let mut out = jpeg[..2].to_vec();
out.extend_from_slice(&[0xff, 0xe1]);
out.extend_from_slice(&u16::try_from(exif.len() + 2).unwrap().to_be_bytes());
out.extend_from_slice(&exif);
out.extend_from_slice(&jpeg[2..]);
out
}
fn assert_jpeg_keeps_stored_pixels(orientation: u16) {
let jpeg = asymmetric_jpeg();
let oriented = jpeg_with_orientation(&jpeg, orientation);
let expected = images::decode(&jpeg).unwrap();
let actual = images::decode(&oriented).unwrap();
assert_eq!((expected.width(), expected.height()), (24, 16));
assert_eq!((actual.width(), actual.height()), (expected.width(), expected.height()));
assert_eq!(actual.data(), expected.data(), "EXIF orientation {orientation} must leave stored pixels in place");
let (expected, _) = images::decode_within(&jpeg, ColorProfile::Srgb, 12, u64::MAX).unwrap();
let (actual, _) = images::decode_within(&oriented, ColorProfile::Srgb, 12, u64::MAX).unwrap();
assert_eq!((expected.width(), expected.height()), (12, 8));
assert_eq!((actual.width(), actual.height()), (expected.width(), expected.height()));
assert_eq!(actual.data(), expected.data(), "thumbnail must keep stored pixels for EXIF orientation {orientation}");
}
#[test]
fn a_jpeg_with_exif_rotation_keeps_its_stored_pixels() {
assert_jpeg_keeps_stored_pixels(6);
}
#[test]
fn a_jpeg_with_exif_mirroring_keeps_its_stored_pixels() {
assert_jpeg_keeps_stored_pixels(2);
}
/// A PNG whose header claims `w` × `h` but which holds no pixel data at all.
fn png_header_only(w: u32, h: u32) -> Vec<u8> {
let mut ihdr = Vec::new();
+4 -2
View File
@@ -120,8 +120,10 @@ pub(crate) fn decode_within(bytes: &[u8], target: ColorProfile, max_side: u32, m
if figcraft_format::jpeg::finer_than_first(bytes) {
return None;
}
let opts =
craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits::new(u32::MAX, u32::MAX, u64::MAX, max_bytes), ..Default::default() };
let opts = craft_image_codecs::DecodeOptions {
limits: craft_image_codecs::Limits::new(u32::MAX, u32::MAX, u64::MAX, max_bytes),
keep_orientation: true,
};
let img = craft_image_codecs::decode_with(bytes, &opts).ok()?;
let source = crate::image_profile::Source::from_decoded(bytes, img.icc.as_deref());
let is_gray = source.is_gray() && img.layout().is_gray();