Bitmap和libyuv在JNI中的字节序

7 篇文章 0 订阅

1. 问题

Android中在使用RGB数据的时候我们会遇到color space 的stored in computer memory的问题。
通常有两种典型的scheme来记录RGB数据:

  1. byte order——按照字节顺序
  2. word order——按照字面顺序
  • 大端字节序的环境 big-endian system
    • byte orderWord order的顺序是一致的
  • 小端字节序 little-endian system
    • byte orderword order 是相反的,比如 ARGB(word-order)的内存字节序存储为BGRA(byte order)

所以,我们在Android中使用RGB数据的时候是使用哪种scheme(word order | byte order)标记的format(RGB)一定要清楚。

2. libyuv

2.1 FOURCC (Four Charactacter Code)

在下载的libyuv源码里libyuv-refs-heads-master\docs\formats.md里有讲述libyuv支持的FOURCC(这里):

On little endian machines, as an int, this would have ‘A’ in the lowest byte. The FOURCC macro reverses the order:

#define FOURCC(a, b, c, d) (((uint32)(a)) | ((uint32)(b) << 8) | ((uint32)(c) << 16) | ((uint32)(d) << 24))

So the “ARGB” string, read as an uint32, is

FOURCC_ARGB = FOURCC('A', 'R', 'G', 'B')

If you were to read ARGB pixels as uint32’s, the alpha would be in the high byte, and the blue in the lowest byte. In memory, these are stored little endian, so ‘B’ is first, then ‘G’, ‘R’ and ‘A’ last.

2.2 libyuv中FOURCC

libyuv使用C/C++语言,是小端字节序。所以word orderbyte order是相反的。看libyuv的源码也能看得出来。
在源码row_common.cc文件中

MAKEROWY(ARGB, 2, 1, 0, 4)  //ARGB(word order)-->BGRA(byte order)-->2,1,0-->RGB
MAKEROWY(BGRA, 1, 2, 3, 4)  //BGRA(word order)-->ARGB(byte order)-->1,2,3-->RGB
MAKEROWY(ABGR, 0, 1, 2, 4)	//ABGR(word order)-->RGBA(byte order)-->0,1,2-->RGB
MAKEROWY(RGBA, 3, 2, 1, 4)
MAKEROWY(RGB24, 2, 1, 0, 3)

MAKEROWY(ARGB, 2, 1, 0, 4)ARGB是使用的word order,所以它的byte orderBGRA,后面的2,1,0,是字节序中的索引,每个取出来后刚好是RGB

convert.h

3. Bitmap

当我们在JNI中使用Bitmap时,需要取出pixel数据的时候同样涉及到实际的内存字节序问题,只有正确知道了字节序也就是byte order,我们才能正确的转换。

  • NDK中的ANDROID_BITMAP_FORMAT_RGBA_8888c/c++语言默认小端字节序

    • 这里的ANDROID_BITMAP_FORMAT_RGBA_8888byte orderABGR
  • 应用层Bitmap.Config.ARGB_8888,java语言默认大端字节序,word orderbyte order是一样的,但是doc上给出的实际字节序的打包方式为ABGR:

    //字节序为  ABGR
    int color = (A & 0xff) << 24 | (B & 0xff) << 16 | (G & 0xff) << 8 | (R & 0xff);
    
  • Android平台OpenGL ES 的glReadPixels的读取type为GL_RGBA时,实际byte order也为ABGR

4. libyuv 转换

  • I420是YUV420格式转换的基准类型,任何要转换为YUV420的数据都可以通过先转换为I420再转换得到
  • ARGB是RGB格式转换的基准类型

RGBA color space

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值