Revision: 8469
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at September 24, 2008 09:51 by nesium
Initial Code
- (CVPixelBufferRef)fastImageFromNSImage:(NSImage *)image { CVPixelBufferRef buffer = NULL; // config size_t width = [image size].width; size_t height = [image size].height; size_t bitsPerComponent = 8; // *not* CGImageGetBitsPerComponent(image); CGColorSpaceRef cs = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); CGBitmapInfo bi = kCGImageAlphaNoneSkipFirst; // *not* CGImageGetBitmapInfo(image); NSDictionary *d = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil]; // create pixel buffer CVPixelBufferCreate(kCFAllocatorDefault, width, height, k32ARGBPixelFormat, (CFDictionaryRef)d, &buffer); CVPixelBufferLockBaseAddress(buffer, 0); void *rasterData = CVPixelBufferGetBaseAddress(buffer); size_t bytesPerRow = CVPixelBufferGetBytesPerRow(buffer); // context to draw in, set to pixel buffer's address CGContextRef ctxt = CGBitmapContextCreate(rasterData, width, height, bitsPerComponent, bytesPerRow, cs, bi); if(ctxt == NULL){ NSLog(@"could not create context"); return NULL; } // draw NSGraphicsContext *nsctxt = [NSGraphicsContext graphicsContextWithGraphicsPort:ctxt flipped:NO]; [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:nsctxt]; [image compositeToPoint:NSMakePoint(0.0, 0.0) operation:NSCompositeCopy]; [NSGraphicsContext restoreGraphicsState]; CVPixelBufferUnlockBaseAddress(buffer, 0); CFRelease(ctxt); return buffer; }
Initial URL
http://lists.apple.com/archives/quartz-dev/2006/Oct/msg00096.html
Initial Description
Initial Title
Convert NSImage to CVPixelBufferRef
Initial Tags
Initial Language
Objective C