使用视频格式从CMSampleBufferRef创建UIImage kCVPixelFormatType_420YpCbCr8BiPlanarFullRange

时间:2012-04-18 09:33:45

标签: iphone objective-c uiimage video-processing pixelformat

使用CVPixelFormatType_420YpCbCr8BiPlanarFullRange从iphone相机设备捕获,以更快地处理灰度平面(平面0)。 我在内存中存储了一定数量的帧,以便以后创建视频。在我以灰度创建视频之前,我只存储了包含luminiscense(平面0)的平面。

现在我必须存储两个平面并从中创建彩色视频,用于存储我使用的帧:

bytesInFrame = width * height * 2; //2 bytes per pixel, is that correct?
frameData = (unsigned char*) malloc(bytesInFrame  * numbeOfFrames);

从我正在使用的灰度缓冲区创建图像的功能:

- (UIImage *) convertBitmapGrayScaleToUIImage:(unsigned char *) buffer 
                                withWidth:(int) width
                               withHeight:(int) height {


size_t bufferLength = width * height * 1;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 8;
size_t bytesPerRow = 1 * width;

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceGray();
if(colorSpaceRef == NULL) {
    DLog(@"Error allocating color space");
    CGDataProviderRelease(provider);
    return nil;
}

CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;

CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGImageRef iref = CGImageCreate(width, 
                                height, 
                                bitsPerComponent, 
                                bitsPerPixel, 
                                bytesPerRow, 
                                colorSpaceRef, 
                                bitmapInfo, 
                                provider,   // data provider
                                NULL,       // decode
                                YES,            // should interpolate
                                renderingIntent);

uint32_t* pixels = (uint32_t*)malloc(bufferLength);

if(pixels == NULL) {
    DLog(@"Error: Memory not allocated for bitmap");
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpaceRef);
    CGImageRelease(iref);       
    return nil;
}

CGContextRef context = CGBitmapContextCreate(pixels, 
                                             width, 
                                             height, 
                                             bitsPerComponent, 
                                             bytesPerRow, 
                                             colorSpaceRef, 
                                             bitmapInfo); 

if(context == NULL) {
    DLog(@"Error context not created");
    free(pixels);
}

UIImage *image = nil;
if(context) {

    CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);

    CGImageRef imageRef = CGBitmapContextCreateImage(context);

    image = [UIImage imageWithCGImage:imageRef];

    CGImageRelease(imageRef);   
    CGContextRelease(context);  
}

CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
CGDataProviderRelease(provider);

if(pixels) {
    free(pixels);
}   
return image;
}

我看到这个问题类似于我想要实现的目标: kCVPixelFormatType_420YpCbCr8BiPlanarFullRange frame to UIImage conversion 但我认为这会为我的转换增添额外的一步。

有没有办法直接从缓冲区创建颜色UIImage? 我很欣赏一些迹象。

0 个答案:

没有答案