From b6e091794bd287c863a39521e8813ebedb936534 Mon Sep 17 00:00:00 2001 From: dirk Date: Sun, 14 Aug 2016 13:51:04 +0200 Subject: [PATCH] Added attribute that can be used to get a description for the value of an exif tag. Former-commit-id: 3bca9a911b0189867911ef22765009909a9bbe00 Former-commit-id: 91add02607f34749757840aa946b1fe987bd234f Former-commit-id: cb69236b11426f8c1abd1928c22d0922c1554baa --- .../Profiles/Exif/ExifTag.cs | 3 ++ .../Exif/ExifTagDescriptionAttribute.cs | 49 +++++++++++++++++++ .../Profiles/Exif/ExifValue.cs | 4 ++ .../Exif/ExifTagDescriptionAttributeTests.cs | 38 ++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 src/ImageProcessorCore/Profiles/Exif/ExifTagDescriptionAttribute.cs create mode 100644 tests/ImageProcessorCore.Tests/Profiles/Exif/ExifTagDescriptionAttributeTests.cs diff --git a/src/ImageProcessorCore/Profiles/Exif/ExifTag.cs b/src/ImageProcessorCore/Profiles/Exif/ExifTag.cs index 944a2a15ad..b7318dfc40 100644 --- a/src/ImageProcessorCore/Profiles/Exif/ExifTag.cs +++ b/src/ImageProcessorCore/Profiles/Exif/ExifTag.cs @@ -159,6 +159,9 @@ namespace ImageProcessorCore /// /// ResolutionUnit /// + [ExifTagDescription((ushort)1, "None")] + [ExifTagDescription((ushort)2, "Inches")] + [ExifTagDescription((ushort)3, "Cm")] ResolutionUnit = 0x0128, /// diff --git a/src/ImageProcessorCore/Profiles/Exif/ExifTagDescriptionAttribute.cs b/src/ImageProcessorCore/Profiles/Exif/ExifTagDescriptionAttribute.cs new file mode 100644 index 0000000000..b752dcf02c --- /dev/null +++ b/src/ImageProcessorCore/Profiles/Exif/ExifTagDescriptionAttribute.cs @@ -0,0 +1,49 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore +{ + using System; + using System.Linq; + using System.Reflection; + + /// + /// Class that provides a description for an ExifTag value. + /// + [AttributeUsage(AttributeTargets.Field, AllowMultiple = true)] + internal sealed class ExifTagDescriptionAttribute : Attribute + { + private object value; + private string description; + + /// + /// Initializes a new instance of the ExifTagDescriptionAttribute class. + /// + /// The value of the exif tag. + /// The description for the value of the exif tag. + public ExifTagDescriptionAttribute(object value, string description) + { + this.value = value; + this.description = description; + } + + public static string GetDescription(ExifTag tag, object value) + { + FieldInfo field = tag.GetType().GetTypeInfo().GetDeclaredField(tag.ToString()); + if (field == null) + return null; + + foreach (CustomAttributeData customAttribute in field.CustomAttributes) + { + object attributeValue = customAttribute.ConstructorArguments[0].Value; + + if (Equals(attributeValue, value)) + return (string)customAttribute.ConstructorArguments[1].Value; + } + + return null; + } + } +} diff --git a/src/ImageProcessorCore/Profiles/Exif/ExifValue.cs b/src/ImageProcessorCore/Profiles/Exif/ExifValue.cs index 860ff30dc9..86b1d3b368 100644 --- a/src/ImageProcessorCore/Profiles/Exif/ExifValue.cs +++ b/src/ImageProcessorCore/Profiles/Exif/ExifValue.cs @@ -561,6 +561,10 @@ namespace ImageProcessorCore private string ToString(object value) { + string description = ExifTagDescriptionAttribute.GetDescription(Tag, value); + if (description != null) + return description; + switch (DataType) { case ExifDataType.Ascii: diff --git a/tests/ImageProcessorCore.Tests/Profiles/Exif/ExifTagDescriptionAttributeTests.cs b/tests/ImageProcessorCore.Tests/Profiles/Exif/ExifTagDescriptionAttributeTests.cs new file mode 100644 index 0000000000..1efb853774 --- /dev/null +++ b/tests/ImageProcessorCore.Tests/Profiles/Exif/ExifTagDescriptionAttributeTests.cs @@ -0,0 +1,38 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageProcessorCore.Tests +{ + using Xunit; + + public class ExifDescriptionAttributeTests + { + [Fact] + public void Test_ExifTag() + { + var exifProfile = new ExifProfile(); + + exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)1); + ExifValue value = exifProfile.GetValue(ExifTag.ResolutionUnit); + Assert.Equal("None", value.ToString()); + + exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)2); + value = exifProfile.GetValue(ExifTag.ResolutionUnit); + Assert.Equal("Inches", value.ToString()); + + exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)3); + value = exifProfile.GetValue(ExifTag.ResolutionUnit); + Assert.Equal("Cm", value.ToString()); + + exifProfile.SetValue(ExifTag.ResolutionUnit, (ushort)4); + value = exifProfile.GetValue(ExifTag.ResolutionUnit); + Assert.Equal("4", value.ToString()); + + exifProfile.SetValue(ExifTag.ImageWidth, 123); + value = exifProfile.GetValue(ExifTag.ImageWidth); + Assert.Equal("123", value.ToString()); + } + } +}