ios - 具有不透明度的 GPUImageLightenBlendFilter

标签 ios image-processing opengl-es gpuimage core-image

使用 GPUImage 我正在尝试复制具有不透明度的 Photoshop Lighten 混合模式。不幸的是,使用 GPUImageLightenBlendFilter 时,alpha channel 没有任何效果。

Photoshop Lighten Blend Mode with opacity

Brad 确认 alpha 可能存在问题: GPUImage's GPUImageOpacityFilter not behaving as expected, doesn't change alpha channel

我已经使用尊重 alpha 值的 CoreImage 成功复制了 PS 混合。

CIImage *ciImage1 = [[CIImage alloc] initWithImage:input1];
CIImage *ciImage2 = [[CIImage alloc] initWithImage:input2];

// Alpha adjustment for input1
CIFilter *alphaFilter = [CIFilter filterWithName:@"CIColorMatrix"];
CGFloat rgba[4] = { 0.0, 0.0, 0.0, 0.5 };
CIVector *alphaVector = [CIVector vectorWithValues:rgba count:4];
[alphaFilter setValue:alphaVector forKey:@"inputAVector"];
[alphaFilter setValue:ciImage1 forKey:kCIInputImageKey];

// Lighten blend
CIFilter *blendFilter = [CIFilter filterWithName:@"CILightenBlendMode"];
[blendFilter setValue:alphaFilter.outputImage forKey:kCIInputImageKey];
[blendFilter setValue:ciImage2 forKey:kCIInputBackgroundImageKey];

我已经尝试了 2 个版本的 GPUImage(他们正在使用不同的方法调整 input1 的 alpha)。

GPUImagePicture *input1 = [[GPUImagePicture alloc] initWithImage:input1];
GPUImagePicture *input2 = [[GPUImagePicture alloc] initWithImage:input2];

// Alpha adjustment for input1
GPUImageOpacityFilter *alphaFilter = [GPUImageOpacityFilter new];
alphaFilter.opacity = 0.5;
[input1 addTarget:alphaFilter];

// Lighten blend
GPUImageLightenBlendFilter *blendFilter = [GPUImageLightenBlendFilter new];
[alphaFilter addTarget:blendFilter];
[input2 addTarget:blendFilter];

或:

GPUImagePicture *input1 = [[GPUImagePicture alloc] initWithImage:input1];
GPUImagePicture *input2 = [[GPUImagePicture alloc] initWithImage:input2];

// Alpha adjustment for input1
GPUImageColorMatrixFilter *alphaFilter = [GPUImageColorMatrixFilter new];
alphaFilter.colorMatrix = (GPUMatrix4x4) {
  { 1.0, 0.0, 0.0, 0.0 },
  { 0.0, 1.0, 0.0, 0.0 },
  { 0.0, 0.0, 1.0, 0.0 },
  { 0.0, 0.0, 0.0, 0.5 }
};
[input1 addTarget:alphaFilter];

// Lighten blend
GPUImageLightenBlendFilter *blendFilter = [GPUImageLightenBlendFilter new];
[alphaFilter addTarget:blendFilter];
[input2 addTarget:blendFilter];

两个 GPUImage 实现都返回输出,就好像 input1 的 alpha 是 1.0

我查看了互联网上不同来源的 Lighten Blending Mode 文档,它们都使用了这个公式:

max(blend, base)

查看 GPUImageLightenBlendFilter 实现中的着色器,它也使用相同的公式:

lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
lowp vec4 textureColor2 = texture2D(inputImageTexture2, textureCoordinate2);
gl_FragColor = max(textureColor, textureColor2);

但是,似乎 Photoshop 和 CoreImage 对 alpha 有一些额外的操作(可能类似于 Gimp:https://github.com/GNOME/gimp/blob/783bbab8a889d4eba80b6a83f2e529937a73a471/app/operations/gimpoperationlightenonlymode.c)。

有人知道如何在 GPUImageLightenBlendFilter 公式中包含 alpha channel 吗?

最佳答案

下面是带有不透明度和数量的 GPUImageLightenBlendFilter 的着色器代码。

precision highp float;
uniform sampler2D inputImageTexture;
uniform sampler2D inputImageTexture2;
uniform float alpha;   // used for opacity....
uniform float amount;  // amount of blend....
varying vec2 textureCoordinate;

void main ()
{
    // Get samples from both layers
    vec4 dst = texture2D(inputImageTexture, textureCoordinate);
    vec4 src = texture2D(inputImageTexture2, textureCoordinate);

src.a *= alpha;
vec4 colour = vec4(0.0, 0.0, 0.0, 0.0);

colour = vec4(max(dst, src).rgb, src.a) * src.a + dst * (1.0 - src.a);
colour = clamp(colour, 0.0, 1.0);
gl_FragColor.xyz = mix(dst, colour, amount).rgb;
gl_FragColor.w = 1.0;
}

非常适合我....

关于ios - 具有不透明度的 GPUImageLightenBlendFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29729802/

相关文章:

ios - 导航栏返回按钮方法

java - 将图像转换为 byteArray 并保持颜色

python - 如何从 Python cv2、scikit 图像和 mahotas 中的 Internet URL 读取图像?

ios - 防止iOS泄漏mmap上的内存(也是:mach_vm_map)

ios - 自定义 UICollectionViewFlowLayout 边框

ios - 快速显示和隐藏 CALayer 的速度似乎很慢

iPhone:IBAction 导致 “Unrecognized Selector Sent to Instance” 错误

haskell - 如何将图像加载到列表中?

ios - 如何在 scenekit 的 SCNNodeRendererDelegate 中使用 openGL 进行绘制

android - 使用 OpenGL 的 2D 示例