Browse Source

Optimize RestoreToBackground

pull/1985/head
James Jackson-South 4 years ago
parent
commit
8528f1a33e
  1. 11
      src/ImageSharp/Formats/Webp/WebpAnimationDecoder.cs
  2. 23
      src/ImageSharp/Memory/Buffer2DRegion{T}.cs
  3. 11
      src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs

11
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<TPixel> pixelRegion = imageFrame.PixelBuffer.GetRegion(interest);
for (int y = 0; y < pixelRegion.Height; y++)
{
Span<TPixel> 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<TPixel>();
pixelRegion.Fill(backgroundPixel);
}
/// <summary>

23
src/ImageSharp/Memory/Buffer2DRegion{T}.cs

@ -141,6 +141,9 @@ namespace SixLabors.ImageSharp.Memory
return ref this.Buffer.DangerousGetRowSpan(y)[x];
}
/// <summary>
/// Clears the contents of this <see cref="Buffer2DRegion{T}"/>.
/// </summary>
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();
}
}
/// <summary>
/// Fills the elements of this <see cref="Buffer2DRegion{T}"/> with the specified value.
/// </summary>
/// <param name="value">The value to assign to each element of the region.</param>
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<T> row = this.DangerousGetRowSpan(y);
row.Fill(value);
}
}
}
}

11
src/ImageSharp/Memory/DiscontiguousBuffers/MemoryGroupExtensions.cs

@ -7,6 +7,12 @@ namespace SixLabors.ImageSharp.Memory
{
internal static class MemoryGroupExtensions
{
/// <summary>
/// Fills the elements of this <see cref="IMemoryGroup{T}"/> with the specified value.
/// </summary>
/// <typeparam name="T">The type of element.</typeparam>
/// <param name="group">The group to fill.</param>
/// <param name="value">The value to assign to each element of the group.</param>
internal static void Fill<T>(this IMemoryGroup<T> group, T value)
where T : struct
{
@ -16,6 +22,11 @@ namespace SixLabors.ImageSharp.Memory
}
}
/// <summary>
/// Clears the contents of this <see cref="IMemoryGroup{T}"/>.
/// </summary>
/// <typeparam name="T">The type of element.</typeparam>
/// <param name="group">The group to clear.</param>
internal static void Clear<T>(this IMemoryGroup<T> group)
where T : struct
{

Loading…
Cancel
Save