Matheus Souza Matheus Souza - 1 month ago 7
C++ Question

How to use GetPixel() to check different positions

I have a program that HAS to use GetPixel, but in different positions, after reading one value and evaluating it, it has to change the x,y positions and re-evaluate the values of the other function.
I have the current code:

while (true) {
HDC hDC;
hDC = GetDC(NULL);
int cx = 793;
int cy = 866;
COLORREF color = GetPixel(hDC, cx, cy); //Use x=793 y=866
ReleaseDC(NULL, hDC);
RGBTRIPLE rgb;
int red = GetRValue(color);
int green = GetGValue(color);
int blue = GetBValue(color);
std::thread t1 (verde, red, green, blue);
t1.join();
std::thread t2(amarelo, red, green, blue);//But then here I would like it to read
//from let's say x=803 y=796
t2.join();
}


The problem is GetPixel should use X=793 AND Y=866 for the
verde
function and then use x=803 y=796 for the
amarelo
function

Answer

The simple answer to your question is that you can call GetPixel() multiple times with different coordinates.

Also, there is no need to call GetDC(0) on each loop iteration. You should call it one time before entering the loop.

Try this:

COLORREF color;
int red, green, blue;

HDC hDC = GetDC(NULL);
while (some_condition_is_true) { // don't write infinite loops!
    color = GetPixel(hDC, 793, 866);
    red = GetRValue(color);
    green = GetGValue(color);
    blue = GetBValue(color);
    std::thread t1(verde, red, green, blue); 
    t1.join();

    color = GetPixel(hDC, 803, 796);
    red = GetRValue(color);
    green = GetGValue(color);
    blue = GetBValue(color);
    std::thread t2(amarelo, red, green, blue);
    t2.join();
}
ReleaseDC(NULL, hDC);

However, that being said, your threads are wasted overhead. Your loop is blocked waiting for the first thread to complete before starting the second thread, and is blocked waiting on the second thread to complete before going to the next loop iteration. You are running your code in a serialized manner, defeating the purpose of using threads, so you may as well just remove the threads altogether and call the functions directly:

COLORREF color;
int red, green, blue;

HDC hDC = GetDC(NULL);
while (some_condition_is_true) {
    color = GetPixel(hDC, 793, 866);
    red = GetRValue(color);
    green = GetGValue(color);
    blue = GetBValue(color);
    verde(red, green, blue);

    color = GetPixel(hDC, 803, 796);
    red = GetRValue(color);
    green = GetGValue(color);
    blue = GetBValue(color);
    amarelo(red, green, blue);
}
ReleaseDC(NULL, hDC);

If you want to use threads to process multiple pixels in parallel, it should look more like this:

COLORREF color;
int red, green, blue;

HDC hDC = GetDC(NULL);
while (some_condition_is_true) {
    color = GetPixel(hDC, 793, 866);
    red = GetRValue(color);
    green = GetGValue(color);
    blue = GetBValue(color);
    std::thread t1(verde, red, green, blue); 

    color = GetPixel(hDC, 803, 796);
    red = GetRValue(color);
    green = GetGValue(color);
    blue = GetBValue(color);
    std::thread t2(amarelo, red, green, blue);

    t1.join();
    t2.join();
}
ReleaseDC(NULL, hDC);

Or even like this:

void verde(HDC hDC, int x, int y)
{
    COLORREF color;
    int red, green, blue;

    while (some_condition_is_true) {
        color = GetPixel(hDC, x, y);
        red = GetRValue(color);
        green = GetGValue(color);
        blue = GetBValue(color);
        //...
    }
} 

void amarelo(HDC hDC, int x, int, y)
{
    COLORREF color;
    int red, green, blue;

    while (some_condition_is_true) {
        color = GetPixel(hDC, x, y);
        red = GetRValue(color);
        green = GetGValue(color);
        blue = GetBValue(color);
        //...
    }
} 

HDC hDC = GetDC(NULL);

std::thread t1(verde, hDC, 793, 866);
std::thread t2(amarelo, hDC, 803, 796);

t1.join();
t2.join();

ReleaseDC(NULL, hDC);
Comments