Rgb888 and rgb565 convert each other

Source: Internet
Author: User

Background: In our computer, images are displayed in rgb888 format. Each pixel of the 24-Bit Bitmap stores 32-bit data, that is, rgb888 + Alpha, and Alpha is a translucent padding Byte ...... However, for a true color image, it is difficult for the naked eye to distinguish between 16 bits. Therefore, in some cases, rgb888 can be converted to rgb565 for storage, which reduces the storage capacity, reduces the data size. When the backend is displayed, rgb565 is converted to rgb888 again to achieve data width matching !!

 

I. rgb888-> rgb565

The method can be extracted as long as the corresponding monochrome high level (R5 G6 B5), but it will lead to the lack of low level, affect the accuracy, and cannot be restored.

Ii. rgb565-> rgb888

The method can be supplemented with a low monochrome position (R3 G2 B3 ).

 

Rgb888 is stored in 32-bit bytes with unsigned int
0 0 0 0 0 0 0 0 R7 R6 R5 R4 R3 R2 R1 R0 G7 G6 G5 G4 G3 G2 G1 G0 B7 B6 B5 B4 B3 B2 B1 B0

 

Rgb565 is stored in 16-bit unsigned short bytes
R7 R6 R5 R4 R3 G7 G6 G5 G4 G3 G2 B7 B6 B5 B4 B3

 

Code:

# Define distinct 0x0000ff00 # define distinct 0x000000ff # define distinct 0xf800 # define distinct 0x07e0 # define distinct short distinct (unsigned int n888color) {unsigned short n565color = 0; // obtain the RGB color and intercept the unsigned char cred = (n888color & rgb888_red)> 19; unsigned char cgreen = (n888color & rgb888_green)> 10; unsigned char cblue = (n888color & rgb888_blue)> 3; // connect n565color = (CRED <11) + (cgreen <5) + (cblue <0 ); return n565color;} unsigned int encode (unsigned short n565color) {unsigned int n888color = 0; // obtain RGB monochrome and fill in low unsigned char cred = (n565color & rgb565_red)> 8; unsigned char cgreen = (n565color & rgb565_green)> 3; unsigned char cblue = (n565color & rgb565_blue) <3; // connect n888color = (CRED <16) + (cgreen <8) + (cblue <0); Return n888color ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.