Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS openGL image invert

I'm trying to get create an inverse of an image, as part of an app. I have implemented some image processing using openGL, following the GLImageProcessing demo from Apple.

My image is loaded from a .jpg, and loaded to a texture. I can happily modify contrast and brightness.

However, I am having unsatisfactory results when trying to create an inverse image (swap black for white). I've tried a few approaches, but nothing works well on the device. so far, I have tried; glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); this works well in the simulator, but on the device produces unstable results. I have also tried; glBlendFunc(GL_ONE_MINUS_SRC_COLOR, GL_ONE_MINUS_DST_COLOR); this creates results as before. Finally, I have tried; glLogicOp(GL_XOR); this simply toggles between the image, and a black screen.

Can you suggest a (relatively) simple solution to invert my image?

like image 936
Deaglan O Meachair Avatar asked Dec 19 '25 13:12

Deaglan O Meachair


1 Answers

How about this shader:

void main() {
    vec4 color = texture2D(texture, textureCoordinate);
    float inverted = 1.0 - color.r; 
    vec4 inverted_vec = vec4( vec3(inverted), 1.0);
    gl_FragColor = clamp(inverted_vec, 0.0, 1.0);"
};
like image 81
Or Arbel Avatar answered Dec 22 '25 17:12

Or Arbel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!