2015-04-19 69 views
0

使用GPUImage我正嘗試複製具有不透明度的Photoshop Lighten混合模式。不幸的是,alpha通道使用GPUImageLightenBlendFilter沒有效果。帶不透明度的GPUImageLightenBlendFilter

Photoshop Lighten Blend Mode with opacity

布拉德證實有可能與阿爾法問題: GPUImage's GPUImageOpacityFilter not behaving as expected, doesn't change alpha channel

我已經成功地複製使用CoreImage,尊重Alpha值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)。

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實現返回輸出彷彿阿爾法爲input11.0

我已經在不同的來源地看着變亮混合模式文檔在互聯網上,他們都使用這個公式:

max(blend, base) 

綜觀實施GPUImageLightenBlendFilter着色器也使用了相同的公式:

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

但是,似乎Photoshop和CoreImage有一些額外的操作與阿爾法(可能類似於Gimp:https://github.com/GNOME/gimp/blob/783bbab8a889d4eba80b6a83f2e529937a73a471/app/operations/gimpoperationlightenonlymode.c)。

任何人都有想法如何在GPUImageLightenBlendFilter公式中包含alpha通道?

回答

0

我發現這個shader代碼最適合我的情況:

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

0

以下是帶有不透明度和數量的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; 
} 

完全適用於我....

+0

感謝您的着色器代碼。 – pirags