-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectBitmap.cs
More file actions
151 lines (121 loc) · 4.12 KB
/
DirectBitmap.cs
File metadata and controls
151 lines (121 loc) · 4.12 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace SimpleEngine;
/// <summary>
/// A custom bitmap implementation that uses direct memory access for very fast pixel calls.
/// </summary>
public class DirectBitmap : IDisposable
{
public DirectBitmap(int width, int height)
{
Width = width;
Height = height;
Bits = new int[width * height];
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(width, height, width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
}
private DirectBitmap(DirectBitmap original)
{
Width = original.Width;
Height = original.Height;
Bits = (int[])original.Bits.Clone();
BitsHandle = GCHandle.Alloc(Bits, GCHandleType.Pinned);
Bitmap = new Bitmap(Width, Height, Width * 4, PixelFormat.Format32bppPArgb, BitsHandle.AddrOfPinnedObject());
}
public Bitmap Bitmap { get; }
public int[] Bits { get; }
public bool Disposed { get; private set; }
public int Height { get; }
public int Width { get; }
protected GCHandle BitsHandle { get; }
public void Dispose()
{
if (Disposed) return;
Disposed = true;
Bitmap.Dispose();
BitsHandle.Free();
}
~DirectBitmap()
{
Dispose();
}
public void SetPixel(int x, int y, Color color)
{
var index = x + y * Width;
var col = color.ToArgb();
Bits[index] = col;
}
public Color GetPixel(int x, int y)
{
var index = x + y * Width;
var col = Bits[index];
var result = Color.FromArgb(col);
return result;
}
int[,] To2DArray()
{
int[,] result = new int[Width, Height];
// Convert the 1D pixel array into 2D
for (int y = 0; y < Height; y++)
{
int pY = y * Width;
for (int x = 0; x < Width; x++)
{
result[x, y] = Bits[x + pY];
}
}
return result;
}
public DirectBitmap Clone()
{
return new DirectBitmap(this);
}
public Bitmap ToGrayscale()
{
var result = new Bitmap(this.Width, this.Height, PixelFormat.Format8bppIndexed);
BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// Copy the bytes from the image into a byte array
byte[] bytes = new byte[data.Height * data.Stride];
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
for (int y = 0; y < this.Height; y++)
{
for (int x = 0; x < this.Width; x++)
{
var c = this.GetPixel(x, y);
var rgb = (byte)((c.B + c.G + c.R) / 3);
bytes[y * data.Stride + x] = rgb;
}
}
// Copy the bytes from the byte array into the image
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
result.UnlockBits(data);
return result;
}
public Bitmap ToTensorFormat()
{
var result = new Bitmap(this.Width, this.Height, PixelFormat.Format8bppIndexed);
BitmapData data = result.LockBits(new Rectangle(0, 0, result.Width, result.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);
// Copy the bytes from the image into a byte array
byte[] bytes = new byte[data.Height * data.Stride];
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
for (int y = 0; y < this.Height; y++)
{
for (int x = 0; x < this.Width; x++)
{
var c = this.GetPixel(x, y);
if (c.Name.Equals("ffffffff")) {
c = Color.Black;
} else
{
c = Color.White;
}
var rgb = (byte)((c.B + c.G + c.R) / 3);
bytes[y * data.Stride + x] = rgb;
}
}
// Copy the bytes from the byte array into the image
Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);
result.UnlockBits(data);
return result;
}
}