FastFind.au3 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. #cs ----------------------------------------------------------------------------
  2. FastFind Version: 2.2
  3. Author: FastFrench
  4. Some Wrappers: frank10
  5. AutoIt Version: 3.3.8.1
  6. Script Function:
  7. All FastFind.dll wrapper functions.
  8. This dll provides optimized screen processing. All actions implies at least 2 dll calls : one to copy the screen data into memory, and a second to make specific action.
  9. This way, you can make several actions even faster when they apply to the same screen data.
  10. Functions exported in FastFind.dll 1.8 are :
  11. SnapShot (Makes captures of the screen, a Window - partial or full - into memory, required before using any of the following.)
  12. ColorPixelSearch (Search the closest pixel with a given color)
  13. ColorCount (Count how many pixels with a given color exist in the SnapShot.)
  14. HasChanged (Says if two snapshots are exactly the same or not. Usefull to check if some changes occured in a given area. )
  15. LocalizeChanges (Same has HasChanged, but returns precisely the smallest rectangle that includes all the changes, and the number of pixels that are different).
  16. GetPixel (Gives the color of a pixel in the SnapShot. Much faster than PixelGetColor if you use this a lot.)
  17. AddColor
  18. RemoveColor (Those 3 functions allow management of a list of colors, instead of using only one)
  19. ResetColors (Tou can have up to 1024 colors active in the list)
  20. AddExcludedArea (Those functions add exceptions in the processing of screen areas, with rectangles that are ignored)
  21. ResetExcludedAreas (You can have up to 1024 areas in the list)
  22. IsExcluded
  23. ColorsSearch (Close to ColorSearch, except that instead of a single color, all colors of the current list are in use)
  24. ColorsPixelSearch (Similar to ColorPixelSearch, except that instead of a single color, all colors of the current list are in use)
  25. ColorSearch (it's the most versatile and powerful function (in 1.3) : you can, at the same time, check for as many colors as you want,
  26. with possibly a "ShadeVariation", multiple subareas to ignore... will Find the closest spot with a all specified criteria.
  27. The spot is a square area of NxN pixels with at least P pixels that are as close as allowed by ShadeVariation
  28. from any of the colors in the list).
  29. ProgressiveSearch (new with 1.4 : similar to ColorSearch, except that if the "ideal" spot can't be found, can still search for the best spot available).
  30. GetLastErrorMsg
  31. FFVersion
  32. -- New in version 1.6
  33. SaveBMP
  34. SaveJPG
  35. GetLastFileSuffix
  36. KeepChanges
  37. KeepColor
  38. -- New in version 1.7
  39. DrawSnapShot
  40. FFSetPixel
  41. DuplicateSnapShot
  42. GetRawData
  43. -- New in version 2.0
  44. Function with an additionnal parameter (ShadeVariation) :
  45. KeepChanges
  46. LocalizeChanges
  47. HasChanged
  48. New functions :
  49. DrawSnapShotXY (same as DrawSnapShot, with specific top-left position for drawing).
  50. ComputeMeanValues (Gives mean Red, Green and Blue values)
  51. ApplyFilterOnSnapShot (apply a AND filter on each pixels in the SnapShot)
  52. FFGetRawData (gets direct access to all pixel data of a ScreenShot)
  53. Bug fix :
  54. FFColorCount with ShadeVariation
  55. -- 2.2
  56. Detects more error patterns (wrong coordinates)
  57. #ce ----------------------------------------------------------------------------
  58. ; User global variables (can be changes by user)
  59. global $FFDefaultSnapShot = 0 ; Default SnapShot Nb
  60. global $FFDefautDebugMode = 0 ; Si below to the meaning of this value. To remove all debug features (file traces, graphical feedback..., use 0 here)
  61. ; System global variables ** do not change them **
  62. global $FFDllHandle = -1
  63. global $FFLastSnap = 0
  64. global const $FFNbSnapMax = 1024
  65. global $FFLastSnapStatus[$FFNbSnapMax] ; array used to automatically make a SnapShot when needed
  66. global const $FFCurrentVersion="2.2"
  67. InitFFDll()
  68. ; Loading the the dll and initialisation of the wrapper
  69. ; -----------------------------------------------------
  70. Func InitFFDll()
  71. for $i = 0 To $FFNbSnapMax-1
  72. $FFLastSnapStatus[$i] = 0
  73. Next
  74. If @AutoItX64 Then
  75. Global $DllName = @ScriptDir & "/utils/FastFind/FastFind64.dll"
  76. Else
  77. Global $DllName = @ScriptDir & "/utils/FastFind/FastFind.dll"
  78. Endif
  79. $FFDllHandle = DllOpen($DllName)
  80. If $FFDllHandle=-1 Then
  81. $FFDllHandle=$DllName
  82. MsgBox(0,"Error","Failed to load "&$DllName&", application probably won't properly work. "&@LF&"Check if the file "&$DllName&"is installed near this script")
  83. Exit(100)
  84. Return
  85. EndIf
  86. if ($FFCurrentVersion<>FFGetVersion()) Then
  87. MsgBox(0, "Error", "Wrong version of "&$DllName&". The dll is version "&FFGetVersion()&" while version "&$FFCurrentVersion&" is required.");
  88. Exit(101)
  89. EndIf
  90. FFSetDebugMode($FFDefautDebugMode)
  91. EndFunc
  92. Func CloseFFDll()
  93. If $FFDllHandle<>-1 Then DllClose($FFDllHandle)
  94. EndFunc
  95. ; Determines the debugging mode.
  96. ; ------------------------------------------------- --------------------
  97. ; The 4 bits determine the channel debugging enabled, they have the following meanings:
  98. ; 0x00 = no debug
  99. ; 0x01 = Information sent to the console (RequireAdmin)
  100. ; 0x02 = debug information sent to a file (trace.txt)
  101. ; 0x04 = Graphic display of points / areas identified
  102. ; 0x08 = Display MessageBox (blocking)
  103. ; Note that in case of error, a MessageBox is displayed in the DLL if DebugMode> 0
  104. ; The following 4 bits are used to filter based on the origin of the debug message
  105. ; 0x0010 / / Excludes internal traces of the DLL
  106. ; 0x0020 / / Excludes detailed internal traces of the DLL
  107. ; 0x0040 / / Excludes external traces (those of the application)
  108. ; 0x0080 / / Error message (priority)
  109. ;
  110. ; Errors (serious) are displayed on all available channels (file, console and MessageBox) if $ DebugMode> 0
  111. ;
  112. ; Proto C function: void SetDebugMode (int NewMode)
  113. Func FFSetDebugMode($DebugMode)
  114. DllCall($FFDllHandle, "none", "SetDebugMode", "int", $DebugMode)
  115. EndFunc
  116. ; The DLL also exposes its debugging functions, allowing the AutoIt application share same traces
  117. Func FFTrace($DebugString)
  118. DllCall($FFDllHandle, "none", "DebugTrace", "str", $DebugString)
  119. EndFunc
  120. ; This function allows you to handle errors (the text appears in the logfile, the console and a MessageBox if $ DebugMode> 0)
  121. Func FFTraceError($DebugString)
  122. DllCall($FFDllHandle, "none", "DebugError", "str", $DebugString)
  123. EndFunc
  124. ; Sets the current window to use
  125. ; -----------------------------------
  126. ; By default, the entire screen is used. You can select a particular window. By default, we will always use the last Window set.
  127. ; If $WindowsHandle = 0, the entire screen : GetDesktopWindow ()
  128. ; If ClientOnly = True, then only the client part of the Window will be capturable, and coordinates will be relative to top-left
  129. ; corner of the client area (client area is the full Window except title bar, possibly menu area, borders...)
  130. ; If ClientOnly = False, then the full Window will be used.
  131. ;
  132. ; Proto C function: void SetHWnd(HWND NewWindowHandle, bool bClientArea);
  133. Func FFSetWnd($WindowHandle, $ClientOnly=True)
  134. DllCall($FFDllHandle, "none", "SetHWnd", "HWND", $WindowHandle, "BOOLEAN", $ClientOnly)
  135. EndFunc
  136. ; Choose the Default SnapShot that will by used in the next operations. This avoid to specify the number of the SnapShot every time when you always work on the same
  137. Func FFSetDefaultSnapShot($NewSnapShot)
  138. $FFDefaultSnapShot = $NewSnapShot
  139. EndFunc
  140. ; Managing the list of colors
  141. ; ================================
  142. ; When a parameter is proposed Dane Color function, the value -1 means that all colors in the list are taken into account
  143. ; Add one or more colors in the list maintained by FastFind
  144. ; Proto C function: int addColor (int newColor)
  145. Func FFAddColor(const $NewColor)
  146. local $res
  147. if (IsArray($NewColor)) Then
  148. For $Color In $NewColor
  149. $res = DllCall($FFDllHandle, "int", "AddColor", "int", $Color)
  150. Next
  151. Else
  152. $res = DllCall($FFDllHandle, "int", "AddColor", "int", $NewColor)
  153. EndIf
  154. if IsArray($res) Then return $res[0]
  155. return $res
  156. EndFunc
  157. ; Remove a color (if any) from the list of colors
  158. ;
  159. ; Proto C function: int RemoveColor (int newColor)
  160. Func FFRemoveColor(const $OldColor)
  161. local $res = DllCall($FFDllHandle, "int", "RemoveColor", "int", $OldColor)
  162. if IsArray($res) Then return $res[0]
  163. return $res
  164. EndFunc
  165. ; Totally Empty the list of colors
  166. ;
  167. ; Proto C function: int ResetColors ()
  168. Func FFResetColors()
  169. DllCall($FFDllHandle, "none", "ResetColors")
  170. EndFunc
  171. ; Exclusion areas management
  172. ; ==========================
  173. ; Exclusion zones can restrict searches with all functions
  174. ; Search
  175. ; It is possible to have up to 1024 rectangles of exclusion, thereby removing precisely
  176. ; Any search area. For example, with flash, the mouse cursor will usually appears on the snapshots
  177. ; (unlike cursors managed by Windows API). You can use an Exclusion rectangle established according to the position
  178. ; of the mouse so the cursor does not affect the Search results.
  179. ;
  180. ; Adds an exclusion zone
  181. ;
  182. ; Proto C function: void WINAPI AddExcludedArea (int x1, int y1, int x2, int y2)
  183. Func FFAddExcludedArea(const $x1, const $y1, const $x2, const $y2)
  184. local $Res = DllCall($FFDllHandle, "int", "AddExcludedArea", "int", $x1, "int", $y1, "int", $x2, "int", $y2)
  185. if IsArray($res) Then return $res[0]
  186. return $res
  187. EndFunc
  188. ; Clears the list of all zones
  189. ;
  190. ; Proto C function: void WINAPI ResetExcludedAreas()
  191. Func FFResetExcludedAreas()
  192. DllCall($FFDllHandle, "none", "ResetExcludedAreas")
  193. EndFunc
  194. ; Through the list of exclusion zones to determine if the point passed as a parameter is excluded or not.
  195. ;
  196. ; Proto C function: bool WINAPI IsExcluded(int x, int y, HWND hWnd)
  197. Func FFIsExcluded(const $x, const $y, const $hWnd)
  198. local $Res = DllCall($FFDllHandle, "BOOLEAN", "IsExcluded", "int", $x, "int", $y, "HWND", $hWnd)
  199. if IsArray($res) Then return $res[0]
  200. return $res
  201. EndFunc
  202. ; FFSnapShot Function - This function allows you to make a copy of the screen, window or only a part in memory
  203. ; - All other functions of FF running from memory, it should first run FFSnapShot (either explicitly or implicitly as designed within this wrapper)
  204. ; It is possible to perform several different catches and work on any thereafter.
  205. ;
  206. ; Input:
  207. ; The area to capture (in coordinates relative to the boundaries of the window if a window handle nonzero indicated) [optional, the entire screen by default]
  208. ; If the area indicated is 0,0,0,0 then this is the entire window (or screen) to be captured
  209. ; The ID to use SnapShot (optional, default to the last used, 0 initially)
  210. ; And a window handle [optional, the same screen as the previous time by default. Initially, the entire screen.]
  211. ;
  212. ; Warning: Graphic data is stored in memory, the use of this feature consumes memory. It takes about 1.8 MB of RAM to capture 800x600.
  213. ; Therefore it should preferably always reuse the same No. SnapShot. Nevertheless, it is possible to store up to 1024 screens.
  214. ;
  215. ; Return Values: If unsuccessful, returns 0 and sets @Error.
  216. ; If successful, returns 1
  217. ; Proto C function: int WINAPI SnapShot(int aLeft, int aTop, int aRight, int aBottom, int NoSnapShot)
  218. Func FFSnapShot(const $Left=0, const $Top=0, const $Right=0, const $Bottom=0, const $NoSnapShot=$FFDefaultSnapShot, const $WindowHandle=-1)
  219. if ($WindowHandle <> -1) Then FFSetWnd($WindowHandle)
  220. $FFDefaultSnapShot = $NoSnapShot ; On mémorise le no du SnapShot utilisé, cela restera le SnapShop par défaut pour les prochains appels
  221. local $Res = DllCall($FFDllHandle, "int", "SnapShot", "int", $Left, "int", $Top, "int", $Right, "int", $Bottom, "int", $NoSnapShot)
  222. If ( ((Not IsArray($Res)) AND ($Res=0)) OR $Res[0]=0) Then
  223. ConsoleWrite("FFSnapShot - SnapShot ("&$Left&","&$Top&","&$Right&","&$Bottom&","&$NoSnapShot&","&Hex($WindowHandle,8)&") failed " & @crlf)
  224. if (IsArray($Res)) Then
  225. ConsoleWrite("FFSnapShot Error - IsArray($Res):"&IsArray($Res)&" - Ubound($Res):"&UBound($Res)&" - $Res[0]:"&$Res[0] & @crlf)
  226. else
  227. ConsoleWrite("FFSnapShot Error - IsArray($Res):"&IsArray($Res)&" - $Res:"&$Res & @crlf)
  228. EndIf
  229. $FFLastSnapStatus[$NoSnapShot] = -1
  230. SetError(2)
  231. Return False
  232. EndIf
  233. $FFLastSnapStatus[$NoSnapShot] = 1
  234. $FFLastSnap = $NoSnapShot
  235. Return True
  236. EndFunc
  237. ; Internal Function, don't use it directly
  238. Func SnapShotPreProcessor($Left, $Top, $Right, $Bottom, $ForceNewSnap, $NoSnapShot, $WindowHandle)
  239. ; Si on impose une nouvelle capture ou si aucun SnapShot valide n'a déjà été effectué pour ce N°
  240. if ($ForceNewSnap OR $FFLastSnapStatus[$NoSnapShot] <> 1) Then return FFSnapShot($Left, $Top, $Right, $Bottom, $NoSnapShot, $WindowHandle)
  241. Return True
  242. EndFunc
  243. ; FFNearestPixel Function - This function works like PixelSearch, except that instead of returning the first pixel found,
  244. ; it returns the closest from a given position ($PosX,$PosY)
  245. ; Return Values: If unsuccessful, returns 0 and sets @Error.
  246. ; If successful, an array of 2 elements:
  247. ; [0] : X coordinate of the pixel found the nearest
  248. ; [1] : Y coordinate of the pixel
  249. ; Example: To find the pixel with color 0x00AB0C45 as close as possible from 500, 500 in full screen
  250. ; $Res = FFNearestPixel(500, 500, 0x00AB0C45)
  251. ; If Not @Error Then MsgBox (0, "Resource", "Found in" & $PosX & "," & $PosY)
  252. ;
  253. ; Proto C function: int WINAPI ColorPixelSearch(int &XRef, int &YRef, int ColorToFind, int NoSnapShot)
  254. Func FFNearestPixel($PosX, $PosY, $Color, $ForceNewSnap=true, $Left=0, $Top=0, $Right=0, $Bottom=0, $NoSnapShot=$FFLastSnap, $WindowHandle=-1)
  255. ;local $NoSnapShot = 2 ; Slot utilisé pour les captures d'écran (entre 0 et 3), choisi arbitrairement
  256. if Not SnapShotPreProcessor($Left, $Top, $Right, $Bottom, $ForceNewSnap, $NoSnapShot, $WindowHandle) Then
  257. SetError(2)
  258. Return False
  259. EndIf
  260. local $Result = DllCall($FFDllHandle, "int", "ColorPixelSearch", "int*", $PosX, "int*",$PosY, "int", $Color, "int", $NoSnapShot)
  261. If ( Not IsArray($Result) OR $Result[0]<>1) Then
  262. SetError(1)
  263. Return False
  264. EndIf
  265. local $CoordResult[2] = [$Result[1], $Result[2]] ; PosX, PosY
  266. return $CoordResult
  267. EndFunc
  268. ; FFNearestSpot Function - This feature allows you to find, among all the area (or "spots") containing a minimum number of pixels
  269. ; a given color, the one that is closest to a reference point.
  270. ; Return Values: If unsuccessful, returns 0 and sets @Error.
  271. ; If successful, an array of 3 elements: [0] : X coordinate of the nearest spot [1] : Y coordinate of the nearest spot [2] : Number of pixels found in the nearest spot
  272. ; For example, suppose you want to detect a blue circle (Color = 0x000000FF) partially obscured, diameter 32 pixels (say with at least 45 pixels having the right color)
  273. ; and the closest possible to the position x = 198 and y = 543, in a full screen, so the function is called as follows:
  274. ; FFNearestSpot $Res = (32, 45, 198, 543, 0x000000FF)
  275. ; If Not @Error Then MsgBox (0, "Blue Circle", "The blue circle closest to the position 198, 543 is at "&$PosX&","&$PosY&@LF&" and contains "&$NbPixel&" blue pixels")
  276. ;
  277. ; Proto C function: int WINAPI GenericColorSearch(int SizeSearch, int &NbMatchMin, int &XRef, int &YRef, int ColorToFind, int ShadeVariation, int NoSnapShot)
  278. Func FFNearestSpot($SizeSearch, $NbPixel, $PosX, $PosY, $Color, $ShadeVariation=0, $ForceNewSnap=true, $Left=0, $Top=0, $Right=0, $Bottom=0, $NoSnapShot=$FFLastSnap, $WindowHandle=-1)
  279. if Not SnapShotPreProcessor($Left, $Top, $Right, $Bottom, $ForceNewSnap, $NoSnapShot, $WindowHandle) Then
  280. SetError(2)
  281. Return False
  282. EndIf
  283. local $Result = DllCall($FFDllHandle, "int", "GenericColorSearch", "int", $SizeSearch, "int*", $NbPixel, "int*", $PosX, "int*",$PosY, "int", $Color, "int", $ShadeVariation, "int", $NoSnapShot)
  284. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  285. SetError(1)
  286. Return False
  287. EndIF
  288. local $CoordResult[3] = [$Result[3], $Result[4], $Result[2]] ; PosX, PoxY, Nombre de pixels
  289. return $CoordResult
  290. EndFunc
  291. ; FFBestSpot Function - This feature is similar to FFNearestSpot, but even more powerful.
  292. ; Suppose for instance that you want to find a spot with ideally 200 blue pixels in a 50x50 area, but some of those pixels may be covered, and also for transparency reasons, the color may be a bit different.
  293. ; So, if it can't find a spot with 200 pure blue pixels, you could accept "lower" results, like only 120 blue pixels minimum, and - if enough pure blue pixels can't be found - try to find something close enough
  294. ; with ShadeVariation.
  295. ; FFBestSpot will do that all for you.
  296. ; Here is how it works :
  297. ; Only one additionnal parameters compared to FFNearestSpot : you give the minimum acceptable number of pixels to find, and then the "optimal" number. All other parameters are the same, with same meaning.
  298. ; First, FFBestSpot will try to find if any spot exist with at least the optimal number of pixels and pure color (or colors). If yes, then it return the one that as the shorter distance with $PoxX/$PosY
  299. ; Otherwise, it will try to find the spots that has the better number of pixels in the pure Color (or colors). If it can find a spot with at least the minimum acceptable number of pixels, then it returns this spot.
  300. ; Otherwise, it will try again the two same searches, but now with ShadeVariation as set in the parameter (if this parameter is not 0)
  301. ; If no proper spot can be found, returns 0 in the first element of the array and set @Error=1.
  302. ;
  303. ; Proto C function: int WINAPI ProgressiveSearch(int SizeSearch, int &NbMatchMin, int NbMatchMax, int &XRef, int &YRef, int ColorToFind/*-1 if several colors*/, int ShadeVariation, int NoSnapShot)
  304. Func FFBestSpot($SizeSearch, $MinNbPixel, $OptNbPixel, $PosX, $PosY, $Color, $ShadeVariation=0, $ForceNewSnap=true, $Left=0, $Top=0, $Right=0, $Bottom=0, $NoSnapShot=$FFLastSnap, $WindowHandle=-1)
  305. if Not SnapShotPreProcessor($Left, $Top, $Right, $Bottom, $ForceNewSnap, $NoSnapShot, $WindowHandle) Then
  306. SetError(2)
  307. Return False
  308. EndIf
  309. local $Result = DllCall($FFDllHandle, "int", "ProgressiveSearch", "int", $SizeSearch, "int*", $MinNbPixel, "int", $OptNbPixel, "int*", $PosX, "int*",$PosY, "int", $Color, "int", $ShadeVariation, "int", $NoSnapShot)
  310. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  311. SetError(1)
  312. Return False
  313. EndIf
  314. local $CoordResult[3] = [$Result[4], $Result[5], $Result[2]] ; PosX, PoxY, Nombre de pixels
  315. return $CoordResult
  316. EndFunc
  317. ; FFColorCount Function - This function counts the number of pixels with the specified color, exact or approximate (ShadeVariation).
  318. ;
  319. ; Proto C : int WINAPI ColorCount(int ColorToFind, int NoSnapShot, int ShadeVariation)
  320. Func FFColorCount($ColorToCount, $ShadeVariation=0, $ForceNewSnap=true, $Left=0, $Top=0, $Right=0, $Bottom=0, $NoSnapShot=$FFLastSnap, $WindowHandle=-1)
  321. if Not SnapShotPreProcessor($Left, $Top, $Right, $Bottom, $ForceNewSnap, $NoSnapShot, $WindowHandle) Then
  322. SetError(2)
  323. Return False
  324. EndIf
  325. $Result = DllCall($FFDllHandle, "int", "ColorCount", "int", $ColorToCount, "int", $NoSnapShot, "int", $ShadeVariation)
  326. If (Not IsArray($Result)) Then Return False
  327. Return $Result[0]
  328. EndFunc
  329. ; FFIsDifferent Function - This function compares two SnapShots of the same window and return whether they have different or not .
  330. ; modified by frank10
  331. ; Proto C : int WINAPI HasChanged(int NoSnapShot, int NoSnapShot2, int ShadeVariation); // ** Changed in version 2.0 : ShadeVariation added **
  332. Func FFIsDifferent($NoSnapShot1, $NoSnapShot2, $ShadeVariation=0)
  333. $Result = DllCall($FFDllHandle, "int", "HasChanged", "int", $NoSnapShot1, "int", $NoSnapShot2, "int", $ShadeVariation)
  334. If (Not IsArray($Result)) Then Return False
  335. Return $Result[0]
  336. EndFunc
  337. ; FFLocalizeChanges Function - This function compares two SnapShots and specifies the number of different pixels and the smallest rectangle containing all changes.
  338. ; modified by frank10
  339. ; If unsuccessful, @Error = 1 and returns 0
  340. ; In case of differences, returns an array of 5 elements thus formed:
  341. ; [0]: left edge of the rectangle
  342. ; [1]: upper edge of the rectangle
  343. ; [2]: right edge of the rectangle
  344. ; [3]: lower edge of the rectangle
  345. ; [4]: Number of pixels that changed
  346. ; Proto C : int WINAPI LocalizeChanges(int NoSnapShot, int NoSnapShot2, int &xMin, int &yMin, int &xMax, int &yMax, int &nbFound, int ShadeVariation); // ** Changed in version 2.0 : ShadeVariation added **
  347. Func FFLocalizeChanges($NoSnapShot1, $NoSnapShot2, $ShadeVariation=0)
  348. $Result = DllCall($FFDllHandle, "int", "LocalizeChanges", "int", $NoSnapShot1, "int", $NoSnapShot2, "int*", 0, "int*", 0, "int*", 0, "int*", 0, "int*", 0, "int" , $ShadeVariation )
  349. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  350. SetError(1)
  351. Return False
  352. EndIF
  353. local $TabRes[5] = [$Result[3], $Result[4], $Result[5], $Result[6], $Result[7]];
  354. Return $TabRes
  355. EndFunc
  356. ; FFGetPixel Function - Cette fonction est close to PixelGetColor, except it works on a SnapShot.
  357. ; In order to make this function as fast as possible, you should explicitely make the snapshot before using it (cf benchmark.au3)
  358. ;
  359. ; Proto C : int WINAPI FFgetPixel(int X, int Y, int NoSnapShot)
  360. Func FFGetPixel($x, $y, $NoSnapShot=$FFLastSnap)
  361. $Result = DllCall($FFDllHandle, "int", "FFGetPixel", "int", $x, "int", $y, "int", $NoSnapShot)
  362. If ( (Not IsArray($Result)) or ($Result[0]=-1) ) Then
  363. SetError(2)
  364. Return -1
  365. EndIf
  366. Return $Result[0]
  367. EndFunc
  368. ; FFGetVersion Function - This function returns the version Nb of FastFind DLL
  369. ;
  370. ; Proto C : LPCTSTR WINAPI FFVersion(void)
  371. Func FFGetVersion()
  372. $Result = DllCall($FFDllHandle, "str", "FFVersion")
  373. If ( (Not IsArray($Result)) ) Then
  374. SetError(2)
  375. Return "???"
  376. EndIf
  377. Return $Result[0]
  378. EndFunc
  379. ; FFGetLastError function - This function will return the last error message, if any (won't work if all debug are disabled, as error strings won't be initialized).
  380. ;
  381. ; Proto C : LPCTSTR WINAPI GetLastErrorMsg(void)
  382. Func FFGetLastError()
  383. $Result = DllCall($FFDllHandle, "str", "GetLastErrorMsg")
  384. If ( (Not IsArray($Result)) ) Then
  385. SetError(2)
  386. Return ""
  387. EndIf
  388. Return $Result[0]
  389. EndFunc
  390. global $LastFileNameParam=""
  391. ; New in version 1.6 => Save a SnapShot in a .BMP file.
  392. ; Exemple of usage: FFSaveBMP("TOTO")
  393. Func FFSaveBMP($FileNameWithNoExtension, $ForceNewSnap=false, $Left=0, $Top=0, $Right=0, $Bottom=0, $NoSnapShot=$FFLastSnap, $WindowHandle=-1)
  394. if Not SnapShotPreProcessor($Left, $Top, $Right, $Bottom, $ForceNewSnap, $NoSnapShot, $WindowHandle) Then
  395. SetError(2)
  396. Return False
  397. EndIf
  398. local $Result = DllCall($FFDllHandle, "BOOLEAN", "SaveBMP", "int", $NoSnapShot, "str", $FileNameWithNoExtension)
  399. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  400. SetError(1)
  401. Return False
  402. EndIf
  403. local $Suffixe = DllCall($FFDllHandle, "int", "GetLastFileSuffix")
  404. If (IsArray($Result)) Then
  405. If ($Result[0]>0) Then
  406. $LastFileNameParam = $FileNameWithNoExtension+".BMP"
  407. Else
  408. $LastFileNameParam = $FileNameWithNoExtension+"_"+$Result[0]+".BMP"
  409. EndIf
  410. EndIf
  411. return true
  412. EndFunc
  413. ; New in version 1.6 => Save a SnapShot in a JPEG file.
  414. ; Exemple of usage: FFSaveJPG("TOTO")
  415. Func FFSaveJPG($FileNameWithNoExtension, $QualityFactor=85, $ForceNewSnap=true, $Left=0, $Top=0, $Right=0, $Bottom=0, $NoSnapShot=$FFLastSnap, $WindowHandle=-1)
  416. if Not SnapShotPreProcessor($Left, $Top, $Right, $Bottom, $ForceNewSnap, $NoSnapShot, $WindowHandle) Then
  417. SetError(2)
  418. Return False
  419. EndIf
  420. local $Result = DllCall($FFDllHandle, "BOOLEAN", "SaveJPG", "int", $NoSnapShot, "str", $FileNameWithNoExtension, "ULONG", $QualityFactor)
  421. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  422. SetError(1)
  423. Return False
  424. EndIf
  425. local $Suffixe = DllCall($FFDllHandle, "int", "GetLastFileSuffix")
  426. If (IsArray($Result)) Then
  427. If ($Result[0]>0) Then
  428. $LastFileNameParam = $FileNameWithNoExtension+".JPG"
  429. Else
  430. $LastFileNameParam = $FileNameWithNoExtension+"_"+$Result[0]+".JPG"
  431. EndIf
  432. EndIf
  433. return true
  434. EndFunc
  435. ; Gives the FileName of the last file written with FFSaveJPG of FFSaveBMP
  436. Func FFGetLastFileName()
  437. return $LastFileNameParam
  438. EndFunc
  439. ; Change a SnapShot so that it keeps only the pixels that are different from another SnapShot.
  440. ; modified by frank10
  441. ; Exemple :
  442. ; FFSnapShot(0, 0, 0, 0, 1) ; Takes FullScreen SnapShot N°1
  443. ; Sleep(1000) ; Wait 1 second
  444. ; FFSnapShot(0, 0, 0, 0, 2) ; Takes another SnapShot (N°2)
  445. ; FFKeepChanges(1, 2, 8) ; SnapShot N°1 will have all pixels black, except those that have changed between the 2 SnapShots with a shadevariation of 8. SnapShot N°2 is kept unchanged.
  446. ; FFSaveBMP("snapshot", false, 0,0,0,0, 1) ; Saves the result into snapshot.bmp
  447. ;
  448. ;Prototype : int WINAPI KeepChanges(int NoSnapShot, int NoSnapShot2, int ShadeVariation); // ** Changed in version 2.0 : ShadeVariation added **
  449. Func FFKeepChanges($NoSnapShot1, $NoSnapShot2, $ShadeVariation=0)
  450. $Result = DllCall($FFDllHandle, "int", "KeepChanges", "int", $NoSnapShot1, "int", $NoSnapShot2, "int", $ShadeVariation)
  451. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  452. SetError(1)
  453. Return False
  454. EndIF
  455. Return true
  456. EndFunc
  457. ; Change a SnapShot so that it keeps only the color (or colors if a list is used) asked. All other pixels will be black.
  458. ; Exemple :
  459. ; FFSnapShot(0, 0, 0, 0, 1) ; Takes FullScreen SnapShot N°1
  460. ; Sleep(1000) ; Wait 1 second
  461. ; FFSnapShot(0, 0, 0, 0, 2) ; Takes another SnapShot (N°2)
  462. ; FFKeepChanges(1, 2) ; SnapShot N°1 will have all pixels black, except those that have changed between the 2 SnapShots. SnapShot N°2 is kept unchanged.
  463. ; FFResetColors() ; Rest of the list of colors
  464. ; local $Couleurs[2]=[0x00FF0000, 0x000000FF] ; Pure blue and pure red
  465. ; FFAddColor($Couleurs)
  466. ; FFKeepColor(-1, 60, false, 0,0,0,0, 1, -1) ; As the SnapShot N°1 now has only very few pixels (only changes), we can now make de detection with very high ShadeVariation value
  467. ; ; After this step, the SnapShot N°1 will only have blue and red pixels left.
  468. ;Prototype : int WINAPI KeepColor(int NoSnapShot, int ColorToFind, int ShadeVariation);
  469. Func FFKeepColor($ColorToFind, $ShadeVariation=0, $ForceNewSnap=true, $Left=0, $Top=0, $Right=0, $Bottom=0, $NoSnapShot=$FFLastSnap, $WindowHandle=-1)
  470. if Not SnapShotPreProcessor($Left, $Top, $Right, $Bottom, $ForceNewSnap, $NoSnapShot, $WindowHandle) Then
  471. SetError(2)
  472. Return False
  473. EndIf
  474. $Result = DllCall($FFDllHandle, "int", "KeepColor", "int", $NoSnapShot, "int", $ColorToFind, "int", $ShadeVariation)
  475. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  476. SetError(1)
  477. Return False
  478. EndIF
  479. Return true
  480. EndFunc
  481. ; FFDrawSnapShot will draw the SnapShot back on screen (using the same Window and same position).
  482. ; Can also be used on modified SnapShots (after use of FFsetPixel, FFKeepChanges or FFKeepColor)
  483. ; Proto C: bool WINAPI DrawSnapShot(int NoSnapShot);
  484. Func FFDrawSnapShot($NoSnapShot=$FFLastSnap)
  485. if $FFLastSnapStatus[$NoSnapShot] <> 1 Then
  486. SetError(2)
  487. Return False
  488. EndIf
  489. $Result = DllCall($FFDllHandle, "BOOLEAN", "DrawSnapShot", "int", $NoSnapShot)
  490. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  491. SetError(1)
  492. Return False
  493. EndIF
  494. Return true
  495. EndFunc
  496. ; FFSetPixel will change the color of a pixel in a given SnapShot
  497. ;bool WINAPI FFSetPixel(int x, int y, int Color, int NoSnapShot);
  498. Func FFSetPixel($x, $y, $Color, $NoSnapShot=$FFLastSnap)
  499. if $FFLastSnapStatus[$NoSnapShot] <> 1 Then
  500. SetError(2)
  501. Return False
  502. EndIf
  503. $Result = DllCall($FFDllHandle, "BOOLEAN", "FFSetPixel", "int", $x, "int", $y, "int", $Color, "int", $NoSnapShot)
  504. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  505. SetError(1)
  506. Return False
  507. EndIF
  508. Return true
  509. EndFunc
  510. ;bool WINAPI DuplicateSnapShot(int Src, int Dst);
  511. Func FFDuplicateSnapShot($NoSnapShotSrc, $NoSnapShotDst)
  512. ; If $NoSnapShotSrc do not exist, then make the capture
  513. if Not SnapShotPreProcessor(0, 0, 0, 0, false, $NoSnapShotSrc, -1) Then
  514. SetError(2)
  515. Return False
  516. EndIf
  517. $Result = DllCall($FFDllHandle, "BOOLEAN", "DuplicateSnapShot", "int", $NoSnapShotSrc, "int", $NoSnapShotDst)
  518. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  519. SetError(1)
  520. Return False
  521. EndIF
  522. Return true
  523. EndFunc
  524. ; GetRawData Function - Gives RawBytes of the SnapShot
  525. ; Wrapper made by frank10
  526. ; If unsuccessful, @Error = 1 and returns 0
  527. ; Success: it returns a string stride with the Raw bytes of the SnapShot in 8 Hex digits (BGRA) of pixels from left to right, top to bottom
  528. ; every pixel can be accessed like this: StringMid($sStride, $pixelNo *8 +1 ,8) and you get 685E5B00 68blue 5Egreen 5Bred 00alpha
  529. ;Proto C: int * WINAPI GetRawData(int NoSnapShot, int &NbBytes);
  530. Func FFGetRawData($NoSnapShot=$FFLastSnap)
  531. $aResult = DllCall($FFDllHandle, "ptr", "GetRawData", "int", $NoSnapShot, "int*", 0)
  532. If ( Not IsArray($aResult) ) Then
  533. SetError(1)
  534. Return False
  535. EndIf
  536. Local $t_Raw = DllStructCreate('ubyte['& $aResult[2] & ']',$aResult[0])
  537. Local $sStride = DllStructGetData($t_Raw, 1)
  538. $sStride = StringRight($sStride,StringLen($sStride)-2)
  539. Return $sStride
  540. EndFunc
  541. ; FFComputeMeanValues Function - Gives mean Red, Green and Blue values, useful for detecting changed areas
  542. ; Wrapper made by frank10
  543. ; If unsuccessful, @Error = 1 and returns 0
  544. ; It returns an array with:
  545. ; [0]: MeanRed
  546. ; [1]: MeanGreen
  547. ; [2]: MeanBlue
  548. ; Proto C : int WINAPI ComputeMeanValues(int NoSnapShot, int &MeanRed, int &MeanGreen, int &MeanBlue);
  549. Func FFComputeMeanValues($NoSnapShot=$FFLastSnap)
  550. $aResult = DllCall($FFDllHandle, "int", "ComputeMeanValues", "int", $NoSnapShot, "int*", 0, "int*", 0, "int*", 0)
  551. If ( Not IsArray($aResult) OR $aResult[0]<>1) Then
  552. SetError(1)
  553. Return False
  554. EndIf
  555. local $MeanResult[3] = [$aResult[2], $aResult[3], $aResult[4]] ; MeanRed, MeanGreen, MeanBlue
  556. return $MeanResult
  557. EndFunc
  558. ; FFApplyFilterOnSnapshot Function - apply an AND filter on each pixels in the SnapShot
  559. ; Wrapper made by frank10
  560. ; If unsuccessful, @Error = 1 and returns 0
  561. ; Success: It returns 1
  562. ;Proto C : int WINAPI ApplyFilterOnSnapShot(int NoSnapShot, int Red, int Green, int Blue); // ** New in version 2.0 **
  563. Func FFApplyFilterOnSnapShot($Red, $Green, $Blue, $NoSnapShot=$FFLastSnap)
  564. $aResult = DllCall($FFDllHandle, "int", "ApplyFilterOnSnapShot", "int", $NoSnapShot, "int", $Red, "int", $Green, "int", $Blue)
  565. If ( Not IsArray($aResult) OR $aResult[0]<>1) Then
  566. SetError(1)
  567. Return False
  568. EndIf
  569. return true
  570. EndFunc
  571. ; FFDrawSnapShotXY Function - same as DrawSnapShot, with specific top-left position for drawing
  572. ; Wrapper made by frank10
  573. ; If unsuccessful, @Error = 1 and returns 0
  574. ; Success: returns 1
  575. ;Proto C : bool WINAPI DrawSnapShotXY(int NoSnapShot, int X, int Y); // ** New in version 2.0 **
  576. Func FFDrawSnapShotXY($iX, $iY, $NoSnapShot = $FFLastSnap)
  577. if $FFLastSnapStatus[$NoSnapShot] <> 1 Then
  578. SetError(2)
  579. Return False
  580. EndIf
  581. $Result = DllCall($FFDllHandle, "BOOLEAN", "DrawSnapShotXY", "int", $NoSnapShot, "int", $iX, "int", $iY)
  582. If ((Not IsArray($Result)) OR $Result[0]<>1) Then
  583. SetError(1)
  584. Return False
  585. EndIF
  586. Return true
  587. EndFunc