Skip to content

Commit 4b4028b

Browse files
Merge pull request #2330 from stefannikolei/stefannikolei/nullable/metadata_profiles
Remove Nullable disable from MetaData.Profiles
2 parents 4d77c68 + 1d49195 commit 4b4028b

File tree

5 files changed

+27
-53
lines changed

5 files changed

+27
-53
lines changed

src/ImageSharp/Formats/Gif/MetadataExtensions.cs

Lines changed: 2 additions & 1 deletion
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 SixLabors.ImageSharp.Formats.Gif;
56
using SixLabors.ImageSharp.Metadata;
67

@@ -37,5 +38,5 @@ public static partial class MetadataExtensions
3738
/// <returns>
3839
/// <see langword="true"/> if the gif frame metadata exists; otherwise, <see langword="false"/>.
3940
/// </returns>
40-
public static bool TryGetGifMetadata(this ImageFrameMetadata source, out GifFrameMetadata metadata) => source.TryGetFormatMetadata(GifFormat.Instance, out metadata);
41+
public static bool TryGetGifMetadata(this ImageFrameMetadata source, [NotNullWhen(true)] out GifFrameMetadata? metadata) => source.TryGetFormatMetadata(GifFormat.Instance, out metadata);
4142
}

src/ImageSharp/Metadata/ImageFrameMetadata.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using SixLabors.ImageSharp.Formats;
65
using SixLabors.ImageSharp.Metadata.Profiles.Exif;
@@ -49,22 +48,22 @@ internal ImageFrameMetadata(ImageFrameMetadata other)
4948
/// <summary>
5049
/// Gets or sets the Exif profile.
5150
/// </summary>
52-
public ExifProfile ExifProfile { get; set; }
51+
public ExifProfile? ExifProfile { get; set; }
5352

5453
/// <summary>
5554
/// Gets or sets the XMP profile.
5655
/// </summary>
57-
public XmpProfile XmpProfile { get; set; }
56+
public XmpProfile? XmpProfile { get; set; }
5857

5958
/// <summary>
6059
/// Gets or sets the ICC profile.
6160
/// </summary>
62-
public IccProfile IccProfile { get; set; }
61+
public IccProfile? IccProfile { get; set; }
6362

6463
/// <summary>
6564
/// Gets or sets the iptc profile.
6665
/// </summary>
67-
public IptcProfile IptcProfile { get; set; }
66+
public IptcProfile? IptcProfile { get; set; }
6867

6968
/// <inheritdoc/>
7069
public ImageFrameMetadata DeepClone() => new(this);
@@ -83,7 +82,7 @@ public TFormatFrameMetadata GetFormatMetadata<TFormatMetadata, TFormatFrameMetad
8382
where TFormatMetadata : class
8483
where TFormatFrameMetadata : class, IDeepCloneable
8584
{
86-
if (this.formatMetadata.TryGetValue(key, out IDeepCloneable meta))
85+
if (this.formatMetadata.TryGetValue(key, out IDeepCloneable? meta))
8786
{
8887
return (TFormatFrameMetadata)meta;
8988
}
@@ -107,11 +106,11 @@ public TFormatFrameMetadata GetFormatMetadata<TFormatMetadata, TFormatFrameMetad
107106
/// <returns>
108107
/// <see langword="true"/> if the frame metadata exists for the specified key; otherwise, <see langword="false"/>.
109108
/// </returns>
110-
public bool TryGetFormatMetadata<TFormatMetadata, TFormatFrameMetadata>(IImageFormat<TFormatMetadata, TFormatFrameMetadata> key, out TFormatFrameMetadata metadata)
109+
public bool TryGetFormatMetadata<TFormatMetadata, TFormatFrameMetadata>(IImageFormat<TFormatMetadata, TFormatFrameMetadata> key, out TFormatFrameMetadata? metadata)
111110
where TFormatMetadata : class
112111
where TFormatFrameMetadata : class, IDeepCloneable
113112
{
114-
if (this.formatMetadata.TryGetValue(key, out IDeepCloneable meta))
113+
if (this.formatMetadata.TryGetValue(key, out IDeepCloneable? meta))
115114
{
116115
metadata = (TFormatFrameMetadata)meta;
117116
return true;

src/ImageSharp/Metadata/Profiles/IPTC/IptcProfile.cs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
3-
#nullable disable
43

54
using System.Buffers.Binary;
65
using System.Collections.ObjectModel;
6+
using System.Diagnostics.CodeAnalysis;
77
using System.Globalization;
88
using System.Text;
99
using SixLabors.ImageSharp.Metadata.Profiles.IPTC;
@@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Metadata.Profiles.Iptc;
1515
/// </summary>
1616
public sealed class IptcProfile : IDeepCloneable<IptcProfile>
1717
{
18-
private Collection<IptcValue> values;
18+
private readonly Collection<IptcValue> values = new();
1919

2020
private const byte IptcTagMarkerByte = 0x1c;
2121

@@ -30,15 +30,15 @@ public sealed class IptcProfile : IDeepCloneable<IptcProfile>
3030
/// Initializes a new instance of the <see cref="IptcProfile"/> class.
3131
/// </summary>
3232
public IptcProfile()
33-
: this((byte[])null)
33+
: this((byte[]?)null)
3434
{
3535
}
3636

3737
/// <summary>
3838
/// Initializes a new instance of the <see cref="IptcProfile"/> class.
3939
/// </summary>
4040
/// <param name="data">The byte array to read the iptc profile from.</param>
41-
public IptcProfile(byte[] data)
41+
public IptcProfile(byte[]? data)
4242
{
4343
this.Data = data;
4444
this.Initialize();
@@ -53,14 +53,9 @@ private IptcProfile(IptcProfile other)
5353
{
5454
Guard.NotNull(other, nameof(other));
5555

56-
if (other.values != null)
56+
foreach (IptcValue value in other.Values)
5757
{
58-
this.values = new Collection<IptcValue>();
59-
60-
foreach (IptcValue value in other.Values)
61-
{
62-
this.values.Add(value.DeepClone());
63-
}
58+
this.values.Add(value.DeepClone());
6459
}
6560

6661
if (other.Data != null)
@@ -78,19 +73,12 @@ private IptcProfile(IptcProfile other)
7873
/// <summary>
7974
/// Gets the byte data of the IPTC profile.
8075
/// </summary>
81-
public byte[] Data { get; private set; }
76+
public byte[]? Data { get; private set; }
8277

8378
/// <summary>
8479
/// Gets the values of this iptc profile.
8580
/// </summary>
86-
public IEnumerable<IptcValue> Values
87-
{
88-
get
89-
{
90-
this.Initialize();
91-
return this.values;
92-
}
93-
}
81+
public IEnumerable<IptcValue> Values => this.values;
9482

9583
/// <inheritdoc/>
9684
public IptcProfile DeepClone() => new(this);
@@ -121,8 +109,6 @@ public List<IptcValue> GetValues(IptcTag tag)
121109
/// <returns>True when the value was found and removed.</returns>
122110
public bool RemoveValue(IptcTag tag)
123111
{
124-
this.Initialize();
125-
126112
bool removed = false;
127113
for (int i = this.values.Count - 1; i >= 0; i--)
128114
{
@@ -144,8 +130,6 @@ public bool RemoveValue(IptcTag tag)
144130
/// <returns>True when the value was found and removed.</returns>
145131
public bool RemoveValue(IptcTag tag, string value)
146132
{
147-
this.Initialize();
148-
149133
bool removed = false;
150134
for (int i = this.values.Count - 1; i >= 0; i--)
151135
{
@@ -312,13 +296,6 @@ private int WriteRecord(int offset, ReadOnlySpan<byte> recordData, IptcRecordNum
312296

313297
private void Initialize()
314298
{
315-
if (this.values != null)
316-
{
317-
return;
318-
}
319-
320-
this.values = new Collection<IptcValue>();
321-
322299
if (this.Data == null || this.Data[0] != IptcTagMarkerByte)
323300
{
324301
return;

src/ImageSharp/Metadata/Profiles/IPTC/IptcValue.cs

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

54
using System.Text;
65

@@ -22,10 +21,7 @@ internal IptcValue(IptcValue other)
2221
other.data.AsSpan().CopyTo(this.data);
2322
}
2423

25-
if (other.Encoding != null)
26-
{
27-
this.Encoding = (Encoding)other.Encoding.Clone();
28-
}
24+
this.encoding = (Encoding)other.Encoding.Clone();
2925

3026
this.Tag = other.Tag;
3127
this.Strict = other.Strict;
@@ -133,7 +129,7 @@ public string Value
133129
/// </summary>
134130
/// <param name="obj">The object to compare this <see cref="IptcValue"/> with.</param>
135131
/// <returns>True when the specified object is equal to the current <see cref="IptcValue"/>.</returns>
136-
public override bool Equals(object obj)
132+
public override bool Equals(object? obj)
137133
{
138134
if (ReferenceEquals(this, obj))
139135
{
@@ -148,7 +144,7 @@ public override bool Equals(object obj)
148144
/// </summary>
149145
/// <param name="other">The iptc value to compare this <see cref="IptcValue"/> with.</param>
150146
/// <returns>True when the specified iptc value is equal to the current <see cref="IptcValue"/>.</returns>
151-
public bool Equals(IptcValue other)
147+
public bool Equals(IptcValue? other)
152148
{
153149
if (other is null)
154150
{

src/ImageSharp/Metadata/Profiles/XMP/XmpProfile.cs

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

4+
using System.Diagnostics;
55
using System.Text;
66
using System.Xml.Linq;
77

@@ -17,15 +17,15 @@ public sealed class XmpProfile : IDeepCloneable<XmpProfile>
1717
/// Initializes a new instance of the <see cref="XmpProfile"/> class.
1818
/// </summary>
1919
public XmpProfile()
20-
: this((byte[])null)
20+
: this((byte[]?)null)
2121
{
2222
}
2323

2424
/// <summary>
2525
/// Initializes a new instance of the <see cref="XmpProfile"/> class.
2626
/// </summary>
2727
/// <param name="data">The UTF8 encoded byte array to read the XMP profile from.</param>
28-
public XmpProfile(byte[] data) => this.Data = data;
28+
public XmpProfile(byte[]? data) => this.Data = data;
2929

3030
/// <summary>
3131
/// Initializes a new instance of the <see cref="XmpProfile"/> class
@@ -42,15 +42,15 @@ private XmpProfile(XmpProfile other)
4242
/// <summary>
4343
/// Gets the XMP raw data byte array.
4444
/// </summary>
45-
internal byte[] Data { get; private set; }
45+
internal byte[]? Data { get; private set; }
4646

4747
/// <summary>
4848
/// Gets the raw XML document containing the XMP profile.
4949
/// </summary>
5050
/// <returns>The <see cref="XDocument"/></returns>
51-
public XDocument GetDocument()
51+
public XDocument? GetDocument()
5252
{
53-
byte[] byteArray = this.Data;
53+
byte[]? byteArray = this.Data;
5454
if (byteArray is null)
5555
{
5656
return null;
@@ -77,6 +77,7 @@ public XDocument GetDocument()
7777
/// <returns>The <see cref="T:Byte[]"/></returns>
7878
public byte[] ToByteArray()
7979
{
80+
Guard.NotNull(this.Data);
8081
byte[] result = new byte[this.Data.Length];
8182
this.Data.AsSpan().CopyTo(result);
8283
return result;

0 commit comments

Comments
 (0)