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.