Browse Source

Changes from Review

* Throw InvalidImageContentException
* Change GetDesctiption to TryGetDescription
* Do not throw when creating exifvalue and adding it to ifdValues
pull/2320/head
Stefan Nikolei 3 years ago
parent
commit
b5c6c22c21
  1. 2
      src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs
  2. 3
      src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs
  3. 18
      src/ImageSharp/Metadata/Profiles/Exif/ExifTagDescriptionAttribute.cs
  4. 6
      src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs
  5. 6
      src/ImageSharp/Metadata/Profiles/Exif/Values/ExifValue{TValueType}.cs

2
src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

@ -793,7 +793,7 @@ internal class TiffDecoderCore : IImageDecoderInternals
{ {
if (!exifProfile.TryGetValue(ExifTag.ImageWidth, out IExifValue<Number> width)) if (!exifProfile.TryGetValue(ExifTag.ImageWidth, out IExifValue<Number> width))
{ {
TiffThrowHelper.ThrowImageFormatException("The TIFF image frame is missing the ImageWidth"); TiffThrowHelper.ThrowInvalidImageContentException("The TIFF image frame is missing the ImageWidth");
} }
DebugGuard.MustBeLessThanOrEqualTo((ulong)width.Value, (ulong)int.MaxValue, nameof(ExifTag.ImageWidth)); DebugGuard.MustBeLessThanOrEqualTo((ulong)width.Value, (ulong)int.MaxValue, nameof(ExifTag.ImageWidth));

3
src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs

@ -10,6 +10,9 @@ internal static class TiffThrowHelper
[DoesNotReturn] [DoesNotReturn]
public static Exception ThrowImageFormatException(string errorMessage) => throw new ImageFormatException(errorMessage); public static Exception ThrowImageFormatException(string errorMessage) => throw new ImageFormatException(errorMessage);
[DoesNotReturn]
public static Exception ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);
[DoesNotReturn] [DoesNotReturn]
public static Exception NotSupportedDecompressor(string compressionType) => throw new NotSupportedException($"Not supported decoder compression method: {compressionType}"); public static Exception NotSupportedDecompressor(string compressionType) => throw new NotSupportedException($"Not supported decoder compression method: {compressionType}");

18
src/ImageSharp/Metadata/Profiles/Exif/ExifTagDescriptionAttribute.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis;
using System.Reflection; using System.Reflection;
namespace SixLabors.ImageSharp.Metadata.Profiles.Exif; namespace SixLabors.ImageSharp.Metadata.Profiles.Exif;
@ -25,17 +26,20 @@ internal sealed class ExifTagDescriptionAttribute : Attribute
/// </summary> /// </summary>
/// <param name="tag">The tag.</param> /// <param name="tag">The tag.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
/// <param name="description">The description.</param>
/// <returns> /// <returns>
/// The <see cref="string"/>. /// True when description was found
/// </returns> /// </returns>
public static string? GetDescription(ExifTag tag, object? value) public static bool TryGetDescription(ExifTag tag, object? value, [NotNullWhen(true)] out string? description)
{ {
var tagValue = (ExifTagValue)(ushort)tag; ExifTagValue tagValue = (ExifTagValue)(ushort)tag;
FieldInfo? field = typeof(ExifTagValue).GetField(tagValue.ToString(), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static); FieldInfo? field = typeof(ExifTagValue).GetField(tagValue.ToString(), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
description = null;
if (field is null) if (field is null)
{ {
return null; return false;
} }
foreach (CustomAttributeData customAttribute in field.CustomAttributes) foreach (CustomAttributeData customAttribute in field.CustomAttributes)
@ -44,10 +48,12 @@ internal sealed class ExifTagDescriptionAttribute : Attribute
if (Equals(attributeValue, value)) if (Equals(attributeValue, value))
{ {
return (string?)customAttribute.ConstructorArguments[1].Value; description = (string?)customAttribute.ConstructorArguments[1].Value;
return description is not null;
} }
} }
return null; return false;
} }
} }

6
src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs

@ -179,9 +179,11 @@ internal sealed class ExifWriter
} }
ExifValue? result = ExifValues.Create(offset); ExifValue? result = ExifValues.Create(offset);
Guard.NotNull(result);
ifdValues.Add(result); if (result is not null)
{
ifdValues.Add(result);
}
return result; return result;
} }

6
src/ImageSharp/Metadata/Profiles/Exif/Values/ExifValue{TValueType}.cs

@ -48,9 +48,5 @@ internal abstract class ExifValue<TValueType> : ExifValue, IExifValue<TValueType
return false; return false;
} }
public override string? ToString() public override string? ToString() => ExifTagDescriptionAttribute.TryGetDescription(this.Tag, this.Value, out string? description) ? description : this.StringValue;
{
string? description = ExifTagDescriptionAttribute.GetDescription(this.Tag, this.Value);
return description ?? this.StringValue;
}
} }

Loading…
Cancel
Save