-
Notifications
You must be signed in to change notification settings - Fork 72
/
InMemoryWindowCapture.ahk
636 lines (618 loc) · 15.7 KB
/
InMemoryWindowCapture.ahk
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
; * Functions for searching/reading what is displayed by a window.
; *
; * License:
; * Display v0.1 by Xander, contributions from Dave (CreateWindowCapture, DeleteWindowCapture, GetPixel)
; * GNU General Public License 3.0 or higher: http://www.gnu.org/licenses/gpl-3.0.txt
; *
; * Link:
; * https://autohotkey.com/board/topic/22675-in-memory-window-capture/page-2
; *
/* Usage:
* Creates an offscreen capture of a window. The window cannot be minimized but may be invisible.
* Parameters:
* id: The window's id of which to create a capture
* device, context, pixels: Blank variables, see Releasing Memory below.
* Releasing Memory:
* After the capture is no longer needed, it's memory must be freed by calling Display_DeleteWindowCapture(device, context, pixels[color=black], id[/color])
* where the [color=black]4[/color] parameters are those that was passed to create the window capture.
*/
Display_CreateWindowCapture(ByRef device, ByRef context, ByRef pixels, ByRef id = "") {
if !id
WinGet, id, ID
device := DllCall("GetDC", UInt, id)
context := DllCall("gdi32.dll\CreateCompatibleDC", UInt, device)
WinGetPos, , , w, h, ahk_id %id%
pixels := DllCall("gdi32.dll\CreateCompatibleBitmap", UInt, device, Int, w, Int, h)
DllCall("gdi32.dll\SelectObject", UInt, context, UInt, pixels)
DllCall("PrintWindow", "UInt", id, UInt, context, UInt, 0)
}
Display_DeleteWindowCapture(ByRef device, ByRef context, ByRef pixels[color=black], ByRef id[/color]) {
DllCall("ReleaseDC", [color=black]UInt, id, [/color]UInt, device)
DllCall("gdi32.dll\DeleteDC", UInt, context)
DllCall("gdi32.dll\DeleteObject", UInt, pixels)
}
/* Usage:
* Gets the pixel from a window capture created from Display_CreateWindowCapture
* Parameters:
* context: the device context as given by Display_CreateWindowCapture
* x, y: the coordinate parameters
* Return:
* The pixel in BGR format.
*/
Display_GetPixel(ByRef context, x, y) {
return DllCall("GetPixel", UInt, context, Int, x, Int, y)
}
/* Usage:
* Searches for the specifed color in the given rectangle of a window capture created from Display_CreateWindowCapture
* Parameters:
* x, y, w, h: the rectangle parameters to search
* color: the color in BGR format or one of:
* BlueBackground, BlueForeground, GreenBackground, GreenForeground, RedBackground, RedForeground
* CyanBackground, CyanForeground, YellowBackground, YellowForeground, VioletBackground, VioletForeground
* DarkBackground, DarkForeground, LightBackground, LightForeground
* variation: the allowed variation from the specified color
* id: either a window id or the letter c followed by the device context handle as given by Display_CreateWindowCapture
* if no id is specified, the Last Found Window will be used.
* Return:
* Returns true if the specified color/variation is found within the given area, false otherwise.
*/
Display_PixelSearch(x, y, w, h, color, variation, ByRef id = "") {
Display_GetContext(device, context, pixels, id)
if color is not integer
isPixel = Display_%color%
Loop, %w% {
j := y
Loop, %h% {
bgr := Display_GetPixel(context, x, j++)
pixel := isPixel ? Display_IsPixel(isPixel, bgr, variation) : Display_CompareColors(bgr, color, variation)
if pixel {
if device
Display_DeleteWindowCapture(device, context, pixels[color=black], id[/color])
return true
}
}
x++
}
if device
Display_DeleteWindowCapture(device, context, pixels[color=black], id[/color])
return false
}
Display_GetContext(ByRef device, ByRef context, ByRef pixels, ByRef id) {
if !id
Display_CreateWindowCapture(device, context, pixels)
else if (SubStr(id, 1, 1) = "c")
context := SubStr(id, 2)
else
Display_CreateWindowCapture(device, context, pixels, id)
}
Display_CompareColors(ByRef bgr1, ByRef bgr2, ByRef variation) {
c1 := bgr1 & 0xff
c2 := bgr2 & 0xff
if (abs(c1 - c2) > variation)
return false
c1 := (bgr1 >> 8) & 0xff
c2 := (bgr2 >> 8) & 0xff
if (abs(c1 - c2) > variation)
return false
c1 := (bgr1 >> 16) & 0xff
c2 := (bgr2 >> 16) & 0xff
if (abs(c1 - c2) > variation)
return false
return true
}
Display_CompareRGBToBGR(ByRef rgb, ByRef bgr, ByRef variation) {
c1 := (rgb >> 16) & 0xff
c2 := bgr & 0xff
if (abs(c1 - c2) > variation)
return false
c1 := (rgb >> 8) & 0xff
c2 := (bgr >> 8) & 0xff
if (abs(c1 - c2) > variation)
return false
c1 := rgb & 0xff
c2 := (bgr >> 16) & 0xff
if (abs(c1 - c2) > variation)
return false
return true
}
Display_IsBlue(ByRef bgr, ByRef variation) {
r := bgr & 0xff
g := (bgr >> 8) & 0xff
b := ((bgr >> 16) & 0xff) - variation
return b > r && b > g
}
Display_IsGreen(ByRef bgr, ByRef variation) {
r := bgr & 0xff
g := ((bgr >> 8) & 0xff) - variation
b := (bgr >> 16) & 0xff
return g > r && g > b
}
Display_IsRed(ByRef bgr, ByRef variation) {
r := (bgr & 0xff) - variation
g := (bgr >> 8) & 0xff
b := (bgr >> 16) & 0xff
return r > b && r > g
}
Display_IsCyan(ByRef bgr, ByRef variation) {
g := (bgr >> 8) & 0xff
if (r < 120)
return false
b := (bgr >> 16) & 0xff
if (g < 120)
return false
d := abs(g-r)
if (d > 32 + variation)
return false
d += (bgr & 0xff) + 16
return r > d && g > d
}
Display_IsViolet(ByRef bgr, ByRef variation) {
r := bgr & 0xff
if (r < 120)
return false
b := (bgr >> 16) & 0xff
if (g < 120)
return false
d := abs(g-r)
if (d > 32 + variation)
return false
d += ((bgr >> 8) & 0xff) + 16
return r > d && g > d
}
Display_IsYellow(ByRef bgr, ByRef variation) {
r := bgr & 0xff
if (r < 120)
return false
g := (bgr >> 8) & 0xff
if (g < 120)
return false
d := abs(g-r)
if (d > 32 + variation)
return false
d += ((bgr >> 16) & 0xff) + 16
return r > d && g > d
}
Display_IsLight(ByRef bgr, ByRef variation) {
c := (bgr & 0xff) - variation
if (c < 200)
return false
c := ((bgr >> 8) & 0xff) - variation
if (c < 200)
return false
c := ((bgr >> 16) & 0xff) - variation
return c >= 200
}
Display_IsDark(ByRef bgr, ByRef variation) {
c := (bgr & 0xff) - variation
if (c < 100)
return true
c := ((bgr >> 8) & 0xff) - variation
if (c < 100)
return true
c := ((bgr >> 16) & 0xff) - variation
return c < 100
}
Display_FindPixelHorizontal(ByRef x, ByRef y, w, h, color, variation, ByRef context) {
if color is not integer
isPixel = Display_%color%
Loop, %h% {
i := x
Loop, %w% {
bgr := Display_GetPixel(context, i++, y)
pixel := isPixel ? Display_IsPixel(isPixel, bgr, variation) : Display_CompareColors(bgr, color, variation)
if pixel {
x := i
return true
}
}
y++
}
y -= h
return false
}
Display_FindPixelVertical(ByRef x, ByRef y, w, h, color, variation, ByRef context) {
if color is not integer
isPixel = Display_%color%
Loop, %w% {
i := y
Loop, %h% {
bgr := Display_GetPixel(context, x, i++)
pixel := isPixel ? Display_IsPixel(isPixel, bgr, variation) : Display_CompareColors(bgr, color, variation)
if pixel {
y := i
return true
}
}
x++
}
x -= w
return false
}
/* Usage:
* Updates the variables y and h based on the vertical position and height of the text found at x,y
* Parameters:
* x: x position to start searching for text
* y: y position where text will be found
* w: maximum width to search
* h: blank variable, will be updated with the correct height
* color: a color in BGR format or one of the following:
* BlueBackground - any color that is not a shade of blue will be recognized as text
*/
Display_FindText(ByRef x, ByRef y, ByRef w, ByRef h, color, variation, ByRef context) {
if color is not integer
isPixel = Display_%color%
minX := x + w
row := y
GoSub, Display_FTFindTop
top := row
row := y+1
column := x
GoSub, Display_FTFindBottom
y := top
h := row - top + 2
if (x != minX)
minX -= 2
w -= minX - x
x := minX
return
Display_FTFindTop:
column := x
Loop, %w% {
bgr := Display_GetPixel(context, column, row)
pixel := isPixel ? Display_IsPixel(isPixel, bgr, variation) : Display_CompareColors(bgr, color, variation)
if pixel {
if (column < minX)
minX := column
row--
GoTo, Display_FTFindTop
}
column++
}
return
Display_FTFindBottom:
column := x
Loop, %w% {
bgr := Display_GetPixel(context, column, row)
pixel := isPixel ? Display_IsPixel(isPixel, bgr, variation) : Display_CompareColors(bgr, color, variation)
if pixel {
if (column < minX)
minX := column
row++
GoTo, Display_FTFindBottom
}
column++
}
return
}
Display_IsPixel(ByRef label, ByRef bgr, ByRef variation) {
GoSub, %label%
return isPixel
Display_BlueForeground:
isPixel := Display_IsBlue(bgr, variation)
return
Display_BlueBackground:
isPixel := !Display_IsBlue(bgr, variation)
return
Display_GreenForeground:
isPixel := Display_IsGreen(bgr, variation)
return
Display_GreenBackground:
isPixel := !Display_IsGreen(bgr, variation)
return
Display_RedForeground:
isPixel := Display_IsRed(bgr, variation)
return
Display_RedBackground:
isPixel := !Display_IsRed(bgr, variation)
return
Display_CyanForeground:
isPixel := Display_IsCyan(bgr, variation)
return
Display_CyanBackground:
isPixel := !Display_IsCyan(bgr, variation)
return
Display_YellowForeground:
isPixel := Display_IsYellow(bgr, variation)
return
Display_YellowBackground:
isPixel := !Display_IsYellow(bgr, variation)
return
Display_VioletForeground:
isPixel := Display_IsViolet(bgr, variation)
return
Display_VioletBackground:
isPixel := !Display_IsViolet(bgr, variation)
return
Display_DarkForeground:
isPixel := Display_IsDark(bgr, variation)
return
Display_DarkBackground:
isPixel := !Display_IsDark(bgr, variation)
return
Display_LightForeground:
isPixel := Display_IsLight(bgr, variation)
return
Display_LightBackground:
isPixel := !Display_IsLight(bgr, variation)
return
}
/* Usage:
* This function has an empty map. That is, it won't read ANYTHING. You must specify the map by providing the necessary signature labels.
* Parameters:
* x: The x position to start reading.
* y: The y position to start reading.
* w: The width to read from x.
* h: The height to read from y.
* color: The color to read in BGR format or one of the following:
* BlueBackground, BlueForeground, GreenBackground, GreenForeground, RedBackground, RedForeground
* CyanBackground, CyanForeground, YellowBackground, YellowForeground, VioletBackground, VioletForeground
* DarkBackground, DarkForeground, LightBackground, LightForeground
* variation: The variation allowed from the specified color to be considered part of the text.
* id: The window id to read or alternatively, the letter c followed by the device context handle of an already created window capture.
* If blank, the specified area will be read from the screen. Otherwise the area will be read from an offscreen capture of the window - this method is more reliable and usually faster than the former.
*/
Display_ReadArea(x, y, w, h, color = 0x000000, variation = 32, ByRef id = "", maxwidth = 0, exclude = "") {
if !maxwidth
maxwidth := h
h2 := h * 2
text := ""
width := 0
column := 1
spaces := 0
xi := x
if exclude {
prevPixel := true
i := InStr(exclude, ",")
if i
StringSplit, exclude, exclude, `,
else {
exclude1 := exclude
exclude2 := 0
}
if exclude1 is not integer
isNotPixel = Display_%exclude1%
}
if color is not integer
isPixel = Display_%color%
if id {
if (SubStr(id, 1, 1) = "c") {
context := SubStr(id, 2)
} else {
Display_CreateWindowCapture(device, context, pixels, id)
}
GoSub, Display_SetPixels
if device
Display_DeleteWindowCapture(device, context, pixels[color=black], id[/color])
} else {
GoSub, Display_SetPixels
}
if replaceColon {
StringGetPos, i, text, `;, R
if (i == StrLen(text) - 3) {
text := SubStr(text, 1, i) . "." . SubStr(text, i+2)
}
StringReplace, text, text, `;, `,, All
}
return text
Display_SetPixels:
Loop, %w% {
yi := y
Loop, %h% {
if context
bgr := DllCall("GetPixel", UInt, context, Int, xi, Int, yi++)
else
PixelGetColor, bgr, xi, yi++
row := A_Index
pixel := isPixel ? Display_IsPixel(isPixel, bgr, variation) : Display_CompareColors(bgr, color, variation)
if exclude {
if (pixel && !prevPixel) {
pixel := !(isNotPixel ? Display_IsPixel(isNotPixel, bgr, exclude2) : Display_CompareColors(bgr, exclude1, exclude2))
}
prevPixel := pixel
}
pixels%column%_%row% := pixel
}
xi++
; Check if column has any pixels or if the pixels are not continuous with the previous column
row = 0
GoSub, Display_NextSetPixel
if !row {
isBreak := 1
} else if !width {
isBreak := 0
} else {
Loop {
c := column - 1
if (pixels%c%_%row%
|| (--row >= 0 && pixels%c%_%row%)
|| ((row+=2) < h && pixels%c%_%row%)) {
isBreak := 0
break
}
GoSub, Display_NextSetPixel
if !row {
; width += 1
isBreak := 2
break
}
}
}
if isBreak {
if width {
if (isBreak > 1)
xi--
GoSub, Display_ReadDigit
spaces := 0
} else {
if (text && ++spaces > h2) {
return
}
}
width := 0
column := 1
} else {
if (++width >= maxwidth) {
GoSub, Display_ReadDigit
spaces := 0
width := 0
column := 1
} else {
column++
}
}
}
return
Display_ReadDigit:
top := h
bottom := 0
Loop, %h% {
r := A_Index
Loop, %width% {
if pixels%A_Index%_%r% {
top := r
GoTo, Display_FoundTop
}
}
}
Display_FoundTop:
r := h
Loop, %h% {
Loop, %width% {
if pixels%A_Index%_%r% {
bottom := r
GoTo, Display_FoundBottom
}
}
r--
}
Display_FoundBottom:
signature = Display_
ht := Round((bottom - top) / 10)
wh := Round(width / 10)
c := 1
GoSub, Display_SetColumnSequences
c := width - wh
GoSub, Display_SetColumnSequences
r := top
GoSub, Display_SetRowSequences
r := bottom - ht
GoSub, Display_SetRowSequences
if IsLabel(signature)
GoSub, %signature%
else
text .= " "
; println(signature . " " . xi . " " . width)
return
Display_SetColumnSequences:
sequences := 0
last := false
set := 0
sig =
Loop, %h% {
i := A_Index
isSet := pixels%c%_%i%
if (!isSet && wh) {
c2 := c
Loop {
c2++
isSet := pixels%c2%_%i%
if (isSet || A_Index >= wh)
break
}
}
if (isSet != last) {
last := isSet
if (isSet) {
set := i
} else {
sequences += 1
clear := i - 1
sig .= set == top ? (clear == bottom ? "D" : "A") : clear == bottom ? "C" : "B"
}
}
}
if (last) {
sequences += 1
sig .= set == top ? "D" : "C"
}
signature .= sequences . sig
return
Display_SetRowSequences:
sequences := 0
last := false
set := 0
sig =
t := width
Loop, %width% {
i := A_Index
isSet := pixels%i%_%r%
if (!isSet && ht) {
r2 := r
Loop {
r2++
isSet := pixels%i%_%r2%
if (isSet || A_Index >= ht)
break
}
}
if (isSet != last) {
last := isSet
if (isSet) {
set := i
} else {
sequences += 1
clear := i - 1
sig .= set == 1 ? (clear == t ? "D" : "A") : clear == t ? "C" : "B"
}
}
}
if (last) {
sequences += 1
sig .= set == 1 ? "D" : "C"
}
signature .= sequences . sig
return
Display_NextSetPixel:
Loop {
if (++row > h)
break
if pixels%column%_%row%
return
}
row = 0
return
Display_NextClearPixel:
Loop {
if (++row > h)
break
if !pixels%column%_%row%
return
}
row = 0
return
Display_NextSetPixelH:
Loop {
if (++column > width)
break
if pixels%column%_%row%
return
}
column = 0
return
Display_NextClearPixelH:
Loop {
if (++column > width)
break
if !pixels%column%_%row%
return
}
column = 0
return
; example map:
/*
Display_2BB2BB1B1A:
Display_2AB2AB1D1B:
text .= "$"
return
*/
}