GPUImage icon indicating copy to clipboard operation
GPUImage copied to clipboard

Adjusting the opacity of an image blended via GPUImageScreenBlendFilter

Open giilby opened this issue 12 years ago • 3 comments

Thanks for this great framework, Brad!

I'm trying to adjust the opacity of an image that I'm overlaying atop another via GPUImageScreenBlendFilter. The screen blend works fine, but the opacity adjustment to the foreground image is not being applied. Notably, when I use a different blend filter, e.g. GPUImageSourceOverBlendFilter, the opacity is applied correctly. Here's my code:

sourcePicture = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"ws-bg.jpg"] smoothlyScaleOutput:YES];
[sourcePicture processImage];
overlayPicture = [[GPUImagePicture alloc] initWithImage:[UIImage imageNamed:@"ws-HD.jpg"] smoothlyScaleOutput:YES];

opacityFilter = [[GPUImageOpacityFilter alloc] init];
opacityFilter.opacity = 0.4;
[overlayPicture addTarget:opacityFilter];
[overlayPicture processImage];

// this maintains 40% opacity: blendFilter = [[GPUImageSourceOverBlendFilter alloc] init];
blendFilter = [[GPUImageScreenBlendFilter alloc] init];
[sourcePicture addTarget:blendFilter];
[opacityFilter addTarget:blendFilter];

[blendFilter addTarget:imageView];

[overlayPicture processImage];

Am I doing something silly?

giilby avatar Oct 17 '13 23:10 giilby

The math for the screen blend (from the shader in that filter) looks like:

 mediump vec4 whiteColor = vec4(1.0);
 gl_FragColor = whiteColor - ((whiteColor - textureColor2) * (whiteColor - textureColor));

In this, the various color channels are processed independently, so alpha has no effect on the other color components. Should it? If so, you'd need to find documentation for how to use alpha to modify color components in the above and we could adjust the shader to take alpha into account. Some of the other blends and color operations were modified in a similar manner to account for alpha in their math.

BradLarson avatar Oct 18 '13 19:10 BradLarson

Thanks for looking into this. Since both Photoshop and CIScreenBlendMode account for alpha in screen blends, I think GPUImageScreenBlendFilter should as well. I'll dig into it and see if I can update the shader, then submit a pull request.

giilby avatar Oct 18 '13 21:10 giilby

Hello, how do we pass the alpha parameter?

kakashilw avatar Apr 23 '24 15:04 kakashilw