use*_*443 2 video argb rgba ios avassetwriter
我使用glReadPixels抓取我的opengl场景的屏幕截图,然后在IOS 4上使用AVAssetWriter将它们转换为视频.我的问题是我需要将alpha通道传递给视频,该视频只接受kCVPixelFormatType_32ARGB和glReadPixels检索RGBA.所以基本上我需要一种方法将我的RGBA转换为ARGB,换句话说,首先放置alpha字节.
int depth = 4;
unsigned char buffer[width * height * depth];
glReadPixels(0,0,width, height, GL_RGBA, GL_UNSIGNED_BYTE, &buffer);
CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer), width*height*depth, NULL );
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast;
CGImageRef image = CGImageCreate(width, height, 8, 32, width*depth, CGColorSpaceCreateDeviceRGB(), bitmapInfo, ref, NULL, true, kCGRenderingIntentDefault);
UIWindow* parentWindow = [self window];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey, [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey, nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_32ARGB, (CFDictionaryRef) options, &pxbuffer);
NSParameterAssert(status == kCVReturnSuccess);
NSParameterAssert(pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata, width, height, 8, depth*width, rgbColorSpace, kCGImageAlphaPremultipliedFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, parentWindow.transform);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer; // chuck pixel buffer into AVAssetWriter
Run Code Online (Sandbox Code Playgroud)
以为我会发布整个代码,因为我可能会帮助别人.
干杯
注意:我假设每个通道有8位.如果不是这样,请相应调整.
要最后移动alpha位,您需要执行旋转.这通常通过位移最容易表达.
在这种情况下,您希望将RGB位向右移动8位,将A位向左移动24位.然后应使用按位OR将这两个值放在一起,这样就可以了argb = (rgba >> 8) | (rgba << 24)
.
归档时间: |
|
查看次数: |
7300 次 |
最近记录: |