Browse Source

Add ICC profile span input

pull/2633/head
James Jackson-South 1 week ago
parent
commit
0bee54f5ba
  1. 8
      src/ImageSharp/Formats/Heif/HeifPropertyParser.cs
  2. 13
      src/ImageSharp/Metadata/Profiles/ICC/IccProfile.cs

8
src/ImageSharp/Formats/Heif/HeifPropertyParser.cs

@ -43,11 +43,9 @@ internal static class HeifPropertyParser
throw new InvalidImageContentException("The HEIF ICC color property contains an empty profile.");
}
// IccProfile retains its input array while the HEIF box-reader buffer is pooled and reused. Perform the one
// required ownership transfer here so item and sequence parsing cannot introduce additional materializations.
byte[] ownedData = GC.AllocateUninitializedArray<byte>(data.Length);
data.CopyTo(ownedData);
IccProfile profile = new(ownedData);
// The source belongs to a pooled box-reader buffer. The span constructor performs the single ownership transfer
// required for the profile to retain its exact bytes after that buffer is returned and reused.
IccProfile profile = new(data);
if (!profile.CheckIsValid())
{
throw new InvalidIccProfileException("Invalid HEIF ICC profile.");

13
src/ImageSharp/Metadata/Profiles/ICC/IccProfile.cs

@ -40,6 +40,19 @@ public sealed partial class IccProfile : IDeepCloneable<IccProfile>
/// <param name="data">The raw ICC profile data</param>
public IccProfile(byte[] data) => this.data = data;
/// <summary>
/// Initializes a new instance of the <see cref="IccProfile"/> class from raw ICC profile data whose source storage does not need to
/// remain valid for the lifetime of the profile.
/// </summary>
/// <param name="data">The raw ICC profile data.</param>
public IccProfile(ReadOnlySpan<byte> data)
{
// A span cannot transfer ownership, while IccProfile retains the exact bytes for lazy parsing and byte-for-byte serialization.
// The destination has exactly data.Length elements, so CopyTo immediately overwrites every byte of the uninitialized array.
this.data = GC.AllocateUninitializedArray<byte>(data.Length);
data.CopyTo(this.data);
}
/// <summary>
/// Initializes a new instance of the <see cref="IccProfile"/> class.
/// </summary>

Loading…
Cancel
Save