CISourceOverComposition unexpected behavior

When a filter is applied, source over compositing resulting in (unexpected behavior) filter bleeding into the background (the color of the background image is changing to gray). I had to use CIBlendWithAlphaMask setting the foreground image as the mask to work around this issue.

CIFilter *tonalFilter = [CIFilter filterWithName:@"CIPhotoEffectTonal"];
        [tonalFilter setValue:filterImage forKey:kCIInputImageKey];
        video1FilteredImage = [tonalFilter valueForKey:@"outputImage"];


CIFilter *colorGenerator = [CIFilter filterWithName:@"CIConstantColorGenerator"];
                CIColor *inputColor = [[CIColor alloc] initWithColor:currentInstruction.compositionBackgroundColor];
                [colorGenerator setValue:inputColor forKey:@"inputColor"];
                CIImage *colorBackgroundImage = [colorGenerator valueForKey:@"outputImage"];


CIFilter *sourceOverCompositing = [CIFilter filterWithName:@"CISourceOverCompositing"];
                [sourceOverCompositing setValue:video1FilteredImage forKey:kCIInputImageKey];
                [sourceOverCompositing setValue:colorBackgroundImage forKey:kCIInputBackgroundImageKey];
                video1FilteredImage = [sourceOverCompositing valueForKey:@"outputImage"];


I guess this is happening because CIPhotoEffectTonal also has an effect on the transparent parts of the image.

If you want to limit the effect to the visible part, you can simply crop the image after the effect is applied and before you blend it over the background:

video1FilteredImage = [video1FilteredImage imageByCroppingToRect:video1FilteredImage.extent];

By the way: instead of using CIConstantColorGenerator you can simply get a colored image like this:

CIImage *colorBackgroundImage = [CIImage imageWithColor:inputColor];
CISourceOverComposition unexpected behavior
 
 
Q