How do I use the Accelerate Framework With Core Graphics?

1.6k views Asked by At

I have a project. It basically takes photo from iPhone camera and applies some effects on photo. Before I apply effect I use core graphics to scale image to appropriate size. After scaling and rotating image I use Accelerate framework(vImage) in order to create effect. My problem is after applying effect it is ended up some bluish image. However If I don't scale image with core graphics this bluish looks doesn't happen.

Scaling code that I use is from this post.

And here is my code that applies effect:

- (UIImage *)applyFiltertoImage:(UIImage *)img
{
    CGImageRef image = img.CGImage;
    vImage_Buffer inBuffer, outBuffer;
    void *pixelBuffer;

    CGDataProviderRef inProvider = CGImageGetDataProvider(image);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

    inBuffer.width = CGImageGetWidth(image);
    inBuffer.height = CGImageGetHeight(image);
    inBuffer.rowBytes = CGImageGetBytesPerRow(image);

    inBuffer.data = (void *)CFDataGetBytePtr(inBitmapData);

    pixelBuffer = malloc(CGImageGetBytesPerRow(image) * CGImageGetHeight(image));

    if (pixelBuffer == NULL) {
        NSLog(@"No buffer");
    }

    outBuffer.data = pixelBuffer;
    outBuffer.width = CGImageGetWidth(image);
    outBuffer.height = CGImageGetHeight(image);
    outBuffer.rowBytes = CGImageGetBytesPerRow(image);

    vImageConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, self.kernel, self.size, self.size, self.divisor, NULL, kvImageEdgeExtend);

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
                                             outBuffer.width,
                                             outBuffer.height,
                                             8,
                                             outBuffer.rowBytes,
                                             colorSpace,
                                             kCGImageAlphaNoneSkipLast);

    CGImageRef imageRef = CGBitmapContextCreateImage(ctx);

    UIImage *blurredImage = [UIImage imageWithCGImage:imageRef];

    CGContextRelease(ctx);
    CGColorSpaceRelease(colorSpace);
    free(pixelBuffer);
    CFRelease(inBitmapData);
    CGImageRelease(imageRef);

    return blurredImage;
}
2

There are 2 answers

0
Ian Ollmann On

Usually, a strong color tint means that the color channel order was lost in translation somewhere along the way, for example you created a CG image with BGRA data, but it was actually ARGB.

Have you looked at vImage_Utilities.h?

0
Cameron Lowell Palmer On

Avoiding manual re-definition of CGContext

Try letting vImage initialize the values for you. vImageBuffer_InitWithCGImage can help you avoid some pain.

The straight forward version

- (UIImage *)applyFiltertoImage:(UIImage *)image
    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 = vImageConvolve_ARGB8888(&inputImageBuffer,
                                    &outputImageBuffer,
                                    NULL,
                                    0,
                                    0,
                                    self.kernel,
                                    self.size,
                                    self.divisor,
                                    NULL,
                                    kvImageEdgeExtend);
    if (error) {
        NSLog(@"vImage error %zd", error);
    }
    free(inputImageBuffer.data);

    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);

    return returnImage;
}

Higher-performance edition

Create the _inputImageBuffer, _outputImageBuffer, and _outputImageFormat one-time per image and then just reapply the filter to the source image. Once vImage warms up it will start shaving off several milliseconds from the call.

- (UIImage *)applyFilter
    vImage_Error error;
    error = vImageConvolve_ARGB8888(&_inputImageBuffer,
                                    &_outputImageBuffer,
                                    NULL,
                                    0,
                                    0,
                                    self.kernel,
                                    self.size,
                                    self.divisor,
                                    NULL,
                                    kvImageEdgeExtend);
    if (error) {
        NSLog(@"vImage error %zd", error);
    }

    CGImageRef modifiedImageRef = vImageCreateCGImageFromBuffer(&_outputImageBuffer,
                          &_outputImageFormat,
                          NULL,
                          NULL,
                          kvImageNoFlags,
                          &error);    
    UIImage *returnImage = [UIImage imageWithCGImage:modifiedImageRef];
    CGImageRelease(modifiedImageRef);

    return returnImage;
}