Advertisement
Guest User

AutoHotKey FindText lib

a guest
Nov 22nd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;; This was generated by the FindText AHK script/library: https://autohotkey.com/boards/viewtopic.php?t=17834
  2.  
  3. ;===== Copy The Following Functions To Your Own Code Just once =====
  4.  
  5.  
  6. ; Note: parameters of the X,Y is the center of the coordinates,
  7. ; and the W,H is the offset distance to the center,
  8. ; So the search range is (X-W, Y-H)-->(X+W, Y+H).
  9. ; err1 is the character "0" fault-tolerant in percentage.
  10. ; err0 is the character "_" fault-tolerant in percentage.
  11. ; Text can be a lot of text to find, separated by "|".
  12. ; ruturn is a array, contains the [X,Y,W,H,Comment] results of Each Find.
  13.  
  14. FindText(x,y,w,h,err1,err0,text)
  15. {
  16.   xywh2xywh(x-w,y-h,2*w+1,2*h+1,x,y,w,h)
  17.   if (w<1 or h<1)
  18.     return, 0
  19.   bch:=A_BatchLines
  20.   SetBatchLines, -1
  21.   ;--------------------------------------
  22.   GetBitsFromScreen(x,y,w,h,Scan0,Stride,bits)
  23.   ;--------------------------------------
  24.   sx:=0, sy:=0, sw:=w, sh:=h, arr:=[]
  25.   Loop, Parse, text, |
  26.   {
  27.     v:=A_LoopField
  28.     IfNotInString, v, $, Continue
  29.     Comment:="", e1:=err1, e0:=err0
  30.     ; You Can Add Comment Text within The <>
  31.     if RegExMatch(v,"<([^>]*)>",r)
  32.       v:=StrReplace(v,r), Comment:=Trim(r1)
  33.     ; You can Add two fault-tolerant in the [], separated by commas
  34.     if RegExMatch(v,"\[([^\]]*)]",r)
  35.     {
  36.       v:=StrReplace(v,r), r1.=","
  37.       StringSplit, r, r1, `,
  38.       e1:=r1, e0:=r2
  39.     }
  40.     StringSplit, r, v, $
  41.     color:=r1, v:=r2
  42.     StringSplit, r, v, .
  43.     w1:=r1, v:=base64tobit(r2), h1:=StrLen(v)//w1
  44.     if (r0<2 or h1<1 or w1>sw or h1>sh or StrLen(v)!=w1*h1)
  45.       Continue
  46.     ;--------------------------------------------
  47.     if InStr(color,"-")
  48.     {
  49.       r:=e1, e1:=e0, e0:=r, v:=StrReplace(v,"1","_")
  50.       v:=StrReplace(StrReplace(v,"0","1"),"_","0")
  51.     }
  52.     mode:=InStr(color,"*") ? 1:0
  53.     color:=RegExReplace(color,"[*\-]") . "@"
  54.     StringSplit, r, color, @
  55.     color:=Round(r1), n:=Round(r2,2)+(!r2)
  56.     n:=Floor(255*3*(1-n)), k:=StrLen(v)*4
  57.     VarSetCapacity(s1, k, 0), VarSetCapacity(s0, k, 0)
  58.     len1:=len0:=0, j:=sw-w1+1, i:=-j
  59.     ListLines, Off
  60.     Loop, Parse, v
  61.     {
  62.       i:=Mod(A_Index,w1)=1 ? i+j : i+1
  63.       if A_LoopField
  64.         NumPut(i, s1, 4*len1++, "int")
  65.       else
  66.         NumPut(i, s0, 4*len0++, "int")
  67.     }
  68.     ListLines, On
  69.     VarSetCapacity(ss, sw*sh, Asc("0"))
  70.     VarSetCapacity(allpos, 1024*4, 0)
  71.     ;--------------------------------------------
  72.     if (num:=PicFind(mode,color,n,Scan0,Stride,sx,sy,sw,sh
  73.       ,ss,s1,s0,len1,len0,e1,e0,w1,h1,allpos))
  74.       or (err1=0 and err0=0
  75.       and (num:=PicFind(mode,color,n,Scan0,Stride,sx,sy,sw,sh
  76.       ,ss,s1,s0,len1,len0,0.05,0.05,w1,h1,allpos)))
  77.     {
  78.       Loop, % num
  79.         pos:=NumGet(allpos, 4*(A_Index-1), "uint")
  80.         , rx:=(pos&0xFFFF)+x, ry:=(pos>>16)+y
  81.         , arr.Push([rx,ry,w1,h1,Comment])
  82.     }
  83.   }
  84.   SetBatchLines, %bch%
  85.   return, arr.MaxIndex() ? arr:0
  86. }
  87.  
  88. PicFind(mode, color, n, Scan0, Stride
  89.   , sx, sy, sw, sh, ByRef ss, ByRef s1, ByRef s0
  90.   , len1, len0, err1, err0, w1, h1, ByRef allpos)
  91. {
  92.   static MyFunc
  93.   if !MyFunc
  94.   {
  95.     x32:="5589E55383EC408B45200FAF45188B551CC1E20201D0894"
  96.     . "5F88B5524B80000000029D0C1E00289C28B451801D08945D0C"
  97.     . "745F400000000C745F000000000837D08000F85F00000008B4"
  98.     . "50CC1E81025FF0000008945CC8B450CC1E80825FF000000894"
  99.     . "5C88B450C25FF0000008945C4C745E800000000E9AC000000C"
  100.     . "745EC00000000E98A0000008B45F883C00289C28B451401D00"
  101.     . "FB6000FB6C02B45CC8945E48B45F883C00189C28B451401D00"
  102.     . "FB6000FB6C02B45C88945E08B55F88B451401D00FB6000FB6C"
  103.     . "02B45C48945DC837DE4007903F75DE4837DE0007903F75DE08"
  104.     . "37DDC007903F75DDC8B55E48B45E001C28B45DC01D03B45107"
  105.     . "F0B8B55F48B452C01D0C600318345EC018345F8048345F4018"
  106.     . "B45EC3B45240F8C6AFFFFFF8345E8018B45D00145F88B45E83"
  107.     . "B45280F8C48FFFFFFE9A30000008B450C83C00169C0E803000"
  108.     . "089450CC745E800000000EB7FC745EC00000000EB648B45F88"
  109.     . "3C00289C28B451401D00FB6000FB6C069D02B0100008B45F88"
  110.     . "3C00189C18B451401C80FB6000FB6C069C04B0200008D0C028"
  111.     . "B55F88B451401D00FB6000FB6C06BC07201C83B450C730B8B5"
  112.     . "5F48B452C01D0C600318345EC018345F8048345F4018B45EC3"
  113.     . "B45247C948345E8018B45D00145F88B45E83B45280F8C75FFF"
  114.     . "FFF8B45242B454883C0018945C08B45282B454C83C0018945B"
  115.     . "C8B453839453C0F4D453C8945D0C745E800000000E9FB00000"
  116.     . "0C745EC00000000E9DF0000008B45E80FAF452489C28B45EC0"
  117.     . "1D08945F88B45408945D88B45448945D4C745F400000000EB7"
  118.     . "08B45F43B45387D2E8B45F48D1485000000008B453001D08B1"
  119.     . "08B45F801D089C28B452C01D00FB6003C31740A836DD801837"
  120.     . "DD800787B8B45F43B453C7D2E8B45F48D1485000000008B453"
  121.     . "401D08B108B45F801D089C28B452C01D00FB6003C30740A836"
  122.     . "DD401837DD40078488345F4018B45F43B45D07C888B45F08D5"
  123.     . "0018955F08D1485000000008B455001D08B4D208B55E801CA8"
  124.     . "9D3C1E3108B4D1C8B55EC01CA09DA8910817DF0FF0300007F2"
  125.     . "8EB0490EB01908345EC018B45EC3B45C00F8C15FFFFFF8345E"
  126.     . "8018B45E83B45BC0F8CF9FEFFFFEB01908B45F083C4405B5DC"
  127.     . "24C00909090"
  128.     x64:="554889E54883EC40894D10895518448945204C894D288B4"
  129.     . "5400FAF45308B5538C1E20201D08945FC8B5548B8000000002"
  130.     . "9D0C1E00289C28B453001D08945D4C745F800000000C745F40"
  131.     . "0000000837D10000F85000100008B4518C1E81025FF0000008"
  132.     . "945D08B4518C1E80825FF0000008945CC8B451825FF0000008"
  133.     . "945C8C745EC00000000E9BC000000C745F000000000E99A000"
  134.     . "0008B45FC83C0024863D0488B45284801D00FB6000FB6C02B4"
  135.     . "5D08945E88B45FC83C0014863D0488B45284801D00FB6000FB"
  136.     . "6C02B45CC8945E48B45FC4863D0488B45284801D00FB6000FB"
  137.     . "6C02B45C88945E0837DE8007903F75DE8837DE4007903F75DE"
  138.     . "4837DE0007903F75DE08B55E88B45E401C28B45E001D03B452"
  139.     . "07F108B45F84863D0488B45584801D0C600318345F0018345F"
  140.     . "C048345F8018B45F03B45480F8C5AFFFFFF8345EC018B45D40"
  141.     . "145FC8B45EC3B45500F8C38FFFFFFE9B60000008B451883C00"
  142.     . "169C0E8030000894518C745EC00000000E98F000000C745F00"
  143.     . "0000000EB748B45FC83C0024863D0488B45284801D00FB6000"
  144.     . "FB6C069D02B0100008B45FC83C0014863C8488B45284801C80"
  145.     . "FB6000FB6C069C04B0200008D0C028B45FC4863D0488B45284"
  146.     . "801D00FB6000FB6C06BC07201C83B451873108B45F84863D04"
  147.     . "88B45584801D0C600318345F0018345FC048345F8018B45F03"
  148.     . "B45487C848345EC018B45D40145FC8B45EC3B45500F8C65FFF"
  149.     . "FFF8B45482B859000000083C0018945C48B45502B859800000"
  150.     . "083C0018945C08B45703945780F4D45788945D4C745EC00000"
  151.     . "000E926010000C745F000000000E90A0100008B45EC0FAF454"
  152.     . "889C28B45F001D08945FC8B85800000008945DC8B858800000"
  153.     . "08945D8C745F800000000E9840000008B45F83B45707D3A8B4"
  154.     . "5F84898488D148500000000488B45604801D08B108B45FC01D"
  155.     . "04863D0488B45584801D00FB6003C31740E836DDC01837DDC0"
  156.     . "00F88910000008B45F83B45787D368B45F84898488D1485000"
  157.     . "00000488B45684801D08B108B45FC01D04863D0488B4558480"
  158.     . "1D00FB6003C30740A836DD801837DD80078568345F8018B45F"
  159.     . "83B45D40F8C70FFFFFF8B45F48D50018955F44898488D14850"
  160.     . "0000000488B85A00000004801D08B4D408B55EC01CAC1E2104"
  161.     . "189D08B4D388B55F001CA4409C28910817DF4FF0300007F28E"
  162.     . "B0490EB01908345F0018B45F03B45C40F8CEAFEFFFF8345EC0"
  163.     . "18B45EC3B45C00F8CCEFEFFFFEB01908B45F44883C4405DC39"
  164.     . "090909090909090909090909090"
  165.     MCode(MyFunc, A_PtrSize=8 ? x64:x32)
  166.   }
  167.   return, DllCall(&MyFunc, "int",mode
  168.     , "uint",color, "int",n, "ptr",Scan0, "int",Stride
  169.     , "int",sx, "int",sy, "int",sw, "int",sh
  170.     , "ptr",&ss, "ptr",&s1, "ptr",&s0, "int",len1, "int",len0
  171.     , "int",Round(len1*err1), "int",Round(len0*err0)
  172.     , "int",w1, "int",h1, "ptr",&allpos)
  173. }
  174.  
  175. xywh2xywh(x1,y1,w1,h1,ByRef x,ByRef y,ByRef w,ByRef h)
  176. {
  177.   SysGet, zx, 76
  178.   SysGet, zy, 77
  179.   SysGet, zw, 78
  180.   SysGet, zh, 79
  181.   left:=x1, right:=x1+w1-1, up:=y1, down:=y1+h1-1
  182.   left:=left<zx ? zx:left, right:=right>zx+zw-1 ? zx+zw-1:right
  183.   up:=up<zy ? zy:up, down:=down>zy+zh-1 ? zy+zh-1:down
  184.   x:=left, y:=up, w:=right-left+1, h:=down-up+1
  185. }
  186.  
  187. GetBitsFromScreen(x,y,w,h,ByRef Scan0,ByRef Stride,ByRef bits)
  188. {
  189.   VarSetCapacity(bits,w*h*4,0), bpp:=32
  190.   Scan0:=&bits, Stride:=((w*bpp+31)//32)*4
  191.   Ptr:=A_PtrSize ? "UPtr" : "UInt", PtrP:=Ptr . "*"
  192.   win:=DllCall("GetDesktopWindow", Ptr)
  193.   hDC:=DllCall("GetWindowDC", Ptr,win, Ptr)
  194.   mDC:=DllCall("CreateCompatibleDC", Ptr,hDC, Ptr)
  195.   ;-------------------------
  196.   VarSetCapacity(bi, 40, 0), NumPut(40, bi, 0, "int")
  197.   NumPut(w, bi, 4, "int"), NumPut(-h, bi, 8, "int")
  198.   NumPut(1, bi, 12, "short"), NumPut(bpp, bi, 14, "short")
  199.   ;-------------------------
  200.   if hBM:=DllCall("CreateDIBSection", Ptr,mDC, Ptr,&bi
  201.     , "int",0, PtrP,ppvBits, Ptr,0, "int",0, Ptr)
  202.   {
  203.     oBM:=DllCall("SelectObject", Ptr,mDC, Ptr,hBM, Ptr)
  204.     DllCall("BitBlt", Ptr,mDC, "int",0, "int",0, "int",w, "int",h
  205.       , Ptr,hDC, "int",x, "int",y, "uint",0x00CC0020|0x40000000)
  206.     DllCall("RtlMoveMemory", Ptr,Scan0, Ptr,ppvBits, Ptr,Stride*h)
  207.     DllCall("SelectObject", Ptr,mDC, Ptr,oBM)
  208.     DllCall("DeleteObject", Ptr,hBM)
  209.   }
  210.   DllCall("DeleteDC", Ptr,mDC)
  211.   DllCall("ReleaseDC", Ptr,win, Ptr,hDC)
  212. }
  213.  
  214. MCode(ByRef code, hex)
  215. {
  216.   ListLines, Off
  217.   bch:=A_BatchLines
  218.   SetBatchLines, -1
  219.   VarSetCapacity(code, StrLen(hex)//2)
  220.   Loop, % StrLen(hex)//2
  221.     NumPut("0x" . SubStr(hex,2*A_Index-1,2), code, A_Index-1, "char")
  222.   Ptr:=A_PtrSize ? "UPtr" : "UInt"
  223.   DllCall("VirtualProtect", Ptr,&code, Ptr
  224.     ,VarSetCapacity(code), "uint",0x40, Ptr . "*",0)
  225.   SetBatchLines, %bch%
  226.   ListLines, On
  227. }
  228.  
  229. base64tobit(s)
  230. {
  231.   ListLines, Off
  232.   Chars:="0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  233.     . "abcdefghijklmnopqrstuvwxyz"
  234.   SetFormat, IntegerFast, d
  235.   StringCaseSense, On
  236.   Loop, Parse, Chars
  237.   {
  238.     i:=A_Index-1, v:=(i>>5&1) . (i>>4&1)
  239.       . (i>>3&1) . (i>>2&1) . (i>>1&1) . (i&1)
  240.     s:=StrReplace(s,A_LoopField,v)
  241.   }
  242.   StringCaseSense, Off
  243.   s:=SubStr(s,1,InStr(s,"1",0,0)-1)
  244.   s:=RegExReplace(s,"[^01]+")
  245.   ListLines, On
  246.   return, s
  247. }
  248.  
  249. bit2base64(s)
  250. {
  251.   ListLines, Off
  252.   s:=RegExReplace(s,"[^01]+")
  253.   s.=SubStr("100000",1,6-Mod(StrLen(s),6))
  254.   s:=RegExReplace(s,".{6}","|$0")
  255.   Chars:="0123456789+/ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  256.     . "abcdefghijklmnopqrstuvwxyz"
  257.   SetFormat, IntegerFast, d
  258.   Loop, Parse, Chars
  259.   {
  260.     i:=A_Index-1, v:="|" . (i>>5&1) . (i>>4&1)
  261.       . (i>>3&1) . (i>>2&1) . (i>>1&1) . (i&1)
  262.     s:=StrReplace(s,v,A_LoopField)
  263.   }
  264.   ListLines, On
  265.   return, s
  266. }
  267.  
  268. ASCII(s)
  269. {
  270.   if RegExMatch(s,"(\d+)\.([\w+/]{3,})",r)
  271.   {
  272.     s:=RegExReplace(base64tobit(r2),".{" r1 "}","$0`n")
  273.     s:=StrReplace(StrReplace(s,"0","_"),"1","0")
  274.   }
  275.   else s=
  276.   return, s
  277. }
  278.  
  279. ; You can put the text library at the beginning of the script,
  280. ; and Use Pic(Text,1) to add the text library to Pic()'s Lib,
  281. ; Use Pic("comment1|comment2|...") to get text images from Lib
  282.  
  283. Pic(comments, add_to_Lib=0) {
  284.   static Lib:=[]
  285.   if (add_to_Lib)
  286.   {
  287.     re:="<([^>]*)>[^$]+\$\d+\.[\w+/]{3,}"
  288.     Loop, Parse, comments, |
  289.       if RegExMatch(A_LoopField,re,r)
  290.         Lib[Trim(r1)]:=r
  291.   }
  292.   else
  293.   {
  294.     text:=""
  295.     Loop, Parse, comments, |
  296.       text.="|" . Lib[Trim(A_LoopField)]
  297.     return, text
  298.   }
  299. }
  300.  
  301. FindTextOCR(nX, nY, nW, nH, err1, err0, Text, Interval=5) {
  302.   OCR:="", Right_X:=nX+nW
  303.   While (ok:=FindText(nX, nY, nW, nH, err1, err0, Text))
  304.   {
  305.     ; For multi text search, This is the number of text images found
  306.     Loop, % ok.MaxIndex()
  307.     {
  308.       ; X is the X coordinates of the upper left corner
  309.       ; and W is the width of the image have been found
  310.       i:=A_Index, x:=ok[i].1, y:=ok[i].2
  311.         , w:=ok[i].3, h:=ok[i].4, comment:=ok[i].5
  312.       ; We need the leftmost X coordinates
  313.       if (A_Index=1 or x<Left_X)
  314.         Left_X:=x, Left_W:=w, Left_OCR:=comment
  315.     }
  316.     ; If the interval exceeds the set value, add "*" to the result
  317.     OCR.=(A_Index>1 and Left_X-Last_X>Interval ? "*":"") . Left_OCR
  318.     ; Update nX and nW for next search
  319.     x:=Left_X+Left_W, nW:=(Right_X-x)//2, nX:=x+nW, Last_X:=x
  320.   }
  321.   Return, OCR
  322. }
  323.  
  324.  
  325. /***** C source code of machine code *****
  326.  
  327. int __attribute__((__stdcall__)) PicFind(int mode
  328.   , unsigned int c, int n, unsigned char * Bmp
  329.   , int Stride, int sx, int sy, int sw, int sh
  330.   , char * ss, int * s1, int * s0
  331.   , int len1, int len0, int err1, int err0
  332.   , int w1, int h1, int * allpos)
  333. {
  334.   int o=sy*Stride+sx*4, j=Stride-4*sw, i=0, num=0;
  335.   int x, y, w, h, r, g, b, rr, gg, bb, e1, e0;
  336.   if (mode==0)  // Color Mode
  337.   {
  338.     rr=(c>>16)&0xFF; gg=(c>>8)&0xFF; bb=c&0xFF;
  339.     for (y=0; y<sh; y++, o+=j)
  340.       for (x=0; x<sw; x++, o+=4, i++)
  341.       {
  342.         r=Bmp[2+o]-rr; g=Bmp[1+o]-gg; b=Bmp[o]-bb;
  343.         if (r<0) r=-r; if (g<0) g=-g; if (b<0) b=-b;
  344.         if (r+g+b<=n) ss[i]='1';
  345.       }
  346.   }
  347.   else  // Gray Threshold Mode
  348.   {
  349.     c=(c+1)*1000;
  350.     for (y=0; y<sh; y++, o+=j)
  351.       for (x=0; x<sw; x++, o+=4, i++)
  352.         if (Bmp[2+o]*299+Bmp[1+o]*587+Bmp[o]*114<c)
  353.           ss[i]='1';
  354.   }
  355.   w=sw-w1+1; h=sh-h1+1;
  356.   j=len1>len0 ? len1 : len0;
  357.   for (y=0; y<h; y++)
  358.   {
  359.     for (x=0; x<w; x++)
  360.     {
  361.       o=y*sw+x; e1=err1; e0=err0;
  362.       for (i=0; i<j; i++)
  363.       {
  364.         if (i<len1 && ss[o+s1[i]]!='1' && (--e1)<0)
  365.           goto NoMatch;
  366.         if (i<len0 && ss[o+s0[i]]!='0' && (--e0)<0)
  367.           goto NoMatch;
  368.       }
  369.       allpos[num++]=(sy+y)<<16|(sx+x);
  370.       if (num>=1024) goto MaxNum;
  371.       NoMatch:
  372.       continue;
  373.     }
  374.   }
  375.   MaxNum:
  376.   return num;
  377. }
  378.  
  379. */
  380.  
  381. ;================= The End =================
  382.  
  383. ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement