mirror of https://github.com/SixLabors/ImageSharp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
74 lines
3.0 KiB
74 lines
3.0 KiB
// Copyright (c) Six Labors.
|
|
// Licensed under the Apache License, Version 2.0.
|
|
|
|
using System.IO;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using SixLabors.ImageSharp.IO;
|
|
using SixLabors.ImageSharp.Memory;
|
|
using SixLabors.ImageSharp.Metadata;
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
|
|
namespace SixLabors.ImageSharp.Formats.Gif
|
|
{
|
|
/// <summary>
|
|
/// Decoder for generating an image out of a gif encoded stream.
|
|
/// </summary>
|
|
public sealed class GifDecoder : IImageDecoder, IGifDecoderOptions, IImageInfoDetector
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being decoded.
|
|
/// </summary>
|
|
public bool IgnoreMetadata { get; set; } = false;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the decoding mode for multi-frame images
|
|
/// </summary>
|
|
public FrameDecodingMode DecodingMode { get; set; } = FrameDecodingMode.All;
|
|
|
|
/// <inheritdoc/>
|
|
public Image<TPixel> Decode<TPixel>(Configuration configuration, Stream stream, CancellationToken cancellationToken = default)
|
|
where TPixel : unmanaged, IPixel<TPixel>
|
|
{
|
|
var decoder = new GifDecoderCore(configuration, this);
|
|
return decoder.Decode<TPixel>(configuration, stream, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Image Decode(Configuration configuration, Stream stream, CancellationToken cancellationToken = default)
|
|
=> this.Decode<Rgba32>(configuration, stream, cancellationToken);
|
|
|
|
/// <inheritdoc/>
|
|
public Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration, Stream stream, CancellationToken cancellationToken)
|
|
where TPixel : unmanaged, IPixel<TPixel>
|
|
{
|
|
var decoder = new GifDecoderCore(configuration, this);
|
|
return decoder.DecodeAsync<TPixel>(configuration, stream, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<Image> DecodeAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken)
|
|
=> await this.DecodeAsync<Rgba32>(configuration, stream, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
/// <inheritdoc/>
|
|
public IImageInfo Identify(Configuration configuration, Stream stream, CancellationToken cancellationToken = default)
|
|
{
|
|
Guard.NotNull(stream, nameof(stream));
|
|
|
|
var decoder = new GifDecoderCore(configuration, this);
|
|
|
|
using var bufferedStream = new BufferedReadStream(configuration, stream);
|
|
return decoder.Identify(bufferedStream, cancellationToken);
|
|
}
|
|
|
|
/// <inheritdoc/>
|
|
public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken)
|
|
{
|
|
Guard.NotNull(stream, nameof(stream));
|
|
|
|
var decoder = new GifDecoderCore(configuration, this);
|
|
return decoder.IdentifyAsync(configuration, stream, cancellationToken);
|
|
}
|
|
}
|
|
}
|
|
|