具有不透明度的 GPUImageLightenBlendFilter

GPUImageLightenBlendFilter with opacity

使用 GPUImage 我正在尝试复制具有不透明度的 Photoshop Lighten 混合模式。不幸的是,alpha 通道对使用 GPUImageLightenBlendFilter.

无效

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

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

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];

我试过 GPUImage 的 2 个版本(他们正在使用不同的方法调整 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 实现 return 的输出就好像 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 通道吗?

下面是 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;
}

非常适合我....

我发现这个着色器代码最适合我的情况:

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

textureColor.rgb *= textureColor.a;
textureColor2.rgb *= textureColor2.a;
lowp vec4 textureOut = max(textureColor, textureColor2);
textureOut.rgb /= textureOut.a;

gl_FragColor = textureOut;

在此处找到: https://github.com/BradLarson/GPUImage/pull/1297