mirror of https://github.com/SixLabors/ImageSharp
16 changed files with 387 additions and 1 deletions
@ -0,0 +1,18 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Registers the image encoders, decoders and mime type detectors for the HEIC format.
|
|||
/// </summary>
|
|||
public sealed class HeicConfigurationModule : IImageFormatConfigurationModule |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public void Configure(Configuration configuration) |
|||
{ |
|||
configuration.ImageFormatsManager.SetEncoder(HeicFormat.Instance, new HeicEncoder()); |
|||
configuration.ImageFormatsManager.SetDecoder(HeicFormat.Instance, HeicDecoder.Instance); |
|||
configuration.ImageFormatsManager.AddImageFormatDetector(new HeicImageFormatDetector()); |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Contains HEIC (and H.265) constant values defined in the specification.
|
|||
/// </summary>
|
|||
internal static class HeicConstants |
|||
{ |
|||
/// <summary>
|
|||
/// The list of mimetypes that equate to a HEIC.
|
|||
/// </summary>
|
|||
public static readonly IEnumerable<string> MimeTypes = new[] { "image/heif", "image/heif-sequence", "image/heic", "image/heic-sequence", "image/avif" }; |
|||
|
|||
/// <summary>
|
|||
/// The list of file extensions that equate to a HEIC.
|
|||
/// </summary>
|
|||
public static readonly IEnumerable<string> FileExtensions = new[] { "heic", "heif", "avif" }; |
|||
} |
|||
@ -0,0 +1,45 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Image decoder for reading HEIC images from a stream.
|
|||
public sealed class HeicDecoder : ImageDecoder |
|||
{ |
|||
private HeicDecoder() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the shared instance.
|
|||
/// </summary>
|
|||
public static HeicDecoder Instance { get; } = new(); |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override ImageInfo Identify(DecoderOptions options, Stream stream, CancellationToken cancellationToken) |
|||
{ |
|||
Guard.NotNull(options, nameof(options)); |
|||
Guard.NotNull(stream, nameof(stream)); |
|||
|
|||
return new HeicDecoderCore(options).Identify(options.Configuration, stream, cancellationToken); |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
protected override Image<TPixel> Decode<TPixel>(DecoderOptions options, Stream stream, CancellationToken cancellationToken) |
|||
{ |
|||
Guard.NotNull(options, nameof(options)); |
|||
Guard.NotNull(stream, nameof(stream)); |
|||
|
|||
HeicDecoderCore decoder = new(options); |
|||
Image<TPixel> image = decoder.Decode<TPixel>(options.Configuration, stream, cancellationToken); |
|||
|
|||
return image; |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
protected override Image Decode(DecoderOptions options, Stream stream, CancellationToken cancellationToken) |
|||
=> this.Decode<Rgb24>(options, stream, cancellationToken); |
|||
} |
|||
@ -0,0 +1,65 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Diagnostics.CodeAnalysis; |
|||
using SixLabors.ImageSharp.IO; |
|||
using SixLabors.ImageSharp.Memory; |
|||
using SixLabors.ImageSharp.Metadata; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Performs the PBM decoding operation.
|
|||
/// </summary>
|
|||
internal sealed class HeicDecoderCore : IImageDecoderInternals |
|||
{ |
|||
/// <summary>
|
|||
/// The general configuration.
|
|||
/// </summary>
|
|||
private readonly Configuration configuration; |
|||
|
|||
/// <summary>
|
|||
/// The <see cref="ImageMetadata"/> decoded by this decoder instance.
|
|||
/// </summary>
|
|||
private ImageMetadata? metadata; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="PbmDecoderCore" /> class.
|
|||
/// </summary>
|
|||
/// <param name="options">The decoder options.</param>
|
|||
public HeicDecoderCore(DecoderOptions options) |
|||
{ |
|||
this.Options = options; |
|||
this.configuration = options.Configuration; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public DecoderOptions Options { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken cancellationToken) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
this.ProcessHeader(stream); |
|||
|
|||
var image = new Image<TPixel>(this.configuration, this.pixelSize.Width, this.pixelSize.Height, this.metadata); |
|||
|
|||
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer(); |
|||
|
|||
// TODO: Implement
|
|||
|
|||
return image; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public ImageInfo Identify(BufferedReadStream stream, CancellationToken cancellationToken) |
|||
{ |
|||
this.ProcessHeader(stream); |
|||
|
|||
// TODO: Implement
|
|||
return new ImageInfo(new PixelTypeInfo(bitsPerPixel), new(this.pixelSize.Width, this.pixelSize.Height), this.metadata); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using SixLabors.ImageSharp.Advanced; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Image encoder for writing an image to a stream as HEIC images.
|
|||
public sealed class HeicEncoder : ImageEncoder |
|||
{ |
|||
/// <inheritdoc/>
|
|||
protected override void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken) |
|||
{ |
|||
HeicEncoderCore encoder = new(image.Configuration, this); |
|||
encoder.Encode(image, stream, cancellationToken); |
|||
} |
|||
} |
|||
@ -0,0 +1,53 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Buffers.Text; |
|||
using SixLabors.ImageSharp.Advanced; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Image encoder for writing an image to a stream as a HEIC image.
|
|||
/// </summary>
|
|||
internal sealed class HeicEncoderCore : IImageEncoderInternals |
|||
{ |
|||
/// <summary>
|
|||
/// The global configuration.
|
|||
/// </summary>
|
|||
private Configuration configuration; |
|||
|
|||
/// <summary>
|
|||
/// The encoder with options.
|
|||
/// </summary>
|
|||
private readonly HeicEncoder encoder; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="HeicEncoderCore"/> class.
|
|||
/// </summary>
|
|||
/// <param name="configuration">The configuration.</param>
|
|||
/// <param name="encoder">The encoder with options.</param>
|
|||
public HeicEncoderCore(Configuration configuration, HeicEncoder encoder) |
|||
{ |
|||
this.configuration = configuration; |
|||
this.encoder = encoder; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Encodes the image to the specified stream from the <see cref="ImageFrame{TPixel}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The pixel format.</typeparam>
|
|||
/// <param name="image">The <see cref="ImageFrame{TPixel}"/> to encode from.</param>
|
|||
/// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
|
|||
/// <param name="cancellationToken">The token to request cancellation.</param>
|
|||
public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken cancellationToken) |
|||
where TPixel : unmanaged, IPixel<TPixel> |
|||
{ |
|||
Guard.NotNull(image, nameof(image)); |
|||
Guard.NotNull(stream, nameof(stream)); |
|||
|
|||
// TODO: Implement
|
|||
|
|||
stream.Flush(); |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Registers the image encoders, decoders and mime type detectors for the HEIC format.
|
|||
/// </summary>
|
|||
public sealed class HeicFormat : IImageFormat<HeicMetadata> |
|||
{ |
|||
private HeicFormat() |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the shared instance.
|
|||
/// </summary>
|
|||
public static HeicFormat Instance { get; } = new(); |
|||
|
|||
/// <inheritdoc/>
|
|||
public string Name => "HEIC"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public string DefaultMimeType => "image/heif"; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IEnumerable<string> MimeTypes => HeicConstants.MimeTypes; |
|||
|
|||
/// <inheritdoc/>
|
|||
public IEnumerable<string> FileExtensions => HeicConstants.FileExtensions; |
|||
|
|||
/// <inheritdoc/>
|
|||
public HeicMetadata CreateDefaultFormatMetadata() => new(); |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using System.Diagnostics.CodeAnalysis; |
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Detects HEIC file headers.
|
|||
/// </summary>
|
|||
public sealed class HeicImageFormatDetector : IImageFormatDetector |
|||
{ |
|||
/// <inheritdoc/>
|
|||
public bool TryDetectFormat(ReadOnlySpan<byte> header, [NotNullWhen(true)] out IImageFormat? format) |
|||
{ |
|||
format = IsSupportedFileFormat(header) ? HeicFormat.Instance : null; |
|||
return format != null; |
|||
} |
|||
|
|||
private static bool IsSupportedFileFormat(ReadOnlySpan<byte> header) |
|||
{ |
|||
// TODO: Implement
|
|||
|
|||
return false; |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Provides HEIC specific metadata information for the image.
|
|||
/// </summary>
|
|||
public class HeicMetadata : IDeepCloneable |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="HeicMetadata"/> class.
|
|||
/// </summary>
|
|||
public HeicMetadata() => |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="HeicMetadata"/> class.
|
|||
/// </summary>
|
|||
/// <param name="other">The metadata to create an instance from.</param>
|
|||
private HeicMetadata(HeicMetadata other) |
|||
{ |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public IDeepCloneable DeepClone() => new HeicMetadata(this); |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Provides enumeration of supported x265's LAN Unit Types.
|
|||
/// </summary>
|
|||
public enum HeicNalUnitType : uint |
|||
{ |
|||
NAL_UNIT_CODED_SLICE_TRAIL_N = 0, |
|||
NAL_UNIT_CODED_SLICE_TRAIL_R, |
|||
NAL_UNIT_CODED_SLICE_TSA_N, |
|||
NAL_UNIT_CODED_SLICE_TSA_R, |
|||
NAL_UNIT_CODED_SLICE_STSA_N, |
|||
NAL_UNIT_CODED_SLICE_STSA_R, |
|||
NAL_UNIT_CODED_SLICE_RADL_N, |
|||
NAL_UNIT_CODED_SLICE_RADL_R, |
|||
NAL_UNIT_CODED_SLICE_RASL_N, |
|||
NAL_UNIT_CODED_SLICE_RASL_R, |
|||
NAL_UNIT_CODED_SLICE_BLA_W_LP = 16, |
|||
NAL_UNIT_CODED_SLICE_BLA_W_RADL, |
|||
NAL_UNIT_CODED_SLICE_BLA_N_LP, |
|||
NAL_UNIT_CODED_SLICE_IDR_W_RADL, |
|||
NAL_UNIT_CODED_SLICE_IDR_N_LP, |
|||
NAL_UNIT_CODED_SLICE_CRA, |
|||
NAL_UNIT_VPS = 32, |
|||
NAL_UNIT_SPS, |
|||
NAL_UNIT_PPS, |
|||
NAL_UNIT_ACCESS_UNIT_DELIMITER, |
|||
NAL_UNIT_EOS, |
|||
NAL_UNIT_EOB, |
|||
NAL_UNIT_FILLER_DATA, |
|||
NAL_UNIT_PREFIX_SEI, |
|||
NAL_UNIT_SUFFIX_SEI, |
|||
Unspecified = 62, |
|||
Invalid = 64, |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
namespace SixLabors.ImageSharp.Formats.Heic; |
|||
|
|||
/// <summary>
|
|||
/// Configuration options for use during HEIC encoding.
|
|||
/// </summary>
|
|||
internal interface IHeicEncoderOptions |
|||
{ |
|||
// None defined yet.
|
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
// Copyright (c) Six Labors.
|
|||
// Licensed under the Six Labors Split License.
|
|||
|
|||
using SixLabors.ImageSharp.Formats.Heic; |
|||
using SixLabors.ImageSharp.Metadata; |
|||
|
|||
namespace SixLabors.ImageSharp; |
|||
|
|||
/// <summary>
|
|||
/// Extension methods for the <see cref="ImageMetadata"/> type.
|
|||
/// </summary>
|
|||
public static partial class MetadataExtensions |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the pbm format specific metadata for the image.
|
|||
/// </summary>
|
|||
/// <param name="metadata">The metadata this method extends.</param>
|
|||
/// <returns>The <see cref="HeicMetadata"/>.</returns>
|
|||
public static HeicMetadata GetHeicMetadata(this ImageMetadata metadata) => metadata.GetFormatMetadata(HeicFormat.Instance); |
|||
} |
|||
Loading…
Reference in new issue