Sign in to follow this  
Followers 0
Insolence

Modifying PixelSearch

3 posts in this topic

#1 ·  Posted (edited)

AUT_RESULT AutoIt_Script::F_PixelSearch (VectorVariant &vParams, Variant &vResult)
{
    uint        iNumParams = vParams.size();
    int         q,r;
    int         col;
    BYTE        red, green, blue;
    BYTE        red_low, red_high, green_low, green_high, blue_low, blue_high;
    HDC         hdc;
    RECT        relrect;
    int         nVar;
    int         nStep = 1;
    int      iReturnAll = 0;  
    int      iIndex = 0;       
    POINT       ptOrigin;

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

    // Convert coords to screen/active window/client
    ConvertCoords(m_nCoordPixelMode, ptOrigin);
    relrect.left += ptOrigin.x;
    relrect.top += ptOrigin.y;
    relrect.right += ptOrigin.x;
    relrect.bottom += ptOrigin.y;


    // Get the search colour and split into components
    col     = vParams[4].nValue();              // Pixel color to find
    if (m_bColorModeBGR == false)
        Util_RGBtoBGR(col);                     // Get RGB color into the standard COLORREF BGR format

    red     = GetRValue(col);
    green   = GetGValue(col);
    blue    = GetBValue(col);

    // Variation required?
    if (iNumParams >= 6)
    {
        nVar = vParams[5].nValue();
        if ( nVar < 0 )
            nVar = 0;
        else if ( nVar > 0xff )
            nVar = 0xff;
    }
    else
        nVar = 0;

    // Step required?
    if (iNumParams >= 7 && vParams[6].nValue() > 1)
        nStep = vParams[6].nValue();
        
    // Find all required?
    if (iNumParams >= 8 && vParams[7].nValue() > 1)
        iReturnAll = vParams[7].nValue();

    // Config the variation code
    if (nVar == 0)                              // Exact match
    {
        red_low = red_high = red;
        green_low = green_high = green;
        blue_low = blue_high = blue;
    }
    else
    {
        // Prevent wrap around
        red_low = (nVar > red) ? 0 : red - nVar;
        green_low = (nVar > green) ? 0 : green - nVar;
        blue_low = (nVar > blue) ? 0 : blue - nVar;
        red_high = (nVar > 0xFF - red) ? 0xFF : red + nVar;
        green_high = (nVar > 0xFF - green) ? 0xFF : green + nVar;
        blue_high = (nVar > 0xFF - blue) ? 0xFF : blue + nVar;
    }

    hdc = GetDC(NULL);
    
    if ( iReturnAll > 0 )
    {
        // Count possible elements
        int length = vParams[0].nValue() - vParams[1].nValue();
        int width  = vParams[2].nValue() - vParams[3].nValue();
        int count  = length * width;
                   
        // Dim the array
        vResult.ArraySubscriptClear();                      // Reset the subscript
        vResult.ArraySubscriptSetNext(count + 1);           // Number of elements
        vResult.ArraySubscriptSetNext(2);                   // Number of elements ([0]=x. [1]=y)
        vResult.ArrayDim();                                 // Dimension array
    }

    for( q=relrect.left; q<=relrect.right; q = q + nStep)
    {
        for( r=relrect.top; r<=relrect.bottom; r = r + nStep)
        {
            col     = GetPixel(hdc, q, r);
            red     = GetRValue(col);
            green   = GetGValue(col);
            blue    = GetBValue(col);

            if (red >= red_low && red <= red_high && green >= green_low && green <= green_high
                    && blue >= blue_low && blue <= blue_high)
            {
                // Match!

                // Convert coords to screen/active window/client
                q -= ptOrigin.x;
                r -= ptOrigin.y;

                if ( iReturnAll > 0 )
                {
                    Util_Variant2DArrayAssign<int>(&vResult, iIndex + 1, 0, q);
                    Util_Variant2DArrayAssign<int>(&vResult, iIndex + 1, 1, r);
                    iIndex++;
                } else 
                {
                    // Setup vResult as an Array to hold the 2 values we want to return
                    Variant *pvTemp;
                    
                    pvTemp = Util_VariantArrayGetRef(&vResult, iIndex); //First element
                    *pvTemp = q;                    // X
        
                    pvTemp = Util_VariantArrayGetRef(&vResult, iIndex);
                    *pvTemp = r;                    // Y
                    
                    return AUT_OK;
                }
            }
        }
    }

    ReleaseDC(NULL,hdc);

    SetFuncErrorCode(1);            // Not found

    return AUT_OK;

} // PixelSearch()

EDIT:

Changing it a bunch, actually starting to make sense now.

Just need to figure out how to do multi-dimensional arrays?

EDIT #2:

I think I have it figured out, I just hate to have that 'friggin If right in the loop. I bet someone here knows a way around that... or possibly creating 'PixelFindAll' or something similar.

Anyway, AutoIT gets an exception as soon as it tries to run that, I also defined PixelSearch to allow 8 parameters.

Edited by Insolence

"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites



Could someone expirienced please have their way with this? :lmao:


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
Share on other sites

C'mon you guys, it's almost been a week... please?


"I thoroughly disapprove of duels. If a man should challenge me, I would take him kindly and forgivingly by the hand and lead him to a quiet place and kill him." - Mark TwainPatient: "It hurts when I do $var_"Doctor: "Don't do $var_" - Lar.

Share this post


Link to post
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
Sign in to follow this  
Followers 0