IO - Objective-c GPUImage查找过滤器使用查找图像4x4生效

时间:2017-09-11 03:16:36

标签: objective-c macos io opengl-es gpuimage

我知道有很多解决方案对图像有效。但我选择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(512 2

enter image description here
但是,当我使用数组图像(拇指在视频或选择图像中的许多图像)内存更高。
我想如果我使用小的查找图像(4x4)内存可以减少。我查找图像4x4(16 2 )。

enter image description here
我尝试编辑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。
谢谢!。

1 个答案:

答案 0 :(得分:1)

以下算法适用于任何大小的查找纹理。您只需要将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);
}