IO - Objective-c GPUImage Lookup Filter 使查找图像 4x4 生效

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

我知道有很多解决图像制作效果的问题。但我选择 GPUImage (GPUImageLookupFilter) 制作我的图像。
我使用的源代码。

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;


首先我使用查找图像 8x8 (5122)


但是当我使用阵列图像(视频中的拇指或选择库中的许多图像)时,内存更高。
我想如果我使用小查找图像 (4x4) 内存可以减少。我做了一个查找图像 4x4(162).


我尝试编辑 GPUImageLookupFilter 的代码,但它不起作用。

 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);
}


您可以帮助我编辑此代码以处理 4x4 图像。
谢谢!

以下算法适用于任何大小的查找纹理。您只需将 tiles 调整为 x 和 y 方向上的图块数量,并将 colTexSize 调整为颜色查找纹理的完整大小。
该算法与原始算法相同,但大小不变的值已被变量 tilescolTexSize.

替代。
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);
}