Skip to content
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 83 additions & 10 deletions src/Magick.NET/Helpers/ByteArrayWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,76 @@

using System;
using System.IO;
#if NETSTANDARD2_1_OR_GREATER
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
using System.Buffers;
#endif

namespace ImageMagick;

internal sealed unsafe class ByteArrayWrapper
{
#if NETSTANDARD2_1_OR_GREATER

private static readonly ArrayPool<byte> _pool = ArrayPool<byte>.Create(1024 * 1024 * 64, 1024);
Comment thread
dlemstra marked this conversation as resolved.
Outdated

private byte[] _bytes = _pool.Rent(1024 * 1024);
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated

#else

private byte[] _bytes = new byte[8192];
private int _offset = 0;
private int _length = 0;

#endif
private int _offset;

private int _length;

#if NETSTANDARD2_1_OR_GREATER
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated

public byte[] GetBytes()
{
var result = new byte[_length];
Array.Copy(_bytes, result, Math.Min(_bytes.Length, _length));
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
_pool.Return(_bytes, true);
return result;
}

~ByteArrayWrapper()
{
if (_bytes is not null)
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
{
_pool.Return(_bytes, true);
}
}
#else

public byte[] GetBytes()
{
ResizeBytes(_length);

return _bytes;
}

#endif
public long Read(IntPtr data, UIntPtr count, IntPtr user_data)
{
if (data == IntPtr.Zero)
{
return 0;
}

var total = (int)count;

if (total == 0)
{
return 0;
}

var length = Math.Min(total, _length - _offset);

if (length != 0)
{
var destination = (byte*)data.ToPointer();

fixed (byte* source = _bytes)
{
NativeMemory.Copy(source + _offset, destination, length);
Expand All @@ -45,42 +87,53 @@ public long Read(IntPtr data, UIntPtr count, IntPtr user_data)
public long Seek(long offset, IntPtr whence, IntPtr user_data)
{
var newOffset = (int)((SeekOrigin)whence switch
{
SeekOrigin.Begin => offset,
SeekOrigin.Current => _offset + offset,
SeekOrigin.End => _length - offset,
_ => -1,
});
{
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
SeekOrigin.Begin => offset,
SeekOrigin.Current => _offset + offset,
SeekOrigin.End => _length - offset,
_ => -1,
});

if (_offset == newOffset)
{
return _offset;
}

if (newOffset < 0)
{
return -1;
}

_offset = newOffset;

return _offset;
}

public long Tell(IntPtr user_data)
=> _offset;
{
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
return _offset;
}

public long Write(IntPtr data, UIntPtr count, IntPtr user_data)
{
if (data == IntPtr.Zero)
{
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
return 0;
}

var total = (int)count;

if (total == 0)
{
return 0;
}

var newOffset = _offset + total;

EnsureLength(newOffset);

var source = (byte*)data.ToPointer();

Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
fixed (byte* destination = _bytes)
{
NativeMemory.Copy(source, destination + _offset, total);
Expand All @@ -94,17 +147,37 @@ public long Write(IntPtr data, UIntPtr count, IntPtr user_data)
private void EnsureLength(int length)
{
if (length < _length)
{
return;
}

_length = length;

if (_length < _bytes.Length)
{
return;
}

var newLength = Math.Max(_bytes.Length * 2, _length);
ResizeBytes(newLength);
}

#if NETSTANDARD2_1_OR_GREATER

private void ResizeBytes(int length)
{
byte[] byte2 = _pool.Rent(length);
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
Array.Copy(_bytes, byte2, Math.Min(_bytes.Length, length));
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
_pool.Return(_bytes, true);
Comment thread
Wak1Z2 marked this conversation as resolved.
Outdated
_bytes = byte2;
}

#else

private void ResizeBytes(int length)
=> Array.Resize(ref _bytes, length);
{
Array.Resize(ref _bytes, length);
}

#endif
}