This article is not completely original, refer to the online part of the essence, do the finishing.
RGB color Mode (also translated as "red, green and blue", less use) is a color standard of industry, is through the red (R), Green (G), Blue (B)
Three color channel changes and their superposition to get a wide range of colors, RGB is the representative of red, green, blue three channels
Color, which covers almost all the colours that human vision can perceive, and is one of the most widely used color systems of all time.
As far as rgb888->rgb565 is concerned: the specific ideas of its conversion are as follows: (Note: A method that represents only one person)
1. Take the high 5 bits of the first byte in the RGB888 as high 5 bits of the second byte of the converted RGB565
2. Take the high 3 bits of the second byte in the RGB888 as the lower 3-bit of the second byte of the converted RGB565
3. Take the 4--6 bit of the second byte in the RGB888, as the high 3 bits of the first byte of the converted RGB565
4. Take the high 5 bits of the third byte of the second byte in the RGB888 as the lower 5-bit of the first byte of the converted RGB565
As far as rgb565->rgb888 is concerned:
RGB565 are stored in the following ways:
R4 R3 R2 R1 R0 G5 G4 G3 G2 G1 G0 B4 B3 B2 B1 B0
1. Take RGB565 first byte low 5 bit as RGB888 high 5 bit
2. Take the lower 3 bits in the second byte of the RGB565, move it 5 bits to the left, as the high 5 bits of the RGB888 second byte
3. Take the high 3 bits of the first byte of the RGB565 to move it to the right 3 bits, as the 4--6 bit of the RGB888 second byte
4. Take the high 5 bits in the second byte of the RGB565 as the RGB888 third byte.
Careful man will find that if we turn low to high, there is some loss of data accuracy,
On the internet to look up the writing of information, drawing on the methods of others; the so-called quantitative compensation, talk about quantitative compression and quantitative compensation
In color format conversion, often encounter changes in the number of color quantization, such as from the 24bit RGB888
Color conversion to 16bit RGB565. 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
This article is from the "Late Evening" blog, please be sure to keep this source http://yiluohuanghun.blog.51cto.com/3407300/860798
According to the above figure, we generally know how the RGB888-> RGB565 compressed, also know rgb565-> RGB888
How to make the compensation, and then look at the code from the point of view of how we specifically implement such a transformation.
Code:
(RR, GG, BB is byte type, byte size)
(1) RGB565-> RGB888
[Java] view plain copy <span style= "FONT-SIZE:14PX;" >RR = byte[1] & 0xf8; GG = (byte[1] << 5) | ((Byte[0] & 0xe0) >> 3); bb = byte[0] << 3; Compensation RR = RR | (RR & 0x38) >> 3); GG = GG | ((GG & 0x0c) >> 2); bb = BB | (BB & 0x38) >> 3); </span>
In addition, also saw a master of two ways, from: http://bbs.csdn.net/topics/350153377
(2) RGB888-> RGB555
[CPP] View plain copy for (Int j = 0; j < abs (bmih.biheight); j++) { word *ptemp = (word *) (Pdata + widthbytes (bmih.biwidth * 16) * j); for (int i = 0; i < bmih.biwidth; i++) { #if 1 *ptemp++ = (Word) (*pr++ << 8) & 0xf800) | (word) (*pg++ < < 3) (&NBSP;&&NBSP;0X07E0) | (WORD) (*pb++ >> 3) & 0x001f); #else int nr = (*PR++&NBSP;+&NBSP;4) >> 3; int nG = (*pg++ + 2) >> 2; int nB = (*PB++&NBSP;+&NBSP;4) >> 3; if ( NR&NBSP;>&NBSP;31) nR = 31; if (ng > 63) nG = 63; if (nb > 31) nB = 31; *pTemp++ = (nr <<