GPUImageLightenBlendFilter具有不透明度

时间:2015-04-19 12:23:37

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

使用GPUImage我试图用不透明度复制Photoshop Lighten Blend Mode。不幸的是,使用GPUImageLightenBlendFilter alpha通道无效。

Photoshop Lighten Blend Mode with opacity

布拉德确认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];

我尝试了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实现都返回输出,就像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通道?

1 个答案:

答案 0 :(得分:0)

我发现这个Shader代码最适合我的情况:

to_i

在这里找到: https://github.com/BradLarson/GPUImage/pull/1297