Correct crop of CIGaussianBlur

后端 未结 9 1616
北海茫月
北海茫月 2020-11-29 22:09

As I noticed when CIGaussianBlur is applied to image, image\'s corners gets blurred so that it looks like being smaller than original. So I figured out that I need to crop i

9条回答
  •  我在风中等你
    2020-11-29 22:21

    There are two issues. The first is that the blur filter samples pixels outside the edges of the input image. These pixels are transparent. That's where the transparent pixels come from. The trick is to extend the edges before you apply the blur filter. This can be done by a clamp filter e.g. like this:

    CIFilter *affineClampFilter = [CIFilter filterWithName:@"CIAffineClamp"];
    
    CGAffineTransform xform = CGAffineTransformMakeScale(1.0, 1.0);
    [affineClampFilter setValue:[NSValue valueWithBytes:&xform
                                               objCType:@encode(CGAffineTransform)]
                         forKey:@"inputTransform"];
    

    This filter extends the edges infinitely and eliminates the transparency. The next step would be to apply the blur filter.

    The second issue is a bit weird. Some renderers produce a bigger output image for the blur filter and you must adapt the origin of the resulting CIImage by some offset e.g. like this:

    CGImageRef cgImage = [context createCGImage:outputImage
                                       fromRect:CGRectOffset([inputImage extend],
                                                             offset, offset)];
    

    The software renderer on my iPhone needs three times the blur radius as offset. The hardware renderer on the same iPhone does not need any offset at all. Maybee you could deduce the offset from the size difference of input and output images, but I did not try...

提交回复
热议问题