1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
using System;
using System.Drawing;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Emgu.CV;
using Emgu.CV.CvEnum;
namespace UVtools.Core
{
public sealed class MatBytes : IDisposable
{
private bool m_IsDisposed;
/// <summary>
/// Gets the byte structure of this Mat
/// </summary>
public byte[] Bytes;
/// <summary>
/// Gets the <see cref="Mat"/>
/// </summary>
public Mat Mat { get; }
/// <summary>
/// Gets the <see cref="GCHandle"/> for the allocated bytes
/// </summary>
public GCHandle Handle { get; }
public byte this[int index]
{
get => Bytes[index];
set => Bytes[index] = value;
}
public byte this[int x, int y]
{
get => Bytes[y * Mat.Width + x];
set => Bytes[y * Mat.Width + x] = value;
}
public MatBytes(Mat mat) : this(mat.Size, (byte) mat.NumberOfChannels, mat.Depth)
{
var s = mat.DataPointer;
}
public MatBytes(Size size, byte channels = 1, DepthType depth = DepthType.Cv8U)
{
Bytes = new byte[size.Width * size.Height * channels];
Handle = GCHandle.Alloc(Bytes, GCHandleType.Pinned);
Mat = new Mat(size, depth, channels, Handle.AddrOfPinnedObject(), size.Width * channels);
}
public MatBytes(Size size, out byte[] bytes, byte channels = 1, DepthType depth = DepthType.Cv8U) : this(size, channels, depth)
{
bytes = Bytes;
}
public void Free()
{
Handle.Free();
}
[MethodImpl(MethodImplOptions.Synchronized)]
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool isDisposing)
{
if (m_IsDisposed)
return;
if (isDisposing)
{
Mat?.Dispose();
Free();
}
m_IsDisposed = true;
}
}
}
|