shorten a bunch of exif-parsing calls

This commit is contained in:
Colin McMillen 2023-08-31 01:05:34 -04:00
parent 75ad5bd478
commit f612d0dc42
2 changed files with 41 additions and 58 deletions

View File

@ -22,73 +22,66 @@ public struct GpsInfo {
public Rational Altitude; public Rational Altitude;
public byte AltitudeRef; public byte AltitudeRef;
// GpsStatus? DateStamp and TimeStamp?
public static GpsInfo? ParseExif(ExifProfile exif) { public static GpsInfo? ParseExif(ExifProfile exif) {
GpsInfo gps; GpsInfo gps;
IExifValue<byte[]>? versionId; if (!Parse(exif, ExifTag.GPSVersionID, out gps.VersionId)) {
IExifValue<string>? status;
IExifValue<string>? datestamp;
IExifValue<Rational[]>? timestamp;
IExifValue<Rational[]>? latitude;
IExifValue<string>? latitudeRef;
IExifValue<Rational[]>? longitude;
IExifValue<string>? longitudeRef;
IExifValue<Rational>? altitude;
IExifValue<byte>? altitudeRef;
if (!exif.TryGetValue(ExifTag.GPSVersionID, out versionId)) {
return null; return null;
} }
gps.VersionId = versionId.Value ?? throw new NullReferenceException();
if (!exif.TryGetValue(ExifTag.GPSStatus, out status)) { if (!Parse(exif, ExifTag.GPSStatus, out gps.Status)) {
return null; return null;
} }
gps.Status = status.Value ?? throw new NullReferenceException();
if (!exif.TryGetValue(ExifTag.GPSDateStamp, out datestamp)) { if (!Parse(exif, ExifTag.GPSDateStamp, out gps.Datestamp)) {
return null; return null;
} }
gps.Datestamp = datestamp.Value ?? throw new NullReferenceException();
if (!exif.TryGetValue(ExifTag.GPSTimestamp, out timestamp)) { if (!Parse(exif, ExifTag.GPSTimestamp, out gps.Timestamp)) {
return null; return null;
} }
gps.Timestamp = timestamp.Value ?? throw new NullReferenceException();
if (!exif.TryGetValue(ExifTag.GPSLatitude, out latitude)) { if (!Parse(exif, ExifTag.GPSLatitude, out gps.Latitude)) {
return null; return null;
} }
gps.Latitude = latitude.Value ?? throw new NullReferenceException();
if (!exif.TryGetValue(ExifTag.GPSLatitudeRef, out latitudeRef)) { if (!Parse(exif, ExifTag.GPSLatitudeRef, out gps.LatitudeRef)) {
return null; return null;
} }
gps.LatitudeRef = latitudeRef.Value ?? throw new NullReferenceException();
if (!exif.TryGetValue(ExifTag.GPSLongitude, out longitude)) { if (!Parse(exif, ExifTag.GPSLongitude, out gps.Longitude)) {
return null; return null;
} }
gps.Longitude = longitude.Value ?? throw new NullReferenceException();
if (!exif.TryGetValue(ExifTag.GPSLongitudeRef, out longitudeRef)) { if (!Parse(exif, ExifTag.GPSLongitudeRef, out gps.LongitudeRef)) {
return null; return null;
} }
gps.LongitudeRef = longitudeRef.Value ?? throw new NullReferenceException();
if (!exif.TryGetValue(ExifTag.GPSAltitude, out altitude)) { if (!Parse(exif, ExifTag.GPSAltitude, out gps.Altitude)) {
return null; return null;
} }
gps.Altitude = altitude.Value;
if (!exif.TryGetValue(ExifTag.GPSAltitudeRef, out altitudeRef)) { if (!Parse(exif, ExifTag.GPSAltitudeRef, out gps.AltitudeRef)) {
return null; return null;
} }
gps.AltitudeRef = altitudeRef.Value;
return gps; return gps;
} }
// FIXME: use this Parse() function in Photo.ParseExif() as well?
private static bool Parse<T>(ExifProfile exif, ExifTag<T> tag, out T result) {
IExifValue<T>? data;
if (exif.TryGetValue(tag, out data)) {
if (data != null && data.Value != null) {
result = data.Value;
return true;
}
}
#pragma warning disable CS8601
result = default(T);
#pragma warning restore CS8601
return false;
}
} }
public class Photo { public class Photo {
@ -284,30 +277,25 @@ public class Photo {
return; return;
} }
IExifValue<ushort>? orientation; if (exifs.TryGetValue(ExifTag.Orientation, out var orientation)) {
if (exifs.TryGetValue(ExifTag.Orientation, out orientation)) {
Orientation = orientation.Value; Orientation = orientation.Value;
} }
IExifValue<string>? model; if (exifs.TryGetValue(ExifTag.Model, out var model)) {
if (exifs.TryGetValue(ExifTag.Model, out model)) {
CameraModel = model.Value ?? ""; CameraModel = model.Value ?? "";
} }
IExifValue<string>? lensModel; if (exifs.TryGetValue(ExifTag.LensModel, out var lensModel)) {
if (exifs.TryGetValue(ExifTag.LensModel, out lensModel)) {
LensModel = lensModel.Value ?? ""; LensModel = lensModel.Value ?? "";
ShortLensModel = GetShortLensModel(LensModel); ShortLensModel = GetShortLensModel(LensModel);
} }
IExifValue<Rational>? focalLength; if (exifs.TryGetValue(ExifTag.FocalLength, out var focalLength)) {
if (exifs.TryGetValue(ExifTag.FocalLength, out focalLength)) {
Rational r = focalLength.Value; Rational r = focalLength.Value;
FocalLength = $"{r.Numerator / r.Denominator}mm"; FocalLength = $"{r.Numerator / r.Denominator}mm";
} }
IExifValue<Rational>? fNumber; if (exifs.TryGetValue(ExifTag.FNumber, out var fNumber)) {
if (exifs.TryGetValue(ExifTag.FNumber, out fNumber)) {
Rational r = fNumber.Value; Rational r = fNumber.Value;
if (r.Numerator % r.Denominator == 0) { if (r.Numerator % r.Denominator == 0) {
FNumber = $"f/{r.Numerator / r.Denominator}"; FNumber = $"f/{r.Numerator / r.Denominator}";
@ -317,9 +305,8 @@ public class Photo {
} }
} }
// FIXME: could also show ExposureBiasValue, ExposureMode, ExposureProgram? // FIXME: could also show ExposureProgram.
IExifValue<Rational>? exposureTime; if (exifs.TryGetValue(ExifTag.ExposureTime, out var exposureTime)) {
if (exifs.TryGetValue(ExifTag.ExposureTime, out exposureTime)) {
Rational r = exposureTime.Value; Rational r = exposureTime.Value;
if (r.Numerator == 1) { if (r.Numerator == 1) {
ExposureTime = $"1/{r.Denominator}"; ExposureTime = $"1/{r.Denominator}";
@ -335,8 +322,7 @@ public class Photo {
} }
} }
IExifValue<ushort[]>? isoSpeed; if (exifs.TryGetValue(ExifTag.ISOSpeedRatings, out var isoSpeed)) {
if (exifs.TryGetValue(ExifTag.ISOSpeedRatings, out isoSpeed)) {
ushort[]? iso = isoSpeed.Value; ushort[]? iso = isoSpeed.Value;
if (iso != null) { if (iso != null) {
if (iso.Length != 1) { if (iso.Length != 1) {
@ -349,8 +335,7 @@ public class Photo {
} }
// FIXME: I think the iPhone stores time in UTC but other cameras report it in local time. // FIXME: I think the iPhone stores time in UTC but other cameras report it in local time.
IExifValue<string>? dateTimeOriginal; if (exifs.TryGetValue(ExifTag.DateTimeOriginal, out var dateTimeOriginal)) {
if (exifs.TryGetValue(ExifTag.DateTimeOriginal, out dateTimeOriginal)) {
DateTime date; DateTime date;
if (DateTime.TryParseExact( if (DateTime.TryParseExact(
dateTimeOriginal.Value ?? "", dateTimeOriginal.Value ?? "",
@ -364,15 +349,13 @@ public class Photo {
} }
} }
IExifValue<string>? subsecTimeOriginal; if (exifs.TryGetValue(ExifTag.SubsecTimeOriginal, out var subsecTimeOriginal)) {
if (exifs.TryGetValue(ExifTag.SubsecTimeOriginal, out subsecTimeOriginal)) {
double fractionalSeconds; double fractionalSeconds;
Double.TryParse("0." + subsecTimeOriginal.Value, out fractionalSeconds); Double.TryParse("0." + subsecTimeOriginal.Value, out fractionalSeconds);
DateTimeOriginal = DateTimeOriginal.AddSeconds(fractionalSeconds); DateTimeOriginal = DateTimeOriginal.AddSeconds(fractionalSeconds);
} }
IExifValue<SignedRational>? exposureBiasValue; if (exifs.TryGetValue(ExifTag.ExposureBiasValue, out var exposureBiasValue)) {
if (exifs.TryGetValue(ExifTag.ExposureBiasValue, out exposureBiasValue)) {
SignedRational r = exposureBiasValue.Value; SignedRational r = exposureBiasValue.Value;
ExposureBiasValue = r.ToString(); ExposureBiasValue = r.ToString();
if (r.Numerator >= 0) { if (r.Numerator >= 0) {
@ -426,7 +409,7 @@ public class Photo {
public string Description() { public string Description() {
string date = DateTimeOriginal.ToString("yyyy-MM-dd HH:mm:ss.ff"); string date = DateTimeOriginal.ToString("yyyy-MM-dd HH:mm:ss.ff");
return String.Format( return String.Format(
"{0,6} {1,-5} {2,-7} {3,-10} {9,-4} {7,4}x{8,-4} {4} {5,-20} {6}", "{0,6} {1,-5} {2,-7} {3,-10} EV {9,-4} {7,4}x{8,-4} {4} {5,-20} {6}",
FocalLength, FNumber, ExposureTime, IsoSpeed, date, ShortLensModel, Filename, Size.X, Size.Y, ExposureBiasValue); FocalLength, FNumber, ExposureTime, IsoSpeed, date, ShortLensModel, Filename, Size.X, Size.Y, ExposureBiasValue);
} }
} }

View File

@ -386,8 +386,8 @@ public class Game : GameWindow {
geometry = new UiGeometry(nwSettings.Size, STAR_FILLED.Size.X); geometry = new UiGeometry(nwSettings.Size, STAR_FILLED.Size.X);
} }
private static string outputRoot = @"c:\users\colin\desktop\totte-output"; // private static string outputRoot = @"c:\users\colin\desktop\totte-output";
// private static string outputRoot = @"c:\users\colin\pictures\photos"; private static string outputRoot = @"c:\users\colin\pictures\photos";
private static Texture TEXTURE_WHITE = new(new Image<Rgba32>(1, 1, new Rgba32(255, 255, 255))); private static Texture TEXTURE_WHITE = new(new Image<Rgba32>(1, 1, new Rgba32(255, 255, 255)));
private static Texture TEXTURE_BLACK = new(new Image<Rgba32>(1, 1, new Rgba32(0, 0, 0))); private static Texture TEXTURE_BLACK = new(new Image<Rgba32>(1, 1, new Rgba32(0, 0, 0)));
@ -696,8 +696,8 @@ public class Game : GameWindow {
// string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\"); // string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\14\");
// string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\23\"); // string[] files = Directory.GetFiles(@"c:\users\colin\pictures\photos\2023\07\23\");
// string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\"); // string[] files = Directory.GetFiles(@"G:\DCIM\100EOSR6\");
// string[] files = Directory.GetFiles(@"c:\users\colin\desktop\totte-output\2023\08\29"); string[] files = Directory.GetFiles(@"c:\users\colin\desktop\totte-output\2023\08\29");
string[] files = Directory.GetFiles(@"c:\users\colin\desktop\import"); // string[] files = Directory.GetFiles(@"c:\users\colin\desktop\import");
// string[] files = Directory.GetFiles(@"C:\Users\colin\Pictures\photos\2018\06\23"); // 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\Germany all\104D7000");
// string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\"); // string[] files = Directory.GetFiles(@"C:\Users\colin\Desktop\many-birds\");