Browse Source

Remove from Gif

pull/1677/head
James Jackson-South 5 years ago
parent
commit
9c96f023fc
  1. 25
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  2. 8
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs

25
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -33,7 +34,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
/// <summary> /// <summary>
/// The global color table. /// The global color table.
/// </summary> /// </summary>
private IManagedByteBuffer globalColorTable; private IMemoryOwner<byte> globalColorTable;
/// <summary> /// <summary>
/// The area to restore. /// The area to restore.
@ -323,12 +324,12 @@ namespace SixLabors.ImageSharp.Formats.Gif
continue; continue;
} }
using (IManagedByteBuffer commentsBuffer = this.MemoryAllocator.AllocateManagedByteBuffer(length)) using IMemoryOwner<byte> commentsBuffer = this.MemoryAllocator.Allocate<byte>(length);
{ Span<byte> commentsSpan = commentsBuffer.GetSpan();
this.stream.Read(commentsBuffer.Array, 0, length);
string commentPart = GifConstants.Encoding.GetString(commentsBuffer.Array, 0, length); this.stream.Read(commentsSpan);
stringBuilder.Append(commentPart); string commentPart = GifConstants.Encoding.GetString(commentsSpan);
} stringBuilder.Append(commentPart);
} }
if (stringBuilder.Length > 0) if (stringBuilder.Length > 0)
@ -348,7 +349,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
{ {
this.ReadImageDescriptor(); this.ReadImageDescriptor();
IManagedByteBuffer localColorTable = null; IMemoryOwner<byte> localColorTable = null;
Buffer2D<byte> indices = null; Buffer2D<byte> indices = null;
try try
{ {
@ -356,8 +357,8 @@ namespace SixLabors.ImageSharp.Formats.Gif
if (this.imageDescriptor.LocalColorTableFlag) if (this.imageDescriptor.LocalColorTableFlag)
{ {
int length = this.imageDescriptor.LocalColorTableSize * 3; int length = this.imageDescriptor.LocalColorTableSize * 3;
localColorTable = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(length, AllocationOptions.Clean); localColorTable = this.Configuration.MemoryAllocator.Allocate<byte>(length, AllocationOptions.Clean);
this.stream.Read(localColorTable.Array, 0, length); this.stream.Read(localColorTable.GetSpan());
} }
indices = this.Configuration.MemoryAllocator.Allocate2D<byte>(this.imageDescriptor.Width, this.imageDescriptor.Height, AllocationOptions.Clean); indices = this.Configuration.MemoryAllocator.Allocate2D<byte>(this.imageDescriptor.Width, this.imageDescriptor.Height, AllocationOptions.Clean);
@ -622,10 +623,10 @@ namespace SixLabors.ImageSharp.Formats.Gif
int globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3; int globalColorTableLength = this.logicalScreenDescriptor.GlobalColorTableSize * 3;
this.gifMetadata.GlobalColorTableLength = globalColorTableLength; this.gifMetadata.GlobalColorTableLength = globalColorTableLength;
this.globalColorTable = this.MemoryAllocator.AllocateManagedByteBuffer(globalColorTableLength, AllocationOptions.Clean); this.globalColorTable = this.MemoryAllocator.Allocate<byte>(globalColorTableLength, AllocationOptions.Clean);
// Read the global color table data from the stream // Read the global color table data from the stream
stream.Read(this.globalColorTable.Array, 0, globalColorTableLength); stream.Read(this.globalColorTable.GetSpan());
} }
} }
} }

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

@ -470,14 +470,16 @@ namespace SixLabors.ImageSharp.Formats.Gif
// The maximum number of colors for the bit depth // The maximum number of colors for the bit depth
int colorTableLength = ColorNumerics.GetColorCountForBitDepth(this.bitDepth) * Unsafe.SizeOf<Rgb24>(); int colorTableLength = ColorNumerics.GetColorCountForBitDepth(this.bitDepth) * Unsafe.SizeOf<Rgb24>();
using IManagedByteBuffer colorTable = this.memoryAllocator.AllocateManagedByteBuffer(colorTableLength, AllocationOptions.Clean); using IMemoryOwner<byte> colorTable = this.memoryAllocator.Allocate<byte>(colorTableLength, AllocationOptions.Clean);
Span<byte> colorTableSpan = colorTable.GetSpan();
PixelOperations<TPixel>.Instance.ToRgb24Bytes( PixelOperations<TPixel>.Instance.ToRgb24Bytes(
this.configuration, this.configuration,
image.Palette.Span, image.Palette.Span,
colorTable.GetSpan(), colorTableSpan,
image.Palette.Length); image.Palette.Length);
stream.Write(colorTable.Array, 0, colorTableLength); stream.Write(colorTableSpan);
} }
/// <summary> /// <summary>

Loading…
Cancel
Save