Browse Source

Update PngDecoder.cs

pull/1861/head
James Jackson-South 5 years ago
parent
commit
65bd0de7a2
  1. 63
      src/ImageSharp/Formats/Png/PngDecoder.cs

63
src/ImageSharp/Formats/Png/PngDecoder.cs

@ -31,39 +31,82 @@ namespace SixLabors.ImageSharp.Formats.Png
IImageInfo info = decoder.Identify(configuration, stream);
stream.Position = 0;
switch (info.Metadata.GetPngMetadata().ColorType)
PngMetadata meta = info.Metadata.GetPngMetadata();
PngColorType color = meta.ColorType.GetValueOrDefault();
PngBitDepth bits = meta.BitDepth.GetValueOrDefault();
return color switch
{
default:
break;
}
PngColorType.Grayscale => (bits == PngBitDepth.Bit16)
? this.Decode<L16>(configuration, stream)
: this.Decode<L8>(configuration, stream),
return this.Decode<Rgba32>(configuration, stream);
PngColorType.Rgb => this.Decode<Rgb24>(configuration, stream),
PngColorType.Palette => this.Decode<Rgba32>(configuration, stream),
PngColorType.GrayscaleWithAlpha => (bits == PngBitDepth.Bit16)
? this.Decode<La32>(configuration, stream)
: this.Decode<La16>(configuration, stream),
PngColorType.RgbWithAlpha => (bits == PngBitDepth.Bit16)
? this.Decode<Rgba64>(configuration, stream)
: this.Decode<Rgba32>(configuration, stream),
_ => this.Decode<Rgba32>(configuration, stream),
};
}
/// <inheritdoc/>
public Task<Image<TPixel>> DecodeAsync<TPixel>(Configuration configuration, Stream stream, CancellationToken cancellationToken)
where TPixel : unmanaged, IPixel<TPixel>
{
var decoder = new PngDecoderCore(configuration, this);
PngDecoderCore decoder = new(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);
{
PngDecoderCore decoder = new(configuration, true);
IImageInfo info = await decoder.IdentifyAsync(configuration, stream, cancellationToken).ConfigureAwait(false);
stream.Position = 0;
PngMetadata meta = info.Metadata.GetPngMetadata();
PngColorType color = meta.ColorType.GetValueOrDefault();
PngBitDepth bits = meta.BitDepth.GetValueOrDefault();
return color switch
{
PngColorType.Grayscale => (bits == PngBitDepth.Bit16)
? await this.DecodeAsync<L16>(configuration, stream, cancellationToken).ConfigureAwait(false)
: await this.DecodeAsync<L8>(configuration, stream, cancellationToken).ConfigureAwait(false),
PngColorType.Rgb => await this.DecodeAsync<Rgb24>(configuration, stream, cancellationToken).ConfigureAwait(false),
PngColorType.Palette => await this.DecodeAsync<Rgba32>(configuration, stream, cancellationToken).ConfigureAwait(false),
PngColorType.GrayscaleWithAlpha => (bits == PngBitDepth.Bit16)
? await this.DecodeAsync<La32>(configuration, stream, cancellationToken).ConfigureAwait(false)
: await this.DecodeAsync<La16>(configuration, stream, cancellationToken).ConfigureAwait(false),
PngColorType.RgbWithAlpha => (bits == PngBitDepth.Bit16)
? await this.DecodeAsync<Rgba64>(configuration, stream, cancellationToken).ConfigureAwait(false)
: await this.DecodeAsync<Rgba32>(configuration, stream, cancellationToken).ConfigureAwait(false),
_ => await this.DecodeAsync<Rgba32>(configuration, stream, cancellationToken).ConfigureAwait(false),
};
}
/// <inheritdoc/>
public IImageInfo Identify(Configuration configuration, Stream stream)
{
var decoder = new PngDecoderCore(configuration, this);
PngDecoderCore decoder = new(configuration, this);
return decoder.Identify(configuration, stream);
}
/// <inheritdoc/>
public Task<IImageInfo> IdentifyAsync(Configuration configuration, Stream stream, CancellationToken cancellationToken)
{
var decoder = new PngDecoderCore(configuration, this);
PngDecoderCore decoder = new(configuration, this);
return decoder.IdentifyAsync(configuration, stream, cancellationToken);
}
}

Loading…
Cancel
Save