pull ParseExif() into a standalone function

This commit is contained in:
Colin McMillen 2023-07-25 16:43:11 -04:00
parent e6d3f197ea
commit 9796827f96

View File

@ -154,6 +154,7 @@ public class Photo {
public string ExposureTime = "<unk>"; public string ExposureTime = "<unk>";
public string IsoSpeed = "<unk>"; public string IsoSpeed = "<unk>";
public int Rating = 0; public int Rating = 0;
public ushort Orientation = 1;
private Texture texture; private Texture texture;
private Texture placeholder; private Texture placeholder;
@ -170,16 +171,47 @@ public class Photo {
// edit the image due to rotation (etc) and don't want to try generating // edit the image due to rotation (etc) and don't want to try generating
// a texture for it until that's already happened. // a texture for it until that's already happened.
Image<Rgba32> tmpImage = await Image.LoadAsync<Rgba32>(File); Image<Rgba32> tmpImage = await Image.LoadAsync<Rgba32>(File);
ParseExif(tmpImage.Metadata.ExifProfile);
TryParseRating(tmpImage.Metadata.XmpProfile, out Rating); TryParseRating(tmpImage.Metadata.XmpProfile, out Rating);
ExifProfile? exifs = tmpImage.Metadata.ExifProfile; Util.RotateImageFromExif(tmpImage, Orientation);
if (exifs != null) { image = tmpImage;
}
private bool TryParseRating(XmpProfile? xmp, out int rating) {
rating = 0;
if (xmp == null) {
return false;
}
XDocument? doc = xmp.GetDocument();
if (doc == null) {
return false;
}
XElement? root = doc.Root;
if (root == null) {
return false;
}
foreach (XElement elt in root.Descendants()) {
if (elt.Name == "{http://ns.adobe.com/xap/1.0/}Rating") {
if (int.TryParse(elt.Value, out rating)) {
return true;
}
}
}
return false;
}
private void ParseExif(ExifProfile? exifs) {
if (exifs == null) {
return;
}
// FIXME: when we write out images, we'll want to correct the Exif Orientation to 1. // 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: handle date shot / edited (and sort by shot date?)
// FIXME: PixelXDimension & PixelYDimension hold the image geometry in Exif. // FIXME: PixelXDimension & PixelYDimension hold the image geometry in Exif.
IExifValue<ushort>? orientation; IExifValue<ushort>? orientation;
if (exifs.TryGetValue(ExifTag.Orientation, out orientation)) { if (exifs.TryGetValue(ExifTag.Orientation, out orientation)) {
Util.RotateImageFromExif(tmpImage, orientation.Value); Orientation = orientation.Value;
} }
IExifValue<string>? model; IExifValue<string>? model;
@ -247,31 +279,6 @@ public class Photo {
// foreach (IExifValue exif in exifs.Values) { // foreach (IExifValue exif in exifs.Values) {
// Console.WriteLine(exif.Tag.ToString() + " " + exif.GetValue().ToString()); // Console.WriteLine(exif.Tag.ToString() + " " + exif.GetValue().ToString());
// } // }
image = tmpImage;
}
}
private bool TryParseRating(XmpProfile? xmp, out int rating) {
rating = 0;
if (xmp == null) {
return false;
}
XDocument? doc = xmp.GetDocument();
if (doc == null) {
return false;
}
XElement? root = doc.Root;
if (root == null) {
return false;
}
foreach (XElement elt in root.Descendants()) {
if (elt.Name == "{http://ns.adobe.com/xap/1.0/}Rating") {
if (int.TryParse(elt.Value, out rating)) {
return true;
}
}
}
return false;
} }
public Texture Texture() { public Texture Texture() {
@ -578,6 +585,16 @@ public class Game : GameWindow {
// string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\Germany all\104D7000");
// string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\");
Console.WriteLine(DateTime.Now.ToString());
for (int i = 0; i < files.Count(); i++) {
string file = files[i];
if (file.ToLower().EndsWith(".jpg")) {
ImageInfo info = Image.Identify(file);
Console.WriteLine(file + " " + i + " " + info.Size);
}
}
Console.WriteLine(DateTime.Now.ToString());
for (int i = 0; i < files.Count(); i++) { for (int i = 0; i < files.Count(); i++) {
string file = files[i]; string file = files[i];
if (file.ToLower().EndsWith(".jpg")) { if (file.ToLower().EndsWith(".jpg")) {