Last active
June 2, 2023 06:51
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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