RGB888至RGB565/位移 [英] RGB888 to RGB565 / Bit Shifting

查看:121
本文介绍了RGB888至RGB565/位移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用移位将三个字符组合成一个短字符.这用于实现RGB565调色板(红色5位,绿色6位,蓝色5位).

I want to combine three characters into a short using bit shifting. This is for implementing the RGB565 color palette (where there are 5 bits for red, 6 for green, 5 for blue).

这是我的示例程序,我只是在中间缺少一个步骤,我认为我需要在其中做一些安定操作.

Here is my example program, i'm just missing a step in the middle i think where i need to do some anding.

#include <stdio.h>

int main( ){
        unsigned char r, g, b;
        unsigned short rgb;

        r = 255;        // 0xFF 1111 1111
        g = 100;        // 0x64 0110 0100
        b = 50;         // 0x32 0011 0010

        r = r >> 3;     // 0x31 0001 1111
        g = g >> 2;     // 0x19 0001 1001
        b = b >> 3;     // 0x06 0000 0110

        //r = r & something; //
        //g = g & something; //
        //b = b & something; //

        // Desired result:
        //          R      G     B
        // 0xFB26 11111 011001 00110
        rgb = r | g | b;

        printf( "r 0x%x g 0x%x b 0x%x, rgb 0x%08x\n", r, g, b, rgb );
}

您可以在最后看到我想要的结果.感谢您的帮助!

You can see my desired result at the end. Thanks for the help!

推荐答案

rgb = ((r & 0b11111000) << 8) | ((g & 0b11111100) << 3) | (b >> 3);

我们将r左移11位,将g左移5位,然后将这些与b右移3位. (注意:这是假设值已经被正确屏蔽,如果需要的话,可以删除所有不需要的位.)

We shift r left by 11 bits, g left by 5 bits and bitwise OR these with b shifted right by 3 bits. (NB: this assumes the values have already been correctly masked, if needed, to remove any unwanted bits.)

这篇关于RGB888至RGB565/位移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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