RGB888 Turn Rgb565__byte

Source: Internet
Author: User

Today, when I review the code, I found this macro definition:

#define COLOR_TO_MTK_COLOR_SIMUL ((color >>) & 0x1f) << 11) \
| (((color) >> Ten & 0x3f) << 5) \
| ((color) >> 3) & 0x1f)

Do you know what this macro is for?

After careful analysis, the original is to realize the conversion of RGB888 to RGB565, access to relevant information, found that there is a network of cattle writing Dongdong, here and you share.

Talk about quantitative compression and quantification compensation.

In color format conversion, often encounter changes in the number of color quantization, such as from 24bit RGB888 to 16bit RGB565 color conversion. The so-called quantitative compression and quantification compensation are my personal concepts, now described below.

Quantitative compression, for example:

Conversion of 24bit RGB888-> 16bit RGB565

24IBT RGB888 R7 R6 R5 R4 R3 R2 R1 R0 G7 G6 G5 G4 G3 G2 G1 G0 B7 B6 B5 B4 B3 B2 B1 B0

16bit RGB656 R7 R6 R5 R4 R3 G7 G6 G5 G4 G3 G2 B7 B6 B5 B4 B3

Quantization digits from 8bit to 5bit or 6bit, take the high position of the original 8bit, the quantization of compression, but the loss of precision.

Quantitative compensation, for example:

Conversion of 16bit RGB565-> 24bit RGB888

16bit RGB656 R4 R3 R2 R1 R0 G5 G4 G3 G2 G1 G0 B4 B3 B2 B1 B0

24IBT RGB888 R4 R3 R2 R1 R0 0 0 0 G5 G4 G3 G2 G1 G0 0 0 B4 B3 B2 B1 B0 0 0 0

24IBT RGB888 R4 R3 R2 R1 R0 R2 R1 R0 G5 G4 G3 G2 G1 G0 G1 G0 B4 B3 B2 B1 B0 B2 B1 B0

Note: The second row of 24bit RGB888 data is converted, not compensated data, there will be loss in precision

The third row of 24bit RGB888 data is a quantified compensation for the data, the low level of the quantitative compensation

It is easy to prove that such a compensation method is a reasonable linear compensation. The principle of compensation is very simple, we think carefully about it, so no longer detailed description.

To sum up:

Quantization compression method: Three words to get high

Methods of quantifying compensation:

1. Fill the original data to the high

2. For low, with the original data low to compensate

3. If there are still unfilled bits, continue to use the low level of the original data for circular compensation

Explain the concept of cyclic compensation:

Conversion of 8bit RGB332-> 24bit RGB888

8bit RGB332 R2 R1 R0 G2 G1 G0 B1 B0

24bit RGB888 R2 R1 R0 0 0 0 0 0 G2 G1 G0 0 0 0 0 B1 B0 0 0 0 0 0 0

24bit RGB888 R2 R1 R0 R2 R1 R0 0 0 G2 G1 G0 G2 G1 G0 0 0 B1 B0 B1 B0 0 0 0 0

24bit RGB888 R2 R1 R0 R2 R1 R0 R2 R1 G2 G1 G0 G2 G1 G0 G2 G1 B1 B0 B1 B0 B1 B0 0 0

24bit RGB888 R2 R1 R0 R2 R1 R0 R2 R1 G2 G1 G0 G2 G1 G0 G2 G1 B1 B0 B1 B0 B1 B0 B1 B0

See this, you should understand it is not the case, where the B component, the four-wheel compensation, to meet the requirements.

The need for quantitative compensation, intuitively, I put forward this method of compensation is correct (because I do not have to prove the strict), to make such a compensation, in the color of all kinds of conversion, can significantly improve the color effect, reduce the loss of precision.

The bitmap picture is a RGB888, consisting of 3 bytes per pixel,r->8bit,g->8bit,b->8bit;

Each pixels of the RGB565 is made up of 2 bytes, r->5bit,g->6bit,b->5bit.

The idea of the conversion is to take out the original point and not to perform the operation on the sample.

#define &NBSP;RGB565_MASK_RED&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;0XF800    #define   rgb565_mask_green                &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;0X07E0    #define  rgb565_mask_blue                           0x001f       void rgb565_2_rgb24 (byte * rgb24, word rgb565)    {     //extract rgb     rgb24[2]  =  (rgb565 & rgb565_mask_red)  >> 11;       rgb24[1] =  (rgb565 & rgb565_mask_green)  >> 5;     rgb24[0] =  (rgb565 & rgb565_mask_blue);       //amplify  The image     rgb24[2] <<= 3;    rgb24[1] <<= 2;    rgb24[0] <<= 3;  }    View plain copy to clipboard print? #define &NBSP;RGB565_MASK_RED&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;0XF800    #define   rgb565_mask_green                &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;0X07E0    #define  rgb565_mask_blue                           0x001f       void rgb565_2_rgb24 (byte * rgb24, word rgb565)    {     //extract rgb     rgb24[2]  =  (rgb565 & rgb565_mask_red)  >> 11;      &NBSP;RGB24[1] =  (rgb565 & rgb565_mask_green)  >> 5;    rgb24[0] =   (rgb565 & rgb565_mask_blue);       //amplify the image     rgb24[2] <<= 3;    rgb24[1] <<= 2;    rgb24[0] <<= 3;  }    #define Rgb565_mask_red 0xf800 #define Rgb565_ Mask_green 0x07e0 #define Rgb565_mask_blue 0x001f void Rgb565_2_rgb24 (BYTE *rgb24, WORD rgb565) {//extract RGB rgb24[2] = (rgb565 & rgb565_mask_red) >> 11; RGB24[1] = (rgb565 & rgb565_mask_green) >> 5; Rgb24[0] = (rgb565 & rgb565_mask_blue); Amplify the image rgb24[2] <<= 3; RGB24[1] <<= 2; Rgb24[0] <<= 3; }

View plain Copy to clipboard print? ushort rgb_24_2_565 (int r, int g, int b)    {        return  (USHORT) ((unsigned (r)  << 8)  & 0xf800)  |                  ((Unsigned (g)  < < 3)  & 0x7e0   |                 ((unsigned (b)  >> 3));  }   view Plain Copy to Clipboard print? ushort rgb_24_2_565 (int r, int g, int b)    {        return  (USHORT) ((unsigned (r)  << 8)  & 0xf800)  |                  ((Unsigned (g)  < < 3)  & 0x7e0)   |                ((unsigned (b)  >> 3));  }    USHORT rgb_24_2_565 (int r, int g, int b) {return (USHORT) ((unsigned (r) << 8) & 0xf800) | ((Unsigned (g) << 3) & 0X7E0) | ((unsigned (b) >> 3))); }

ushort rgb_24_2_555 (int r, int g, int b)    {        return  (USHORT) ((unsigned (r)  << 7)  & 0x7c00)  |                  ((Unsigned (g)  < &LT;&NBSP;2)  & 0x3e0   |                 ((unsigned (b)  >> 3));  }      COLORREF  rgb_555_2_24 (int rgb555)    {       unsigned r =   ((rgb555 >> 7)  & 0xf8);       unsigned g  =  ((rgb555 >> 2)  & 0xf8);       unsigned  b =  ((rgb555 << 3)  & 0xf8);        Return rgb (R,G,B);  }      void rgb_555_2_bgr24 (byte* p, int rgb555)     {       p[0] =  (rgb555 << 3)  &  0XF8);       p[1] =  (rgb555 >> 2)  & 0xf8 );       p[2] =  ((rgb555 >> 7)  & 0xf8);   }     ushort rgb_24_2_555 (int r, int g, int b)     {       return  (USHORT) ((unsigned (r)  << 7)  & &NBSP;0X7C00)  |                 ((Unsigned (g)  << 2)  & 0x3e0)   |                 ((unsigned (b)  >> 3));  }    &NBSP;  colorref rgb_555_2_24 (int rgb555)    {       unsigned  r =  ((rgb555 >> 7)  & 0xf8);        unsigned g =  ((rgb555 >> 2)  & 0xf8);        unsigned b =  ((rgb555 << 3)  & 0xf8);       return rgb (r,g,b);  }      Void rgb_555_2_bgr24 ( byte* p, int rgb555)    {       p[0] =  (rgb555  << 3)  & 0xf8);       p[1] =  (rgb555 &NBSP;&GT;&GT;&NBSP;2)  & 0xf8);       p[2] =  (rgb555  >> 7)  & 0xf8);  }   USHORT rgb_24_2_555 (int r, int g, int b) { Return (USHORT)(((Unsigned (r) << 7) & 0X7C00) | ((Unsigned (g) << 2) & 0X3E0) | ((unsigned (b) >> 3))); } colorref rgb_555_2_24 (int rgb555) {unsigned r = ((rgb555 >> 7) & 0xf8) unsigned g = ((rgb555 >> 2) &am P 0XF8); unsigned B = ((rgb555 << 3) & 0xf8); Return RGB (R,G,B); } void Rgb_555_2_bgr24 (byte* p, int rgb555) {p[0] = ((rgb555 << 3) & 0xf8); p[1] = ((rgb555 >> 2) & 0 XF8); P[2] = ((rgb555 >> 7) & 0xf8); }

#define RGB565_VAL (R,g,b) (WORD) (r) <<11 | (g) <<5 |      (b)) word rgb555_2_rgb565 (Word rgb555) {BYTE r,g,b;      FLOAT frate = 63/31;      Get r G B R = (BYTE) ((rgb555 >> 7) & 0xf8) >>3);      g = (BYTE) (((rgb555 >> 2) & 0xf8) >>3) *frate);      b = (BYTE) ((rgb555 << 3) & 0xf8) >>3); * (WORD *) pdest = Rgb565_val (rdest,gdest,bdest);

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.