我正在使用 glReadPixels 抓取我的 opengl 场景的屏幕截图,然后在 IOS 4 上使用 AVAssetWriter 将它们变成视频。我的问题是我需要将 alpha channel 传递给只接受 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
我想我会发布整个代码,因为我可能会帮助其他人。
干杯
最佳答案
注意:我假设每个 channel 8 位。如果不是这种情况,请相应地进行调整。
要最后移动 alpha 位,您需要执行旋转。这通常最容易通过位移表示。
在这种情况下,您希望将 RGB 位向右移动 8 位,将 A 位向左移动 24 位。然后应使用按位或将这两个值放在一起,这样就变成了 argb = (rgba >> 8) | (rgba << 24)
.
关于video - iPhone RGBA 到 ARGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3950014/