Browse Source

- Use memory manager in Bytes

pull/431/head
Lauri Kotilainen 8 years ago
parent
commit
26155568f6
  1. 26
      src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs
  2. 11
      src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs
  3. 2
      src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs

26
src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/Bytes.cs

@ -6,6 +6,8 @@ using System.Buffers;
using System.IO;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
{
/// <summary>
@ -25,12 +27,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// buffer[i:j] are the buffered bytes read from the underlying
/// stream that haven't yet been passed further on.
/// </summary>
public byte[] Buffer;
public Buffer<byte> Buffer;
/// <summary>
/// Values of <see cref="Buffer"/> converted to <see cref="int"/>-s
/// </summary>
public int[] BufferAsInt;
public Buffer<int> BufferAsInt;
/// <summary>
/// Start of bytes read
@ -48,20 +50,17 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// </summary>
public int UnreadableBytes;
private static readonly ArrayPool<byte> BytePool = ArrayPool<byte>.Create(BufferSize, 50);
private static readonly ArrayPool<int> IntPool = ArrayPool<int>.Create(BufferSize, 50);
/// <summary>
/// Creates a new instance of the <see cref="Bytes"/>, and initializes it's buffer.
/// </summary>
/// <param name="memoryManager">The <see cref="MemoryManager"/> to use for buffer allocations.</param>
/// <returns>The bytes created</returns>
public static Bytes Create()
public static Bytes Create(MemoryManager memoryManager)
{
return new Bytes
{
Buffer = BytePool.Rent(BufferSize),
BufferAsInt = IntPool.Rent(BufferSize)
Buffer = memoryManager.Allocate<byte>(BufferSize),
BufferAsInt = memoryManager.Allocate<int>(BufferSize)
};
}
@ -70,11 +69,8 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// </summary>
public void Dispose()
{
if (this.Buffer != null)
{
BytePool.Return(this.Buffer);
IntPool.Return(this.BufferAsInt);
}
this.Buffer?.Dispose();
this.BufferAsInt?.Dispose();
this.Buffer = null;
this.BufferAsInt = null;
@ -244,7 +240,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
}
// Fill in the rest of the buffer.
int n = inputStream.Read(this.Buffer, this.J, this.Buffer.Length - this.J);
int n = inputStream.Read(this.Buffer.Array, this.J, this.Buffer.Length - this.J);
if (n == 0)
{
return OrigDecoderErrorCode.UnexpectedEndOfStream;

11
src/ImageSharp/Formats/Jpeg/GolangPort/Components/Decoder/InputProcessor.cs

@ -5,6 +5,8 @@ using System;
using System.IO;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Memory;
namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
{
/// <summary>
@ -26,12 +28,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
/// <summary>
/// Initializes a new instance of the <see cref="InputProcessor"/> struct.
/// </summary>
/// <param name="memoryManager">The <see cref="MemoryManager"/> to use for buffer allocations.</param>
/// <param name="inputStream">The input <see cref="Stream"/></param>
/// <param name="temp">Temporal buffer, same as <see cref="OrigJpegDecoderCore.Temp"/></param>
public InputProcessor(Stream inputStream, byte[] temp)
public InputProcessor(MemoryManager memoryManager, Stream inputStream, byte[] temp)
{
this.Bits = default(Bits);
this.Bytes = Bytes.Create();
this.Bytes = Bytes.Create(memoryManager);
this.InputStream = inputStream;
this.Temp = temp;
this.LastErrorCode = OrigDecoderErrorCode.NoError;
@ -155,13 +158,13 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort.Components.Decoder
{
if (this.Bytes.J - this.Bytes.I >= length)
{
Array.Copy(this.Bytes.Buffer, this.Bytes.I, data, offset, length);
Array.Copy(this.Bytes.Buffer.Array, this.Bytes.I, data, offset, length);
this.Bytes.I += length;
length -= length;
}
else
{
Array.Copy(this.Bytes.Buffer, this.Bytes.I, data, offset, this.Bytes.J - this.Bytes.I);
Array.Copy(this.Bytes.Buffer.Array, this.Bytes.I, data, offset, this.Bytes.J - this.Bytes.I);
offset += this.Bytes.J - this.Bytes.I;
length -= this.Bytes.J - this.Bytes.I;
this.Bytes.I += this.Bytes.J - this.Bytes.I;

2
src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs

@ -210,7 +210,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
{
this.MetaData = new ImageMetaData();
this.InputStream = stream;
this.InputProcessor = new InputProcessor(stream, this.Temp);
this.InputProcessor = new InputProcessor(this.configuration.MemoryManager, stream, this.Temp);
// Check for the Start Of Image marker.
this.InputProcessor.ReadFull(this.Temp, 0, 2);

Loading…
Cancel
Save