Skip to content

Instantly share code, notes, and snippets.

@palmerc
Last active Oct 10, 2017
Embed
What would you like to do?
vImage Blur Example
- (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