Dashboard/Quik/Media/Color/ImageBuffer.cs

60 lines
1.4 KiB
C#

using System;
using System.Runtime.InteropServices;
namespace Quik.Media.Color
{
public class ImageBuffer : IDisposable
{
private byte[] buffer;
GCHandle handle;
public QImageFormat Format { get; }
public int Width { get; }
public int Height { get; }
public int Depth { get; }
public ImageBuffer(QImageFormat format, int width, int height, int depth = 1)
{
Format = format;
Width = width;
Height = height;
Depth = depth;
buffer = new byte[width * height * depth];
}
~ImageBuffer()
{
Dispose(false);
}
public QImageLock Lock()
{
handle.Free();
handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
return new QImageLock(Format, Width, Height, Depth, ptr);
}
public void Unlock()
{
handle.Free();
}
private bool isDiposed = false;
private void Dispose(bool disposing)
{
if (isDiposed) return;
buffer = null;
handle.Free();
isDiposed = true;
GC.SuppressFinalize(this);
}
public void Dispose()
{
Dispose(true);
}
}
}