我正在尝试为我的应用程序开发一些图像颜色过滤器。我们使用查找图像,因为它们易于使用以从其他程序复制过滤器,并且方便在各个平台上获得相同的结果。我们的查找图像如下所示:
除了这个顶点着色器之外:
之前,我们使用了GPUImage
来使用查找图像进行过滤,但我想避免这种依赖关系,因为它有惊人的5.4mb大小,而我们只需要这一个功能。
搜索了几个小时后,我似乎找不到任何关于如何使用查找图像通过CoreImage
过滤图像的资源。然而,在查看文档时,CIColorMatrix
看起来是合适的工具。问题在于,我太蠢了,不理解它的工作原理。这就引出了我的问题;
是否有人有使用CIColorMatrix
从查找图像中过滤图像的示例?(或者任何指针,告诉我该如何自己解决)
我已经仔细研究了GPUImage
的代码,看起来他们用于从查找图像中过滤的着色器定义如下:
查找图像着色器:
varying vec2 textureCoordinate;
varying vec2 textureCoordinate2;
uniform sampler2D inputImageTexture;
uniform sampler2D inputImageTexture2;
// lookup texture uniform float intensity;
void main() {
vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
float blueColor = textureColor.b * 63.0;
vec2 quad1;
quad1.y = floor(floor(blueColor) / 8.0);
quad1.x = floor(blueColor) - (quad1.y * 8.0);
vec2 quad2;
quad2.y = floor(ceil(blueColor) / 8.0);
quad2.x = ceil(blueColor) - (quad2.y * 8.0);
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);
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);
vec4 newColor1 = texture2D(inputImageTexture2, texPos1);
vec4 newColor2 = texture2D(inputImageTexture2, texPos2);
vec4 newColor = mix(newColor1, newColor2, fract(blueColor));
gl_FragColor = mix(textureColor, vec4(newColor.rgb, textureColor.w), intensity);
}
除了这个顶点着色器之外:
attribute vec4 position;
attribute vec4 inputTextureCoordinate;
attribute vec4 inputTextureCoordinate2;
varying vec2 textureCoordinate;
varying vec2 textureCoordinate2;
void main() {
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
textureCoordinate2 = inputTextureCoordinate2.xy;
}
我应该使用这些着色器创建自己的滤镜吗?