From 3ca1a1847441c3f3bd8ea7c499c1bdaeb2841f7f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 22 Jun 2021 12:12:03 +1000 Subject: [PATCH] Update TgaDecoderCore.cs --- src/ImageSharp/Formats/Tga/TgaDecoderCore.cs | 21 ++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs index f717c2230..8f9786140 100644 --- a/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs +++ b/src/ImageSharp/Formats/Tga/TgaDecoderCore.cs @@ -114,9 +114,10 @@ namespace SixLabors.ImageSharp.Formats.Tga int colorMapPixelSizeInBytes = this.fileHeader.CMapDepth / 8; int colorMapSizeInBytes = this.fileHeader.CMapLength * colorMapPixelSizeInBytes; - using (IManagedByteBuffer palette = this.memoryAllocator.AllocateManagedByteBuffer(colorMapSizeInBytes, AllocationOptions.Clean)) + using (IMemoryOwner palette = this.memoryAllocator.Allocate(colorMapSizeInBytes, AllocationOptions.Clean)) { - this.currentStream.Read(palette.Array, this.fileHeader.CMapStart, colorMapSizeInBytes); + Span paletteSpan = palette.GetSpan(); + this.currentStream.Read(paletteSpan, this.fileHeader.CMapStart, colorMapSizeInBytes); if (this.fileHeader.ImageType == TgaImageType.RleColorMapped) { @@ -124,7 +125,7 @@ namespace SixLabors.ImageSharp.Formats.Tga this.fileHeader.Width, this.fileHeader.Height, pixels, - palette.Array, + paletteSpan, colorMapPixelSizeInBytes, origin); } @@ -134,7 +135,7 @@ namespace SixLabors.ImageSharp.Formats.Tga this.fileHeader.Width, this.fileHeader.Height, pixels, - palette.Array, + paletteSpan, colorMapPixelSizeInBytes, origin); } @@ -224,7 +225,7 @@ namespace SixLabors.ImageSharp.Formats.Tga /// The color palette. /// Color map size of one entry in bytes. /// The image origin. - private void ReadPaletted(int width, int height, Buffer2D pixels, byte[] palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) + private void ReadPaletted(int width, int height, Buffer2D pixels, Span palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { TPixel color = default; @@ -304,7 +305,7 @@ namespace SixLabors.ImageSharp.Formats.Tga /// The color palette. /// Color map size of one entry in bytes. /// The image origin. - private void ReadPalettedRle(int width, int height, Buffer2D pixels, byte[] palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) + private void ReadPalettedRle(int width, int height, Buffer2D pixels, Span palette, int colorMapPixelSizeInBytes, TgaImageOrigin origin) where TPixel : unmanaged, IPixel { int bytesPerPixel = 1; @@ -704,7 +705,7 @@ namespace SixLabors.ImageSharp.Formats.Tga } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadPalettedBgra16Pixel(byte[] palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) + private void ReadPalettedBgra16Pixel(Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { int colorIndex = this.currentStream.ReadByte(); @@ -713,7 +714,7 @@ namespace SixLabors.ImageSharp.Formats.Tga } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadPalettedBgra16Pixel(byte[] palette, int index, int colorMapPixelSizeInBytes, ref TPixel color) + private void ReadPalettedBgra16Pixel(Span palette, int index, int colorMapPixelSizeInBytes, ref TPixel color) where TPixel : unmanaged, IPixel { Bgra5551 bgra = default; @@ -729,7 +730,7 @@ namespace SixLabors.ImageSharp.Formats.Tga } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadPalettedBgr24Pixel(byte[] palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) + private void ReadPalettedBgr24Pixel(Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { int colorIndex = this.currentStream.ReadByte(); @@ -738,7 +739,7 @@ namespace SixLabors.ImageSharp.Formats.Tga } [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void ReadPalettedBgra32Pixel(byte[] palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) + private void ReadPalettedBgra32Pixel(Span palette, int colorMapPixelSizeInBytes, int x, TPixel color, Span pixelRow) where TPixel : unmanaged, IPixel { int colorIndex = this.currentStream.ReadByte();