Dashboard/Quik/Media/Color/ImageBuffer.cs

70 lines
1.9 KiB
C#
Raw Normal View History

using System;
using System.Runtime.InteropServices;
2024-05-15 22:17:01 +02:00
namespace Quik.Media.Color
{
2023-09-22 18:30:17 +02:00
public class QImageBuffer : QImage
{
private byte[] buffer;
GCHandle handle;
2024-05-15 22:17:01 +02:00
private bool isSdf = false;
2023-09-22 18:30:17 +02:00
public override QImageFormat InternalFormat { get; }
public override int Width { get; }
public override int Height { get; }
public override int Depth { get; }
2024-05-15 22:17:01 +02:00
public override bool IsSdf => isSdf;
2023-09-22 18:30:17 +02:00
public QImageBuffer(QImageFormat format, int width, int height, int depth = 1)
{
2023-09-22 18:30:17 +02:00
InternalFormat = format;
Width = width;
Height = height;
Depth = depth;
2024-05-15 22:17:01 +02:00
buffer = new byte[width * height * depth];
}
2023-09-22 18:30:17 +02:00
~QImageBuffer()
{
Dispose(false);
}
2023-09-22 18:30:17 +02:00
private QImageLock Lock()
{
2024-05-15 22:17:01 +02:00
if (handle.IsAllocated) handle.Free();
handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, 0);
2023-09-22 18:30:17 +02:00
return new QImageLock(InternalFormat, Width, Height, Depth, ptr);
}
2023-09-22 18:30:17 +02:00
protected override void Dispose(bool disposing)
{
2023-09-22 18:30:17 +02:00
buffer = null;
2024-05-15 22:17:01 +02:00
if (handle.IsAllocated) handle.Free();
2023-09-22 18:30:17 +02:00
GC.SuppressFinalize(this);
}
2023-09-22 18:30:17 +02:00
public override void LockBits2d(out QImageLock imageLock, QImageLockOptions options)
{
2024-05-15 22:17:01 +02:00
imageLock = Lock();
2023-09-22 18:30:17 +02:00
}
public override void LockBits3d(out QImageLock imageLock, QImageLockOptions options)
{
2024-05-15 22:17:01 +02:00
imageLock = Lock();
2023-09-22 18:30:17 +02:00
}
public override void LockBits3d(out QImageLock imageLock, QImageLockOptions options, int depth)
{
2024-05-15 22:17:01 +02:00
imageLock = Lock();
}
2023-09-22 18:30:17 +02:00
public override void UnlockBits()
{
2024-05-15 22:17:01 +02:00
handle.Free();
}
2024-05-15 22:17:01 +02:00
public void SetSdf(bool value = true) => isSdf = value;
}
}