Skip to content

Commit b5c6c22

Browse files
committed
Changes from Review
* Throw InvalidImageContentException * Change GetDesctiption to TryGetDescription * Do not throw when creating exifvalue and adding it to ifdValues
1 parent ff54427 commit b5c6c22

File tree

5 files changed

+21
-14
lines changed

5 files changed

+21
-14
lines changed

src/ImageSharp/Formats/Tiff/TiffDecoderCore.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ private static int GetImageWidth(ExifProfile exifProfile)
793793
{
794794
if (!exifProfile.TryGetValue(ExifTag.ImageWidth, out IExifValue<Number> width))
795795
{
796-
TiffThrowHelper.ThrowImageFormatException("The TIFF image frame is missing the ImageWidth");
796+
TiffThrowHelper.ThrowInvalidImageContentException("The TIFF image frame is missing the ImageWidth");
797797
}
798798

799799
DebugGuard.MustBeLessThanOrEqualTo((ulong)width.Value, (ulong)int.MaxValue, nameof(ExifTag.ImageWidth));

src/ImageSharp/Formats/Tiff/TiffThrowHelper.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ internal static class TiffThrowHelper
1010
[DoesNotReturn]
1111
public static Exception ThrowImageFormatException(string errorMessage) => throw new ImageFormatException(errorMessage);
1212

13+
[DoesNotReturn]
14+
public static Exception ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);
15+
1316
[DoesNotReturn]
1417
public static Exception NotSupportedDecompressor(string compressionType) => throw new NotSupportedException($"Not supported decoder compression method: {compressionType}");
1518

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

4+
using System.Diagnostics.CodeAnalysis;
45
using System.Reflection;
56

67
namespace SixLabors.ImageSharp.Metadata.Profiles.Exif;
@@ -25,17 +26,20 @@ public ExifTagDescriptionAttribute(object value, string description)
2526
/// </summary>
2627
/// <param name="tag">The tag.</param>
2728
/// <param name="value">The value.</param>
29+
/// <param name="description">The description.</param>
2830
/// <returns>
29-
/// The <see cref="string"/>.
31+
/// True when description was found
3032
/// </returns>
31-
public static string? GetDescription(ExifTag tag, object? value)
33+
public static bool TryGetDescription(ExifTag tag, object? value, [NotNullWhen(true)] out string? description)
3234
{
33-
var tagValue = (ExifTagValue)(ushort)tag;
35+
ExifTagValue tagValue = (ExifTagValue)(ushort)tag;
3436
FieldInfo? field = typeof(ExifTagValue).GetField(tagValue.ToString(), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
3537

38+
description = null;
39+
3640
if (field is null)
3741
{
38-
return null;
42+
return false;
3943
}
4044

4145
foreach (CustomAttributeData customAttribute in field.CustomAttributes)
@@ -44,10 +48,12 @@ public ExifTagDescriptionAttribute(object value, string description)
4448

4549
if (Equals(attributeValue, value))
4650
{
47-
return (string?)customAttribute.ConstructorArguments[1].Value;
51+
description = (string?)customAttribute.ConstructorArguments[1].Value;
52+
53+
return description is not null;
4854
}
4955
}
5056

51-
return null;
57+
return false;
5258
}
5359
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,11 @@ private static int WriteInt32(int value, Span<byte> destination, int offset)
179179
}
180180

181181
ExifValue? result = ExifValues.Create(offset);
182-
Guard.NotNull(result);
183182

184-
ifdValues.Add(result);
183+
if (result is not null)
184+
{
185+
ifdValues.Add(result);
186+
}
185187

186188
return result;
187189
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,5 @@ public override bool TrySetValue(object? value)
4848
return false;
4949
}
5050

51-
public override string? ToString()
52-
{
53-
string? description = ExifTagDescriptionAttribute.GetDescription(this.Tag, this.Value);
54-
return description ?? this.StringValue;
55-
}
51+
public override string? ToString() => ExifTagDescriptionAttribute.TryGetDescription(this.Tag, this.Value, out string? description) ? description : this.StringValue;
5652
}

0 commit comments

Comments
 (0)