Skip to content

Commit

Permalink
fix SA1117
Browse files Browse the repository at this point in the history
  • Loading branch information
Poker-sang committed Oct 25, 2023
1 parent bd2d455 commit dcadefd
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 23 deletions.
13 changes: 11 additions & 2 deletions src/ImageSharp/Formats/Webp/AlphaEncoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 Vp8LEncoder(
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,
Expand Down
15 changes: 12 additions & 3 deletions src/ImageSharp/Formats/Webp/Lossy/WebpLossyDecoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public void Decode<TPixel>(Buffer2D<TPixel> pixels, int width, int height, WebpI
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);
Expand All @@ -102,8 +106,13 @@ public void Decode<TPixel>(Buffer2D<TPixel> pixels, int width, int height, WebpI

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);
Expand Down
19 changes: 13 additions & 6 deletions src/ImageSharp/Formats/Webp/WebpDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,30 @@ public Image<TPixel> Decode<TPixel>(BufferedReadStream stream, CancellationToken
{
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);
}

image = new Image<TPixel>(this.configuration, (int)this.webImageInfo.Width, (int)this.webImageInfo.Height, metadata);
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);
}

Expand Down
58 changes: 46 additions & 12 deletions src/ImageSharp/Formats/Webp/WebpEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,35 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken

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);
if (hasAnimation)
{
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);
}
Expand All @@ -155,18 +171,36 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream, CancellationToken
}
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);
}
Expand Down

0 comments on commit dcadefd

Please sign in to comment.