Skip to content

Commit 54df7ab

Browse files
Safer cast, cleanup and comment
1 parent b8fe22c commit 54df7ab

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the Six Labors Split License.
33

44
using System.Diagnostics.CodeAnalysis;
5-
using System.Runtime.CompilerServices;
65
using SixLabors.ImageSharp.PixelFormats;
76

87
namespace SixLabors.ImageSharp.Metadata.Profiles.Exif;
@@ -79,7 +78,7 @@ private ExifProfile(ExifProfile other)
7978

8079
this.InvalidTags = other.InvalidTags.Count > 0
8180
? new List<ExifTag>(other.InvalidTags)
82-
: (IReadOnlyList<ExifTag>)Array.Empty<ExifTag>();
81+
: Array.Empty<ExifTag>();
8382

8483
if (other.values != null)
8584
{
@@ -161,7 +160,7 @@ public bool TryCreateThumbnail<TPixel>([NotNullWhen(true)] out Image<TPixel>? im
161160
return false;
162161
}
163162

164-
using (var memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength))
163+
using (MemoryStream memStream = new(this.data, this.thumbnailOffset, this.thumbnailLength))
165164
{
166165
image = Image.Load<TPixel>(memStream);
167166
return true;
@@ -237,12 +236,12 @@ public void SetValue<TValueType>(ExifTag<TValueType> tag, TValueType value)
237236
return Array.Empty<byte>();
238237
}
239238

240-
var writer = new ExifWriter(this.values, this.Parts);
239+
ExifWriter writer = new(this.values, this.Parts);
241240
return writer.GetData();
242241
}
243242

244243
/// <inheritdoc/>
245-
public ExifProfile DeepClone() => new ExifProfile(this);
244+
public ExifProfile DeepClone() => new(this);
246245

247246
/// <summary>
248247
/// Returns the value with the specified tag.
@@ -267,6 +266,7 @@ public void SetValue<TValueType>(ExifTag<TValueType> tag, TValueType value)
267266
/// </summary>
268267
/// <param name="tag">The tag of the exif value.</param>
269268
/// <param name="value">The value.</param>
269+
/// <exception cref="NotSupportedException">Newly created value is null.</exception>
270270
internal void SetValueInternal(ExifTag tag, object? value)
271271
{
272272
foreach (IExifValue exifValue in this.Values)
@@ -281,7 +281,7 @@ internal void SetValueInternal(ExifTag tag, object? value)
281281
ExifValue? newExifValue = ExifValues.Create(tag);
282282
if (newExifValue is null)
283283
{
284-
throw new NotSupportedException();
284+
throw new NotSupportedException($"Newly created value for tag {tag} is null.");
285285
}
286286

287287
newExifValue.TrySetValue(value);
@@ -310,7 +310,7 @@ private void SyncResolution(ExifTag<Rational> tag, double resolution)
310310
this.RemoveValue(value.Tag);
311311
}
312312

313-
var newResolution = new Rational(resolution, false);
313+
Rational newResolution = new(resolution, false);
314314
this.SetValue(tag, newResolution);
315315
}
316316

@@ -328,13 +328,13 @@ private void InitializeValues()
328328
return;
329329
}
330330

331-
var reader = new ExifReader(this.data);
331+
ExifReader reader = new(this.data);
332332

333333
this.values = reader.ReadValues();
334334

335335
this.InvalidTags = reader.InvalidTags.Count > 0
336336
? new List<ExifTag>(reader.InvalidTags)
337-
: (IReadOnlyList<ExifTag>)Array.Empty<ExifTag>();
337+
: Array.Empty<ExifTag>();
338338

339339
this.thumbnailOffset = (int)reader.ThumbnailOffset;
340340
this.thumbnailLength = (int)reader.ThumbnailLength;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public ExifReader(byte[] exifData)
2222
public ExifReader(byte[] exifData, MemoryAllocator? allocator)
2323
: base(new MemoryStream(exifData ?? throw new ArgumentNullException(nameof(exifData))), allocator)
2424
{
25+
// TODO: We never call this constructor passing a non-null allocator.
2526
}
2627

2728
/// <summary>

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public byte[] GetData()
9090
if (gpsLength > 0)
9191
{
9292
i = this.WriteHeaders(this.gpsValues, result, i);
93-
i = this.WriteData(startIndex, this.gpsValues, result, i);
93+
this.WriteData(startIndex, this.gpsValues, result, i);
9494
}
9595

9696
return result;
@@ -228,9 +228,8 @@ private static bool HasValue(IExifValue exifValue)
228228
return false;
229229
}
230230

231-
if (exifValue.DataType == ExifDataType.Ascii)
231+
if (exifValue.DataType == ExifDataType.Ascii && value is string stringValue)
232232
{
233-
string stringValue = (string)value;
234233
return stringValue.Length > 0;
235234
}
236235

0 commit comments

Comments
 (0)