Saturday, 12 June 2021

How to create a simple fragment shader that maps color from a lookup table?

I am trying to create image filters like Instagram for my Android application. I am new to image processing and have just stumbled upon this term called color mapping. After many research, I tried to create my own filter using OpenGL using a lookup table (LUT). But upon adding the filter to a camera, Here is the result:

enter image description here

You can see that there is this weird blueish color at the edge of my thumb. It only happens on overexposed areas of the image.

Here is the fragment shader code:

#extension GL_OES_EGL_image_external : require
precision lowp float;
             
varying highp vec2 vTextureCoord;
             
uniform samplerExternalOES inputImage;
uniform sampler2D lookup;
             
void main() {
     vec2 tiles    = vec2(8.0);
     vec2 tileSize = vec2(64.0);
     vec4 texel = texture2D(inputImage, vTextureCoord);
     float index = texel.b * (tiles.x * tiles.y - 1.0);
     float index_min = min(62.0, floor(index));
     float index_max = index_min + 1.0;

     vec2 tileIndex_min;
     tileIndex_min.y = floor(index_min / tiles.x);
     tileIndex_min.x = floor(index_min - tileIndex_min.y * tiles.x);
     vec2 tileIndex_max;
     tileIndex_max.y = floor(index_max / tiles.x);
     tileIndex_max.x = floor(index_max - tileIndex_max.y * tiles.x);

     vec2 tileUV = mix(0.5/tileSize, (tileSize-0.5)/tileSize, texel.rg);
            
     vec2 tableUV_1 = tileIndex_min / tiles + tileUV / tiles;
     vec2 tableUV_2 = tileIndex_max / tiles + tileUV / tiles;
            
     vec3 lookUpColor_1 = texture2D(lookup, tableUV_1).rgb;
     vec3 lookUpColor_2 = texture2D(lookup, tableUV_2).rgb;
     vec3 lookUpColor   = mix(lookUpColor_1, lookUpColor_2, index-index_min);
     gl_FragColor = vec4(lookUpColor, 1.0);
}

Here is the lookup table. This is a base lookup table. I tried editing the lookup tables and applying the filter but the result is same, irrespective of the table.

What is causing this issue? Can anyone show me how to create a simple fragment shader that maps color from lookup table to the current texture? Any help would be appreciated. Regards.



from How to create a simple fragment shader that maps color from a lookup table?

No comments:

Post a Comment