RGB888 to RGB565 / Bit Shifting

前端 未结 2 1711
执念已碎
执念已碎 2021-01-02 04:45

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 blu

相关标签:
2条回答
  • 2021-01-02 05:11
    rgb = ((r & 0b11111000) << 8) | ((g & 0b11111100) << 3) | (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.)

    0 讨论(0)
  • 2021-01-02 05:16

    Thanks for A2A. I had also faced the same issue. The below code would help you.

    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
    

    You can find the python program in the GIThub. https://github.com/ajay126z/RGB888ToRGB565-Converter

    0 讨论(0)
提交回复
热议问题