From 247834f5299894de2ac0afeed3f48a944221afcf Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 15 Nov 2020 18:07:37 +0100 Subject: [PATCH 1/3] Use bulk conversion to rgba in Write8BitColor --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 454440f634..322d08e4c8 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -6,7 +6,6 @@ using System.IO; using System.Runtime.InteropServices; using System.Threading; -using System.Threading.Tasks; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Common.Helpers; using SixLabors.ImageSharp.Memory; @@ -340,15 +339,15 @@ private void Write8BitColor(Stream stream, ImageFrame image, Spa { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration); using IndexedImageFrame quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds()); + using IMemoryOwner rgbColorsBuffer = this.memoryAllocator.Allocate(quantized.Palette.Length); + Span rgbColors = rgbColorsBuffer.GetSpan(); ReadOnlySpan quantizedColors = quantized.Palette.Span; - var color = default(Rgba32); + PixelOperations.Instance.ToRgba32(Configuration.Default, quantizedColors, rgbColors); - // TODO: Use bulk conversion here for better perf int idx = 0; - foreach (TPixel quantizedColor in quantizedColors) + foreach (Rgba32 color in rgbColors) { - quantizedColor.ToRgba32(ref color); colorPalette[idx] = color.B; colorPalette[idx + 1] = color.G; colorPalette[idx + 2] = color.R; From e4d0ffe6dbce456226c57af34e0b58c1374ad105 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 15 Nov 2020 18:51:34 +0100 Subject: [PATCH 2/3] Dont use the default config in ToRgba32 --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index 322d08e4c8..c7e7a534b7 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -343,7 +343,7 @@ private void Write8BitColor(Stream stream, ImageFrame image, Spa Span rgbColors = rgbColorsBuffer.GetSpan(); ReadOnlySpan quantizedColors = quantized.Palette.Span; - PixelOperations.Instance.ToRgba32(Configuration.Default, quantizedColors, rgbColors); + PixelOperations.Instance.ToRgba32(this.configuration, quantizedColors, rgbColors); int idx = 0; foreach (Rgba32 color in rgbColors) From 3970510861a6bf4cc4b7ecdafcffc69978f1df9c Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Sun, 15 Nov 2020 19:20:59 +0100 Subject: [PATCH 3/3] Use colorPalette span as destination of bulk conversion --- src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs index c7e7a534b7..01bdbd1c0b 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -339,22 +339,13 @@ private void Write8BitColor(Stream stream, ImageFrame image, Spa { using IQuantizer frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer(this.configuration); using IndexedImageFrame quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds()); - using IMemoryOwner rgbColorsBuffer = this.memoryAllocator.Allocate(quantized.Palette.Length); - Span rgbColors = rgbColorsBuffer.GetSpan(); ReadOnlySpan quantizedColors = quantized.Palette.Span; - PixelOperations.Instance.ToRgba32(this.configuration, quantizedColors, rgbColors); - - int idx = 0; - foreach (Rgba32 color in rgbColors) + PixelOperations.Instance.ToBgra32(this.configuration, quantizedColors, MemoryMarshal.Cast(colorPalette)); + Span colorPaletteAsUInt = MemoryMarshal.Cast(colorPalette); + for (int i = 0; i < colorPaletteAsUInt.Length; i++) { - colorPalette[idx] = color.B; - colorPalette[idx + 1] = color.G; - colorPalette[idx + 2] = color.R; - - // Padding byte, always 0. - colorPalette[idx + 3] = 0; - idx += 4; + colorPaletteAsUInt[i] = colorPaletteAsUInt[i] & 0x00FFFFFF; // Padding byte, always 0. } stream.Write(colorPalette);