Skip to content

Instantly share code, notes, and snippets.

@jmenter
Last active June 2, 2023 06:51
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jmenter/6568138 to your computer and use it in GitHub Desktop.
Save jmenter/6568138 to your computer and use it in GitHub Desktop.
C Function to create a blurred CGimageRef (supply an image and a blur radius). Uses vImage Accelerate Framework for Mac OS X/iOS 5+ and is VERY fast.
// Assumes ARGB8888
CGImageRef CGImageCreateBlurredImage(CGImageRef inImage, NSUInteger blurRadius)
{
if (!inImage) { return NULL; }
uint32_t radius = (blurRadius % 2) ? (uint32_t)blurRadius : (uint32_t)++blurRadius;
vImage_Error error;
vImage_CGImageFormat imageFormat = {(uint32_t)CGImageGetBitsPerComponent(inImage), (uint32_t)CGImageGetBitsPerPixel(inImage), CGImageGetColorSpace(inImage), CGImageGetBitmapInfo(inImage), 0, NULL, kCGRenderingIntentDefault};
vImage_Buffer source;
error = vImageBuffer_InitWithCGImage(&source, &imageFormat, NULL, inImage, kvImageNoFlags);
vImage_Buffer destination;
error = vImageBuffer_Init(&destination, CGImageGetHeight(inImage), CGImageGetWidth(inImage), (uint32_t)CGImageGetBitsPerPixel(inImage), kvImageNoFlags);
error = vImageTentConvolve_ARGB8888(&source, &destination, NULL, 0, 0, radius, radius, NULL, kvImageEdgeExtend);
if (error) {
free(source.data);
free(destination.data);
return NULL;
}
CGImageRef outImage = vImageCreateCGImageFromBuffer(&destination, &imageFormat, NULL, NULL, kvImageNoFlags, &error);
free(source.data);
free(destination.data);
return outImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment