Skip to content

Commit 3970510

Browse files
committed
Use colorPalette span as destination of bulk conversion
1 parent e4d0ffe commit 3970510

1 file changed

Lines changed: 4 additions & 13 deletions

File tree

src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -339,22 +339,13 @@ private void Write8BitColor<TPixel>(Stream stream, ImageFrame<TPixel> image, Spa
339339
{
340340
using IQuantizer<TPixel> frameQuantizer = this.quantizer.CreatePixelSpecificQuantizer<TPixel>(this.configuration);
341341
using IndexedImageFrame<TPixel> quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds());
342-
using IMemoryOwner<Rgba32> rgbColorsBuffer = this.memoryAllocator.Allocate<Rgba32>(quantized.Palette.Length);
343-
Span<Rgba32> rgbColors = rgbColorsBuffer.GetSpan();
344342

345343
ReadOnlySpan<TPixel> quantizedColors = quantized.Palette.Span;
346-
PixelOperations<TPixel>.Instance.ToRgba32(this.configuration, quantizedColors, rgbColors);
347-
348-
int idx = 0;
349-
foreach (Rgba32 color in rgbColors)
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-
colorPalette[idx] = color.B;
352-
colorPalette[idx + 1] = color.G;
353-
colorPalette[idx + 2] = color.R;
354-
355-
// Padding byte, always 0.
356-
colorPalette[idx + 3] = 0;
357-
idx += 4;
348+
colorPaletteAsUInt[i] = colorPaletteAsUInt[i] & 0x00FFFFFF; // Padding byte, always 0.
358349
}
359350

360351
stream.Write(colorPalette);

0 commit comments

Comments
 (0)