From c461d55101e6398f3f7f21bcc334eb06fb1a3df7 Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 25 Jul 2023 14:28:57 -0400 Subject: [PATCH] start of Exif orientation handling --- Program.cs | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/Program.cs b/Program.cs index 9b83762..36c7220 100644 --- a/Program.cs +++ b/Program.cs @@ -27,6 +27,7 @@ public class CameraInfo { public static readonly CameraInfo IPHONE_12_MINI = new(new Vector2i(4032, 3024)); } +// FIXME: switch to immediate mode?? public class Shader : IDisposable { public int Handle; private bool init = false; @@ -165,14 +166,22 @@ public class Photo { } public async void Load() { - image = await Image.LoadAsync(File); - TryParseRating(image.Metadata.XmpProfile, out Rating); - ExifProfile? exifs = image.Metadata.ExifProfile; + // We don't assign to this.image until Load() is done, because we might + // edit the image due to rotation (etc) and don't want to try generating + // a texture for it until that's already happened. + Image tmpImage = await Image.LoadAsync(File); + TryParseRating(tmpImage.Metadata.XmpProfile, out Rating); + ExifProfile? exifs = tmpImage.Metadata.ExifProfile; if (exifs != null) { - // FIXME: handle Orientation + // FIXME: when we write out images, we'll want to correct the Exif Orientation to 1. // FIXME: handle date shot / edited (and sort by shot date?) // FIXME: PixelXDimension & PixelYDimension hold the image geometry in Exif. + IExifValue? orientation; + if (exifs.TryGetValue(ExifTag.Orientation, out orientation)) { + Util.RotateImageFromExif(tmpImage, orientation.Value); + } + IExifValue? model; if (exifs.TryGetValue(ExifTag.Model, out model)) { CameraModel = model.Value ?? ""; @@ -238,6 +247,7 @@ public class Photo { // foreach (IExifValue exif in exifs.Values) { // Console.WriteLine(exif.Tag.ToString() + " " + exif.GetValue().ToString()); // } + image = tmpImage; } } @@ -374,6 +384,16 @@ public static class Util { return new((int) Math.Ceiling(width), (int) Math.Ceiling(height)); } + // https://sirv.com/help/articles/rotate-photos-to-be-upright/ + public static void RotateImageFromExif(Image image, ushort orientation) { + if (orientation == 1) { + return; + } + if (orientation == 8) { + image.Mutate(x => x.RotateFlip(RotateMode.Rotate270, FlipMode.None)); + } + } + public static Texture RenderText(string text) { return RenderText(text, 16); } @@ -542,8 +562,8 @@ public class Game : GameWindow { // Load textures from JPEGs. // string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\"); // string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\"); - // string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\"); - string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23"); + string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\"); + // string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\");