- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectBitmap.cs
More file actions
Latest commit
151 lines (121 loc) · 4.12 KB
/
Copy pathDirectBitmap.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
usingSystem.Drawing.Imaging;
usingSystem.Runtime.InteropServices;
namespaceSimpleEngine;
/// <summary>
/// A custom bitmap implementation that uses direct memory access for very fast pixel calls.
/// </summary>
publicclassDirectBitmap:IDisposable
{
publicDirectBitmap(intwidth,intheight)
{
Width=width;
Height=height;
Bits=newint[width*height];
BitsHandle=GCHandle.Alloc(Bits,GCHandleType.Pinned);
Bitmap=newBitmap(width,height,width*4,PixelFormat.Format32bppPArgb,BitsHandle.AddrOfPinnedObject());
}
privateDirectBitmap(DirectBitmaporiginal)
{
Width=original.Width;
Height=original.Height;
Bits=(int[])original.Bits.Clone();
BitsHandle=GCHandle.Alloc(Bits,GCHandleType.Pinned);
Bitmap=newBitmap(Width,Height,Width*4,PixelFormat.Format32bppPArgb,BitsHandle.AddrOfPinnedObject());
}
publicBitmapBitmap{get;}
publicint[]Bits{get;}
publicboolDisposed{get;privateset;}
publicintHeight{get;}
publicintWidth{get;}
protectedGCHandleBitsHandle{get;}
publicvoidDispose()
{
if(Disposed)return;
Disposed=true;
Bitmap.Dispose();
BitsHandle.Free();
}
~DirectBitmap()
{
Dispose();
}
publicvoidSetPixel(intx,inty,Colorcolor)
{
varindex=x+y*Width;
varcol=color.ToArgb();
Bits[index]=col;
}
publicColorGetPixel(intx,inty)
{
varindex=x+y*Width;
varcol=Bits[index];
varresult=Color.FromArgb(col);
returnresult;
}
int[,]To2DArray()
{
int[,]result=newint[Width,Height];
// Convert the 1D pixel array into 2D
for(inty=0;y<Height;y++)
{
intpY=y*Width;
for(intx=0;x<Width;x++)
{
result[x,y]=Bits[x+pY];
}
}
returnresult;
}
publicDirectBitmapClone()
{
returnnewDirectBitmap(this);
}
publicBitmapToGrayscale()
{
varresult=newBitmap(this.Width,this.Height,PixelFormat.Format8bppIndexed);
BitmapDatadata=result.LockBits(newRectangle(0,0,result.Width,result.Height),ImageLockMode.WriteOnly,PixelFormat.Format8bppIndexed);
// Copy the bytes from the image into a byte array
byte[]bytes=newbyte[data.Height*data.Stride];
Marshal.Copy(data.Scan0,bytes,0,bytes.Length);
for(inty=0;y<this.Height;y++)
{
for(intx=0;x<this.Width;x++)
{
varc=this.GetPixel(x,y);
varrgb=(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);
returnresult;
}
publicBitmapToTensorFormat()
{
varresult=newBitmap(this.Width,this.Height,PixelFormat.Format8bppIndexed);
BitmapDatadata=result.LockBits(newRectangle(0,0,result.Width,result.Height),ImageLockMode.WriteOnly,PixelFormat.Format8bppIndexed);
// Copy the bytes from the image into a byte array
byte[]bytes=newbyte[data.Height*data.Stride];
Marshal.Copy(data.Scan0,bytes,0,bytes.Length);
for(inty=0;y<this.Height;y++)
{
for(intx=0;x<this.Width;x++)
{
varc=this.GetPixel(x,y);
if(c.Name.Equals("ffffffff")){
c=Color.Black;
}else
{
c=Color.White;
}
varrgb=(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);
returnresult;
}
}