-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathGdip_GrayScale.ahk
121 lines (110 loc) · 4.77 KB
/
Gdip_GrayScale.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
; Link: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=83208
; Author: Xtra
; Date: 13.11.2020
; for: AHK_L
/* Example use
pBitmap := Gdip_CreateBitmapFromFile(sFile)
Gdip_GrayScale(pBitmap)
Gdip_SaveBitmapToFile(pBitmap, outputFile)
Gdip_DisposeImage(pBitmap)
*/
;
; Grayscale done with machine code. Requires Gdip bitmap as input.
; Source based on: https://www.codeproject.com/Questions/327689/how-to-convert-coloured-image-to-grayscale-in-C-or
; See solution #2
; -----------------------------------------------------------------------------------------------------
; unsigned int Convert_GrayScale(unsigned int * bitmap, int w, int h, int Stride)
; {
; unsigned int a, r, g, b, gray, ARGB;
; int x, y, offset = Stride/4;
; for (y = 0; y < h; ++y) {
; for (x = 0; x < w; ++x) {
; ARGB = bitmap[x+(y*offset)];
; a = (ARGB & 0xFF000000) >> 24;
; r = (ARGB & 0x00FF0000) >> 16;
; g = (ARGB & 0x0000FF00) >> 8;
; b = (ARGB & 0x000000FF);
; gray = ((217 * r) + (733 * g) + (74 * b)) >> 10;
; bitmap[x+(y*offset)] = (gray << 16) | (gray << 8) | gray | (a << 24);
; }
; }
; return 0;
; }
; -----------------------------------------------------------------------------------------------------
; Compiled with: https://www.autohotkey.com/boards/viewtopic.php?t=73099
; Thanks Bentchi and Plorence!
;
;
Gdip_GrayScale(pBitmap) {
static GrayScaleMCode
if (GrayScaleMCode = "") {
if (A_PtrSize = 4) { ; (32bit)
MCode := ""
. "2,x86:VVdWU4PsCItMJCiLVCQgjUEDhckPScGLTCQkwfgChcl+"
. "f4XSfnvB4AKLfCQcMe2JRCQEjQSVAAAAAIkEJI12AIsEJIn7jT"
. "Q4kI20JgAAAACLE4PDBInQD7bOwegQD7bAacndAgAAacDZAAAA"
. "AcgPtsqB4gAAAP9ryUoByMHoConBCcLB4AjB4RAJygnQiUP8Od"
. "51vIPFAQN8JAQ5bCQkdZ+DxAgxwFteX13D"
}
else { ; A_PtrSize = 8 (64bit)
mCode := ""
. "2,x64:QVdBVkFVQVRVV1ZTSIPsOA8pdCQgQY1BA0WFyUEPScHB"
. "+AJFhcCJRCQYD46ABAAAhdIPjngEAABIY0QkGEiJzkUx5EG7Aw"
. "AAADHtSImMJIAAAABmD28dAAAAAEiJRCQQSMHgAkiJRCQIjUL/"
. "Zg9vJRAAAACJRCQcZg9vLSAAAADp+QMAAEGJ0oseidgPts/B6B"
. "APtsBEacndAgAAacDZAAAARAHIRA+2y4HjAAAA/0VryUpEAcjB"
. "6ApBicEJw8HgCEHB4RBECcsJw0GD+gG4AQAAAIkeD4Q5AQAASI"
. "uMJIAAAABBjUP+SJhIjRyBRIsLRInIRInJwegQD7bND7bARGnp"
. "3QIAAGnA2QAAAEQB6EUPtulBgeEAAAD/RWvtSkQB6MHoCkGJxU"
. "EJwcHgCEHB5RBFCelBCcFBg/oCuAIAAABEiQsPhMsAAABIi4wk"
. "gAAAAEGNQ/9ImEiNHIFEiwtEichEicnB6BAPts0PtsBEaendAg"
. "AAacDZAAAARAHoRQ+26UGB4QAAAP9Fa+1KRAHowegKQYnFQQnB"
. "weAIQcHlEEUJ6UEJwUGD+gS4AwAAAESJC3VhSIuMJIAAAABJY8"
. "NIjRyBRIsLRInID7bERGno3QIAAESJyMHoEA+2wGnA2QAAAEQB"
. "6EUPtulBgeEAAAD/RWvtSkQB6MHoCkGJxUEJwcHgCEHB5RBFCe"
. "l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dAgAA3QIAAAAAAP8AAA"
. "D/AAAA/wAAAP8="
}
GrayScaleMCode := MCode(mCode)
}
Gdip_GetImageDimensions(pBitmap, w, h)
if !(w && h)
return -1
if (E1 := Gdip_LockBits(pBitmap, 0, 0, w, h, stride, scan, bitmapData))
return -2
E := DllCall(GrayScaleMCode, "uint", scan, "int", w, "int", h, "int", stride, "cdecl")
Gdip_UnlockBits(pBitmap, bitmapData)
return (E = "") ? -3 : E
}
MCode(mcode) { ; <Bentchi function>
static e := {1:4, 2:1}, c := (A_PtrSize=8) ? "x64" : "x86"
if (!regexmatch(mcode, "^([0-9]+),(" c ":|.*?," c ":)([^,]+)", m))
return
if (!DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", 0, "uint*", s, "ptr", 0, "ptr", 0))
return
p := DllCall("GlobalAlloc", "uint", 0, "ptr", s, "ptr")
if (c="x64")
DllCall("VirtualProtect", "ptr", p, "ptr", s, "uint", 0x40, "uint*", op)
if (DllCall("crypt32\CryptStringToBinary", "str", m3, "uint", 0, "uint", e[m1], "ptr", p, "uint*", s, "ptr", 0, "ptr", 0))
return p
DllCall("GlobalFree", "ptr", p)
}