Created
February 3, 2011 22:18
-
-
Save ashwinb/810346 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (CVPixelBufferRef) pixelBufferFromCGImage:(CGImageRef) image { | |
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: | |
[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, | |
[NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, | |
nil]; | |
CVPixelBufferRef pxbuffer = NULL; | |
int width = 640; | |
int height = 480; | |
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, | |
width, | |
height, | |
kCVPixelFormatType_32ARGB, | |
(CFDictionaryRef) options, | |
&pxbuffer); | |
// NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL); | |
CVPixelBufferLockBaseAddress(pxbuffer, 0); | |
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer); | |
// NSParameterAssert(pxdata != NULL); | |
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB(); | |
CGContextRef context = CGBitmapContextCreate(pxdata, | |
width, | |
height, | |
8, | |
4 * width, | |
rgbColorSpace, | |
kCGImageAlphaNoneSkipFirst); | |
// NSParameterAssert(context); | |
// CGContextConcatCTM(context, CGAffineTransformMakeRotation(0)); | |
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); | |
CGColorSpaceRelease(rgbColorSpace); | |
CGContextRelease(context); | |
CVPixelBufferUnlockBaseAddress(pxbuffer, 0); | |
return pxbuffer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment