I am currently loading an image using SDL_Image (SDL2) and put that in a texture. Significative lines of C++ my code, using "source" as a temp SDL_Surface for other reasons, are:
SDL_Surface* source;
SDL_Surface* target;
//Use SDL_CreateRGBSurface to initialize target
SDL_Rect offset;
//valorize the offset
source = IMG_Load(pathOfTheImage);
if (source != nullptr)
{
SDL_BlitScaled(source, &offset, target, NULL);
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, target->w, target->h, GL_RGBA, GL_UNSIGNED_BYTE, target->pixels);
SDL_FreeSurface(source);
So far, so good. I would like to load the image and, based on a variable, invert colors. By invert colors, I mean to change black in white, white in black... Something like
red = 255 -red;
green = 255 - green;
blue = 255 - blue;
Note: My purpose is not invert colors such as red channel values becomes blue channel, switching GL_RBGA to GL_BGRA.
I found this solutions on the internet:
- Change every pixel with loops like this answer
- Do the same thing applying a shader.
There is a way to accomplish this by adding just another SDL call (nor the for loop over all the pixels, neither the shader)? The loop seems that would slower the program a lot.
I am dreaming something like SDL_SwapColors(source) or any other elegant solutions :) Thank you in advance for any tip.