Browse Source

Safer cast, cleanup and comment

pull/2320/head
James Jackson-South 3 years ago
parent
commit
54df7ab641
  1. 18
      src/ImageSharp/Metadata/Profiles/Exif/ExifProfile.cs
  2. 1
      src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs
  3. 5
      src/ImageSharp/Metadata/Profiles/Exif/ExifWriter.cs

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

@ -2,7 +2,6 @@
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Metadata.Profiles.Exif; namespace SixLabors.ImageSharp.Metadata.Profiles.Exif;
@ -79,7 +78,7 @@ public sealed class ExifProfile : IDeepCloneable<ExifProfile>
this.InvalidTags = other.InvalidTags.Count > 0 this.InvalidTags = other.InvalidTags.Count > 0
? new List<ExifTag>(other.InvalidTags) ? new List<ExifTag>(other.InvalidTags)
: (IReadOnlyList<ExifTag>)Array.Empty<ExifTag>(); : Array.Empty<ExifTag>();
if (other.values != null) if (other.values != null)
{ {
@ -161,7 +160,7 @@ public sealed class ExifProfile : IDeepCloneable<ExifProfile>
return false; return false;
} }
using (var memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength)) using (MemoryStream memStream = new(this.data, this.thumbnailOffset, this.thumbnailLength))
{ {
image = Image.Load<TPixel>(memStream); image = Image.Load<TPixel>(memStream);
return true; return true;
@ -237,12 +236,12 @@ public sealed class ExifProfile : IDeepCloneable<ExifProfile>
return Array.Empty<byte>(); return Array.Empty<byte>();
} }
var writer = new ExifWriter(this.values, this.Parts); ExifWriter writer = new(this.values, this.Parts);
return writer.GetData(); return writer.GetData();
} }
/// <inheritdoc/> /// <inheritdoc/>
public ExifProfile DeepClone() => new ExifProfile(this); public ExifProfile DeepClone() => new(this);
/// <summary> /// <summary>
/// Returns the value with the specified tag. /// Returns the value with the specified tag.
@ -267,6 +266,7 @@ public sealed class ExifProfile : IDeepCloneable<ExifProfile>
/// </summary> /// </summary>
/// <param name="tag">The tag of the exif value.</param> /// <param name="tag">The tag of the exif value.</param>
/// <param name="value">The value.</param> /// <param name="value">The value.</param>
/// <exception cref="NotSupportedException">Newly created value is null.</exception>
internal void SetValueInternal(ExifTag tag, object? value) internal void SetValueInternal(ExifTag tag, object? value)
{ {
foreach (IExifValue exifValue in this.Values) foreach (IExifValue exifValue in this.Values)
@ -281,7 +281,7 @@ public sealed class ExifProfile : IDeepCloneable<ExifProfile>
ExifValue? newExifValue = ExifValues.Create(tag); ExifValue? newExifValue = ExifValues.Create(tag);
if (newExifValue is null) if (newExifValue is null)
{ {
throw new NotSupportedException(); throw new NotSupportedException($"Newly created value for tag {tag} is null.");
} }
newExifValue.TrySetValue(value); newExifValue.TrySetValue(value);
@ -310,7 +310,7 @@ public sealed class ExifProfile : IDeepCloneable<ExifProfile>
this.RemoveValue(value.Tag); this.RemoveValue(value.Tag);
} }
var newResolution = new Rational(resolution, false); Rational newResolution = new(resolution, false);
this.SetValue(tag, newResolution); this.SetValue(tag, newResolution);
} }
@ -328,13 +328,13 @@ public sealed class ExifProfile : IDeepCloneable<ExifProfile>
return; return;
} }
var reader = new ExifReader(this.data); ExifReader reader = new(this.data);
this.values = reader.ReadValues(); this.values = reader.ReadValues();
this.InvalidTags = reader.InvalidTags.Count > 0 this.InvalidTags = reader.InvalidTags.Count > 0
? new List<ExifTag>(reader.InvalidTags) ? new List<ExifTag>(reader.InvalidTags)
: (IReadOnlyList<ExifTag>)Array.Empty<ExifTag>(); : Array.Empty<ExifTag>();
this.thumbnailOffset = (int)reader.ThumbnailOffset; this.thumbnailOffset = (int)reader.ThumbnailOffset;
this.thumbnailLength = (int)reader.ThumbnailLength; this.thumbnailLength = (int)reader.ThumbnailLength;

1
src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs

@ -22,6 +22,7 @@ internal class ExifReader : BaseExifReader
public ExifReader(byte[] exifData, MemoryAllocator? allocator) public ExifReader(byte[] exifData, MemoryAllocator? allocator)
: base(new MemoryStream(exifData ?? throw new ArgumentNullException(nameof(exifData))), allocator) : base(new MemoryStream(exifData ?? throw new ArgumentNullException(nameof(exifData))), allocator)
{ {
// TODO: We never call this constructor passing a non-null allocator.
} }
/// <summary> /// <summary>

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

@ -90,7 +90,7 @@ internal sealed class ExifWriter
if (gpsLength > 0) if (gpsLength > 0)
{ {
i = this.WriteHeaders(this.gpsValues, result, i); i = this.WriteHeaders(this.gpsValues, result, i);
i = this.WriteData(startIndex, this.gpsValues, result, i); this.WriteData(startIndex, this.gpsValues, result, i);
} }
return result; return result;
@ -228,9 +228,8 @@ internal sealed class ExifWriter
return false; return false;
} }
if (exifValue.DataType == ExifDataType.Ascii) if (exifValue.DataType == ExifDataType.Ascii && value is string stringValue)
{ {
string stringValue = (string)value;
return stringValue.Length > 0; return stringValue.Length > 0;
} }

Loading…
Cancel
Save