Browse Source

Merge in required changes from upstream

pull/2633/head
Ynse Hoornenborg 2 years ago
parent
commit
b8322fc554
  1. 17
      src/ImageSharp/Formats/Heif/HeifDecoderCore.cs
  2. 4
      src/ImageSharp/Formats/Heif/HeifEncoderCore.cs
  3. 40
      src/ImageSharp/Formats/Heif/HeifMetadata.cs
  4. 2
      src/ImageSharp/ImageSharp.csproj
  5. 2
      tests/ImageSharp.Tests/Formats/Heif/HeifEncoderTests.cs

17
src/ImageSharp/Formats/Heif/HeifDecoderCore.cs

@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Formats.Heif;
/// <summary> /// <summary>
/// Performs the HEIF decoding operation. /// Performs the HEIF decoding operation.
/// </summary> /// </summary>
internal sealed class HeifDecoderCore : IImageDecoderInternals internal sealed class HeifDecoderCore : ImageDecoderCore
{ {
/// <summary> /// <summary>
/// The general configuration. /// The general configuration.
@ -42,8 +42,8 @@ internal sealed class HeifDecoderCore : IImageDecoderInternals
/// </summary> /// </summary>
/// <param name="options">The decoder options.</param> /// <param name="options">The decoder options.</param>
public HeifDecoderCore(DecoderOptions options) public HeifDecoderCore(DecoderOptions options)
: base(options)
{ {
this.Options = options;
this.configuration = options.Configuration; this.configuration = options.Configuration;
this.metadata = new ImageMetadata(); this.metadata = new ImageMetadata();
this.items = new List<HeifItem>(); this.items = new List<HeifItem>();
@ -51,14 +51,7 @@ internal sealed class HeifDecoderCore : IImageDecoderInternals
} }
/// <inheritdoc/> /// <inheritdoc/>
public DecoderOptions Options { get; } protected override Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
/// <inheritdoc/>
public Size Dimensions { get; }
/// <inheritdoc/>
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{ {
if (!this.CheckFileTypeBox(stream)) if (!this.CheckFileTypeBox(stream))
{ {
@ -106,7 +99,7 @@ internal sealed class HeifDecoderCore : IImageDecoderInternals
} }
/// <inheritdoc/> /// <inheritdoc/>
public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken) protected override ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken)
{ {
this.CheckFileTypeBox(stream); this.CheckFileTypeBox(stream);
@ -134,7 +127,7 @@ internal sealed class HeifDecoderCore : IImageDecoderInternals
this.UpdateMetadata(this.metadata, item); this.UpdateMetadata(this.metadata, item);
return new ImageInfo(new PixelTypeInfo(item.BitsPerPixel), new(item.Extent.Width, item.Extent.Height), this.metadata); return new ImageInfo(new(item.Extent.Width, item.Extent.Height), this.metadata);
} }
private bool CheckFileTypeBox(BufferedReadStream stream) private bool CheckFileTypeBox(BufferedReadStream stream)

4
src/ImageSharp/Formats/Heif/HeifEncoderCore.cs

@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Heif;
/// <summary> /// <summary>
/// Image encoder for writing an image to a stream as a HEIF image. /// Image encoder for writing an image to a stream as a HEIF image.
/// </summary> /// </summary>
internal sealed class HeifEncoderCore : IImageEncoderInternals internal sealed class HeifEncoderCore
{ {
/// <summary> /// <summary>
/// The global configuration. /// The global configuration.
@ -326,7 +326,7 @@ internal sealed class HeifEncoderCore : IImageEncoderInternals
using MemoryStream stream = new(); using MemoryStream stream = new();
JpegEncoder encoder = new() JpegEncoder encoder = new()
{ {
ColorType = JpegEncodingColor.YCbCrRatio420 ColorType = JpegColorType.YCbCrRatio420
}; };
await image.SaveAsJpegAsync(stream, encoder, cancellationToken); await image.SaveAsJpegAsync(stream, encoder, cancellationToken);
return stream.ToArray(); return stream.ToArray();

40
src/ImageSharp/Formats/Heif/HeifMetadata.cs

@ -1,12 +1,14 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Formats.Heif; namespace SixLabors.ImageSharp.Formats.Heif;
/// <summary> /// <summary>
/// Provides HEIF specific metadata information for the image. /// Provides HEIF specific metadata information for the image.
/// </summary> /// </summary>
public class HeifMetadata : IDeepCloneable public class HeifMetadata : IFormatMetadata<HeifMetadata>
{ {
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="HeifMetadata"/> class. /// Initializes a new instance of the <see cref="HeifMetadata"/> class.
@ -28,5 +30,39 @@ public class HeifMetadata : IDeepCloneable
public HeifCompressionMethod CompressionMethod { get; set; } public HeifCompressionMethod CompressionMethod { get; set; }
/// <inheritdoc/> /// <inheritdoc/>
public IDeepCloneable DeepClone() => new HeifMetadata(this); public static HeifMetadata FromFormatConnectingMetadata(FormatConnectingMetadata metadata)
{
return new HeifMetadata
{
CompressionMethod = HeifCompressionMethod.LegacyJpeg
};
}
/// <inheritdoc/>
public PixelTypeInfo GetPixelTypeInfo()
{
int bpp = 8;
PixelColorType colorType = PixelColorType.RGB;
PixelComponentInfo info = PixelComponentInfo.Create(3, bpp, 8, 8, 8);
return new PixelTypeInfo(bpp)
{
AlphaRepresentation = PixelAlphaRepresentation.None,
ColorType = colorType,
ComponentInfo = info,
};
}
/// <inheritdoc/>
public FormatConnectingMetadata ToFormatConnectingMetadata()
=> new()
{
PixelTypeInfo = this.GetPixelTypeInfo(),
};
/// <inheritdoc/>
IDeepCloneable IDeepCloneable.DeepClone() => this.DeepClone();
/// <inheritdoc/>
public HeifMetadata DeepClone() => new(this);
} }

2
src/ImageSharp/ImageSharp.csproj

@ -56,6 +56,7 @@
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
<DependentUpon>Heif4CharCode.tt</DependentUpon> <DependentUpon>Heif4CharCode.tt</DependentUpon>
</Compile>
<Compile Update="Formats\_Generated\ImageMetadataExtensions.cs"> <Compile Update="Formats\_Generated\ImageMetadataExtensions.cs">
<DesignTime>True</DesignTime> <DesignTime>True</DesignTime>
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>
@ -162,6 +163,7 @@
<None Update="Formats\Heif\Heif4CharCode.tt"> <None Update="Formats\Heif\Heif4CharCode.tt">
<Generator>TextTemplatingFileGenerator</Generator> <Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Heif4CharCode.cs</LastGenOutput> <LastGenOutput>Heif4CharCode.cs</LastGenOutput>
</None>
<None Update="Formats\_Generated\ImageMetadataExtensions.tt"> <None Update="Formats\_Generated\ImageMetadataExtensions.tt">
<LastGenOutput>ImageMetadataExtensions.cs</LastGenOutput> <LastGenOutput>ImageMetadataExtensions.cs</LastGenOutput>
<Generator>TextTemplatingFileGenerator</Generator> <Generator>TextTemplatingFileGenerator</Generator>

2
tests/ImageSharp.Tests/Formats/Heif/HeifEncoderTests.cs

@ -17,7 +17,7 @@ public class HeifEncoderTests
public static void Encode<TPixel>(TestImageProvider<TPixel> provider, HeifCompressionMethod compressionMethod) public static void Encode<TPixel>(TestImageProvider<TPixel> provider, HeifCompressionMethod compressionMethod)
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
using Image<TPixel> image = provider.GetImage(new MagickReferenceDecoder()); using Image<TPixel> image = provider.GetImage(new MagickReferenceDecoder(HeifFormat.Instance));
using MemoryStream stream = new(); using MemoryStream stream = new();
HeifEncoder encoder = new(); HeifEncoder encoder = new();
image.Save(stream, encoder); image.Save(stream, encoder);

Loading…
Cancel
Save