Last active
October 10, 2017 02:38
vImage Blur Example
This file contains 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
- (UIImage *)boxblurImage:(UIImage *)image boxSize:(int)boxSize | |
{ | |
CGImageRef originalImageRef = image.CGImage; | |
CGColorSpaceRef originalColorSpace = CGColorSpaceRetain(CGImageGetColorSpace(originalImageRef)); | |
if (_pixelBuffer == NULL) { | |
_pixelBuffer = malloc(CGImageGetBytesPerRow(originalImageRef) * CGImageGetHeight(originalImageRef)); | |
} | |
vImage_CGImageFormat inputImageFormat = | |
{ | |
.bitsPerComponent = (uint32_t) CGImageGetBitsPerComponent(originalImageRef), | |
.bitsPerPixel = (uint32_t) CGImageGetBitsPerComponent(originalImageRef) * (uint32_t)(CGColorSpaceGetNumberOfComponents(originalColorSpace) + (kCGImageAlphaNone != CGImageGetAlphaInfo(originalImageRef))), | |
.colorSpace = originalColorSpace, | |
.bitmapInfo = CGImageGetBitmapInfo(originalImageRef), | |
.version = 0, | |
.decode = NULL, | |
.renderingIntent = kCGRenderingIntentDefault | |
}; | |
vImage_Buffer inputImageBuffer; | |
vImageBuffer_InitWithCGImage(&inputImageBuffer, &inputImageFormat, NULL, originalImageRef, kvImageNoFlags); | |
vImage_Buffer outputImageBuffer = { | |
.data = _pixelBuffer, | |
.width = CGImageGetWidth(originalImageRef), | |
.height = CGImageGetHeight(originalImageRef), | |
.rowBytes = CGImageGetBytesPerRow(originalImageRef) | |
}; | |
vImage_Error error; | |
error = vImageBoxConvolve_ARGB8888(&inputImageBuffer, | |
&outputImageBuffer, | |
NULL, | |
0, | |
0, | |
boxSize, | |
boxSize, | |
NULL, | |
kvImageEdgeExtend); | |
if (error) { | |
NSLog(@"vImage error %zd", error); | |
} | |
vImage_CGImageFormat outFormat = | |
{ | |
.bitsPerComponent = (uint32_t) CGImageGetBitsPerComponent(originalImageRef), | |
.bitsPerPixel = (uint32_t) CGImageGetBitsPerComponent(originalImageRef) * (uint32_t)(CGColorSpaceGetNumberOfComponents(originalColorSpace) + (kCGImageAlphaNone != CGImageGetAlphaInfo(originalImageRef))), | |
.colorSpace = originalColorSpace, | |
.bitmapInfo = CGImageGetBitmapInfo(originalImageRef), | |
.version = 0, | |
.decode = NULL, | |
.renderingIntent = kCGRenderingIntentDefault | |
}; | |
CGImageRef modifiedImageRef = vImageCreateCGImageFromBuffer(&outputImageBuffer, | |
&outFormat, | |
NULL, | |
NULL, | |
kvImageNoFlags, | |
&error); | |
CGColorSpaceRelease(originalColorSpace); | |
UIImage *returnImage = [UIImage imageWithCGImage:modifiedImageRef]; | |
CGImageRelease(modifiedImageRef); | |
free(inputImageBuffer.data); | |
return returnImage; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment