iosobjective-cuiimagecore-graphicsvimage

vImage_Buffer not rendering into CGContext, Why?


The code below is an attempt to get an image, extract its components into a vImage_Buffer and then loop through this buffer and grab each component and invert the red and blue channels by populating an outputBuffer. After that I want to take the resulting outputBuffer and create a UIImage out of it.

Even though the following code compiles and runs fine, the outputImage is blank and I'm not getting any errors.

CGFloat inputImageScale = inputImage.scale;

CGSize outputImageSizeInPoints = inputImage.size;
CGRect outputImageRectInPoints = { CGPointZero, outputImageSizeInPoints };

UIGraphicsBeginImageContextWithOptions(outputImageRectInPoints.size, NO, inputImageScale);
CGContextRef outputContext = UIGraphicsGetCurrentContext();
CGContextScaleCTM(outputContext, 1.0, -1.0);
CGContextTranslateCTM(outputContext, 0, -outputImageRectInPoints.size.height);

vImage_Buffer originalBuffer;

vImage_Buffer *inputBuffer;
vImage_Buffer *outputBuffer;
vImage_CGImageFormat format = {
    .bitsPerComponent = 8,
    .bitsPerPixel = 32,
    .colorSpace = NULL,
    // (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little)
    // requests a BGRA buffer.
    .bitmapInfo = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little,
    .version = 0,
    .decode = NULL,
    .renderingIntent = kCGRenderingIntentDefault
};

vImage_Error e = vImageBuffer_InitWithCGImage(&originalBuffer, &format, NULL, inputImage.CGImage, kvImagePrintDiagnosticsToConsole);
if (e != kvImageNoError)
{
    NSLog(@"*** error: vImageBuffer_InitWithCGImage returned error code %zi for inputImage: %@", e, inputImage);
    UIGraphicsEndImageContext();
    return nil;
}

inputBuffer = &originalBuffer;
vImageBuffer_Init(outputBuffer, originalBuffer.height, originalBuffer.width, format.bitsPerPixel, kvImageNoFlags);

Byte *data = (Byte*)inputBuffer->data;

for(vImagePixelCount i = 0; i < inputBuffer->width + inputBuffer->height; i+=4) {
    Byte B = (Byte)data[i];
    Byte G = (Byte)data[i + 1];
    Byte R = (Byte)data[i + 2];
    Byte A = (Byte)data[i + 3];
    NSLog(@"1: (%d) %d, %d, %d, %d", i, R,G,B,A);
    ((Byte*)outputBuffer->data)[i] = R;
    ((Byte*)outputBuffer->data)[i + 1] = G;
    ((Byte*)outputBuffer->data)[i + 2] = B;
    ((Byte*)outputBuffer->data)[i + 3] = A;
}

CGImageRef effectCGImage = vImageCreateCGImageFromBuffer(outputBuffer, &format, &cleanupBuffer, NULL, kvImageNoFlags, &e);

// draw effect image
CGContextSaveGState(outputContext);
CGContextDrawImage(outputContext, outputImageRectInPoints, effectCGImage);
CGContextRestoreGState(outputContext);

// Cleanup
CGImageRelease(effectCGImage);

// Output image is ready.
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return outputImage;

So, what is wrong with the above code?

P.S.: I know that for inverting channels I could use some vImage_ functions but that is not the point I'm trying to achieve. What I want to achieve is altering an image by changing each one of its pixels one by one. Ultimately my intent is creating a DisplacementMapFilter (transform the image pixels based on a provided map).


Solution

  • Figured out the problem. The particular problem in this case was this:

    for(vImagePixelCount i = 0; i < (inputBuffer->width * inputBuffer->height * 4); i+=4)

    I was looping just through a small part of the buffer. Now I am looping through the entire buffer.