forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWIA.ahk
296 lines (296 loc) · 14.7 KB
/
WIA.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
; ======================================================================================================================
; WIA - Windows Image Acquisition Automation Layer -> msdn.microsoft.com/en-us/library/ms630827(v=vs.85).aspx
; Function: Functions for images.
; Namespace: WIA
; Tested with: AHK 1.1.22.00 (A32/U32/U64)
; Tested on: Win 8.1 (x64)
; Changelog:
; 1.0.02.00/2015-05-03/just me - added WIA_CreateImage()
; 1.0.01.00/2015-04-27/just me - fixed WIA_ScaleImage() preserve-aspect-ratio handling
; 1.0.00.00/2015-04-26/just me - initial release
; Credits:
; maestrith for Crop.ahk (-> http://www.autohotkey.net/~maestrith/crop/Crop.ahk)
; sinkfaze for his COM example (-> http://ahkscript.org/boards/viewtopic.php?f=6&t=77&p=619)
; License:
; The Unlicense (http://unlicense.org/)
; ======================================================================================================================
; Common function parameters (not mentioned below):
; ImgObj - ImageFile object returned by WIA_LoadImage().
; Common return values (except noted below):
; A new ImageFile object on success, otherwise False.
; ======================================================================================================================
; Creates a new image.
; Parameters:
; Px... - Number of pixels for the corresponding dimension.
; ARGBData - Array of ARGB color data, one per pixel, to put into the image. The data will be used to create
; exactly PxWidth * PxHeight pixels. The alpha value will be ignored.
; ======================================================================================================================
WIA_CreateImage(PxWidth, PxHeight, ARGBData) {
If !WIA_IsInteger(PxWidth, PxHeight) || !WIA_IsPositive(PxWidth, PxHeight)
Return False
DataCount := PxWidth * PxHeight
Vector := ComObjCreate("WIA.Vector")
I := 1
Loop
For Each, ARGB In ARGBData
Vector.Add(ComObject(0x3, ARGB))
Until (++I > DataCount)
Until (I > DataCount)
Return Vector.ImageFile(PxWidth, PxHeight)
}
; ======================================================================================================================
; Converts an image into the specified format.
; Parameters:
; NewFormat - New format as one of the string keys of FormatID.
; Quality - Quality used for JPEG compression (1 - 100).
; Compression - Compression method used for TIFF compression as one of the string keys of Comp.
; Note:
; The function also sets the FileExtension property of the new ImageFile object according to the new format.
; ======================================================================================================================
WIA_ConvertImage(ImgObj, NewFormat, Quality := 100, Compression := "LZW") {
Static FormatID := {BMP: "{B96B3CAB-0728-11D3-9D7B-0000F81EF32E}"
, JPEG: "{B96B3CAE-0728-11D3-9D7B-0000F81EF32E}"
, GIF: "{B96B3CB0-0728-11D3-9D7B-0000F81EF32E}"
, PNG: "{B96B3CAF-0728-11D3-9D7B-0000F81EF32E}"
, TIFF: "{B96B3CB1-0728-11D3-9D7B-0000F81EF32E}"}
Static Comp := {CCITT3: 1, CCITT4: 1, LZW: 1, RLE: 1, Uncompressed: 1}
If (ComObjType(ImgObj, "Name") <> "IImageFile")
Return False
If ((NewFormat := FormatID[NewFormat]) = "")
Return False
If Quality Not Between 1 And 100
Return False
If (Comp[Compression] = "")
Return False
ImgProc := WIA_ImageProcess()
ImgProc.Filters.Add(ImgProc.FilterInfos("Convert").FilterID)
ImgProc.Filters[1].Properties("FormatID") := NewFormat
ImgProc.Filters[1].Properties("Quality") := Quality
ImgProc.Filters[1].Properties("Compression") := Compression
Return ImgProc.Apply(ImgObj)
}
; ======================================================================================================================
; Crops an image.
; Parameters:
; Px... - Number of pixels to crop at the corresponding edge.
; ======================================================================================================================
WIA_CropImage(ImgObj, PxLeft, PxTop, PxRight, PxBottom) {
If (ComObjType(ImgObj, "Name") <> "IImageFile")
Return False
If !WIA_IsInteger(PxLeft, PxTop, PxRight, PxBottom) || !WIA_IsPositive(PxLeft, PxTop, PxRight, PxBottom)
Return False
If ((ImgObj.Width - PxLeft - PxRight) < 0) || ((ImgObj.Height - PxTop - PxBottom) < 0)
Return False
ImgProc := WIA_ImageProcess()
ImgProc.Filters.Add(ImgProc.FilterInfos("Crop").FilterID)
ImgProc.Filters[1].Properties("Left") := PxLeft
ImgProc.Filters[1].Properties("Top") := PxTop
ImgProc.Filters[1].Properties("Right") := PxRight
ImgProc.Filters[1].Properties("Bottom") := PxBottom
Return ImgProc.Apply(ImgObj)
}
; ======================================================================================================================
; Retrieves descriptions of all available filters or the specified filter.
; Might be useful to provide help informations.
; Parameters:
; FilterName - Filter name, omit the parameter or pass an empty string to get the descriptions of all available
; filters.
; Return values:
; A string containing the filter name(s), ID(s), and description(s) on success, otherwise an empty string.
; ======================================================================================================================
WIA_FilterDescriptions(FilterName := "") {
Static SubTypes := ["Range", "List", "Flag"]
Static Delimiter := "============================================================"
. "========================================`r`n"
Description := Delimiter
ImgProc := WIA_ImageProcess()
For FilterInfo In ImgProc.FilterInfos {
If (Filtername <> "") {
If (FilterInfo.Name = FilterName) {
Description .= FilterInfo.Name . " (ID: " . FilterInfo.FilterId . ")`r`n`r`n"
. FilterInfo.Description . "`r`n" . Delimiter
Break
}
}
Else
Description .= FilterInfo.Name . " (ID: " . FilterInfo.FilterId . ")`r`n`r`n"
. FilterInfo.Description . "`r`n" . Delimiter
}
Return (Description <> Delimiter ? RTrim(Description) : "")
}
; ======================================================================================================================
; Flips an image.
; Parameters:
; Mode - Flip "Horizontal" or "Vertical"
; ======================================================================================================================
WIA_FlipImage(ImgObj, Mode := "Horizontal") {
Static Modes := {"Horizontal": 1, "Vertical": 1}
If !(Modes[Mode]) || (ComObjType(ImgObj, "Name") <> "IImageFile")
Return False
ImgProc := WIA_ImageProcess()
ImgProc.Filters.Add(ImgProc.FilterInfos("RotateFlip").FilterID)
ImgProc.Filters[1].Properties("Flip" . Mode) := True
Return ImgProc.Apply(ImgObj)
}
; ======================================================================================================================
; Retrieves the bitmap data of the image.
; Return values:
; A new Picture object on success, otherwise false.
; Note:
; To retrieve the HBITMAP handle for the returned object use object.Handle
; ======================================================================================================================
WIA_GetImageBitmap(ImgObj) {
; To retrieve the HBITMAP handle for the returned object use object.Handle
Return (ComObjType(ImgObj, "Name") = "IImageFile") ? ImgObj.Filedata.Picture : False
}
; ======================================================================================================================
; Retrieves the extended properties of the image.
; Parameters:
; AsString - Set to true to get the properties as a string instead of an array.
; Return values:
; On success: A property string or a property array indexed by the numerical property ID. Each array element
; contains an object with the key/value pairs 'Name' and 'Value'.
; On failure: False
; ======================================================================================================================
WIA_GetImageProperties(ImgObj, AsString := False) {
If (ComObjType(ImgObj, "Name") <> "IImageFile")
Return False
PropObj := []
For Prop In ImgObj.Properties {
PropObj[Prop.PropertyID, "Name"] := Prop.Name
If Prop.IsVector
Value := "[vector data not emitted]"
Else If (Prop.Type = 1006) || (Prop.Type = 1007) ; RationalImagePropertyType || UnsignedRationalImagePropertyType
Value := Prop.Value.Value . " (" . Prop.Value.Numerator . " / " . Prop.Value.Denominator . ")"
Else If (Prop.Type = 1002) ; StringImagePropertyType
Value := """" . Prop.Value . """"
Else
Value := Prop.Value
PropObj[Prop.PropertyID, "Value"] := Value
}
If !(AsString)
Return PropObj
Else {
PropString := ""
For PropID, Prop In PropObj
PropString .= "ID: " . PropID . " - Name: " . Prop.Name . " - Value: " . Prop.Value . "`r`n"
Return RTrim(PropString, "`r`n")
}
}
; ======================================================================================================================
; Loads an image file.
; Parameters:
; ImgPath - Path and name of the image file.
; ======================================================================================================================
WIA_LoadImage(ImgPath) {
ImgObj := ComObjCreate("WIA.ImageFile")
ComObjError(0)
ImgObj.LoadFile(ImgPath)
ComObjError(1)
Return A_LastError ? False : ImgObj
}
; ======================================================================================================================
; Rotates an image in steps of 90°.
; Parameters:
; Mode - Direction: "Left" or "Right"
; ======================================================================================================================
WIA_RotateImage(ImgObj, Mode := "Right") {
Static Modes := {"Left": 270, "Right": 90}
If !(Mode := Modes[Mode]) || (ComObjType(ImgObj, "Name") <> "IImageFile")
Return False
ImgProc := WIA_ImageProcess()
ImgProc.Filters.Add(ImgProc.FilterInfos("RotateFlip").FilterID)
ImgProc.Filters[1].Properties("RotationAngle") := Mode
Return ImgProc.Apply(ImgObj)
}
; ======================================================================================================================
; Saves an image to disk.
; Parameters:
; ImgPath - Path and name of the image file.
; Return values:
; True on success, otherwise false. The error code is stored in A_LastError.
; Note:
; The extension of the file name passed in ImgPath must be one of "bmp", "gif", "jpg", "png", or "tif" and match
; the extension stored in the FileExtension property of the ImageFile object. Otherwise the file won't be stored.
; Also, the function won't overwrite existing files.
; ======================================================================================================================
WIA_SaveImage(ImgObj, ImgPath) {
If (ComObjType(ImgObj, "Name") <> "IImageFile")
Return False
SplitPath, ImgPath, FileName, FileDir, FileExt
If (ImgObj.FileExtension <> FileExt)
Return False
ComObjError(0)
ImgObj.SaveFile(ImgPath)
ComObjError(1)
Return !A_LastError
}
; ======================================================================================================================
; Scales/resizes an image.
; Parameters:
; Px... - New image width and height in pixels.
; Note:
; If either PxWidth or PxHeight are zero or negative, the corresponding dimension will be scaled preserving the
; aspect ratio of the image.
; ======================================================================================================================
WIA_ScaleImage(ImgObj, PxWidth, PxHeight) {
If (ComObjType(ImgObj, "Name") <> "IImageFile")
Return False
If !WIA_IsInteger(PxWidth, PxHeight) || ((PxWidth < 1) && (PxHeight < 1))
Return False
KeepRatio := (PxWidth < 1) || (PxHeight < 1) ? True : False
ImgProc := WIA_ImageProcess()
ImgProc.Filters.Add(ImgProc.FilterInfos("Scale").FilterID)
ImgProc.Filters[1].Properties("MaximumWidth") := PxWidth > 0 ? PxWidth : PxHeight
ImgProc.Filters[1].Properties("MaximumHeight") := PxHeight > 0 ? PxHeight : PxWidth
ImgProc.Filters[1].Properties("PreserveAspectRatio") := KeepRatio
Return ImgProc.Apply(ImgObj)
}
; ======================================================================================================================
; Stamps the image passed in StampObj onto the image.
; Parameters:
; StampObj - Image object as returned from one of the WIA functions.
; Px... - Distance to the left/top edge of the image in pixels.
; Note:
; Both PxLeft or PxTop must be positive. Also, (PxLeft + StampObj.Width) and (PxTop + STampObj.Height) must not
; exceed the image dimensions.
; ======================================================================================================================
WIA_StampImage(ImgObj, StampObj, PxLeft, PxTop) {
If (ComObjType(ImgObj, "Name") <> "IImageFile") || (ComObjType(StampObj, "Name") <> "IImageFile")
Return False
If ((PxLeft + StampObj.Width) > ImgObj.Width) || ((PxTop + StampObj.Height) > ImgObj.Height)
Return False
ImgProc := WIA_ImageProcess()
ImgProc.Filters.Add(ImgProc.FilterInfos("Stamp").FilterID)
ImgProc.Filters[1].Properties("ImageFile") := StampObj
ImgProc.Filters[1].Properties("Left") := PxLeft
ImgProc.Filters[1].Properties("Top") := PxTop
Return ImgProc.Apply(ImgObj)
}
; ======================================================================================================================
; For internal use!!!
; ======================================================================================================================
WIA_ImageProcess() {
Static ImageProcess := ComObjCreate("WIA.ImageProcess")
While (ImageProcess.Filters.Count)
ImageProcess.Filters.Remove(1)
Return ImageProcess
}
; ======================================================================================================================
WIA_IsInteger(Values*) {
If Values.MaxIndex() = ""
Return False
For Each, Value In Values
If Value Is Not Integer
Return False
Return True
}
; ======================================================================================================================
WIA_IsPositive(Values*) {
If Values.MaxIndex() = ""
Return False
For Each, Value In Values
If (Value < 0)
Return False
Return True
}