  graph.lua    :
GetImage
SaveImage
LoadImage
FindColor
FindImage

              .        
,        . ,   ,    
   .  ,           
 LoadImage,        FindColor  FindImage,     findcolor
 findimage.
                ,       
 ,   .     ,  
   LUA\,     Scripts\        :
  require[[Scripts\LUA\graph]]

**        .

  .

  1)    (   ).         
      .       ,       
      .
  2)     ,            .  
                 ,          
       .

  1)  .
  2)   .
  3)   (   ).

           CI -  lua ,    -  ,
                     
.  CI[0]     FindColor.    CI[1]    CI[2]  -   
,              .        
   ,    -        
  ,  1920  1080.     ,       
  ,          .

**     .           ,      
**                 
** -.        -        .    
**   2- :
**   1)        - (          
**       FindColor        1920  1080,        
**             20 736 000 );
**   2)       ,   ffi.cast (     
**      )


 .
------------------------------------------------------------------------------------------------------------------
--    GetImage                                                                                           --
------------------------------------------------------------------------------------------------------------------
GetImage( x1, y1, x2, y2 [,numBitMap [,handle] ] )
  x1, y1, x2, y2 -   
  numBitMap      -       (1  2),   = 1.
  handle         -   ,    

   numBitMap -        .
    handle  ,        handle   - 
         (  abs).

   :
    GetImage( 0, 0, 99, 99 )            -    -      1.
    GetImage( 0, 0, 99, 99, 2 )         -    -      2.
    GetImage( 0, 0, 99, 99, 1, 0)       -   -      1.
    GetImage( 2000, 0, 2099, 99, 1, 0)  -   ( 2- ) -   
                                            1.

             2 073 600    (1920 * 1080),    
maxWidth  () maxHeight  4-  .

**  4             .


------------------------------------------------------------------------------------------------------------------
--    SaveImage                                                                                          --
------------------------------------------------------------------------------------------------------------------
SaveImage(path [,numBitMap [,x1, y1, x2, y2] ] )
  path           -      .
  numBitMap      -         (1  2),   = 1.
  x1, y1, x2, y2 -  ,    (    ).

   :
    GetImage( 0, 0, 99, 99 )            -     -      1.
    SaveImage(C:\temp.bmp)              -    ,       1.

      ,  ,             
           ( ),         .
              2 .

    GetImage( 0, 0, 99, 99, 2 )         -     -      2.
    SaveImage(C:\temp.bmp, 2)                  -    ,     2.
    SaveImage(C:\temp.bmp, 2, 20, 40, 89, 89)  -     ,     2,
           .
      .  x2  y2     ,      GetImage.


------------------------------------------------------------------------------------------------------------------
--    LoadImage                                                                                          --
------------------------------------------------------------------------------------------------------------------
LoadImage(path [,numBitMap] )
  path           -       .
  numBitMap      -       (1  2),   = 1.

         (    )
    length, width, height = GetBMI(numBitMap)
       length -    ; width, height -      .


------------------------------------------------------------------------------------------------------------------
--    FindColor                                                                                          --
------------------------------------------------------------------------------------------------------------------
bool, numFind = FindColor( x1, y1 [,x2,y2] , {condition} )   -   ,  ""  .
  bool             -   - ,   () ,    .
  numFind          -   , -1    , 0 -  1    .

  x1, y1 [,x2,y2]  -   ,    2  -    .
  {condition}      -   ( ).   .      ,
                        .       
                           ,     .
       :
      hW           -    ( = 0 -    ),    , 
                          ,       1 (  ).
      bm           -        ,   = 1.
                         ,         
                       ,      .
      nP           -   ,   = 1.   -   .
      stX          -     X,   = 1.
      stY          -     Y,   = 1.
      r     -    RED   (0 - 255)
      R     -    RED  ,     r,
                     r
      g     -    GREEN  
      G     -    GREEN  ,     g
                     g
      b     -    BLUE  
      B     -    BLUE  ,     b
                     b
      rg    -     RED-GREEN ( ) (-255 - 255)
      RG    -     RED-GREEN,   255
                     rg
      rb    -     RED-BLUE (-255 - 255)
      RB    -     RED-BLUE,   255
                     rb
      gb    -     GREEN-BLUE (-255 - 255)
      GB    -     GREEN-BLUE,   255
                     gb

   (   --lua  )  :

--lua
require[[Scripts\LUA\graph]]                      --      
log 'clear' log 'mode compact'
  --\/            RED =  0  255 ( ) ...
  --\/      ,      .
if FindColor( 7, 2, {hW=workwindow(), bm=2, r=0, R=255} ) then
    log('X=',CI[0][0][0])           --    ,  0, ..  ,
                                    --    = +7
    log('Y=',CI[0][0][1])           --    ,  0
    log(' BLUE=',CI[0][0][2])  --    BLUE   ...
    log(' GREEN=',CI[0][0][3])
    log(' RED=',CI[0][0][4])
end
  --/\           2 (  ), 
  --/\     (      ).
  --/\ CI[0][0] -    ()   ; CI[0][1] -    ..

-------------------------

        .    ,         .    
                 path.
        :  local path = [[A:\find\]]

--lua
local path = [[A:\find\]]                         -- !!!!!      BMP
require[[Scripts\LUA\graph]]                      --      
log 'clear' log 'mode compact'
LoadImage(path..[[Empire.bmp]])                   --        1 ( )
  --\/   8 "" ,    (     1)
local b,k = FindColor (0, 0, 900, 900, {rg=-20, RG=20, rb=-20, RB=20, gb=-20, GB=20, nP=8})
if b then                                         --     (     if k>=0)
    log(' ',k+1,' ') --  1, ..      .
    log('X=',CI[0][k][0])           --     
    log('Y=',CI[0][k][1])           --     
    log(' BLUE=',CI[0][k][2])  --    BLUE    ...
    log(' GREEN=',CI[0][k][3])
    log(' RED=',CI[0][k][4])
end ------------------------------------------------------------------------------

                 .        
    .        .         
         RGB, :
    {r=200, R=255, rg=-40, RG=40, rb=-40, RB=40, gb=-40, GB=40}   --   "" "" 
    {rg=25, rb=25} --        ,   .

          ,        CI[0].      
        CI[0][0],    CI[0][1],   CI[0][2]  ..     
      FindColor  CI[0]  ,      
    ,       ,    FindColor.


------------------------------------------------------------------------------------------------------------------
--    FindImage                                                                                          --
------------------------------------------------------------------------------------------------------------------
NumFindPic, ResultArray = FindImage ( x1, y1, x2, y2, {picture} [, {sourse} ] )
  numFindPic       -   , 0    .
  ResultArray      - ,    

  x1, y1, x2, y2   -   
  picture          -  
  sourse           -   (  )

      sourse -     .
      hW           -   ,    ,       ,
                           1 (  ).
      bm           -        ,   = 1.
                         ,         
                        (),      .
      nP           -   ,   = 1.   -   .
      shiX         -     X,    ,   = 1. 
      shiY         -     Y,    ,   = 1.
                          ,   ,    .
      sim          -       -   accuracy ()  .
                       ,      ,    
                        ,       .
                           .
 **             ,         .
 **                ,   
 **             .      .
 **   v1           -  ()     ,   =2 - RED
 **   v2           -      ,   =1 - GREEN
 **   v3           -      ,   =0 - BLUE
 **            ,           (0,1,2),
 **               .
  
  picture -  .    2 - {<  >, <  >}.
                ,
       FindImage.      ""      
  ,      ,               .
       :

    CreateFindArray( { {condition} }, LengthBM)
      condition  -         .
                       .
      LengthBM   -          .   
                   ,  FindImage     ,  -
                     .
        
               {condition}.
          nf    -   .      LoadImage,    ,
                       .
          bm    -         (  ),    2
                  ( ,       )
          zat   -        ,   0%
                    (   )    
              (condition[1].nf  condition[1].bm  condition[1].zat)

                        .
          r, R, g, G, b, B, rg, RG, rb, RB, gb, GB -     FindColor
          acc   -   -   %   RGB    
                     ,   =0
          dev   -   -   ,   =0,
                     deviation    %,     ( 0  255).
                   ,      .
          fgr   -        ( ) .
                    (     = false)      ,
                          .
                   ,  fgr = true,      .
                    : {r=0, R=255, fgr=true} -     ;
                             {rg=20, rb=20, fgr=true} -        ;
                             {r=0} -    ,  RED     .

   ,    (accurace  deviation)     ,
    ,      (similar),      .
          ( )   .
           .
        ():
  { {r=0, dev=5},
    {fgr=true, g=255, dev=10}
    {fgr=true, b=255, acc=20}
  }
             RED   .
                
   RED   (     )   GREEN = 255.
                 
   RED = 0     GREEN ~= 255     BLUE = 255.
           .
           .
       ,   ,  .
        ,     ().

      -     ""     .
  
=======================================================================================================================


          FindImage,       ,
     .           
  (          ).

**     ,       -          
**  .   ,    ,   ,        
**         .     ,      
**  Ш   .     ,     .

    ,     ,     Empire.bmp.   - ,  
 ,     .  ,        
,        .
    !   ,  40 ,    - !

--lua
local path = [[A:\find\]]                        -- !!!!!      BMP
log 'clear' log 'mode compact'
local screen = loadimage (path..[[Empire.bmp]])  --    
local pict = loadimage (path..[[gold.bmp]])      --    (    )
local accuracy = 80                              --   (   )
local count = -1                                 --   
local deviation = 30                             --   ( = 76  = 30%  255)
local time=os.clock()                            --  
local arr = findimage (0, 0, 1919, 968, {pict}, screen, accuracy, count, deviation)
log ('   : ' .. tostring(os.clock() - time))
if arr then    --   
    log(' : ' .. tostring(#arr) .. '\r\n :')
    for i = 1, #arr do
        log(i .. ' -', arr[i][1], arr[i][2], '\t% :', arr[i][5])
    end
end --------------------------------------------------------------------------------

     {  }:

   : 42.192000000039
 : 24
 :
1 - 910 255 	% : 100
2 - 978 367 	% : 80   { 4  }
3 - 1089 391 	% : 88
4 - 1090 391 	% : 82   {}
5 - 1339 422 	% : 89
6 - 1339 423 	% : 80   {}
7 - 1183 438 	% : 97
8 - 1120 469 	% : 84
9 - 1121 469 	% : 89   {}
10 - 1245 469 	% : 88
11 - 1246 469 	% : 81   {}
12 - 1183 500 	% : 88
13 - 590 516 	% : 97
14 - 1027 516 	% : 97
15 - 528 547 	% : 96
16 - 652 547 	% : 83
17 - 653 547 	% : 87   {}
18 - 964 547 	% : 83
19 - 965 547 	% : 87   {}
20 - 1089 547 	% : 88
21 - 1090 547 	% : 81   {}
22 - 1027 578 	% : 88
23 - 871 594 	% : 97
24 - 996 625 	% : 96

.
         4 (  2).
  ,        ( 2,3,6,7)
  ,       22
    7  ,    .

          FindImage

--lua
local path = [[A:\find\]]                      -- !!!!!      BMP
require[[Scripts\LUA\graph]]                   --      
log 'clear' log 'mode compact'
LoadImage (path..[[Empire.bmp]])
LoadImage (path..[[gold.bmp]],2)
local L,w,h=GetBMI(1)                          --     (  1)
--\/     .
--             gold.bmp
--    RGB=(110,117,41).    ( )   = 76 
--   L -       (Empire.bmp)
local gold=CreateFindArray({ {bm=2, r=110, g=117, b=41, dev=76} }, L)
local time=os.clock()
--\/     1 ( )  (gold),
--      (nP=-1),  80%
local k, m = FindImage( 0, 0, w-1, h-1, gold, {nP=-1, sim=80} )
log ('   : ' .. tostring(os.clock() - time))
if k>0 then
    log(k)
    for i=1,math.min(k,30)do  --   (    30 )
        log(m[i][1],m[i][2])
    end
  else  log("  ")
end --------------------------------------------------------------------------------

     {  }:

   : 9.6900000000605
 : 27
 :
910 255
978 367
1089 391
1090 391   {}
1339 422
1339 423   {}
1183 438
1120 469
1121 469   {}
1245 469
1246 469   {}
1183 500
1183 501   {}
590 516
1027 516
528 547
652 547
653 547    {}
964 547
965 547    {}
1089 547
1090 547   {}
1027 578
1027 579   {}
871 594
996 625
902 641

.
       9 ( 2 )  .       22,      
   .
.
        ,  -              
     accuracy = 79  deviation = 31.    27 ,    22 
     ,    .           .
            ,      ,
            .


  :
   16   :
  local k, m = FindImage( 0, 0, w-1, h-1, gold, {nP=-1, sim=80} )
 ,   shiX=1, shiY=1
  local k, m = FindImage( 0, 0, w-1, h-1, gold, {nP=-1, sim=80, shiX=1, shiY=1} )

        18  ( ).  ,       X      
  50 (     ),    Y  3-  .


     .                 ,
                      .
  2 (2-  )           
:

local gold=CreateFindArray({{bm=2, r=0, R=255, fgr=true, dev=76}},L)
--/\  fgr=true         ,     () 
-- (  ),   r=0, R=255 -       ,
--    ,         ( ).


    .        .       
   ""  .            ,  
     .       "",  
     (       ).
              .
                  .      ""  
- ,         .
    ,   .   ?    ( ) 
.     (   )          
  2 .     ,           2
,         "" .     
 -      .              goldF1.bmp .
   ""   RGB(0,0,0) -   .

       -     ,        ,
     22  (    ).    
       (acc),    (dev).      ,    
     ,       ,   .
          30 (      ).

--lua
local path = [[A:\find\]]                          -- !!!!!      BMP
require[[Scripts\LUA\graph]]                       --      
log 'clear' log 'mode compact'
LoadImage (path..[[Empire.bmp]])                   --    (  1)
LoadImage (path..[[goldF1.bmp]],2)                 --    (  2)
local L,w,h=GetBMI(1)                              --     (  1)
local NotFirstFind = true                          --        
for i = 100, 0, -1 do                              --       ""
  --\/          
  --\/    (   2)     
  --\/     (r=0).
  local k, m = FindImage( 0, 30, w-1, h-1, { {r=0, acc=i} },   {nP=-1, shiX=20, shiY=3} )
  if NotFirstFind  and  k >= 22 then                 --               22 ( )
    if k == 22 then                                    --    ,  
        log("   =",i)         --      
        NotFirstFind = false                               --  ,        
      else                                               -- ,    
        log(" -   ")                --     
        break                                              --  
    end
  end
  if k>22 then                                       --       (  ,  )
    log("   =",i+1)          --       (   )
    break                                              --  
  end
end --------------------------------------------------------------------------------

    :

   = 74
   = 72

     .   ,    ,        
 .      (dev -      ).
  .

--lua
local path = [[A:\find\]]                          -- !!!!!      BMP
require[[Scripts\LUA\graph]]                       --      
log 'clear' log 'mode compact'
LoadImage (path..[[Empire.bmp]])                   --    (  1)
LoadImage (path..[[goldF1.bmp]],2)                 --    (  2)
local L,w,h=GetBMI(1)                              --     (  1)
local NotFirstFind = true                          --        
for i = 0, 200 do                                  --       ""
  --\/          
  --\/    (   2)     
  --\/     (r=0).
  local k, m = FindImage( 0, 30, w-1, h-1, { {r=0, dev=i} },   {nP=-1, shiX=20, shiY=3} )
  if NotFirstFind  and  k >= 22 then                 --               22 ( )
    if k == 22 then                                    --    ,  
        log("   =",i)      --      
        NotFirstFind = false                               --  ,        
      else                                               -- ,    
        log(" -   ")                --     
        break                                              --  
    end
  end
  if k>22 then                                       --       (  ,  )
    log("   =",i-1)       --       (   )
    break                                              --  
  end
end --------------------------------------------------------------------------------

    :

   = 23
   = 61

       ,     38 .  ,   
        BLUE,          
BLUE   .      , ,         
      BLUE.   .    ""  
  -   goldF2.bmp. ,     ,     .
    ,       6 ( ).

--lua
local path = [[A:\find\]]                          -- !!!!!      BMP
require[[Scripts\LUA\graph]]                       --      
log 'clear' log 'mode compact'
LoadImage (path..[[Empire.bmp]])                   --    (  1)
LoadImage (path..[[goldF2.bmp]],2)                 --    (  2)
local L,w,h=GetBMI(1)                              --     (  1)
local NotFirstFind = true                          --        
for i = 100, 0, -1 do                              --       ""
  --\/          
  --\/    (   2)     
  --\/     (r=0).
  local k, m = FindImage( 0, 30, w-1, h-1, { {r=0, acc=i} },   {nP=-1, shiX=20, shiY=3} )
  if NotFirstFind  and  k >= 22 then                 --               22 ( )
    if k == 22 then                                    --    ,  
        log("   =",i)         --      
        NotFirstFind = false                               --  ,        
      else                                               -- ,    
        log(" -   ")                --     
        break                                              --  
    end
  end
  if k>22 then                                       --       (  ,  )
    log("   =",i+1)          --       (   )
    break                                              --  
  end
end --------------------------------------------------------------------------------

    :

   = 82
   = 72

      .  10%        -  .  
- ,    BLUE   "  ".        
       ,       ,        
.      ,  -      ,      ,    
    .   ""   ,    dev = 5,  
       BLUE.
    ,     ""       "",  
      2  1,  {acc=20, dev=10}.
    , ,       dev=5,  12 :
  local k, m = FindImage( 0, 30, w-1, h-1, { {r=0, acc=i} },   {nP=-1, shiX=20, shiY=3} )

  local k, m = FindImage( 0, 30, w-1, h-1, { {r=0, acc=i, dev=5} },   {nP=-1, shiX=20, shiY=3} )

    :

   = 87
   = 74

       13%      5%          .  
      ,    -  ,    
,    -         .
        .          ,   
      .           
,     .
    ! -         ,     
     ,      .        
,      .       ""  (    
 ),       ""  (         
  ).      .          
   "".           .  
     BLUE (    ),    RGB=(255,255,240), 
  BLUE.    goldF3.bmp.
    

--lua
local path = [[A:\find\]]                          -- !!!!!      BMP
require[[Scripts\LUA\graph]]                       --      
log 'clear' log 'mode compact'
LoadImage (path..[[Empire.bmp]])                   --    (  1)
LoadImage (path..[[goldF3.bmp]],2)                 --    (  2)
local L,w,h=GetBMI(1)                              --     (  1)
local NotFirstFind = true                          --        
for i = 100, 0, -1 do                              --       ""
  --\/          
  --\/    (   2)     
  --\/ !!!!!   ,        "" .
  --\/            80% -     RGB (204,204,192)
  --\/     .     (r=0)     .
  local k, m = FindImage( 0, 30, w-1, h-1, { {b=240, fgr=true, acc=80}, {r=0, acc=i, dev=5} },   {nP=-1, shiX=20, shiY=3} )
  if NotFirstFind  and  k >= 22 then                 --               22 ( )
    if k == 22 then                                    --    ,  
        log("   =",i)         --      
        NotFirstFind = false                               --  ,        
      else                                               -- ,    
        log(" -   ")                --     
        break                                              --  
    end
  end
  if k>22 then                                       --       (  ,  )
    log("   =",i+1)          --       (   )
    break                                              --  
  end
end --------------------------------------------------------------------------------
     
    :

   = 86
   = 63

        .               (
 )   23%.        ,  
  acc=74.             
  ,     ,              
""  . ,      1% -         .
 ,                
  1-2 ,   .
      :


--lua
local path = [[A:\find\]]                          -- !!!!!      BMP
require[[Scripts\LUA\graph]]                       --      
log 'clear' log 'mode compact'
LoadImage (path..[[Empire.bmp]])                   --    (  1)
local L,w,h=GetBMI(1)                              --     (  1)
local time=os.clock()
--\/     1 ( )  (goldF3.bmp),    
local k, m = FindImage( 0, 30, w-1, h-1, { {nf=path..[[goldF3.bmp]], b=240, fgr=true, acc=80}, {r=0, acc=74, dev=5} },   {nP=-1, shiX=20, shiY=3} )
log ('   : ' .. tostring(os.clock() - time))
if k>0 then
    log("  - ",k)
    for i=1,math.min(k,30)do  --   (    30 )
        log(i," - ",m[i][1],m[i][2]+30)
    end
  else  log("  ")
end --------------------------------------------------------------------------------

==================================================================================================================

P.S.
         -   ,  " "     ,   .
        "" ,  ,   .
    ,      .   , .      .
         -    .         , 
    ,    ,    ,     ,   -  
    .
       , ,  ,       ( hammer).
          .      Cirus-, 
     - - .   -     .
           .    ,   
       ,      ,     ...
          .  !   - .
       - .   - ,        .
