Sign in to follow this  
Followers 0
Insolence

PixelFindAll

12 posts in this topic

#1 ·  Posted (edited)

I don't know where to add everything and I'm not sure if I did this properly, took about 15 minutes. I copied a lot of code from WinList/PixelSearch:

///////////////////////////////////////////////////////////////////////////////
// PixelFindAll()
// Returns a 2d array of the found pixel
// PixelFindAll(<left>,<top>,<right>,<bottom>,<pixel>)
///////////////////////////////////////////////////////////////////////////////

AUT_RESULT AutoIt_Script::F_PixelFindAll (VectorVariant &vParams, Variant &vResult)
{
    RECT        relrect;
    POINT       ptOrigin;
    
    int         col;
    BYTE        red, green, blue;
    BYTE        red_low, red_high, green_low, green_high, blue_low, blue_high;
    
    int         x, y;
    
    int      length,width,area;
    
    int      i=0;
    
    HDC         hdc;
    
    Variant     *pvVariant;
    
    // Creating rectangle to search
    relrect.left   = vParams[0].nValue();
    relrect.top = vParams[1].nValue();
    relrect.right  = vParams[2].nValue();
    relrect.bottom = vParams[3].nValue();
    
    // Generating area
    length = vParams[0].nValue() - vParams[2].nValue();
    width  = vParams[1].nValue() - vParams[3].nValue();
    area   = length * width;

    // 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_low = red_high = red;
    green_low = green_high = green;
    blue_low = blue_high = blue;
    
    // Create a 2d array big enough for all the pixels, +1 for the count
    vResult.ArraySubscriptClear();                         // Reset the subscript
    vResult.ArraySubscriptSetNext(area + 1);               // Number of elements 
    vResult.ArraySubscriptSetNext(2);                      // Number of elements ([0]=x. [1]=y)
    vResult.ArrayDim();                                    // Dimension array
       
    hdc = GetDC(NULL);   
      
    for( x=relrect.left; x<=relrect.right; x++)
    {
        for( y=relrect.top; y<=relrect.bottom; y++)
        { 
            col     = GetPixel(hdc, x, y);
            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)
            {
                // Increment _before_ adding to array, starts at 0 where the array size is stored
                i++;

                // Convert coords to screen/active window/client
                x -= ptOrigin.x;
                y -= ptOrigin.y;
                
                // add x to the array
                vResult.ArraySubscriptClear();                      // Reset the subscript
                vResult.ArraySubscriptSetNext(i);
                vResult.ArraySubscriptSetNext(0);                   // [i][0]
                pvVariant = vResult.ArrayGetRef();                  // Get reference to the element
                *pvVariant = x;                                     // Store the x coord
                
                // add y to the array
                vResult.ArraySubscriptClear();                      // Reset the subscript
                vResult.ArraySubscriptSetNext(i);
                vResult.ArraySubscriptSetNext(1);                   // [i][1]
                pvVariant = vResult.ArrayGetRef();                  // Get reference to the element
                *pvVariant = y;                                     // Store the y coord
            }
        }
    }   
    
    ReleaseDC(NULL,hdc);
    
    // Reset the array to number of found pixels
    vResult.ArraySubscriptClear();                         // Reset the subscript
    vResult.ArraySubscriptSetNext(i);                      // Number of elements 
    vResult.ArraySubscriptSetNext(2);                      // Number of elements ([0]=x. [1]=y)
    vResult.ArrayDim();                                    // Dimension array
        
    if ( i == 0 ) 
    {
        SetFuncErrorCode(1);                                // Not found
        return AUT_OK;    
    } else {
        // Set up the count in [0][0]
        vResult.ArraySubscriptClear();                      // Reset the subscript
        vResult.ArraySubscriptSetNext(0);
        vResult.ArraySubscriptSetNext(0);                   // [0][0]
        pvVariant = vResult.ArrayGetRef();                  // Get reference to the element
        *pvVariant = i;                                     // Store the count
        return AUT_OK;    
    }
       
} // PixelFindAll()

EDIT:

Just found the code submission sticky... I should've noticed that -.-. Regardless, I'm still a bit unsure what to do.

EDIT #2:

Updated code, I have it compiling fine and everything... setting error to 1 though. Added a message box to the 'If Pixel found' check, nothing coming up.

EDIT #3:

Fixed for loop error... hopefully I'll find something else. Still getting @error = 1

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



#1 - What exactly is it supposed to do? Return a 2D array with every pixel that matched? It would help if you would show how it was supposed to be called.

#2 - I'm curious what happens if the target window is actively being updated by another process while this is running? The GetDC() isn't making a copy to work from, right?

Share this post


Link to post
Share on other sites

#1 - Return a 2d array, exactly. PixelFindAll(0,0,100,100,111111) for example.

#2 - I'm not sure? Works just like pixelsearch.

I actually have to update that post, lots of problems I've already found/fixed.


"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

Alright, once I get this to work I'll do that :)


"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

#6 ·  Posted (edited)

Doesn't return anything... quite funky. Not exactly sure I did all the color stuff right, I left in most of the color variation stuff to add it later.

EDIT:

Having a bit of trouble trying to figure out how to ReDim the array, can't think of any examples already used...

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

5 days later...


"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

A few more days later -.-


"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... this should be a piece of cake. I'm trying to contribute here.


"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

I'm 90% done with this fellas, I just need a little help. 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

#11 ·  Posted

I feel your pain. Just don't have time to test for you myself, I ended up having to write all my own "search for a bitmap" C++ functions as well to make my stuff work with reasonable speed...

Share this post


Link to post
Share on other sites

#12 ·  Posted

Oh well, at least you replied :(


"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