Browse Source

Remove stackalloc

pull/1138/head
James Jackson-South 6 years ago
parent
commit
dd32cdfd1b
  1. 6
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  2. 4
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  3. 4
      src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs

6
src/ImageSharp/Formats/Gif/GifEncoderCore.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
@ -217,10 +218,11 @@ namespace SixLabors.ImageSharp.Formats.Gif
where TPixel : unmanaged, IPixel<TPixel>
{
// Transparent pixels are much more likely to be found at the end of a palette.
// Palette length maxes out at 256 so safe to stackalloc.
int index = -1;
ReadOnlySpan<TPixel> paletteSpan = quantized.Palette.Span;
Span<Rgba32> rgbaSpan = stackalloc Rgba32[paletteSpan.Length];
using IMemoryOwner<Rgba32> rgbaOwner = quantized.Configuration.MemoryAllocator.Allocate<Rgba32>(paletteSpan.Length);
Span<Rgba32> rgbaSpan = rgbaOwner.GetSpan();
PixelOperations<TPixel>.Instance.ToRgba32(quantized.Configuration, paletteSpan, rgbaSpan);
ref Rgba32 rgbaSpanRef = ref MemoryMarshal.GetReference(rgbaSpan);

4
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -566,8 +566,8 @@ namespace SixLabors.ImageSharp.Formats.Png
ref byte alphaTableRef = ref MemoryMarshal.GetReference(alphaTable.GetSpan());
// Bulk convert our palette to RGBA to allow assignment to tables.
// Palette length maxes out at 256 so safe to stackalloc.
Span<Rgba32> rgbaPaletteSpan = stackalloc Rgba32[paletteLength];
using IMemoryOwner<Rgba32> rgbaOwner = quantized.Configuration.MemoryAllocator.Allocate<Rgba32>(paletteLength);
Span<Rgba32> rgbaPaletteSpan = rgbaOwner.GetSpan();
PixelOperations<TPixel>.Instance.ToRgba32(quantized.Configuration, quantized.Palette.Span, rgbaPaletteSpan);
ref Rgba32 rgbaPaletteRef = ref MemoryMarshal.GetReference(rgbaPaletteSpan);

4
src/ImageSharp/Processing/Processors/Quantization/WuFrameQuantizer{TPixel}.cs

@ -632,7 +632,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Quantization
/// </summary>
private void BuildCube()
{
Span<double> vv = stackalloc double[this.maxColors];
// Store the volume variance.
using IMemoryOwner<double> vvOwner = this.Configuration.MemoryAllocator.Allocate<double>(this.maxColors);
Span<double> vv = vvOwner.GetSpan();
ref Box cube = ref this.colorCube[0];
cube.RMin = cube.GMin = cube.BMin = cube.AMin = 0;

Loading…
Cancel
Save