From 8528f1a33ecffc65887c94c32a48d64ffb7f12eb Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 17 May 2022 14:31:42 +1000 Subject: [PATCH] Optimize RestoreToBackground --- .../Formats/Webp/WebpAnimationDecoder.cs | 11 ++------- src/ImageSharp/Memory/Buffer2DRegion{T}.cs | 23 +++++++++++++++++++ .../MemoryGroupExtensions.cs | 11 +++++++++ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs index 73246778c..93ded17f8 100644 --- a/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs +++ b/src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs @@ -356,15 +356,8 @@ namespace SixLabors.ImageSharp.Formats.Webp var interest = Rectangle.Intersect(imageFrame.Bounds(), this.restoreArea.Value); Buffer2DRegion pixelRegion = imageFrame.PixelBuffer.GetRegion(interest); - for (int y = 0; y < pixelRegion.Height; y++) - { - Span pixelRow = pixelRegion.DangerousGetRowSpan(y); - for (int x = 0; x < pixelRow.Length; x++) - { - ref TPixel pixel = ref pixelRow[x]; - pixel.FromRgba32(backgroundColor); - } - } + TPixel backgroundPixel = backgroundColor.ToPixel(); + pixelRegion.Fill(backgroundPixel); } /// diff --git a/src/ImageSharp/Memory/Buffer2DRegion{T}.cs b/src/ImageSharp/Memory/Buffer2DRegion{T}.cs index 13b339597..9b9c1aa5b 100644 --- a/src/ImageSharp/Memory/Buffer2DRegion{T}.cs +++ b/src/ImageSharp/Memory/Buffer2DRegion{T}.cs @@ -141,6 +141,9 @@ namespace SixLabors.ImageSharp.Memory return ref this.Buffer.DangerousGetRowSpan(y)[x]; } + /// + /// Clears the contents of this . + /// internal void Clear() { // Optimization for when the size of the area is the same as the buffer size. @@ -156,5 +159,25 @@ namespace SixLabors.ImageSharp.Memory row.Clear(); } } + + /// + /// Fills the elements of this with the specified value. + /// + /// The value to assign to each element of the region. + internal void Fill(T value) + { + // Optimization for when the size of the area is the same as the buffer size. + if (this.IsFullBufferArea) + { + this.Buffer.FastMemoryGroup.Fill(value); + return; + } + + for (int y = 0; y < this.Rectangle.Height; y++) + { + Span row = this.DangerousGetRowSpan(y); + row.Fill(value); + } + } } } diff --git a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs index d200b223a..7a4e0df5d 100644 --- a/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs +++ b/src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs @@ -7,6 +7,12 @@ namespace SixLabors.ImageSharp.Memory { internal static class MemoryGroupExtensions { + /// + /// Fills the elements of this with the specified value. + /// + /// The type of element. + /// The group to fill. + /// The value to assign to each element of the group. internal static void Fill(this IMemoryGroup group, T value) where T : struct { @@ -16,6 +22,11 @@ namespace SixLabors.ImageSharp.Memory } } + /// + /// Clears the contents of this . + /// + /// The type of element. + /// The group to clear. internal static void Clear(this IMemoryGroup group) where T : struct {