Search code examples
objective-cmacosioopengl-esgpuimage

IO - Objective-c GPUImage Lookup Filter make effect with lookup image 4x4


I know have a many solve make effect for image. But i choice GPUImage (GPUImageLookupFilter) make my image.
My source code i use.

GPUImagePicture *sourceImagePic = [[GPUImagePicture alloc] initWithImage:sourceImage];
GPUImagePicture *lookupImageSource = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"lookup.png"]];
GPUImageLookupFilter *lookupImageFilter = [[GPUImageLookupFilter alloc] init];

[sourceImagePic addTarget:lookupImageFilter];
[lookupImageSource addTarget:lookupImageFilter];
[lookupImageFilter useNextFrameForImageCapture];

[sourceImagePic processImage];
[lookupImageSource processImage];
resultImage = [lookupImageFilter imageFromCurrentFramebufferWithOrientation:UIImageOrientationUp];

return resultImage;


First i use lookup image 8x8 (5122)

enter image description here
But when i working with array image (thumb in video or choice many image in library) memory higher.
I think if i use small lookup image (4x4) memory can reduced. I make a lookup image 4x4(162).

enter image description here
And i try edit code of GPUImageLookupFilter but it not work.

 void main(){
 highp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);

 highp float blueColor = textureColor.b * 63.0;

 highp vec2 quad1;
 quad1.y = floor(floor(blueColor) / 8.0);
 quad1.x = floor(blueColor) - (quad1.y * 8.0);

 highp vec2 quad2;
 quad2.y = floor(ceil(blueColor) / 8.0);
 quad2.x = ceil(blueColor) - (quad2.y * 8.0);

 highp vec2 texPos1;
 texPos1.x = (quad1.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
 texPos1.y = (quad1.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);

 highp vec2 texPos2;
 texPos2.x = (quad2.x * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.r);
 texPos2.y = (quad2.y * 0.125) + 0.5/512.0 + ((0.125 - 1.0/512.0) * textureColor.g);

 lowp vec4 newColor1 = texture2D(inputImageTexture2, texPos1);
 lowp vec4 newColor2 = texture2D(inputImageTexture2, texPos2);

 lowp vec4 newColor = mix(newColor1, newColor2, fract(blueColor));
 gl_FragColor = mix(textureColor, vec4(newColor.rgb, textureColor.w), intensity);
}


You can help me edit this code work with image 4x4.
Thank you!.


Solution

  • The following algorithm will work for any size of the lookup texture. You just have to adapt tiles to the number of the tiles in the x and y direction, and colTexSize to the full size of the color lookup texture.
    The algorithm is identical to the original algorithm, except the constant size values, which have been replaced by the variables tiles and colTexSize.

    void main()
    {
        vec2 tiles      = vec2( 4.0, 4.0 );   // original texture vec2( 8.0, 8.0 )
        vec2 colTexSize = vec2( 64.0, 64.0 )  // original texture vec2( 512.0, 512.0 )
    
        highp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
    
        highp float blueColor = textureColor.b * ((tiles.x*tiles.y)-1.0);
    
        highp vec2 quad1;
        quad1.y = floor(floor(blueColor) / tiles.y);
        quad1.x = floor(blueColor) - (quad1.y * tiles.x);
    
        highp vec2 quad2;
        quad2.y = floor(ceil(blueColor) / tiles.y);
        quad2.x = ceil(blueColor) - (quad2.y * tiles.x);
    
        highp vec2 texPos1;
        texPos1.x = (quad1.x / tiles.x) + 0.5/colTexSize.x + (1.0/(tiles.x - colTexSize.x) * textureColor.r);
        texPos1.y = (quad1.y / tiles.y) + 0.5/colTexSize.y + (1.0/(tiles.y - colTexSize.y) * textureColor.g);
    
        highp vec2 texPos2;
        texPos2.x = (quad2.x / tiles.x) + 0.5/colTexSize.x + (1.0/(tiles.x - colTexSize.x) * textureColor.r);
        texPos2.y = (quad2.y / tiles.y) + 0.5/colTexSize.y + (1.0/(tiles.y - colTexSize.y) * textureColor.g);
    
        lowp vec4 newColor1 = texture2D(inputImageTexture2, texPos1);
        lowp vec4 newColor2 = texture2D(inputImageTexture2, texPos2);
    
        lowp vec4 newColor = mix(newColor1, newColor2, fract(blueColor));
        gl_FragColor = mix(textureColor, vec4(newColor.rgb, textureColor.w), intensity);
    }