From 3a9192b478d72df054aa8dbb8e326089c2ebb31a Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Tue, 25 Jul 2023 09:41:13 -0400 Subject: [PATCH] parse star ratings from canon image metadata --- Program.cs | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Program.cs b/Program.cs index 6164b43..7ffcdb0 100644 --- a/Program.cs +++ b/Program.cs @@ -7,9 +7,11 @@ using OpenTK.Windowing.GraphicsLibraryFramework; using Image = SixLabors.ImageSharp.Image; using SixLabors.Fonts; using SixLabors.ImageSharp.Metadata.Profiles.Exif; +using SixLabors.ImageSharp.Metadata.Profiles.Xmp; using SixLabors.ImageSharp.Drawing.Processing; using SixLabors.ImageSharp.Drawing; using System.Runtime.CompilerServices; +using System.Xml.Linq; namespace SemiColinGames; @@ -150,6 +152,7 @@ public class Photo { public string FNumber = ""; public string ExposureTime = ""; public string IsoSpeed = ""; + public int Rating = 0; private Texture texture; private Texture placeholder; @@ -163,6 +166,7 @@ public class Photo { public async void Load() { image = await Image.LoadAsync(File); + ParseRating(image); ExifProfile? exifs = image.Metadata.ExifProfile; if (exifs != null) { // FIXME: handle Orientation @@ -236,6 +240,29 @@ public class Photo { } } + private void ParseRating(Image image) { + XmpProfile? xmp = image.Metadata.XmpProfile; + if (xmp == null) { + return; + } + XDocument? doc = xmp.GetDocument(); + if (doc == null) { + return; + } + XElement? root = doc.Root; + if (root == null) { + return; + } + foreach (XElement elt in root.Descendants()) { + if (elt.Name == "{http://ns.adobe.com/xap/1.0/}Rating") { + int rating; + if (int.TryParse(elt.Value, out rating)) { + Rating = rating; + } + } + } + } + public Texture Texture() { if (texture == placeholder && image != null) { texture = new Texture(image); @@ -512,9 +539,9 @@ public class Game : GameWindow { GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float)); // Load textures from JPEGs. - string[] files = Directory.GetFiles(@"c:\users\colin\desktop\photos-test\"); + // 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(@"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\");