]> patthoyts.tk Git - srfdump/commitdiff
Declare a ExifTag type as enum. pt/dotnet-support
authorPat Thoyts <pat.thoyts@gmail.com>
Tue, 16 Dec 2025 00:15:49 +0000 (00:15 +0000)
committerPat Thoyts <pat.thoyts@gmail.com>
Tue, 16 Dec 2025 00:15:49 +0000 (00:15 +0000)
dotnet/JpegUtil.Test/JpegReaderTests.cs
dotnet/JpegUtil/Exif.cs

index 85d009e4e329451895b3c1eab97ad1b58dd4470f..ea16b8ff2067d2077f1bd1f295d74b8fc6eff93f 100644 (file)
@@ -23,6 +23,8 @@ public class JpegReaderTests
         using var reader = new JpegReader(stream, _outputhelper.WriteLine);
         var data = reader.Exif().ToList();
         data.Count.ShouldBe(12);
-        // data.Select(x => x. ShouldContain()
+        var items = data.ToDictionary(keySelector: x => x.Tag, elementSelector: x => x);
+        items.ShouldContainKey(ExifTag.ImageDescription);
+        (items[ExifTag.ImageDescription].Value as string).ShouldBe("white-light image");
     }
 }
index 46720f0017571eb4a219d531f5afea38bb173776..7426804069169469af4da2b8e212c8d14cd89c7a 100644 (file)
@@ -1,14 +1,46 @@
 using System;
 using System.Collections;
 using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
 using System.Text;
 
-using ExifTag = ushort;
-
 namespace JpegUtil;
 
+public enum ExifTag : ushort
+{
+    ImageWidth = 0x0100,
+    ImageHeight = 0x0101,
+    BitsPerSample = 0x0102,
+    Compression = 0x0103,
+    PhotometricInterpretation = 0x0106,
+    Thresholding = 0x0107,
+    ImageDescription = 0x010e,
+    Make = 0x010f,
+    Orientation = 0x0112,
+    XResolution = 0x011a,
+    YResolution = 0x011b,
+    PlanarConfiguration = 0x011c,
+    XPosition = 0x011e,
+    YPosition = 0x011f,
+    ResolutionUnit = 0x0128,
+    Software = 0x0131,
+    ModifyDate = 0x0132,
+    IFD = 0x8769, // special tag that is a pointer to more tags
+    ColorSpace = 0xa001,
+    ExifImageWidth = 0xa002,
+    ExifImageHeight = 0xa003,
+    FocalPlaneXResolution = 0xa20e,
+    FocalPlaneYResolution = 0xa20f,
+    FocalPlaneResolutionUnit = 0xa210,
+    // Renishaw WiRE Custom Tags
+    WiREPosition = 0xfea0,
+    WiREFoV = 0xfea1,
+    WiREObjective = 0xfea2,
+    WiRELUTLimits = 0xfea3,
+    WiRERotationAngle = 0xfea4,
+    WiRERotationCenter = 0xfea5,
+    WiREZPosition = 0xfea6,
+};
+
 public enum ExifType
 {
     Byte = 1, // uint8_t
@@ -31,7 +63,6 @@ public struct ExifItem
     public ExifType Type { get; private set; }
     public object Value { get; private set; }
 
-    const ExifTag EXIF_IFD_TAG = 0x8769; // special tag that is a pointer to more tags
     const ushort EXIF_IFD_SIZE = 12; // size in bytes of an IFD entry
     const ushort EXIF_USHORT_SIZE = 2;
     const ushort EXIF_BOM_BE = 0x4d4d;
@@ -71,7 +102,7 @@ public struct ExifItem
     public static IEnumerable<ExifItem> ExifParseItem(byte[] data, int off)
     {
         // uint item_count = 0;
-        ushort tag = get_uint16(data, off);
+        ExifTag tag = (ExifTag)get_uint16(data, off);
         ExifType type = (ExifType)get_uint16(data, off + 2);
         int count = (int)get_uint32(data, off + 4);
         int dataOffset = (int)get_uint32(data, off + 8);
@@ -90,7 +121,7 @@ public struct ExifItem
             {
                 if (count < 5)
                     dataOffset = hdrEnd;
-                value = Encoding.ASCII.GetString(data, dataOffset, count);
+                value = Encoding.ASCII.GetString(data, dataOffset, count).TrimEnd('\0');
                 break;
             }
             case ExifType.Short:
@@ -108,7 +139,7 @@ public struct ExifItem
             }
             case ExifType.Long:
             {
-                if (tag == EXIF_IFD_TAG)
+                if (ExifTag.IFD == tag)
                 {
                     ushort ifd_count = get_uint16(data, dataOffset);
                     dataOffset += 2;
@@ -233,7 +264,7 @@ public static class ExifExtensions
     public static string Print(this ExifItem item)
     {
         StringBuilder str = new(
-            TagNames.ContainsKey(item.Tag) ? TagNames[item.Tag] : $"{item.Tag:X4}"
+            Enum.IsDefined(typeof(ExifTag), item.Tag) ? Enum.GetName(typeof(ExifTag), item.Tag) : $"{item.Tag:X4}"
         );
         int count = 1;
         if (item.Value.IsGenericList())
@@ -257,39 +288,4 @@ public static class ExifExtensions
         }
         return str.ToString();
     }
-
-    public static readonly Dictionary<ExifTag, string> TagNames = new()
-    {
-        { 0x0100, "ImageWidth" },
-        { 0x0101, "ImageHeight" },
-        { 0x0102, "BitsPerSample" },
-        { 0x0103, "Compression" },
-        { 0x0106, "PhotometricInterpretation" },
-        { 0x0107, "Thresholding" },
-        { 0x010e, "ImageDescription" },
-        { 0x010f, "Make" },
-        { 0x0112, "Orientation" },
-        { 0x011a, "XResolution" },
-        { 0x011b, "YResolution" },
-        { 0x011c, "PlanarConfiguration" },
-        { 0x011e, "XPosition" },
-        { 0x011f, "YPosition" },
-        { 0x0128, "ResolutionUnit" },
-        { 0x0131, "Software" },
-        { 0x0132, "ModifyDate" },
-        { 0xa001, "ColorSpace" },
-        { 0xa002, "ExifImageWidth" },
-        { 0xa003, "ExifImageHeight" },
-        { 0xa20e, "FocalPlaneXResolution" },
-        { 0xa20f, "FocalPlaneYResolution" },
-        { 0xa210, "FocalPlaneResolutionUnit" },
-        /* Renishaw WiRE Custom Tags */
-        { 0xfea0, "WiREPosition" },
-        { 0xfea1, "WiREFoV" },
-        { 0xfea2, "WiREObjective" },
-        { 0xfea3, "WiRELUTLimits" },
-        { 0xfea4, "WiRERotationAngle" },
-        { 0xfea5, "WiRERotationCenter" },
-        { 0xfea6, "WiREZPosition" }
-    };
 }