I need to apply a Gaussian Blur to a NSImage
.
Since this image changes a lot over the lifetime of the application, the operation should be quick so that the UI stays somewhat responsive.
Unfortunately, I have not found a satisfying solution for this problem.
So basically, I've tried three things:
- Core Animation (Not an option)
- CIFilter (Terribly slow, 1s or more per operation)
- GPUImage (Also terribly slow, 1s or more per operation)
What would be the fastest way of applying a Gaussian Blur (or any filter, for that matter) to a NSImage
?
CIFilter
CIImage *imageToBlur = [CIImage imageWithData:[backgroundImage TIFFRepresentation]];
CIFilter *gaussianBlurFilter = [CIFilter filterWithName: @"CIGaussianBlur"];
[gaussianBlurFilter setValue:imageToBlur forKey:kCIInputImageKey];
[gaussianBlurFilter setValue:[NSNumber numberWithFloat: 10] forKey: @"inputRadius"];
NSCIImageRep *rep = [NSCIImageRep imageRepWithCIImage:[gaussianBlurFilter valueForKey:kCIOutputImageKey]];
NSImage *nsImage = [[NSImage alloc] initWithSize:rep.size];
[nsImage addRepresentation:rep];
_backgroundImage = nsImage;
GPUImage
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:backgroundImage];
GPUImageGaussianBlurFilter *gaussianBlurFilter = [[GPUImageGaussianBlurFilter alloc] init];
gaussianBlurFilter.blurRadiusInPixels = 10.0;
[stillImageSource addTarget:gaussianBlurFilter];
[stillImageSource processImage];
_backgroundImage = [gaussianBlurFilter imageFromCurrentlyProcessedOutput];