Skip to content

Commit

Permalink
Merge pull request #47 from ilamp/main
Browse files Browse the repository at this point in the history
Restoring Martialing to fix buffer length issues
  • Loading branch information
wherewhere authored Apr 4, 2023
2 parents 9c72a09 + 7691321 commit e4d83fd
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void ToImageTest()
FramebufferHeader header = FramebufferHeader.Read(data);
header.Width = 1;
header.Height = 1;

header.Size = 4;
byte[] framebuffer = File.ReadAllBytes("Assets/framebuffer.bin");
using Bitmap image = (Bitmap)header.ToImage(framebuffer);
Assert.NotNull(image);
Expand Down
18 changes: 16 additions & 2 deletions AdvancedSharpAdbClient/Framebuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Drawing;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Buffers;

#if NET
using System.Runtime.Versioning;
Expand Down Expand Up @@ -35,7 +37,8 @@ public Framebuffer(DeviceData device, AdbClient client)
this.client = client ?? throw new ArgumentNullException(nameof(client));

// Initialize the headerData buffer
headerData = new byte[52];
var size = Marshal.SizeOf(default(FramebufferHeader));
this.headerData = new byte[size];
}

/// <summary>
Expand Down Expand Up @@ -84,7 +87,13 @@ public async Task RefreshAsync(CancellationToken cancellationToken = default)

if (Data == null || Data.Length < Header.Size)
{
Data = new byte[Header.Size];
// Optimization on .NET Core: Use the BufferPool to rent buffers
if (Data != null)
{
ArrayPool<byte>.Shared.Return(Data, clearArray: false);
}

Data = ArrayPool<byte>.Shared.Rent((int)Header.Size);
}

// followed by the actual framebuffer content
Expand All @@ -108,6 +117,11 @@ public Image ToImage()
/// <inheritdoc/>
public void Dispose()
{
if (Data != null)
{
ArrayPool<byte>.Shared.Return(Data, clearArray: false);
}

headerData = null;
headerInitialized = false;
disposed = true;
Expand Down
10 changes: 5 additions & 5 deletions AdvancedSharpAdbClient/Models/FramebufferHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public Image ToImage(byte[] buffer)
#if HAS_Process
Bitmap bitmap = new((int)Width, (int)Height, pixelFormat);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, pixelFormat);
Marshal.Copy(buffer, 0, bitmapData.Scan0, buffer.Length);
Marshal.Copy(buffer, 0, bitmapData.Scan0, (int)Size);
bitmap.UnlockBits(bitmapData);

return bitmap;
Expand Down Expand Up @@ -196,10 +196,10 @@ private PixelFormat StandardizePixelFormat(byte[] buffer)
throw new ArgumentNullException(nameof(buffer));
}

if (buffer.Length != Width * Height * (Bpp / 8))
if (buffer.Length < Width * Height * (Bpp / 8))
{
throw new ArgumentOutOfRangeException(nameof(buffer), $"The buffer length {buffer.Length} does not match the expected buffer " +
$"length for a picture of width {Width}, height {Height} and pixel depth {Bpp}");
throw new ArgumentOutOfRangeException(nameof(buffer), $"The buffer length {buffer.Length} is less than expected buffer " +
$"length ({Width * Height * (Bpp / 8)}) for a picture of width {Width}, height {Height} and pixel depth {Bpp}");
}

if (Width == 0 || Height == 0 || Bpp == 0)
Expand Down Expand Up @@ -230,7 +230,7 @@ private PixelFormat StandardizePixelFormat(byte[] buffer)
uint alphaIndex = Alpha.Offset / 8;

// Loop over the array and re-order as required
for (int i = 0; i < buffer.Length; i += 4)
for (int i = 0; i < (int)Size; i += 4)
{
byte red = buffer[i + redIndex];
byte blue = buffer[i + blueIndex];
Expand Down

0 comments on commit e4d83fd

Please sign in to comment.