首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >手动将rgba8转换为rgba5551

手动将rgba8转换为rgba5551
EN

Stack Overflow用户
提问于 2013-05-25 03:16:04
回答 1查看 1K关注 0票数 0

我需要手动将rgba8转换为rgba5551。我在另一篇文章中发现了一些有用的代码,我想修改它以将其从rgba8转换为rgba5551。我真的没有使用bitewise东西的经验,而且我自己也没有任何运气来处理代码。

代码语言:javascript
复制
void* rgba8888_to_rgba4444( void* src, int src_bytes)
{
    // compute the actual number of pixel elements in the buffer.
    int num_pixels = src_bytes / 4;
    unsigned long*  psrc = (unsigned long*)src;
    unsigned short* pdst = (unsigned short*)src;
    // convert every pixel
    for(int i = 0; i < num_pixels; i++){
        // read a source pixel
        unsigned px = psrc[i];
        // unpack the source data as 8 bit values
        unsigned r = (px << 8)  & 0xf000;
        unsigned g = (px >> 4)  & 0x0f00;
        unsigned b = (px >> 16) & 0x00f0;
        unsigned a = (px >> 28) & 0x000f;
        // and store
        pdst[i] = r | g | b | a;
    }
    return pdst;
}
EN

回答 1

Stack Overflow用户

发布于 2013-05-26 15:53:52

RGBA5551的值是它将颜色信息压缩为16位-或两个字节,其中只有一位用于alpha通道(打开或关闭)。另一方面,RGBA8888对每个通道使用一个字节。(如果你不需要alpha通道,我听说RGB565更好--因为人类对绿色更敏感)。现在,对于5位,您得到了数字0到31,因此r、g和b都需要转换为0到31之间的某个数字,并且由于它们最初都是一个字节(0-255),因此我们将它们乘以31/255。下面是一个函数,它将RGBA字节作为输入,并将RGBA5551作为一个短值输出:

代码语言:javascript
复制
short int RGBA8888_to_RGBA5551(unsigned char r, unsigned char g, unsigned char b, unsigned char a){
    unsigned char r5 = r*31/255; // All arithmetic is integer arithmetic, and so floating points are truncated. If you want to round to the nearest integer, adjust this code accordingly.
    unsigned char g5 = g*31/255;
    unsigned char b5 = b*31/255;
    unsigned char a1 = (a > 0) ? 1 : 0; // 1 if a is positive, 0 else. You must decide what is sensible.

    // Now that we have our 5 bit r, g, and b and our 1 bit a, we need to shift them into place before combining.

    short int rShift = (short int)r5 << 11; // (short int)r5 looks like 00000000000vwxyz - 11 zeroes. I'm not sure if you need (short int), but I've wasted time tracking down bugs where I didn't typecast properly before shifting.
    short int gShift = (short int)g5 << 6;
    short int bShift = (short int)b5 << 1;

    // Combine and return
    return rShift | gShift | bShift | a1;
}

当然,你可以压缩这段代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16742152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档