-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathScreenshotToTreeDoubleBuffered.cs
More file actions
executable file
·158 lines (120 loc) · 4.83 KB
/
ScreenshotToTreeDoubleBuffered.cs
File metadata and controls
executable file
·158 lines (120 loc) · 4.83 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
152
153
154
155
156
157
158
using Prefab;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrefabVideoCapture
{
class ScreenshotToTreeDoubleBuffered
{
private static int WIDTH = (int) System.Windows.SystemParameters.VirtualScreenWidth;
private static int HEIGHT = (int) System.Windows.SystemParameters.VirtualScreenHeight;
private int[] _currentBuffer = new int[WIDTH * HEIGHT];
private int[] _previousBuffer = new int[WIDTH * HEIGHT];
private Bitmap _currentFrame;
private Bitmap _previousFrame;
public ScreenshotToTreeDoubleBuffered()
{
}
public Tree GetScreenshotAndCreateTree(int width, int height, System.Drawing.Bitmap bitmap, Dictionary<string, object> tags)
{
CopyPixelsFromDrawingBitmap(bitmap, _currentBuffer);
_currentFrame = Bitmap.FromPixels(bitmap.Width, bitmap.Height, _currentBuffer);
// Perform diff
//if (_currentFrame.Pixels.Equals(_previousFrame.Pixels))
if (_currentFrame.Equals(_previousFrame))
{
return null;
}
IBoundingBox invalidated = GetDirtyRect(_previousFrame, _currentFrame);
tags.Add("invalidated", invalidated);
Tree root = Tree.FromPixels(_currentFrame, tags);
//Swap the buffers we're writting to
int[] tmp = _currentBuffer;
_currentBuffer = _previousBuffer;
_previousBuffer = tmp;
//Set the previous bitmap
_previousFrame = Bitmap.FromPixels(_currentFrame.Width, _currentFrame.Height, _previousBuffer);
return root;
}
public static IBoundingBox GetDirtyRect(Bitmap previous, Bitmap current)
{
if (previous == null || previous.Width != current.Width || previous.Height != current.Height)
return new BoundingBox(0, 0, current.Width, current.Height);
List<RectData> rects = new List<RectData>();
Parallel.For<RectData>(0, current.Height,
() => new RectData(int.MaxValue, int.MaxValue, 0, 0),
(row, loop, bb) =>
{
for (int col = 0; col < current.Width; col++)
{
if (current[row, col] != previous[row, col])
{
if (row < bb.Top)
bb.Top = row;
if (col < bb.Left)
bb.Left = col;
if (col > bb.Right)
bb.Right = col;
if (row > bb.Bottom)
bb.Bottom = row;
}
}
return bb;
},
(bb) =>
{
lock (((ICollection)rects).SyncRoot)
rects.Add(bb);
}
);
IBoundingBox dirty = Union(rects);
return dirty;
}
private class RectData
{
public int Left, Top, Right, Bottom;
public RectData(int left, int top, int right, int bottom)
{
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
}
private static IBoundingBox Union(IEnumerable<RectData> rects)
{
int left = int.MaxValue;
int top = int.MaxValue;
int bottom = 0;
int right = 0;
bool assigned = false;
foreach (RectData rect in rects)
{
if (rect.Top != int.MaxValue)
{
left = Math.Min(left, rect.Left);
top = Math.Min(top, rect.Top);
bottom = Math.Max(bottom, rect.Bottom);
right = Math.Max(right, rect.Right);
assigned = true;
}
}
if (assigned)
return new BoundingBox(left, top, right - left + 1, bottom - top + 1);
return null;
}
public static void CopyPixelsFromDrawingBitmap(System.Drawing.Bitmap source, int[] buffer)
{
System.Drawing.Imaging.BitmapData bitmapData = source.LockBits(
new System.Drawing.Rectangle(0, 0, source.Width, source.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb
);
System.Runtime.InteropServices.Marshal.Copy(bitmapData.Scan0, buffer, 0, source.Width * source.Height);
source.UnlockBits(bitmapData);
}
}
}