Browse Source

fix SA1117

pull/2569/head
Poker 2 years ago
parent
commit
d4483217b6
No known key found for this signature in database GPG Key ID: C65A6AD457D5C8F8
  1. 13
      src/ImageSharp/Formats/Webp/AlphaEncoder.cs
  2. 15
      src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs
  3. 19
      src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
  4. 58
      src/ImageSharp/Formats/Webp/WebpEncoderCore.cs

13
src/ImageSharp/Formats/Webp/AlphaEncoder.cs

@ -43,8 +43,17 @@ internal static class AlphaEncoder
{
const WebpEncodingMethod effort = WebpEncodingMethod.Default;
const int quality = 8 * (int)effort;
using Vp8LEncoder lossLessEncoder = new Vp8LEncoder(memoryAllocator, configuration, width, height, quality,
skipMetadata, effort, WebpTransparentColorMode.Preserve, false, 0);
using Vp8LEncoder lossLessEncoder = new(
memoryAllocator,
configuration,
width,
height,
quality,
skipMetadata,
effort,
WebpTransparentColorMode.Preserve,
false,
0);
// The transparency information will be stored in the green channel of the ARGB quadruplet.
// The green channel is allowed extra transformation steps in the specification -- unlike the other channels,

15
src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs

@ -76,7 +76,11 @@ internal sealed class WebpLossyDecoder
Vp8Proba proba = new Vp8Proba();
Vp8SegmentHeader vp8SegmentHeader = this.ParseSegmentHeader(proba);
using (Vp8Decoder decoder = new Vp8Decoder(info.Vp8FrameHeader, pictureHeader, vp8SegmentHeader, proba,
using (Vp8Decoder decoder = new Vp8Decoder(
info.Vp8FrameHeader,
pictureHeader,
vp8SegmentHeader,
proba,
this.memoryAllocator))
{
Vp8Io io = InitializeVp8Io(decoder, pictureHeader);
@ -102,8 +106,13 @@ internal sealed class WebpLossyDecoder
if (info.Features?.Alpha == true)
{
using (AlphaDecoder alphaDecoder = new AlphaDecoder(width, height, alphaData,
info.Features.AlphaChunkHeader, this.memoryAllocator, this.configuration))
using (AlphaDecoder alphaDecoder = new AlphaDecoder(
width,
height,
alphaData,
info.Features.AlphaChunkHeader,
this.memoryAllocator,
this.configuration))
{
alphaDecoder.Decode();
DecodePixelValues(width, height, decoder.Pixels.Memory.Span, pixels, alphaDecoder.Alpha);

19
src/ImageSharp/Formats/Webp/WebpDecoderCore.cs

@ -91,8 +91,11 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
{
if (this.webImageInfo.Features is { Animation: true })
{
using WebpAnimationDecoder animationDecoder = new WebpAnimationDecoder(this.memoryAllocator,
this.configuration, this.maxFrames, this.backgroundColorHandling);
using WebpAnimationDecoder animationDecoder = new WebpAnimationDecoder(
this.memoryAllocator,
this.configuration,
this.maxFrames,
this.backgroundColorHandling);
return animationDecoder.Decode<TPixel>(stream, this.webImageInfo.Features, this.webImageInfo.Width, this.webImageInfo.Height, fileSize);
}
@ -100,14 +103,18 @@ internal sealed class WebpDecoderCore : IImageDecoderInternals, IDisposable
Buffer2D<TPixel> pixels = image.GetRootFramePixelBuffer();
if (this.webImageInfo.IsLossless)
{
WebpLosslessDecoder losslessDecoder = new WebpLosslessDecoder(this.webImageInfo.Vp8LBitReader,
this.memoryAllocator, this.configuration);
WebpLosslessDecoder losslessDecoder = new WebpLosslessDecoder(
this.webImageInfo.Vp8LBitReader,
this.memoryAllocator,
this.configuration);
losslessDecoder.Decode(pixels, image.Width, image.Height);
}
else
{
WebpLossyDecoder lossyDecoder = new WebpLossyDecoder(this.webImageInfo.Vp8BitReader,
this.memoryAllocator, this.configuration);
WebpLossyDecoder lossyDecoder = new WebpLossyDecoder(
this.webImageInfo.Vp8BitReader,
this.memoryAllocator,
this.configuration);
lossyDecoder.Decode(pixels, image.Width, image.Height, this.webImageInfo, this.alphaData);
}

58
src/ImageSharp/Formats/Webp/WebpEncoderCore.cs

@ -129,9 +129,17 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals
if (lossless)
{
using Vp8LEncoder encoder = new Vp8LEncoder(this.memoryAllocator, this.configuration, image.Width,
image.Height, this.quality, this.skipMetadata, this.method, this.transparentColorMode,
this.nearLossless, this.nearLosslessQuality);
using Vp8LEncoder encoder = new Vp8LEncoder(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.skipMetadata,
this.method,
this.transparentColorMode,
this.nearLossless,
this.nearLosslessQuality);
bool hasAnimation = image.Frames.Count > 1;
encoder.EncodeHeader(image, stream, hasAnimation);
@ -139,9 +147,17 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals
{
foreach (ImageFrame<TPixel> imageFrame in image.Frames)
{
using Vp8LEncoder enc = new Vp8LEncoder(this.memoryAllocator, this.configuration, image.Width,
image.Height, this.quality, this.skipMetadata, this.method, this.transparentColorMode,
this.nearLossless, this.nearLosslessQuality);
using Vp8LEncoder enc = new Vp8LEncoder(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.skipMetadata,
this.method,
this.transparentColorMode,
this.nearLossless,
this.nearLosslessQuality);
enc.Encode(imageFrame, stream, true);
}
@ -155,18 +171,36 @@ internal sealed class WebpEncoderCore : IImageEncoderInternals
}
else
{
using Vp8Encoder encoder = new Vp8Encoder(this.memoryAllocator, this.configuration, image.Width,
image.Height, this.quality, this.skipMetadata, this.method, this.entropyPasses, this.filterStrength,
this.spatialNoiseShaping, this.alphaCompression);
using Vp8Encoder encoder = new Vp8Encoder(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.skipMetadata,
this.method,
this.entropyPasses,
this.filterStrength,
this.spatialNoiseShaping,
this.alphaCompression);
if (image.Frames.Count > 1)
{
encoder.EncodeHeader(image, stream, false, true);
foreach (ImageFrame<TPixel> imageFrame in image.Frames)
{
using Vp8Encoder enc = new Vp8Encoder(this.memoryAllocator, this.configuration, image.Width,
image.Height, this.quality, this.skipMetadata, this.method, this.entropyPasses,
this.filterStrength, this.spatialNoiseShaping, this.alphaCompression);
using Vp8Encoder enc = new Vp8Encoder(
this.memoryAllocator,
this.configuration,
image.Width,
image.Height,
this.quality,
this.skipMetadata,
this.method,
this.entropyPasses,
this.filterStrength,
this.spatialNoiseShaping,
this.alphaCompression);
enc.EncodeAnimation(imageFrame, stream);
}

Loading…
Cancel
Save