Advertisement
phamduc0110

Gdip_ImageSearch

Mar 23rd, 2019
581
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;**********************************************************************************
  2. ;
  3. ; Gdip_ImageSearch()
  4. ; by MasterFocus - 02/APRIL/2013 00:30h BRT
  5. ; Thanks to guest3456 for helping me ponder some ideas
  6. ; Requires GDIP, Gdip_SetBitmapTransColor() and Gdip_MultiLockedBitsSearch()
  7. ; http://www.autohotkey.com/board/topic/71100-gdip-imagesearch/
  8. ;
  9. ; Licensed under CC BY-SA 3.0 -> http://creativecommons.org/licenses/by-sa/3.0/
  10. ; I waive compliance with the "Share Alike" condition of the license EXCLUSIVELY
  11. ; for these users: tic , Rseding91 , guest3456
  12. ;
  13. ;==================================================================================
  14. ;
  15. ; This function searches for pBitmapNeedle within pBitmapHaystack
  16. ; The returned value is the number of instances found (negative = error)
  17. ;
  18. ; ++ PARAMETERS ++
  19. ;
  20. ; pBitmapHaystack and pBitmapNeedle
  21. ;   Self-explanatory bitmap pointers, are the only required parameters
  22. ;
  23. ; OutputList
  24. ;   ByRef variable to store the list of coordinates where a match was found
  25. ;
  26. ; OuterX1, OuterY1, OuterX2, OuterY2
  27. ;   Equivalent to ImageSearch's X1,Y1,X2,Y2
  28. ;   Default: 0 for all (which searches the whole haystack area)
  29. ;
  30. ; Variation
  31. ;   Just like ImageSearch, a value from 0 to 255
  32. ;   Default: 0
  33. ;
  34. ; Trans
  35. ;   Needle RGB transparent color, should be a numerical value from 0 to 0xFFFFFF
  36. ;   Default: blank (does not use transparency)
  37. ;
  38. ; SearchDirection
  39. ;   Haystack search direction
  40. ;     Vertical preference:
  41. ;       1 = top->left->right->bottom [default]
  42. ;       2 = bottom->left->right->top
  43. ;       3 = bottom->right->left->top
  44. ;       4 = top->right->left->bottom
  45. ;     Horizontal preference:
  46. ;       5 = left->top->bottom->right
  47. ;       6 = left->bottom->top->right
  48. ;       7 = right->bottom->top->left
  49. ;       8 = right->top->bottom->left
  50. ;
  51. ; Instances
  52. ;   Maximum number of instances to find when searching (0 = find all)
  53. ;   Default: 1 (stops after one match is found)
  54. ;
  55. ; LineDelim and CoordDelim
  56. ;   Outer and inner delimiters for the list of coordinates (OutputList)
  57. ;   Defaults: "`n" and ","
  58. ;
  59. ; ++ RETURN VALUES ++
  60. ;
  61. ; -1001 ==> invalid haystack and/or needle bitmap pointer
  62. ; -1002 ==> invalid variation value
  63. ; -1003 ==> X1 and Y1 cannot be negative
  64. ; -1004 ==> unable to lock haystack bitmap bits
  65. ; -1005 ==> unable to lock needle bitmap bits
  66. ; any non-negative value ==> the number of instances found
  67. ;
  68. ;==================================================================================
  69. ;
  70. ;**********************************************************************************
  71.  
  72. /* Gdip_ImageSearch_Wait by MilliMeter
  73.  - Không hoạt động ở các cửa sổ như: chrome,... Do dùng Gdip_BitmapFromHWND để chụp cửa sổ bị che khuất
  74. */
  75.  
  76.  /*;Example
  77. CoordMode,Mouse,Window
  78. f1::
  79.  
  80. pos:=Gdip_ImageSearch_Wait(WinExist("ahk_exe Garena.exe"),A_Desktop "\h1.png")
  81. MouseMove,% pos[1],% pos[2]
  82.  
  83. return
  84.  
  85. Esc::ExitApp
  86. */
  87.  
  88. Gdip_ImageSearch_Wait(WinHwnd,Image)
  89. {
  90.     Loop
  91.     {
  92.         pToken:=Gdip_Startup()
  93.         bmpHaystack := Gdip_BitmapFromHWND(Winhwnd)
  94.         bmpNeedle := Gdip_CreateBitmapFromFile(Image)
  95.         RET := Gdip_ImageSearch(bmpHaystack,bmpNeedle,LIST)
  96.         addx:=Round(Gdip_GetImageWidth(bmpNeedle)/2)
  97.         addy:=Round(Gdip_GetImageHeight(bmpNeedle)/2)
  98.         Gdip_DisposeImage(bmpHaystack)
  99.         Gdip_DisposeImage(bmpNeedle)
  100.         Gdip_Shutdown(pToken)
  101.     }until (List<>"")
  102.     pos:=StrSplit(list,",")
  103.     xp:=pos.1+addx ,yp:=pos.2+addy
  104.     return [xp,yp]
  105. }
  106.  
  107. Gdip_ImageSearch(pBitmapHaystack,pBitmapNeedle,ByRef OutputList=""
  108. ,OuterX1=0,OuterY1=0,OuterX2=0,OuterY2=0,Variation=0,Trans=""
  109. ,SearchDirection=1,Instances=1,LineDelim="`n",CoordDelim=",") {
  110.  
  111.     ; Some validations that can be done before proceeding any further
  112.     If !( pBitmapHaystack && pBitmapNeedle )
  113.         Return -1001
  114.     If Variation not between 0 and 255
  115.         return -1002
  116.     If ( ( OuterX1 < 0 ) || ( OuterY1 < 0 ) )
  117.         return -1003
  118.     If SearchDirection not between 1 and 8
  119.         SearchDirection := 1
  120.     If ( Instances < 0 )
  121.         Instances := 0
  122.  
  123.     ; Getting the dimensions and locking the bits [haystack]
  124.     Gdip_GetImageDimensions(pBitmapHaystack,hWidth,hHeight)
  125.     ; Last parameter being 1 says the LockMode flag is "READ only"
  126.     If Gdip_LockBits(pBitmapHaystack,0,0,hWidth,hHeight,hStride,hScan,hBitmapData,1)
  127.     OR !(hWidth := NumGet(hBitmapData,0))
  128.     OR !(hHeight := NumGet(hBitmapData,4))
  129.         Return -1004
  130.  
  131.     ; Careful! From this point on, we must do the following before returning:
  132.     ; - unlock haystack bits
  133.  
  134.     ; Getting the dimensions and locking the bits [needle]
  135.     Gdip_GetImageDimensions(pBitmapNeedle,nWidth,nHeight)
  136.     ; If Trans is correctly specified, create a backup of the original needle bitmap
  137.     ; and modify the current one, setting the desired color as transparent.
  138.     ; Also, since a copy is created, we must remember to dispose the new bitmap later.
  139.     ; This whole thing has to be done before locking the bits.
  140.     If Trans between 0 and 0xFFFFFF
  141.     {
  142.         pOriginalBmpNeedle := pBitmapNeedle
  143.         pBitmapNeedle := Gdip_CloneBitmapArea(pOriginalBmpNeedle,0,0,nWidth,nHeight)
  144.         Gdip_SetBitmapTransColor(pBitmapNeedle,Trans)
  145.         DumpCurrentNeedle := true
  146.     }
  147.  
  148.     ; Careful! From this point on, we must do the following before returning:
  149.     ; - unlock haystack bits
  150.     ; - dispose current needle bitmap (if necessary)
  151.  
  152.     If Gdip_LockBits(pBitmapNeedle,0,0,nWidth,nHeight,nStride,nScan,nBitmapData)
  153.     OR !(nWidth := NumGet(nBitmapData,0))
  154.     OR !(nHeight := NumGet(nBitmapData,4))
  155.     {
  156.         If ( DumpCurrentNeedle )
  157.             Gdip_DisposeImage(pBitmapNeedle)
  158.         Gdip_UnlockBits(pBitmapHaystack,hBitmapData)
  159.         Return -1005
  160.     }
  161.    
  162.     ; Careful! From this point on, we must do the following before returning:
  163.     ; - unlock haystack bits
  164.     ; - unlock needle bits
  165.     ; - dispose current needle bitmap (if necessary)
  166.  
  167.     ; Adjust the search box. "OuterX2,OuterY2" will be the last pixel evaluated
  168.     ; as possibly matching with the needle's first pixel. So, we must avoid going
  169.     ; beyond this maximum final coordinate.
  170.     OuterX2 := ( !OuterX2 ? hWidth-nWidth+1 : OuterX2-nWidth+1 )
  171.     OuterY2 := ( !OuterY2 ? hHeight-nHeight+1 : OuterY2-nHeight+1 )
  172.  
  173.     OutputCount := Gdip_MultiLockedBitsSearch(hStride,hScan,hWidth,hHeight
  174.     ,nStride,nScan,nWidth,nHeight,OutputList,OuterX1,OuterY1,OuterX2,OuterY2
  175.     ,Variation,SearchDirection,Instances,LineDelim,CoordDelim)
  176.  
  177.     Gdip_UnlockBits(pBitmapHaystack,hBitmapData)
  178.     Gdip_UnlockBits(pBitmapNeedle,nBitmapData)
  179.     If ( DumpCurrentNeedle )
  180.         Gdip_DisposeImage(pBitmapNeedle)
  181.  
  182.     Return OutputCount
  183. }
  184.  
  185. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  186. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  187. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  188.  
  189. ;**********************************************************************************
  190. ;
  191. ; Gdip_SetBitmapTransColor()
  192. ; by MasterFocus - 02/APRIL/2013 00:30h BRT
  193. ; Requires GDIP
  194. ; http://www.autohotkey.com/board/topic/71100-gdip-imagesearch/
  195. ;
  196. ; Licensed under CC BY-SA 3.0 -> http://creativecommons.org/licenses/by-sa/3.0/
  197. ; I waive compliance with the "Share Alike" condition of the license EXCLUSIVELY
  198. ; for these users: tic , Rseding91 , guest3456
  199. ;
  200. ;**********************************************************************************
  201.  
  202. ;==================================================================================
  203. ;
  204. ; This function modifies the Alpha component for all pixels of a certain color to 0
  205. ; The returned value is 0 in case of success, or a negative number otherwise
  206. ;
  207. ; ++ PARAMETERS ++
  208. ;
  209. ; pBitmap
  210. ;   A valid pointer to the bitmap that will be modified
  211. ;
  212. ; TransColor
  213. ;   The color to become transparent
  214. ;   Should range from 0 (black) to 0xFFFFFF (white)
  215. ;
  216. ; ++ RETURN VALUES ++
  217. ;
  218. ; -2001 ==> invalid bitmap pointer
  219. ; -2002 ==> invalid TransColor
  220. ; -2003 ==> unable to retrieve bitmap positive dimensions
  221. ; -2004 ==> unable to lock bitmap bits
  222. ; -2005 ==> DllCall failed (see ErrorLevel)
  223. ; any non-negative value ==> the number of pixels modified by this function
  224. ;
  225. ;==================================================================================
  226.  
  227. Gdip_SetBitmapTransColor(pBitmap,TransColor) {
  228.     static _SetBmpTrans, Ptr, PtrA
  229.     if !( _SetBmpTrans ) {
  230.         Ptr := A_PtrSize ? "UPtr" : "UInt"
  231.         PtrA := Ptr . "*"
  232.         MCode_SetBmpTrans := "
  233.            (LTrim Join
  234.            8b44240c558b6c241cc745000000000085c07e77538b5c2410568b74242033c9578b7c2414894c24288da424000000
  235.            0085db7e458bc18d1439b9020000008bff8a0c113a4e0275178a4c38013a4e01750e8a0a3a0e7508c644380300ff450083c0
  236.            0483c204b9020000004b75d38b4c24288b44241c8b5c2418034c242048894c24288944241c75a85f5e5b33c05dc3,405
  237.            34c8b5424388bda41c702000000004585c07e6448897c2410458bd84c8b4424304963f94c8d49010f1f800000000085db7e3
  238.            8498bc1488bd3660f1f440000410fb648023848017519410fb6480138087510410fb6083848ff7507c640020041ff024883c
  239.            00448ffca75d44c03cf49ffcb75bc488b7c241033c05bc3
  240.            )"
  241.         if ( A_PtrSize == 8 ) ; x64, after comma
  242.             MCode_SetBmpTrans := SubStr(MCode_SetBmpTrans,InStr(MCode_SetBmpTrans,",")+1)
  243.         else ; x86, before comma
  244.             MCode_SetBmpTrans := SubStr(MCode_SetBmpTrans,1,InStr(MCode_SetBmpTrans,",")-1)
  245.         VarSetCapacity(_SetBmpTrans, LEN := StrLen(MCode_SetBmpTrans)//2, 0)
  246.         Loop, %LEN%
  247.             NumPut("0x" . SubStr(MCode_SetBmpTrans,(2*A_Index)-1,2), _SetBmpTrans, A_Index-1, "uchar")
  248.         MCode_SetBmpTrans := ""
  249.         DllCall("VirtualProtect", Ptr,&_SetBmpTrans, Ptr,VarSetCapacity(_SetBmpTrans), "uint",0x40, PtrA,0)
  250.     }
  251.     If !pBitmap
  252.         Return -2001
  253.     If TransColor not between 0 and 0xFFFFFF
  254.         Return -2002
  255.     Gdip_GetImageDimensions(pBitmap,W,H)
  256.     If !(W && H)
  257.         Return -2003
  258.     If Gdip_LockBits(pBitmap,0,0,W,H,Stride,Scan,BitmapData)
  259.         Return -2004
  260.     ; The following code should be slower than using the MCode approach,
  261.     ; but will the kept here for now, just for reference.
  262.     /*
  263.     Count := 0
  264.     Loop, %H% {
  265.         Y := A_Index-1
  266.         Loop, %W% {
  267.             X := A_Index-1
  268.             CurrentColor := Gdip_GetLockBitPixel(Scan,X,Y,Stride)
  269.             If ( (CurrentColor & 0xFFFFFF) == TransColor )
  270.                 Gdip_SetLockBitPixel(TransColor,Scan,X,Y,Stride), Count++
  271.         }
  272.     }
  273.     */
  274.     ; Thanks guest3456 for helping with the initial solution involving NumPut
  275.     Gdip_FromARGB(TransColor,A,R,G,B), VarSetCapacity(TransColor,0), VarSetCapacity(TransColor,3,255)
  276.     NumPut(B,TransColor,0,"UChar"), NumPut(G,TransColor,1,"UChar"), NumPut(R,TransColor,2,"UChar")
  277.     MCount := 0
  278.     E := DllCall(&_SetBmpTrans, Ptr,Scan, "int",W, "int",H, "int",Stride, Ptr,&TransColor, "int*",MCount, "cdecl int")
  279.     Gdip_UnlockBits(pBitmap,BitmapData)
  280.     If ( E != 0 ) {
  281.         ErrorLevel := E
  282.         Return -2005
  283.     }
  284.     Return MCount
  285. }
  286.  
  287. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  288. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  289. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  290.  
  291. ;**********************************************************************************
  292. ;
  293. ; Gdip_MultiLockedBitsSearch()
  294. ; by MasterFocus - 24/MARCH/2013 06:20h BRT
  295. ; Requires GDIP and Gdip_LockedBitsSearch()
  296. ; http://www.autohotkey.com/board/topic/71100-gdip-imagesearch/
  297. ;
  298. ; Licensed under CC BY-SA 3.0 -> http://creativecommons.org/licenses/by-sa/3.0/
  299. ; I waive compliance with the "Share Alike" condition of the license EXCLUSIVELY
  300. ; for these users: tic , Rseding91 , guest3456
  301. ;
  302. ;**********************************************************************************
  303.  
  304. ;==================================================================================
  305. ;
  306. ; This function returns the number of instances found
  307. ; The 8 first parameters are the same as in Gdip_LockedBitsSearch()
  308. ; The other 10 parameters are the same as in Gdip_ImageSearch()
  309. ; Note: the default for the Intances parameter here is 0 (find all matches)
  310. ;
  311. ;==================================================================================
  312.  
  313. Gdip_MultiLockedBitsSearch(hStride,hScan,hWidth,hHeight,nStride,nScan,nWidth,nHeight
  314. ,ByRef OutputList="",OuterX1=0,OuterY1=0,OuterX2=0,OuterY2=0,Variation=0
  315. ,SearchDirection=1,Instances=0,LineDelim="`n",CoordDelim=",")
  316. {
  317.     OutputList := ""
  318.     OutputCount := !Instances
  319.     InnerX1 := OuterX1 , InnerY1 := OuterY1
  320.     InnerX2 := OuterX2 , InnerY2 := OuterY2
  321.  
  322.     ; The following part is a rather ugly but working hack that I
  323.     ; came up with to adjust the variables and their increments
  324.     ; according to the specified Haystack Search Direction
  325.     /*
  326.     Mod(SD,4) = 0 --> iX = 2 , stepX = +0 , iY = 1 , stepY = +1
  327.     Mod(SD,4) = 1 --> iX = 1 , stepX = +1 , iY = 1 , stepY = +1
  328.     Mod(SD,4) = 2 --> iX = 1 , stepX = +1 , iY = 2 , stepY = +0
  329.     Mod(SD,4) = 3 --> iX = 2 , stepX = +0 , iY = 2 , stepY = +0
  330.     SD <= 4   ------> Vertical preference
  331.     SD > 4    ------> Horizontal preference
  332.     */
  333.     ; Set the index and the step (for both X and Y) to +1
  334.     iX := 1, stepX := 1, iY := 1, stepY := 1
  335.     ; Adjust Y variables if SD is 2, 3, 6 or 7
  336.     Modulo := Mod(SearchDirection,4)
  337.     If ( Modulo > 1 )
  338.         iY := 2, stepY := 0
  339.     ; adjust X variables if SD is 3, 4, 7 or 8
  340.     If !Mod(Modulo,3)
  341.         iX := 2, stepX := 0
  342.     ; Set default Preference to vertical and Nonpreference to horizontal
  343.     P := "Y", N := "X"
  344.     ; adjust Preference and Nonpreference if SD is 5, 6, 7 or 8
  345.     If ( SearchDirection > 4 )
  346.         P := "X", N := "Y"
  347.     ; Set the Preference Index and the Nonpreference Index
  348.     iP := i%P%, iN := i%N%
  349.  
  350.     While (!(OutputCount == Instances) && (0 == Gdip_LockedBitsSearch(hStride,hScan,hWidth,hHeight,nStride
  351.     ,nScan,nWidth,nHeight,FoundX,FoundY,OuterX1,OuterY1,OuterX2,OuterY2,Variation,SearchDirection)))
  352.     {
  353.         OutputCount++
  354.         OutputList .= LineDelim FoundX CoordDelim FoundY
  355.         Outer%P%%iP% := Found%P%+step%P%
  356.         Inner%N%%iN% := Found%N%+step%N%
  357.         Inner%P%1 := Found%P%
  358.         Inner%P%2 := Found%P%+1
  359.         While (!(OutputCount == Instances) && (0 == Gdip_LockedBitsSearch(hStride,hScan,hWidth,hHeight,nStride
  360.         ,nScan,nWidth,nHeight,FoundX,FoundY,InnerX1,InnerY1,InnerX2,InnerY2,Variation,SearchDirection)))
  361.         {
  362.             OutputCount++
  363.             OutputList .= LineDelim FoundX CoordDelim FoundY
  364.             Inner%N%%iN% := Found%N%+step%N%
  365.         }
  366.     }
  367.     OutputList := SubStr(OutputList,1+StrLen(LineDelim))
  368.     OutputCount -= !Instances
  369.     Return OutputCount
  370. }
  371.  
  372. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  373. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  374. ;///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  375.  
  376. ;**********************************************************************************
  377. ;
  378. ; Gdip_LockedBitsSearch()
  379. ; by MasterFocus - 24/MARCH/2013 06:20h BRT
  380. ; Mostly adapted from previous work by tic and Rseding91
  381. ;
  382. ; Requires GDIP
  383. ; http://www.autohotkey.com/board/topic/71100-gdip-imagesearch/
  384. ;
  385. ; Licensed under CC BY-SA 3.0 -> http://creativecommons.org/licenses/by-sa/3.0/
  386. ; I waive compliance with the "Share Alike" condition of the license EXCLUSIVELY
  387. ; for these users: tic , Rseding91 , guest3456
  388. ;
  389. ;**********************************************************************************
  390.  
  391. ;==================================================================================
  392. ;
  393. ; This function searches for a single match of nScan within hScan
  394. ;
  395. ; ++ PARAMETERS ++
  396. ;
  397. ; hStride, hScan, hWidth and hHeight
  398. ;   Haystack stuff, extracted from a BitmapData, extracted from a Bitmap
  399. ;
  400. ; nStride, nScan, nWidth and nHeight
  401. ;   Needle stuff, extracted from a BitmapData, extracted from a Bitmap
  402. ;
  403. ; x and y
  404. ;   ByRef variables to store the X and Y coordinates of the image if it's found
  405. ;   Default: "" for both
  406. ;
  407. ; sx1, sy1, sx2 and sy2
  408. ;   These can be used to crop the search area within the haystack
  409. ;   Default: "" for all (does not crop)
  410. ;
  411. ; Variation
  412. ;   Same as the builtin ImageSearch command
  413. ;   Default: 0
  414. ;
  415. ; sd
  416. ;   Haystack search direction
  417. ;     Vertical preference:
  418. ;       1 = top->left->right->bottom [default]
  419. ;       2 = bottom->left->right->top
  420. ;       3 = bottom->right->left->top
  421. ;       4 = top->right->left->bottom
  422. ;     Horizontal preference:
  423. ;       5 = left->top->bottom->right
  424. ;       6 = left->bottom->top->right
  425. ;       7 = right->bottom->top->left
  426. ;       8 = right->top->bottom->left
  427. ;   This value is passed to the internal MCoded function
  428. ;
  429. ; ++ RETURN VALUES ++
  430. ;
  431. ; -3001 to -3006 ==> search area incorrectly defined
  432. ; -3007 ==> DllCall returned blank
  433. ; 0 ==> DllCall succeeded and a match was found
  434. ; -4001 ==> DllCall succeeded but a match was not found
  435. ; anything else ==> the error value returned by the unsuccessful DllCall
  436. ;
  437. ;==================================================================================
  438.  
  439. Gdip_LockedBitsSearch(hStride,hScan,hWidth,hHeight,nStride,nScan,nWidth,nHeight
  440. ,ByRef x="",ByRef y="",sx1=0,sy1=0,sx2=0,sy2=0,Variation=0,sd=1)
  441. {
  442.     static _ImageSearch, Ptr, PtrA
  443.  
  444.     ; Initialize all MCode stuff, if necessary
  445.     if !( _ImageSearch ) {
  446.         Ptr := A_PtrSize ? "UPtr" : "UInt"
  447.         PtrA := Ptr . "*"
  448.  
  449.         MCode_ImageSearch := "
  450.            (LTrim Join
  451.            8b44243883ec205355565783f8010f857a0100008b7c2458897c24143b7c24600f8db50b00008b44244c8b5c245c8b
  452.            4c24448b7424548be80fafef896c242490897424683bf30f8d0a0100008d64240033c033db8bf5896c241c895c2420894424
  453.            183b4424480f8d0401000033c08944241085c90f8e9d0000008b5424688b7c24408beb8d34968b54246403df8d4900b80300
  454.            0000803c18008b442410745e8b44243c0fb67c2f020fb64c06028d04113bf87f792bca3bf97c738b44243c0fb64c06018b44
  455.            24400fb67c28018d04113bf87f5a2bca3bf97c548b44243c0fb63b0fb60c068d04113bf87f422bca3bf97c3c8b4424108b7c
  456.            24408b4c24444083c50483c30483c604894424103bc17c818b5c24208b74241c0374244c8b44241840035c24508974241ce9
  457.            2dffffff8b6c24688b5c245c8b4c244445896c24683beb8b6c24240f8c06ffffff8b44244c8b7c24148b7424544703e8897c
  458.            2414896c24243b7c24600f8cd5feffffe96b0a00008b4424348b4c246889088b4424388b4c24145f5e5d890833c05b83c420
  459.            c383f8020f85870100008b7c24604f897c24103b7c24580f8c310a00008b44244c8b5c245c8b4c24448bef0fafe8f7d88944
  460.            24188b4424548b742418896c24288d4900894424683bc30f8d0a0100008d64240033c033db8bf5896c2420895c241c894424
  461.            243b4424480f8d0401000033c08944241485c90f8e9d0000008b5424688b7c24408beb8d34968b54246403df8d4900b80300
  462.            0000803c03008b442414745e8b44243c0fb67c2f020fb64c06028d04113bf87f792bca3bf97c738b44243c0fb64c06018b44
  463.            24400fb67c28018d04113bf87f5a2bca3bf97c548b44243c0fb63b0fb60c068d04113bf87f422bca3bf97c3c8b4424148b7c
  464.            24408b4c24444083c50483c30483c604894424143bc17c818b5c241c8b7424200374244c8b44242440035c245089742420e9
  465.            2dffffff8b6c24688b5c245c8b4c244445896c24683beb8b6c24280f8c06ffffff8b7c24108b4424548b7424184f03ee897c
  466.            2410896c24283b7c24580f8dd5feffffe9db0800008b4424348b4c246889088b4424388b4c24105f5e5d890833c05b83c420
  467.            c383f8030f85650100008b7c24604f897c24103b7c24580f8ca10800008b44244c8b6c245c8b5c24548b4c24448bf70faff0
  468.            4df7d8896c242c897424188944241c8bff896c24683beb0f8c020100008d64240033c033db89742424895c2420894424283b
  469.            4424480f8d76ffffff33c08944241485c90f8e9f0000008b5424688b7c24408beb8d34968b54246403dfeb038d4900b80300
  470.            0000803c03008b442414745e8b44243c0fb67c2f020fb64c06028d04113bf87f752bca3bf97c6f8b44243c0fb64c06018b44
  471.            24400fb67c28018d04113bf87f562bca3bf97c508b44243c0fb63b0fb60c068d04113bf87f3e2bca3bf97c388b4424148b7c
  472.            24408b4c24444083c50483c30483c604894424143bc17c818b5c24208b7424248b4424280374244c40035c2450e92bffffff
  473.            8b6c24688b5c24548b4c24448b7424184d896c24683beb0f8d0affffff8b7c24108b44241c4f03f0897c2410897424183b7c
  474.            24580f8c580700008b6c242ce9d4feffff83f8040f85670100008b7c2458897c24103b7c24600f8d340700008b44244c8b6c
  475.            245c8b5c24548b4c24444d8bf00faff7896c242c8974241ceb098da424000000008bff896c24683beb0f8c020100008d6424
  476.            0033c033db89742424895c2420894424283b4424480f8d06feffff33c08944241485c90f8e9f0000008b5424688b7c24408b
  477.            eb8d34968b54246403dfeb038d4900b803000000803c03008b442414745e8b44243c0fb67c2f020fb64c06028d04113bf87f
  478.            752bca3bf97c6f8b44243c0fb64c06018b4424400fb67c28018d04113bf87f562bca3bf97c508b44243c0fb63b0fb60c068d
  479.            04113bf87f3e2bca3bf97c388b4424148b7c24408b4c24444083c50483c30483c604894424143bc17c818b5c24208b742424
  480.            8b4424280374244c40035c2450e92bffffff8b6c24688b5c24548b4c24448b74241c4d896c24683beb0f8d0affffff8b4424
  481.            4c8b7c24104703f0897c24108974241c3b7c24600f8de80500008b6c242ce9d4feffff83f8050f85890100008b7c2454897c
  482.            24683b7c245c0f8dc40500008b5c24608b6c24588b44244c8b4c2444eb078da42400000000896c24103beb0f8d200100008b
  483.            e80faf6c2458896c241c33c033db8bf5896c2424895c2420894424283b4424480f8d0d01000033c08944241485c90f8ea600
  484.            00008b5424688b7c24408beb8d34968b54246403dfeb0a8da424000000008d4900b803000000803c03008b442414745e8b44
  485.            243c0fb67c2f020fb64c06028d04113bf87f792bca3bf97c738b44243c0fb64c06018b4424400fb67c28018d04113bf87f5a
  486.            2bca3bf97c548b44243c0fb63b0fb60c068d04113bf87f422bca3bf97c3c8b4424148b7c24408b4c24444083c50483c30483
  487.            c604894424143bc17c818b5c24208b7424240374244c8b44242840035c245089742424e924ffffff8b7c24108b6c241c8b44
  488.            244c8b5c24608b4c24444703e8897c2410896c241c3bfb0f8cf3feffff8b7c24688b6c245847897c24683b7c245c0f8cc5fe
  489.            ffffe96b0400008b4424348b4c24688b74241089088b4424385f89305e5d33c05b83c420c383f8060f85670100008b7c2454
  490.            897c24683b7c245c0f8d320400008b6c24608b5c24588b44244c8b4c24444d896c24188bff896c24103beb0f8c1a0100008b
  491.            f50faff0f7d88974241c8944242ceb038d490033c033db89742424895c2420894424283b4424480f8d06fbffff33c0894424
  492.            1485c90f8e9f0000008b5424688b7c24408beb8d34968b54246403dfeb038d4900b803000000803c03008b442414745e8b44
  493.            243c0fb67c2f020fb64c06028d04113bf87f752bca3bf97c6f8b44243c0fb64c06018b4424400fb67c28018d04113bf87f56
  494.            2bca3bf97c508b44243c0fb63b0fb60c068d04113bf87f3e2bca3bf97c388b4424148b7c24408b4c24444083c50483c30483
  495.            c604894424143bc17c818b5c24208b7424248b4424280374244c40035c2450e92bffffff8b6c24108b74241c0374242c8b5c
  496.            24588b4c24444d896c24108974241c3beb0f8d02ffffff8b44244c8b7c246847897c24683b7c245c0f8de60200008b6c2418
  497.            e9c2feffff83f8070f85670100008b7c245c4f897c24683b7c24540f8cc10200008b6c24608b5c24588b44244c8b4c24444d
  498.            896c241890896c24103beb0f8c1a0100008bf50faff0f7d88974241c8944242ceb038d490033c033db89742424895c242089
  499.            4424283b4424480f8d96f9ffff33c08944241485c90f8e9f0000008b5424688b7c24408beb8d34968b54246403dfeb038d49
  500.            00b803000000803c18008b442414745e8b44243c0fb67c2f020fb64c06028d04113bf87f752bca3bf97c6f8b44243c0fb64c
  501.            06018b4424400fb67c28018d04113bf87f562bca3bf97c508b44243c0fb63b0fb60c068d04113bf87f3e2bca3bf97c388b44
  502.            24148b7c24408b4c24444083c50483c30483c604894424143bc17c818b5c24208b7424248b4424280374244c40035c2450e9
  503.            2bffffff8b6c24108b74241c0374242c8b5c24588b4c24444d896c24108974241c3beb0f8d02ffffff8b44244c8b7c24684f
  504.            897c24683b7c24540f8c760100008b6c2418e9c2feffff83f8080f85640100008b7c245c4f897c24683b7c24540f8c510100
  505.            008b5c24608b6c24588b44244c8b4c24448d9b00000000896c24103beb0f8d200100008be80faf6c2458896c241c33c033db
  506.            8bf5896c2424895c2420894424283b4424480f8d9dfcffff33c08944241485c90f8ea60000008b5424688b7c24408beb8d34
  507.            968b54246403dfeb0a8da424000000008d4900b803000000803c03008b442414745e8b44243c0fb67c2f020fb64c06028d04
  508.            113bf87f792bca3bf97c738b44243c0fb64c06018b4424400fb67c28018d04113bf87f5a2bca3bf97c548b44243c0fb63b0f
  509.            b604068d0c103bf97f422bc23bf87c3c8b4424148b7c24408b4c24444083c50483c30483c604894424143bc17c818b5c2420
  510.            8b7424240374244c8b44242840035c245089742424e924ffffff8b7c24108b6c241c8b44244c8b5c24608b4c24444703e889
  511.            7c2410896c241c3bfb0f8cf3feffff8b7c24688b6c24584f897c24683b7c24540f8dc5feffff8b4424345fc700ffffffff8b
  512.            4424345e5dc700ffffffffb85ff0ffff5b83c420c3,4c894c24204c89442418488954241048894c24085355565741544
  513.            155415641574883ec188b8424c80000004d8bd94d8bd0488bda83f8010f85b3010000448b8c24a800000044890c24443b8c2
  514.            4b80000000f8d66010000448bac24900000008b9424c0000000448b8424b00000008bbc2480000000448b9424a0000000418
  515.            bcd410fafc9894c24040f1f84000000000044899424c8000000453bd00f8dfb000000468d2495000000000f1f80000000003
  516.            3ed448bf933f6660f1f8400000000003bac24880000000f8d1701000033db85ff7e7e458bf4448bce442bf64503f7904d63c
  517.            14d03c34180780300745a450fb65002438d040e4c63d84c035c2470410fb64b028d0411443bd07f572bca443bd17c50410fb
  518.            64b01450fb650018d0411443bd07f3e2bca443bd17c37410fb60b450fb6108d0411443bd07f272bca443bd17c204c8b5c247
  519.            8ffc34183c1043bdf7c8fffc54503fd03b42498000000e95effffff8b8424c8000000448b8424b00000008b4c24044c8b5c2
  520.            478ffc04183c404898424c8000000413bc00f8c20ffffff448b0c24448b9424a000000041ffc14103cd44890c24894c24044
  521.            43b8c24b80000000f8cd8feffff488b5c2468488b4c2460b85ff0ffffc701ffffffffc703ffffffff4883c418415f415e415
  522.            d415c5f5e5d5bc38b8424c8000000e9860b000083f8020f858c010000448b8c24b800000041ffc944890c24443b8c24a8000
  523.            0007cab448bac2490000000448b8424c00000008b9424b00000008bbc2480000000448b9424a0000000418bc9410fafcd418
  524.            bc5894c2404f7d8894424080f1f400044899424c8000000443bd20f8d02010000468d2495000000000f1f80000000004533f
  525.            6448bf933f60f1f840000000000443bb424880000000f8d56ffffff33db85ff0f8e81000000418bec448bd62bee4103ef496
  526.            3d24903d3807a03007460440fb64a02418d042a4c63d84c035c2470410fb64b02428d0401443bc87f5d412bc8443bc97c554
  527.            10fb64b01440fb64a01428d0401443bc87f42412bc8443bc97c3a410fb60b440fb60a428d0401443bc87f29412bc8443bc97
  528.            c214c8b5c2478ffc34183c2043bdf7c8a41ffc64503fd03b42498000000e955ffffff8b8424c80000008b9424b00000008b4
  529.            c24044c8b5c2478ffc04183c404898424c80000003bc20f8c19ffffff448b0c24448b9424a0000000034c240841ffc9894c2
  530.            40444890c24443b8c24a80000000f8dd0feffffe933feffff83f8030f85c4010000448b8c24b800000041ffc944898c24c80
  531.            00000443b8c24a80000000f8c0efeffff8b842490000000448b9c24b0000000448b8424c00000008bbc248000000041ffcb4
  532.            18bc98bd044895c24080fafc8f7da890c24895424048b9424a0000000448b542404458beb443bda0f8c13010000468d249d0
  533.            000000066660f1f84000000000033ed448bf933f6660f1f8400000000003bac24880000000f8d0801000033db85ff0f8e960
  534.            00000488b4c2478458bf4448bd6442bf64503f70f1f8400000000004963d24803d1807a03007460440fb64a02438d04164c6
  535.            3d84c035c2470410fb64b02428d0401443bc87f63412bc8443bc97c5b410fb64b01440fb64a01428d0401443bc87f48412bc
  536.            8443bc97c40410fb60b440fb60a428d0401443bc87f2f412bc8443bc97c27488b4c2478ffc34183c2043bdf7c8a8b8424900
  537.            00000ffc54403f803b42498000000e942ffffff8b9424a00000008b8424900000008b0c2441ffcd4183ec04443bea0f8d11f
  538.            fffff448b8c24c8000000448b542404448b5c240841ffc94103ca44898c24c8000000890c24443b8c24a80000000f8dc2fef
  539.            fffe983fcffff488b4c24608b8424c8000000448929488b4c2468890133c0e981fcffff83f8040f857f010000448b8c24a80
  540.            0000044890c24443b8c24b80000000f8d48fcffff448bac2490000000448b9424b00000008b9424c0000000448b8424a0000
  541.            0008bbc248000000041ffca418bcd4489542408410fafc9894c2404669044899424c8000000453bd00f8cf8000000468d249
  542.            5000000000f1f800000000033ed448bf933f6660f1f8400000000003bac24880000000f8df7fbffff33db85ff7e7e458bf44
  543.            48bce442bf64503f7904d63c14d03c34180780300745a450fb65002438d040e4c63d84c035c2470410fb64b028d0411443bd
  544.            07f572bca443bd17c50410fb64b01450fb650018d0411443bd07f3e2bca443bd17c37410fb60b450fb6108d0411443bd07f2
  545.            72bca443bd17c204c8b5c2478ffc34183c1043bdf7c8fffc54503fd03b42498000000e95effffff8b8424c8000000448b842
  546.            4a00000008b4c24044c8b5c2478ffc84183ec04898424c8000000413bc00f8d20ffffff448b0c24448b54240841ffc14103c
  547.            d44890c24894c2404443b8c24b80000000f8cdbfeffffe9defaffff83f8050f85ab010000448b8424a000000044890424443
  548.            b8424b00000000f8dc0faffff8b9424c0000000448bac2498000000448ba424900000008bbc2480000000448b8c24a800000
  549.            0428d0c8500000000898c24c800000044894c2404443b8c24b80000000f8d09010000418bc4410fafc18944240833ed448bf
  550.            833f6660f1f8400000000003bac24880000000f8d0501000033db85ff0f8e87000000448bf1448bce442bf64503f74d63c14
  551.            d03c34180780300745d438d040e4c63d84d03da450fb65002410fb64b028d0411443bd07f5f2bca443bd17c58410fb64b014
  552.            50fb650018d0411443bd07f462bca443bd17c3f410fb60b450fb6108d0411443bd07f2f2bca443bd17c284c8b5c24784c8b5
  553.            42470ffc34183c1043bdf7c8c8b8c24c8000000ffc54503fc4103f5e955ffffff448b4424048b4424088b8c24c80000004c8
  554.            b5c24784c8b54247041ffc04103c4448944240489442408443b8424b80000000f8c0effffff448b0424448b8c24a80000004
  555.            1ffc083c10444890424898c24c8000000443b8424b00000000f8cc5feffffe946f9ffff488b4c24608b042489018b4424044
  556.            88b4c2468890133c0e945f9ffff83f8060f85aa010000448b8c24a000000044894c2404443b8c24b00000000f8d0bf9ffff8
  557.            b8424b8000000448b8424c0000000448ba424900000008bbc2480000000428d0c8d00000000ffc88944240c898c24c800000
  558.            06666660f1f840000000000448be83b8424a80000000f8c02010000410fafc4418bd4f7da891424894424084533f6448bf83
  559.            3f60f1f840000000000443bb424880000000f8df900000033db85ff0f8e870000008be9448bd62bee4103ef4963d24903d38
  560.            07a03007460440fb64a02418d042a4c63d84c035c2470410fb64b02428d0401443bc87f64412bc8443bc97c5c410fb64b014
  561.            40fb64a01428d0401443bc87f49412bc8443bc97c41410fb60b440fb60a428d0401443bc87f30412bc8443bc97c284c8b5c2
  562.            478ffc34183c2043bdf7c8a8b8c24c800000041ffc64503fc03b42498000000e94fffffff8b4424088b8c24c80000004c8b5
  563.            c247803042441ffcd89442408443bac24a80000000f8d17ffffff448b4c24048b44240c41ffc183c10444894c2404898c24c
  564.            8000000443b8c24b00000000f8ccefeffffe991f7ffff488b4c24608b4424048901488b4c246833c0448929e992f7ffff83f
  565.            8070f858d010000448b8c24b000000041ffc944894c2404443b8c24a00000000f8c55f7ffff8b8424b8000000448b8424c00
  566.            00000448ba424900000008bbc2480000000428d0c8d00000000ffc8890424898c24c8000000660f1f440000448be83b8424a
  567.            80000000f8c02010000410fafc4418bd4f7da8954240c8944240833ed448bf833f60f1f8400000000003bac24880000000f8
  568.            d4affffff33db85ff0f8e89000000448bf1448bd6442bf64503f74963d24903d3807a03007460440fb64a02438d04164c63d
  569.            84c035c2470410fb64b02428d0401443bc87f63412bc8443bc97c5b410fb64b01440fb64a01428d0401443bc87f48412bc84
  570.            43bc97c40410fb60b440fb60a428d0401443bc87f2f412bc8443bc97c274c8b5c2478ffc34183c2043bdf7c8a8b8c24c8000
  571.            000ffc54503fc03b42498000000e94fffffff8b4424088b8c24c80000004c8b5c24780344240c41ffcd89442408443bac24a
  572.            80000000f8d17ffffff448b4c24048b042441ffc983e90444894c2404898c24c8000000443b8c24a00000000f8dcefeffffe
  573.            9e1f5ffff83f8080f85ddf5ffff448b8424b000000041ffc84489442404443b8424a00000000f8cbff5ffff8b9424c000000
  574.            0448bac2498000000448ba424900000008bbc2480000000448b8c24a8000000428d0c8500000000898c24c800000044890c2
  575.            4443b8c24b80000000f8d08010000418bc4410fafc18944240833ed448bf833f6660f1f8400000000003bac24880000000f8
  576.            d0501000033db85ff0f8e87000000448bf1448bce442bf64503f74d63c14d03c34180780300745d438d040e4c63d84d03da4
  577.            50fb65002410fb64b028d0411443bd07f5f2bca443bd17c58410fb64b01450fb650018d0411443bd07f462bca443bd17c3f4
  578.            10fb603450fb6108d0c10443bd17f2f2bc2443bd07c284c8b5c24784c8b542470ffc34183c1043bdf7c8c8b8c24c8000000f
  579.            fc54503fc4103f5e955ffffff448b04248b4424088b8c24c80000004c8b5c24784c8b54247041ffc04103c44489042489442
  580.            408443b8424b80000000f8c10ffffff448b442404448b8c24a800000041ffc883e9044489442404898c24c8000000443b842
  581.            4a00000000f8dc6feffffe946f4ffff8b442404488b4c246089018b0424488b4c2468890133c0e945f4ffff
  582.            )"
  583.         if ( A_PtrSize == 8 ) ; x64, after comma
  584.             MCode_ImageSearch := SubStr(MCode_ImageSearch,InStr(MCode_ImageSearch,",")+1)
  585.         else ; x86, before comma
  586.             MCode_ImageSearch := SubStr(MCode_ImageSearch,1,InStr(MCode_ImageSearch,",")-1)
  587.         VarSetCapacity(_ImageSearch, LEN := StrLen(MCode_ImageSearch)//2, 0)
  588.         Loop, %LEN%
  589.             NumPut("0x" . SubStr(MCode_ImageSearch,(2*A_Index)-1,2), _ImageSearch, A_Index-1, "uchar")
  590.         MCode_ImageSearch := ""
  591.         DllCall("VirtualProtect", Ptr,&_ImageSearch, Ptr,VarSetCapacity(_ImageSearch), "uint",0x40, PtrA,0)
  592.     }
  593.  
  594.     ; Abort if an initial coordinates is located before a final coordinate
  595.     If ( sx2 < sx1 )
  596.         return -3001
  597.     If ( sy2 < sy1 )
  598.         return -3002
  599.  
  600.     ; Check the search box. "sx2,sy2" will be the last pixel evaluated
  601.     ; as possibly matching with the needle's first pixel. So, we must
  602.     ; avoid going beyond this maximum final coordinate.
  603.     If ( sx2 > (hWidth-nWidth+1) )
  604.         return -3003
  605.     If ( sy2 > (hHeight-nHeight+1) )
  606.         return -3004
  607.  
  608.     ; Abort if the width or height of the search box is 0
  609.     If ( sx2-sx1 == 0 )
  610.         return -3005
  611.     If ( sy2-sy1 == 0 )
  612.         return -3006
  613.  
  614.     ; The DllCall parameters are the same for easier C code modification,
  615.     ; even though they aren't all used on the _ImageSearch version
  616.     x := 0, y := 0
  617.     , E := DllCall( &_ImageSearch, "int*",x, "int*",y, Ptr,hScan, Ptr,nScan, "int",nWidth, "int",nHeight
  618.     , "int",hStride, "int",nStride, "int",sx1, "int",sy1, "int",sx2, "int",sy2, "int",Variation
  619.     , "int",sd, "cdecl int")
  620.     Return ( E == "" ? -3007 : E )
  621. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement