-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.Capture.cs
More file actions
256 lines (227 loc) · 9.06 KB
/
Copy pathMainWindow.Capture.cs
File metadata and controls
256 lines (227 loc) · 9.06 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows;
using Windows.Media.Ocr;
using BitmapAlphaMode = Windows.Graphics.Imaging.BitmapAlphaMode;
using BitmapPixelFormat = Windows.Graphics.Imaging.BitmapPixelFormat;
namespace ScreenSearchOverlay;
public partial class MainWindow : Window
{
// FNV-1a 64-bit over 1 row out of 8. Sampling makes it ~8× faster on
// 4K screens; collision risk is irrelevant in practice because any real
// scroll moves pixels across all rows.
private static unsafe ulong ComputeHash(Bitmap bmp)
{
const ulong FNV_OFFSET = 14695981039346656037UL;
const ulong FNV_PRIME = 1099511628211UL;
var data = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
try
{
ulong h = FNV_OFFSET;
byte* p = (byte*)data.Scan0;
int stride = data.Stride;
int height = bmp.Height;
int qwords = stride / 8;
for (int y = 0; y < height; y += 8)
{
ulong* row = (ulong*)(p + y * stride);
for (int i = 0; i < qwords; i++)
{
h ^= row[i];
h *= FNV_PRIME;
}
}
return h;
}
finally { bmp.UnlockBits(data); }
}
internal static Bitmap CaptureScreen(int x, int y, int width, int height)
{
var bmp = new Bitmap(width, height);
using var g = Graphics.FromImage(bmp);
g.CopyFromScreen(x, y, 0, 0, new System.Drawing.Size(width, height));
return bmp;
}
// Push pixels from the captured GDI bitmap into the persistent
// WriteableBitmap. Single memcpy into the WPF back buffer; no GC,
// no WIC, no HBITMAP — sub-5 ms even at 4K. UI-thread only.
private void UpdateScreenshotBitmap(Bitmap src)
{
var w = src.Width;
var h = src.Height;
// Defensive: realloc if dimensions ever change (e.g. monitor swap)
if (w != _screenshotBitmap.PixelWidth || h != _screenshotBitmap.PixelHeight)
{
_screenshotBitmap = new System.Windows.Media.Imaging.WriteableBitmap(
w, h, 96, 96, System.Windows.Media.PixelFormats.Bgra32, null);
ScreenshotImage.Source = _screenshotBitmap;
}
var data = src.LockBits(
new System.Drawing.Rectangle(0, 0, w, h),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
try
{
// Format32bppArgb (System.Drawing) and Bgra32 (WPF) are the same
// byte layout in little-endian — direct memcpy is correct.
_screenshotBitmap.WritePixels(
new Int32Rect(0, 0, w, h),
data.Scan0,
data.Stride * h,
data.Stride);
}
finally
{
src.UnlockBits(data);
}
}
// Pre-computed gamma 0.7 LUT — applied per-pixel to give the OCR a
// moderate contrast boost without scanning min/max twice.
private static readonly byte[] GammaLut = BuildGammaLut(0.7);
private static byte[] BuildGammaLut(double gamma)
{
var lut = new byte[256];
var invG = 1.0 / gamma;
for (int i = 0; i < 256; i++)
{
var v = Math.Pow(i / 255.0, invG) * 255.0;
lut[i] = (byte)Math.Clamp(v, 0, 255);
}
return lut;
}
// Single-pass `unsafe` grayscale + gamma boost. No byte[] allocations,
// no Marshal.Copy round-trip — direct pointer reads/writes against the
// locked DIB. ~3-4× faster than the old two-pass + Marshal.Copy version.
// BT.601 weights in fixed-point: (29*B + 150*G + 77*R + 128) >> 8.
private static unsafe Bitmap EnhanceForOcr(Bitmap source)
{
var width = source.Width;
var height = source.Height;
var enhanced = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var srcData = source.LockBits(
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var dstData = enhanced.LockBits(
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.Imaging.ImageLockMode.WriteOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
try
{
int srcStride = srcData.Stride;
int dstStride = dstData.Stride;
byte* srcBase = (byte*)srcData.Scan0;
byte* dstBase = (byte*)dstData.Scan0;
fixed (byte* lut = GammaLut)
{
for (int y = 0; y < height; y++)
{
byte* s = srcBase + y * srcStride;
byte* d = dstBase + y * dstStride;
for (int x = 0; x < width; x++)
{
// BGRA byte order
int gray = (29 * s[0] + 150 * s[1] + 77 * s[2] + 128) >> 8;
byte v = lut[gray];
d[0] = v;
d[1] = v;
d[2] = v;
d[3] = 255;
s += 4;
d += 4;
}
}
}
}
finally
{
source.UnlockBits(srcData);
enhanced.UnlockBits(dstData);
}
return enhanced;
}
private static Windows.Graphics.Imaging.SoftwareBitmap BitmapToSoftwareBitmap(Bitmap source)
{
var width = source.Width;
var height = source.Height;
var data = source.LockBits(
new System.Drawing.Rectangle(0, 0, width, height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
var bytes = Math.Abs(data.Stride) * height;
var pixels = new byte[bytes];
Marshal.Copy(data.Scan0, pixels, 0, bytes);
source.UnlockBits(data);
var sb = new Windows.Graphics.Imaging.SoftwareBitmap(
BitmapPixelFormat.Bgra8, width, height, BitmapAlphaMode.Premultiplied);
sb.CopyFromBuffer(pixels.AsBuffer());
return sb;
}
private async Task RunOcrAsync(Bitmap screenshot)
{
// Capture UI-bound dimensions before going to the background thread
var actualW = ActualWidth;
var actualH = ActualHeight;
// Enhance + convert to SoftwareBitmap entirely off the UI thread —
// these are the most expensive sync steps in the OCR pipeline (full
// double pass over every pixel + a copy into a Windows.Graphics buffer).
using var softwareBitmap = await Task.Run(() =>
{
using var enhanced = EnhanceForOcr(screenshot);
return BitmapToSoftwareBitmap(enhanced);
});
var scaleX = actualW / softwareBitmap.PixelWidth;
var scaleY = actualH / softwareBitmap.PixelHeight;
_ocrWords.Clear();
var app = (App)System.Windows.Application.Current;
var languages = app.SelectedOcrLanguages;
// Run OCR for each selected language in parallel
var tasks = new List<Task<OcrResult>>();
foreach (var lang in languages)
{
var engine = OcrEngine.TryCreateFromLanguage(lang);
if (engine != null)
tasks.Add(engine.RecognizeAsync(softwareBitmap).AsTask());
}
if (tasks.Count == 0) return;
var results = await Task.WhenAll(tasks);
// Merge results from all languages, deduplicate by position
foreach (var result in results)
{
foreach (var line in result.Lines)
{
foreach (var word in line.Words)
{
var bounds = new Rect(
word.BoundingRect.X * scaleX,
word.BoundingRect.Y * scaleY,
word.BoundingRect.Width * scaleX,
word.BoundingRect.Height * scaleY);
// Skip if we already have a word at roughly the same position
bool duplicate = false;
foreach (var existing in _ocrWords)
{
if (Math.Abs(existing.Bounds.X - bounds.X) < 5 &&
Math.Abs(existing.Bounds.Y - bounds.Y) < 5)
{
duplicate = true;
break;
}
}
if (!duplicate)
{
_ocrWords.Add(new OcrWordInfo
{
Text = word.Text,
Bounds = bounds
});
}
}
}
}
}
}