imagemacoscocoaimage-processingnsbitmapimagerep

Create RGB image from each channel


I have 3 files, one with only a red channel, one with only a green channel, one with only a blue channel. Now i want to combine those 3 images to one, where every image is one color-channel in the finished image.

How can i do this with cocoa? I have a solution that is working but is too slow:

NSBitmapImageRep *rRep = [[rImage representations] objectAtIndex: 0];
NSBitmapImageRep *gRep = [[gImage representations] objectAtIndex: 0];
NSBitmapImageRep *bRep = [[bImage representations] objectAtIndex: 0];
NSBitmapImageRep *finalRep = [rRep copy];
for (NSUInteger i = 0; i < [rRep pixelsWide]; i++) {
    for (NSUInteger j = 0; j < [rRep pixelsHigh]; j++) {
        CGFloat r = [[rRep colorAtX:i y:j] redComponent];
        CGFloat g = [[gRep colorAtX:i y:j] greenComponent];
        CGFloat b = [[bRep colorAtX:i y:j] blueComponent];
        [finalRep setColor:[NSColor colorWithCalibratedRed:r green:g blue:b alpha:1.0] atX:i y:j];
    }
}
NSData *data = [finalRep representationUsingType:NSJPEGFileType properties:[NSDictionary dictionaryWithObject:[NSNumber numberWithDouble:0.7] forKey:NSImageCompressionFactor]];
[data writeToURL:[panel URL] atomically:YES];

Solution

  • The Accelerate.framework provides a function to combine 3 planar images into one destination: vImageConvert_Planar8toRGB888.
    I haven't tried your approach but the vImage based method below is quite fast.
    I was able to combine three (R,G,B) planes of a 1680x1050 image in ~0.1s on my Mac. The actual conversion takes ~1/3 of that time - The rest is setup & file IO.

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        NSDate* start = [NSDate date];
    
        NSURL* redImageURL = [[NSBundle mainBundle] URLForImageResource:@"red"];
        NSURL* greenImageURL = [[NSBundle mainBundle] URLForImageResource:@"green"];
        NSURL* blueImageURL = [[NSBundle mainBundle] URLForImageResource:@"blue"];
        NSData* redImageData = [self newChannelDataFromImageAtURL:redImageURL];
        NSData* greenImageData = [self newChannelDataFromImageAtURL:greenImageURL];
        NSData* blueImageData = [self newChannelDataFromImageAtURL:blueImageURL];
    
        //We use our "Red" image to measure the dimensions. We assume that all images & the destination have the same size
        CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)redImageURL, NULL);
        NSDictionary* properties = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
        CGFloat width = [properties[(id)kCGImagePropertyPixelWidth] doubleValue];
        CGFloat height = [properties[(id)kCGImagePropertyPixelHeight] doubleValue];
    
        self.image = [self newImageWithSize:CGSizeMake(width, height) fromRedChannel:redImageData greenChannel:greenImageData blueChannel:blueImageData];
        NSLog(@"Combining 3 (R, G, B) planes of size %@ took:%fs", NSStringFromSize(CGSizeMake(width, height)), [[NSDate date] timeIntervalSinceDate:start]);
    }
    
    - (NSImage*)newImageWithSize:(CGSize)size fromRedChannel:(NSData*)redImageData greenChannel:(NSData*)greenImageData blueChannel:(NSData*)blueImageData
    {
        vImage_Buffer redBuffer;
        redBuffer.data = (void*)redImageData.bytes;
        redBuffer.width = size.width;
        redBuffer.height = size.height;
        redBuffer.rowBytes = [redImageData length]/size.height;
    
        vImage_Buffer greenBuffer;
        greenBuffer.data = (void*)greenImageData.bytes;
        greenBuffer.width = size.width;
        greenBuffer.height = size.height;
        greenBuffer.rowBytes = [greenImageData length]/size.height;
    
        vImage_Buffer blueBuffer;
        blueBuffer.data = (void*)blueImageData.bytes;
        blueBuffer.width = size.width;
        blueBuffer.height = size.height;
        blueBuffer.rowBytes = [blueImageData length]/size.height;
    
        size_t destinationImageBytesLength = size.width*size.height*3;
        const void* destinationImageBytes = valloc(destinationImageBytesLength);
        NSData* destinationImageData = [[NSData alloc] initWithBytes:destinationImageBytes length:destinationImageBytesLength];
        vImage_Buffer destinationBuffer;
        destinationBuffer.data = (void*)destinationImageData.bytes;
        destinationBuffer.width = size.width;
        destinationBuffer.height = size.height;
        destinationBuffer.rowBytes = [destinationImageData length]/size.height;
    
        vImage_Error result = vImageConvert_Planar8toRGB888(&redBuffer, &greenBuffer, &blueBuffer, &destinationBuffer, 0);
        NSImage* image = nil;
        if(result == kvImageNoError)
        {
            //TODO: If you need color matching, use an appropriate colorspace here
            CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
            CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((__bridge CFDataRef)(destinationImageData));
            CGImageRef finalImageRef = CGImageCreate(size.width, size.height, 8, 24, destinationBuffer.rowBytes, colorSpace, kCGBitmapByteOrder32Big|kCGImageAlphaNone, dataProvider, NULL, NO, kCGRenderingIntentDefault);
            CGColorSpaceRelease(colorSpace);
            CGDataProviderRelease(dataProvider);
            image = [[NSImage alloc] initWithCGImage:finalImageRef size:NSMakeSize(size.width, size.height)];
            CGImageRelease(finalImageRef);
        }
        free((void*)destinationImageBytes);
        return image;
    }
    
    - (NSData*)newChannelDataFromImageAtURL:(NSURL*)imageURL
    {
        CGImageSourceRef imageSource = CGImageSourceCreateWithURL((__bridge CFURLRef)imageURL, NULL);
        if(imageSource == NULL){return NULL;}
        CGImageRef image = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
        CFRelease(imageSource);
        if(image == NULL){return NULL;}
        CGColorSpaceRef colorSpace = CGImageGetColorSpace(image);
        CGFloat width = CGImageGetWidth(image);
        CGFloat height = CGImageGetHeight(image);
        size_t bytesPerRow = CGImageGetBytesPerRow(image);
        CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(image);
        CGContextRef bitmapContext = CGBitmapContextCreate(NULL, width, height, 8, bytesPerRow, colorSpace, bitmapInfo);
        NSData* data = NULL;
        if(NULL != bitmapContext)
        {
            CGContextDrawImage(bitmapContext, CGRectMake(0.0, 0.0, width, height), image);
            CGImageRef imageRef = CGBitmapContextCreateImage(bitmapContext);
            if(NULL != imageRef)
            {
                data = (NSData*)CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(imageRef)));
            }
            CGImageRelease(imageRef);
            CGContextRelease(bitmapContext);
        }
        CGImageRelease(image);
        return data;
    }