从每个通道创建RGB图像



我有三个文件,一个只有一个红色通道,一个只有一个绿色通道,还有一个只有蓝色通道。现在我想把这3个图像组合成一个,每个图像都是完成图像中的一个颜色通道。

我怎么能用可可做这个?我有一个正在工作但速度太慢的解决方案:

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

Accelerate.framework提供了将3个平面图像组合到一个目的地的功能:vImageConvert_Planar8到RGB888。
我还没有尝试过你的方法,但下面基于vImage的方法相当快
我能够在我的Mac上在大约0.1s内组合1680x1050图像的三个(R、G、B)平面。实际的转换需要大约三分之一的时间-其余的是设置&文件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;
}

您的程序创建许多许多许多许多颜色的对象。

尽管您的程序可以简单地访问图像代表的bitmapData,但它需要您的程序了解很多位图表示。

在采取这种方法之前,您应该让Quartz完成繁重的工作,将每个图像渲染到CGBitmapContext(例如使用CGContextDrawImage(gtx, rect, img.CGImage)),然后从渲染结果中提取/复制渲染组件值到目标RGB位图。

如果你的输入不是多组分颜色模型(例如灰度),那么你应该渲染到源颜色模型,以节省大量的CPU时间和内存。

相关内容

  • 没有找到相关文章

最新更新