Finding correct pixels

Hello all.
I am loading a rectangle.png file.
The bg color is black and my rect’s are khaki and dodBlue.

void findPixels()
{
/*  Example code comes from
    https://stackoverflow.com/questions/19107476/get-pixel-info-from-sdl2-texture
    user: user2437378
*/

    SDL_QueryTexture(t, NULL, 0, &w, &h);
    SDL_LockTexture(t, NULL, &pixels, &pitch);

    Uint8 *upixels = (Uint8*) pixels;

 // you will need to know the color of the pixel even if it's transparent
    Uint8 Khaki   = SDL_MapRGBA(surface->format, 94, 90, 54,  255);
    Uint8 dodBlue = SDL_MapRGBA(surface->format, 11, 56, 100, 255);

 // Search pixels
    for (int i = 0; i < w; i++) {
        for(int j = 0; j < h; j++){
            if (upixels[j] == Khaki){
                printf("khaki pixel found\n");
            }
            else if(upixels[j] == dodBlue){
		        printf("dodBlue pixel found\n");
            }

             /* Eventually math will be performed
                to find the (x, y, w, h) of a rectangle.
                This will be stored in an vector
             */
            }
        }
    }

    SDL_UnlockTexture(t);
}

int main(int argc, char* args[])
{
    if(createWindow()){
       findPixels();
    }

    CloseWindow();
    return 0;
}

As it stands, I don’t get any printouts on the console window.
There’s no error also. It loads the graphic and then close.
That’s correct since I’m not not doing a while loop.

Did I convert the code wrong?

As the documentation for SDL_LockTexture() states (and the replies to the Stack Overflow page you linked to), it’s best to think of it as a write-only function. For optimization reasons, the pointer it gives you may not contain the texture’s previous contents, which is probably what’s happening in your case.

SDL_LockTexture() is meant for completely replacing a texture’s contents.

If you need to preserve the texture’s contents, then you’ll need to do that separately on your own. In this case, just check the pixel data before creating a texture with it.

Thanks for the response.

I did see where someone complained about
doing it that way and his response was the
reason I tried the code in the first place.

I don’t want to change the image, just see
if the colors match.

I have been looking at other ways to get it
done; to which, cImg looks like it may work.

Why are you throwing on Uint8 ?

Just load the image into memory with something like stb_image and go through the pixels? There’s no need to create an SDL texture.

edit: also, yeah, your colors need to be uint32, since that’s what SDL_MapRGBA() returns

I read on some site, I think it may have been stackoverflow, that you should cast
images to Uint8. Especially if you’re dealing with 8bit color scheme. It saves
time and such. I looked at stb_image today before I read this post. I was youtubing
and came across this header.

Thanks for your help. I’m new to the forum, so do I need to mark this as closed?
If so, how?

I’m talking about this

Uint8 Khaki = SDL_MapRGBA(surface->format, 94, 90, 54, 255);
Uint8 dodBlue = SDL_MapRGBA(surface->format, 11, 56, 100, 255);

SDL_MapRGBA returns a Uint32. So unless surface->format is an 8-bit paletted/indexed one (in which case you should explicitly cast the result to Uint8 to be clearer and make the compiler be quiet), you’re dealing with 32-bit color.

It’s perfectly fine to cast 32-bit color images to uint32_t or SDL’s equivalent if you’re just dealing with the whole pixel and don’t need to mess with individual color channels. Like in your case, where you’re just checking if the whole pixel matches some predetermined color, you can just treat it as uint32_t and save yourself having to compare each channel individually.

1 Like

Been a busy few days.

SDL’s equivalent if you’re just dealing with the whole pixel and don’t need to mess with individual color channels. Like in your case, where you’re just checking if the whole pixel matches some predetermined color, you can just treat it as uint32_t and save yourself having to compare each channel individually.

Yes, i am looking at the whole pixel to see if the color matches. I’m still looking into options for achieving this.
Meanwhile, I’m coding other aspects of the project.

It’s been a while. I found a solution to my original question but that just created a new one.

As long as the image was static there wasn’t any problems. However, when I employed the screenshot function. The image became skewed.

imagemagik scripts helped with that but brought the program to a complete crawl.
I think it’s due to the write delays.

So I’m trying to do it in a new way just reading the image in memory.
It’s not showing up though. It’s gotta be because of SDL_CreateRGBSurfaceFrom().

I couldn’t post the links to the GDI section I found via the net.
I’m not claiming it as my own. I’m just trying to utilize it
.

Oh, If I bypass the dynamic screenshot section and just load a static
image of the screenshot, everything works.

What am I missing?
This the last part and I’ll be done with the GUI section.

Here’s some of the solutions I went with.
For those who search and want to know how you can
load image data into texture:

SDL_LockTexture(Shapes, nullptr, (void**)&LT_Pixels, &pitch);

for(int i = 0; i<texImgSize; i++){
LT_Pixels[i] = stbPixels[i];
}

SDL_UnlockTexture(Shapes);

To find the colored pixels I did used a Uint8*. When I did my loops
I added the red, blue, green, alpha together in a int variable.
If they matched my numbers for khaki or dodBlue then I did some
things.

int Khaki = 425;
int dodBlue = 630;
RGBA = 4;
int index = RGBA * (y * wth + x);

int Cnt = static_cast(image[index + 0]) +
static_cast(image[index + 1]) +
static_cast(image[index + 2]);

if(Cnt == Khaki || Cnt == dodBlue){
// do stuff
}

It took some time for me to get all of this right due to other parts of the coding
I needed to complete. Then relearning what I needed to do in the first place.

Thanks for the suggestions and hopefully it can help another googler later.