Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Rotate and flip: drop the EXIF preview that still shows the photo unturned

SeoJCalived02fcc254804
diff --git a/src/Filee.Engines/Magick/ImageEncoder.cs b/src/Filee.Engines/Magick/ImageEncoder.cs
index bf692d80..b055c430 100644
--- a/src/Filee.Engines/Magick/ImageEncoder.cs
+++ b/src/Filee.Engines/Magick/ImageEncoder.cs
@@ -163,6 +163,7 @@ internal static class ImageEncoder
/// <summary>Applies resize / colour / metadata options to one frame.</summary>
public static void ApplyOptions(IMagickImage<byte> image, ImageOptions options, string targetFormat)
{
+ var turned = options.TurnsOrMirrors || image.Orientation is not (OrientationType.TopLeft or OrientationType.Undefined);
image.AutoOrient();
// After AutoOrient, so a phone photo turns the way it looks, not the way its pixels are stored.
@@ -183,6 +184,13 @@ internal static class ImageEncoder
image.Flop();
else if (options.Flip == ImageFlip.Vertical)
image.Flip();
+ // The small preview kept in the EXIF data still shows the picture as it was: Explorer and photo apps that
+ // use it would show it unturned. As HeicWriter does, drop it once the pixels turned.
+ if (turned && image.GetExifProfile() is { } exif && exif.ThumbnailLength > 0)
+ {
+ exif.RemoveThumbnail();
+ image.SetProfile(exif);
+ }
switch (options.Resize)
{
diff --git a/tests/Filee.Engines.Tests/ImageToolTests.cs b/tests/Filee.Engines.Tests/ImageToolTests.cs
index a3c84890..fc726d86 100644
--- a/tests/Filee.Engines.Tests/ImageToolTests.cs
+++ b/tests/Filee.Engines.Tests/ImageToolTests.cs
@@ -77,6 +77,54 @@ public class ImageToolTests(EngineFixture fx) : IClassFixture<EngineFixture>
Assert.True(result.Orientation is OrientationType.TopLeft or OrientationType.Undefined, $"{result.Orientation}");
}
+ /// <summary>
+ /// EXIF data with orientation <paramref name="orientation"/> and a small JPEG preview, as cameras and phones write
+ /// it: a TIFF header, IFD0 with the orientation, IFD1 pointing at the preview.
+ /// </summary>
+ private static ExifProfile ExifWithThumbnail(ushort orientation)
+ {
+ byte[] thumbnail;
+ using (var small = new MagickImage(MagickColors.Blue, 8, 4))
+ thumbnail = small.ToByteArray(MagickFormat.Jpeg);
+ var data = new List<byte>();
+ void U16(int v) => data.AddRange(BitConverter.GetBytes((ushort)v));
+ void U32(int v) => data.AddRange(BitConverter.GetBytes((uint)v));
+ data.AddRange("II"u8.ToArray());
+ U16(42);
+ U32(8);
+ U16(1); // IFD0 at 8: one entry
+ U16(0x0112); U16(3); U32(1); U16(orientation); U16(0);
+ U32(26); // IFD1 at 8 + 2 + 12 + 4
+ U16(2); // IFD1: the preview's offset and length
+ U16(0x0201); U16(4); U32(1); U32(26 + 2 + 24 + 4);
+ U16(0x0202); U16(4); U32(1); U32(thumbnail.Length);
+ U32(0);
+ data.AddRange(thumbnail);
+ return new ExifProfile([.. "Exif\0\0"u8.ToArray(), .. data]);
+ }
+
+ [Fact]
+ public async Task A_turned_photo_drops_the_unturned_preview_in_its_EXIF_data()
+ {
+ var dir = fx.NewFolder();
+ var source = Path.Combine(dir, "camera.jpg");
+ using (var image = Marked())
+ {
+ image.SetProfile(ExifWithThumbnail(1));
+ image.Write(source, MagickFormat.Jpeg);
+ }
+ using (var written = new MagickImage(source))
+ Assert.True(written.GetExifProfile()!.ThumbnailLength > 0); // the test photo has a preview
+
+ var job = await fx.ConvertAsync([source], Tool("rotate-right"));
+
+ Assert.Equal(JobState.Completed, job.State);
+ using var result = new MagickImage(job.Outputs.Single());
+ var exif = result.GetExifProfile();
+ Assert.NotNull(exif); // the metadata stays (KeepMetadata), only the stale preview goes
+ Assert.Equal(0u, exif.ThumbnailLength);
+ }
+
[Fact]
public async Task Every_frame_of_an_animation_turns()
{