将 RGBA 转换为 ARGB (glReadPixels -> AVAssetWriter) [英] Converting RGBA to ARGB (glReadPixels -> AVAssetWriter)

查看:43
本文介绍了将 RGBA 转换为 ARGB (glReadPixels -> AVAssetWriter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 AVAssetWriter 的帮助下将使用 OpenGL 渲染的图像记录到电影文件中.问题出现了,从 OpenGL 帧缓冲区访问像素的唯一方法是使用 glReadPixels,它仅支持 iOS 上的 RGBA 像素格式.但是 AVAssetWriter 不支持这种格式.在这里,我可以使用 ARGB 或 BGRA.由于可以忽略 alpha 值,我得出的结论是,将 RGBA 转换为 ARGB 的最快方法是为 glReadPixels 提供移动一个字节的缓冲区:

I want to record images, rendered with OpenGL, into a movie-file with the help of AVAssetWriter. The problem arises, that the only way to access pixels from an OpenGL framebuffer is by using glReadPixels, which only supports the RGBA-pixel format on iOS. But AVAssetWriter doesn't support this format. Here I can either use ARGB or BGRA. As the alpha-values can be ignored, I came to the conclusion, that the fastest way to convert RGBA to ARGB would be to give glReadPixels the buffer shifted by one byte:

UInt8 *buffer = malloc(width*height*4+1);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer+1);

问题是,glReadPixels 调用会导致 EXC_BAD_ACCESS 崩溃.如果我不将缓冲区移动一个字节,它就可以完美地工作(但显然视频文件中的颜色错误).这里有什么问题?

The problem is, that the glReadPixels call leads to a EXC_BAD_ACCESS crash. If I don't shift the buffer by one byte, it works perfectly (but obviously with wrong colors in the video-file). What's the problem here?

推荐答案

我得出的结论是,将 RGBA 转换为 ARGB 的最快方法是为 glReadPixels 提供移动一个字节的缓冲区

I came to the conclusion, that the fastest way to convert RGBA to ARGB would be to give glReadPixels the buffer shifted by one byte

不过,这也会将您的 alpha 值移动 1 个像素.这是另一个建议:

This will however shift your alpha values by 1 pixel as well. Here's another suggestion:

将图片渲染为纹理(使用带有该纹理的 FBO 作为颜色附件).接下来将该纹理渲染到另一个帧缓冲区,使用 swizzling 片段着色器:

Render the picture to a texture (using a FBO with that texture as color attachment). Next render that texture to another framebuffer, with a swizzling fragment shader:

#version ...
uniform sampler2D image;
uniform vec2 image_dim;
void main() 
{
    // we want to address texel centers by absolute fragment coordinates, this
    // requires a bit of work (OpenGL-ES SL doesn't provide texelFetch function).
    gl_FragColor.rgba = 
        texture2D(image, vec2( (2*gl_FragCoord.x + 1)/(2*image_dim.y), 
                               (2*gl_FragCoord.y + 1)/(2*image_dim.y) )
        ).argb; // this swizzles RGBA into ARGB order if read into a RGBA buffer
}

这篇关于将 RGBA 转换为 ARGB (glReadPixels -> AVAssetWriter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆