-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_InsertBinToPNG.ahk
337 lines (303 loc) · 6.45 KB
/
class_InsertBinToPNG.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
#Include gdip.ahk
class BinInPNG
{
static _init:=0
init()
{
if(this._init)
Return 1
If !this.pToken := Gdip_Startup()
{
MsgBox, 48, gdiplus error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
Return -1
}
this._init:=1
Return 0
}
selectPic(sFile)
{
if(!this._init)
Return -1
this.pBitmap:=Gdip_CreateBitmapFromFile(sFile)
this.Width:=Gdip_GetImageWidth(this.pBitmap)
this.Height:=Gdip_GetImageHeight(this.pBitmap)
this.volume:=((this.Width*this.Height*3)//8)
if(this.volume<=37)
Return -2
Return this.volume
}
getChar(char_Index)
{
if(!this.pBitmap)
Return -1
pixel_Index:=Ceil((char_Index*8-7)/3)
y:=pixel_Index//this.Width+1
x:=Mod(pixel_Index, this.Width)
rgb_Index:=Mod(char_Index*8-7, 3)
if(rgb_Index=0)
loopCount:=4
Else
loopCount:=3
array:={}
Loop, % loopCount
{
Gdip_FromARGB(Gdip_GetPixel(this.pBitmap, x, y), A, R, G, B)
array.Insert(R&0x1)
array.Insert(G&0x1)
array.Insert(B&0x1)
x:=x=this.Width?1:x+1
y:=x=1?y+1:y
}
if(rgb_Index=0)
startCount:=3
Else
startCount:=rgb_Index
output:=0
Loop, 8
{
output<<=1
output+=array[A_Index+startCount-1]
}
Return, output
}
putChar(uchar,char_Index)
{
if(!this.pBitmap)
Return -1
pixel_Index:=Ceil((char_Index*8-7)/3)
y:=pixel_Index//this.Width+1
x:=Mod(pixel_Index, this.Width)
rgb_Index:=Mod(char_Index*8-7, 3)
if(rgb_Index=0)
startCount:=3
Else
startCount:=rgb_Index
temp:=0x01000000>>(startCount*8)
ARGB:=Gdip_GetPixel(this.pBitmap, x, y)
Loop, 8
{
if(uchar&0x80)
ARGB|=temp
Else
ARGB&=0xFFFFFFFF^temp
uchar<<=1
if(temp=0x1){
temp=0x00010000
Gdip_SetPixel(this.pBitmap, x, y, ARGB)
x:=x=this.Width?1:x+1
y:=x=1?y+1:y
ARGB:=Gdip_GetPixel(this.pBitmap, x, y)
} Else {
temp>>=8
}
}
Gdip_SetPixel(this.pBitmap, x, y, ARGB)
Return 0
}
putStr(byref str,char_Index=1)
{
if(!this.pBitmap)
Return -1
if(!IsObject(str))
Return -2
pixel_Index:=Ceil((char_Index*8-7)/3)
y:=pixel_Index//this.Width+1
x:=Mod(pixel_Index, this.Width)
rgb_Index:=Mod(char_Index*8-7, 3)
if(rgb_Index=0)
startCount:=3
Else
startCount:=rgb_Index
temp:=0x01000000>>(startCount*8)
ARGB:=Gdip_GetPixel(this.pBitmap, x, y)
Loop, % str.MaxIndex()
{
uchar:=str[A_Index]
Loop, 8
{
if(uchar&0x80)
ARGB|=temp
Else
ARGB&=0xFFFFFFFF^temp
uchar<<=1
if(temp=0x1){
temp:=0x00010000
Gdip_SetPixel(this.pBitmap, x, y, ARGB)
x:=x=this.Width?1:x+1
y:=x=1?y+1:y
ARGB:=Gdip_GetPixel(this.pBitmap, x, y)
} Else {
temp>>=8
}
}
}
Gdip_SetPixel(this.pBitmap, x, y, ARGB)
Return 0
}
putStrFromHead(byref str)
{
return this.putStr(str,1)
}
putBinMsg(byref binMsg)
{
if(!IsObject(binMsg))
Return -2
if(binMsg.id!=0xb807dfb6)
Return -1
if(binMsg.head.filename.MaxIndex()+binMsg.content.MaxIndex()+17>=this.volume)
Return this.volume-1-(binMsg.head.filename.MaxIndex()+binMsg.content.MaxIndex()+17) ;need more pixel
this.putChar(binMsg.version,1)
offset:=this.getChar(2)
this.putChar((binMsg.version+offset)&0xff,3)
this.putStr(binMsg.head.size,4)
this.putStr(binMsg.head.filename,17)
this.putStr(binMsg.content,17+binMsg.head.filename.MaxIndex())
binMsg.free()
Return 0
}
getBinMsg(filename="$FileName$") ;$FileName$ $NameNoExt$ $Ext$
{
if(!this.pBitmap)
Return -1
_head:={}
_head.version:=this.getChar(1)
if(_head.version!=0xA1)
Return -2
offset:=this.getChar(2)
if((_head.version+offset&0xff)!=this.getChar(3))
Return -3
_head.size:={}
Loop, 3
_head.size.Insert(this.getChar(A_index+3))
size:=(_head.size[1]<<16)|(_head.size[2]<<8)|(_head.size[3])
_head.filename:={}
Loop
{
_head.filename.Insert(this.getChar(A_index+16))
if((A_Index&0x1)=0) ;str end check
{
if(_head.filename[A_Index]=0 and _head.filename[A_Index-1]=0)
break
}
}
; if(_head.filename.MaxIndex()<=2)
; {
; ;no name
; }
VarSetCapacity(_name,_head.filename.MaxIndex())
Loop, % _head.filename.MaxIndex()
NumPut(_head.filename[A_index],_name,A_Index-1,"UChar")
fullname:=strGetVar(_name)
SplitPath, fullname,,,Ext,NameNoExt
StringReplace, filename, filename,$FileName$,% fullname, All
StringReplace, filename, filename,$NameNoExt$,% NameNoExt, All
StringReplace, filename, filename,$Ext$,% Ext, All
_content:={}
Loop, % size
{
_content.Insert(this.getChar(A_Index+16+_head.filename.MaxIndex()))
}
oFile:=FileOpen(filename, "w")
Loop, % size
oFile.WriteUChar(_content[A_Index])
oFile.Close()
}
savePng(sOutput)
{
Gdip_SaveBitmapToFile(this.pBitmap, sOutput ".png")
}
version()
{
Return, "alpha 0"
}
}
/*
alpha 0:
字节1为版本号A1
字节2从原图中读取,为全局偏移量
字节3为添加偏移量的版本号
字节4,5,6为长度,最大16Mb
字节7-16保留
字节17-(结束0x00,0x00)为文件名
之后为内容
字节为MSB顺序
生成图像时,若空间不足文件名长度+17+1,则应提示空间不足
*/
class binMsg
{
; static version:=0xA1
__New(sFile)
{
IfNotExist, % sFile
Return -1
FileGetSize, size, % sFile, B
if(size>10000000)
Return -2
oFile:=FileOpen(sFile, "r")
this.content:={}
while(!oFile.AtEOF){
oFile.RawRead(bytes,1)
this.content.Insert(asc(bytes))
}
size:=this.content.MaxIndex() ;they are not equal sometimes
oFile.Close()
this.head:={}
this.head.version:=this.version
this.head.size:={}
this.head.size.Insert((size>>16)&0xff)
this.head.size.Insert((size>>8)&0xff)
this.head.size.Insert(size&0xff)
this.head.filename:={}
SplitPath, sFile, name
nameLength:=strPutVar(name, str)*2
Loop, % nameLength
this.head.filename.Insert(NumGet(str,A_Index-1,"UChar"))
Return this
}
addOffset(offset)
{
if(offset>=0)
{
offset&=0xff
}
Else
{
offset:=-offset
offset&=0xff
offset:=-offset
}
Loop, % this.content.maxIndex()
{
if(offset<0)
this.content[A_Index]+=0x100
this.content[A_Index]+=offset
this.content[A_Index]&=0xff
}
}
__Get(key)
{
if(key="length")
Return, this.content.Maxindex()
else if(key="version")
Return, 0xA1
else if(key="id")
Return, 0xb807dfb6
Else
Msgbox,4096,, No key named %key%
Return -1
}
free()
{
this.content:=""
this.__Delete()
}
}
strPutVar(string, ByRef var)
{
VarSetCapacity(var,StrPut(string,"utf-16")*2)
return StrPut(string, &var,"utf-16")
}
strGetVar(ByRef var)
{
return StrGet(&var,"utf-16")
}