将BGRA转换为ARGB [英] Converting BGRA to ARGB

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

问题描述

我正在阅读本教程从iPhone相机获取像素数据。

I'm reading this tutorial on getting pixel data from the iPhone camera.

虽然我没有运行和使用此代码的问题,但我需要获取相机数据的输出(来自BGRA) )并将其转换为ARGB,以便我可以将它与外部库一起使用。我该怎么做?

While I have no issue running and using this code, I need to take the output of the camera data (which comes in BGRA) and convert it to ARGB so that I can use it with an external library. How do I do this?

推荐答案

如果您使用的是iOS 5.0,可以在Accelerate框架中使用vImage来执行NEON优化的颜色组件交换使用如下代码(来自Apple的 WebCore源代码):

If you're on iOS 5.0, you can use vImage within the Accelerate framework to do a NEON-optimized color component swap using code like the following (drawn from Apple's WebCore source code):

vImage_Buffer src;
src.height = height;
src.width = width;
src.rowBytes = srcBytesPerRow;
src.data = srcRows;

vImage_Buffer dest;
dest.height = height;
dest.width = width;
dest.rowBytes = destBytesPerRow;
dest.data = destRows;

// Swap pixel channels from BGRA to RGBA.
const uint8_t map[4] = { 2, 1, 0, 3 };
vImagePermuteChannels_ARGB8888(&src, &dest, map, kvImageNoFlags);

其中宽度 height ,并且 srcBytesPerRow 是通过 CVPixelBufferGetWidth()从像素缓冲区获得的, CVPixelBufferGetHeight() CVPixelBufferGetBytesPerRow() srcRows 将是指向像素缓冲区中字节基址的指针, destRows 将是您分配的内存保持输出RGBA图像。

where width, height, and srcBytesPerRow are obtained from your pixel buffer via CVPixelBufferGetWidth(), CVPixelBufferGetHeight(), and CVPixelBufferGetBytesPerRow(). srcRows would be the pointer to the base address of the bytes in the pixel buffer, and destRows would be memory you allocated to hold the output RGBA image.

这比简单地迭代字节和交换颜色组件要快得多。

This should be much faster than simply iterating over the bytes and swapping the color components.

根据图像大小的不同,更快的解决方案是将帧上传到OpenGL ES,渲染一个简单的矩形,将其作为纹理,并使用glReadPixels()来下拉RGBA值。更好的方法是使用iOS 5.0的纹理缓存进行上传和下载,其中iPhone 4上的720p帧只需1-3毫秒。当然,使用OpenGL ES意味着需要提供更多支持代码这个关闭。

Depending on the image size, an even faster solution would be to upload the frame to OpenGL ES, render a simple rectangle with this as a texture, and use glReadPixels() to pull down the RGBA values. Even better would be to use iOS 5.0's texture caches for both upload and download, where this process only takes 1-3 ms for a 720p frame on an iPhone 4. Of course, using OpenGL ES means a lot more supporting code to pull this off.

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

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