繁体   English   中英

RGB888到RGB565 /位移

[英]RGB888 to RGB565 / Bit Shifting

我希望使用位移组合三个字符。 这是为了实现RGB565调色板(红色有5位,绿色有6位,蓝色有5位)。

这是我的示例程序,我只是错过了中间的一步,我认为我需要做一些事情。

#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 );
}

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

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

我们移位r由11位左, g由5位和位左或这些与b右移3位。 (注意:这假设已经正确屏蔽了值,如果需要,可以删除任何不需要的位。)

感谢A2A。 我也面临同样的问题。 以下代码可以帮助您。

unsigned int r,g,b; // Pixel data in the RGB
unsigned char x1,x2; // The container for resulting 2 bytes

x1 = (r & 0xF8) | (g >> 5); // Take 5 bits of Red component and 3 bits of G component

x2 = ((g & 0x1C) << 3) | (b  >> 3); // Take remaining 3 Bits of G component and 5 bits of Blue component

你可以在GIThub中找到python程序。 https://github.com/ajay126z/RGB888ToRGB565-Converter

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM