diff --git a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs index 87784d1cb4..8333dbf31c 100644 --- a/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs +++ b/src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs @@ -793,7 +793,7 @@ internal class TiffDecoderCore : IImageDecoderInternals { if (!exifProfile.TryGetValue(ExifTag.ImageWidth, out IExifValue 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)); diff --git a/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs b/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs index 3957ccb02f..4a03bd44f6 100644 --- a/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs +++ b/src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs @@ -10,6 +10,9 @@ internal static class TiffThrowHelper [DoesNotReturn] public static Exception ThrowImageFormatException(string errorMessage) => throw new ImageFormatException(errorMessage); + [DoesNotReturn] + public static Exception ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage); + [DoesNotReturn] public static Exception NotSupportedDecompressor(string compressionType) => throw new NotSupportedException($"Not supported decoder compression method: {compressionType}"); diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifTagDescriptionAttribute.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifTagDescriptionAttribute.cs index da5214087f..7a520343af 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifTagDescriptionAttribute.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifTagDescriptionAttribute.cs @@ -1,6 +1,7 @@ // Copyright (c) Six Labors. // Licensed under the Six Labors Split License. +using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace SixLabors.ImageSharp.Metadata.Profiles.Exif; @@ -25,17 +26,20 @@ internal sealed class ExifTagDescriptionAttribute : Attribute /// /// The tag. /// The value. + /// The description. /// - /// The . + /// True when description was found /// - 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); + description = null; + if (field is null) { - return null; + return false; } foreach (CustomAttributeData customAttribute in field.CustomAttributes) @@ -44,10 +48,12 @@ internal sealed class ExifTagDescriptionAttribute : Attribute 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; } } diff --git a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs index 26442b2c41..76fb2ff39a 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs @@ -179,9 +179,11 @@ internal sealed class ExifWriter } ExifValue? result = ExifValues.Create(offset); - Guard.NotNull(result); - ifdValues.Add(result); + if (result is not null) + { + ifdValues.Add(result); + } return result; } diff --git a/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifValue{TValueType}.cs b/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifValue{TValueType}.cs index 6442f26846..e7a2fa5e5d 100644 --- a/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifValue{TValueType}.cs +++ b/src/ImageSharp/Metadata/Profiles/Exif/Values/ExifValue{TValueType}.cs @@ -48,9 +48,5 @@ internal abstract class ExifValue : ExifValue, IExifValue ExifTagDescriptionAttribute.TryGetDescription(this.Tag, this.Value, out string? description) ? description : this.StringValue; }