Jump to content

PixelSearch userdefined increments


FunkyDo
 Share

Recommended Posts

///////////////////////////////////////////////////////////////////////////////
// PixelSearch()
// Finds a pixel of certain color within a RECT of pixels
// PixelSearch(<left>,<top>,<right>,<bottom>,<decimal pixel color>, [<xIncrement>,<yIncrement>])
///////////////////////////////////////////////////////////////////////////////

AUT_RESULT AutoIt_Script::PixelSearch (Vector<Variant> &vParams, Variant &vResult)
{
    int     q,r,t,u;
    COLORREF    s;
    HDC     hdc;
    RECT  rect,relrect;

    relrect.left = vParams[0].nValue();
    relrect.top = vParams[1].nValue();
    relrect.right = vParams[2].nValue();
    relrect.bottom = vParams[3].nValue();

    if (m_bPixelModeScreen == false)
    {
  GetWindowRect(GetForegroundWindow(), &rect);
  relrect.left += rect.left;
  relrect.top += rect.top;
  relrect.right += rect.left;
  relrect.bottom += rect.top;
    }

    t = 1;
    u = 1;
    if (vParams.size() == 7)
    {
  t = vParams[5].nValue();
  u = vParams[6].nValue();
    }

    s = vParams[4].nValue();    // Decimal pixel color to find
    if( (hdc = GetDC(NULL)) )
    {
  for( q=relrect.left;q<=relrect.right;q+=t)
  {
    for( r=relrect.top;r<=relrect.bottom;r+=u)
    {
    if(GetPixel(hdc,q,r)==s)    // Found the pixel
    {
        // Setup vResult as an Array to hold the 2
        // values we want to return
        Variant *pvTemp;

        Util_VariantArrayDim(&vResult, 2);
    
        if (m_bPixelModeScreen == false)
        {
      q-=rect.left;
      r-=rect.top;
        }
        pvTemp = Util_VariantArrayGetRef(&vResult, 0);  //First element
        *pvTemp = q;        // X

        pvTemp = Util_VariantArrayGetRef(&vResult, 1);
        *pvTemp = r;        // Y

        ReleaseDC(NULL,hdc);

        return AUT_OK;
    }      //
    }
  }         // Pixel
  ReleaseDC(NULL,hdc);
    }        //
    SetFuncErrorCode(1);    // Not found
    return AUT_OK;

} // PixelSearch()

Also changing m_nFunctionNumParams for PixelSearch() from 5,5 to 5,7

****************************************************

Test Started [12/16/2003 at 17:03:16]Scan Resolution: 800x600

Increment: 1

- PixelSearch(default): 2469.98451965665 ms

- PixelSearch(edited):  2492.56425904085 ms

Increment: 2

- PixelSearch(default): 2480.47573946259 ms

- PixelSearch(edited):  629.758000377704 ms

Increment: 3

- PixelSearch(default): 2576.90136294153 ms

- PixelSearch(edited):  469.024729995047 ms

Increment: 4

- PixelSearch(default): 2482.52637942154 ms

- PixelSearch(edited):  261.593757905952 ms

Increment: 5

- PixelSearch(default): 3231.39896968747 ms

- PixelSearch(edited):  168.811004728427 ms

I tried a non-Optimized script base increment scanner, however the time results were unrealistic to complete all tests.

-FunkyDo

Edited by FunkyDo
Link to comment
Share on other sites

Yeah, the "shades of variation" concept seems to work well for me. It's not quite as simple as you might think, since I made it so that the colors don't "wrap around" past zero or 255.

In case it's of interest, here's the command description.

And here's the relevant part of the source:

if (aVariation < 0) aVariation = 0;
    if (aVariation > 255) aVariation = 255;
    BYTE search_red = GetRValue(aColor);
    BYTE search_green = GetGValue(aColor);
    BYTE search_blue = GetBValue(aColor);
    BYTE red_low, red_high, green_low, green_high, blue_low, blue_high;
    if (aVariation <= 0)  // User wanted an exact match.
    {
  red_low = red_high = search_red;
  green_low = green_high = search_green;
  blue_low = blue_high = search_blue;
    }
    else
    {
  // Allow colors to vary within the spectrum of intensity, rather than having them
  // wrap around (which doesn't seem to make much sense).  For example, if the user specified
  // a variation of 5, but the red component of aColor is only 0x01, we don't want red_low to go
  // below zero, which would cause it to wrap around to a very intense red color:
  red_low = (aVariation > search_red) ? 0 : search_red - aVariation;
  green_low = (aVariation > search_green) ? 0 : search_green - aVariation;
  blue_low = (aVariation > search_blue) ? 0 : search_blue - aVariation;
  red_high = (aVariation > 0xFF - search_red) ? 0xFF : search_red + aVariation;
  green_high = (aVariation > 0xFF - search_green) ? 0xFF : search_green + aVariation;
  blue_high = (aVariation > 0xFF - search_blue) ? 0xFF : search_blue + aVariation;
    }

    int xpos, ypos, color;
    BYTE red, green, blue;
    bool match_found;
    ResultType result = OK;
    for (xpos = aLeft; xpos <= aRight; ++xpos)
    {
  for (ypos = aTop; ypos <= aBottom; ++ypos)
  {
    color = GetPixel(hdc, xpos, ypos);
    if (aVariation <= 0)  // User wanted an exact match.
    match_found = (color == aColor);
    else  // User specified that some variation in each of the RGB components is allowable.
    {
    red = GetRValue(color);
    green = GetGValue(color);
    blue = GetBValue(color);
    match_found = (red >= red_low && red <= red_high
        && green >= green_low && green <= green_high
        && blue >= blue_low && blue <= blue_high);
    }
    if (match_found) // This pixel matches one of the specified color(s).
    {
    ReleaseDC(NULL, hdc);
    if (output_var_x)
        // Adjust coords to make them relative to the position of the target window:
        if (!output_var_x->Assign(xpos - rect.left))
      result = FAIL;
    if (output_var_y)
        if (!output_var_y->Assign(ypos - rect.top))
      result = FAIL;
    if (result == OK)
        g_ErrorLevel->Assign(ERRORLEVEL_NONE); // Indicate success.
    return result;
    }
  }
    }
Edited by cmallett
Link to comment
Share on other sites

Would you be able to adapt this to be able to input a bitmap .bmp file and output the start location of it within a bigger bitmap?

Instead of just having a PixelSearch, we'd have a BitmapSearch

Edited by MattNis

[quote]I was busy waiting all night for the Columbus Day Bunny to come down my chimney and light fireworks in my pumpkin.There's so much wrong with that.Oh, I'm sorry, i forgot you were Jewish.[/quote]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...