Skip to content

Commit dea0737

Browse files
Merge pull request #1428 from SixLabors/bp/bmp8BitBulkConv
Use bulk conversion in Write8BitColor
2 parents 59b65a9 + 04881fd commit dea0737

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.IO;
77
using System.Runtime.InteropServices;
88
using System.Threading;
9-
using System.Threading.Tasks;
109
using SixLabors.ImageSharp.Advanced;
1110
using SixLabors.ImageSharp.Common.Helpers;
1211
using SixLabors.ImageSharp.Memory;
@@ -342,20 +341,11 @@ private void Write8BitColor<TPixel>(Stream stream, ImageFrame<TPixel> image, Spa
342341
using IndexedImageFrame<TPixel> quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds());
343342

344343
ReadOnlySpan<TPixel> quantizedColors = quantized.Palette.Span;
345-
var color = default(Rgba32);
346-
347-
// TODO: Use bulk conversion here for better perf
348-
int idx = 0;
349-
foreach (TPixel quantizedColor in quantizedColors)
344+
PixelOperations<TPixel>.Instance.ToBgra32(this.configuration, quantizedColors, MemoryMarshal.Cast<byte, Bgra32>(colorPalette));
345+
Span<uint> colorPaletteAsUInt = MemoryMarshal.Cast<byte, uint>(colorPalette);
346+
for (int i = 0; i < colorPaletteAsUInt.Length; i++)
350347
{
351-
quantizedColor.ToRgba32(ref color);
352-
colorPalette[idx] = color.B;
353-
colorPalette[idx + 1] = color.G;
354-
colorPalette[idx + 2] = color.R;
355-
356-
// Padding byte, always 0.
357-
colorPalette[idx + 3] = 0;
358-
idx += 4;
348+
colorPaletteAsUInt[i] = colorPaletteAsUInt[i] & 0x00FFFFFF; // Padding byte, always 0.
359349
}
360350

361351
stream.Write(colorPalette);

0 commit comments

Comments
 (0)