From 7de5c591021cc1589b612d2390a256e8edbe5890 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 454440f63..322d08e4c 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -6,7 +6,6 @@ using System.Buffers; 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 @@ namespace SixLabors.ImageSharp.Formats.Bmp { 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 9449f947e2edc88c5a7361b2d774587a3f1e836a 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 322d08e4c..c7e7a534b 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -343,7 +343,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp 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 85b65d393ab131ca9afc1e439729ff9d07a8cdff 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 c7e7a534b..01bdbd1c0 100644 --- a/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs @@ -339,22 +339,13 @@ namespace SixLabors.ImageSharp.Formats.Bmp { 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);