```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 ``` ```/* Fast method to convert 565 to UNORM 8 */ /* This would retain the perfect black and white by */ /* mapping all 0 to all 0 and all 1 to all 1 and almost */ /* linear mapping for the remaining */ unsigned int convert5To8bits(unsigned int in) { /* assert(in <= 0x1F); */ const unsigned int out = (in << 3) | (in >> 2); printf("0x%02X -> 0x%02X\n", in, out); return out; } unsigned int convert6To8bits(unsigned int in) { /* assert(in <= 0x3F); */ const unsigned int out = (in << 2) | (in >> 4); printf("0x%02X -> 0x%02X\n", in, out); return out; } unsigned int convert565To888bits(unsigned int in) { /* assert(in <= 0xFFFF); */ const unsigned int clr[] = {(in >> 11), ( (in >> 5) & 0x3F ), (in & 0x1F)}; const unsigned int out = (convert5To8bits(clr[0]) << 16) | (convert6To8bits(clr[1]) << 8) | (convert5To8bits(clr[2]) << 0); printf("0x%04X -> 0x%06X\n", in, out); return out; } int main() { unsigned int i; printf("Converting 5 bits to 8 bits:\n"); for(i = 0U; i <= 0x1FU; ++i) convert5To8bits(i); printf("\n"); printf("Converting 6 bits to 8 bits:\n"); for(i = 0U; i <= 0x3FU; ++i) convert6To8bits(i); printf("\n"); printf("Converting 565 to 888:\n"); convert565To888bits(0x0000); printf("\n"); convert565To888bits(0x1212); printf("\n"); convert565To888bits(0xFFFF); return 0; } ```
 ```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 ``` ```Converting 5 bits to 8 bits: 0x00 -> 0x00 0x01 -> 0x08 0x02 -> 0x10 0x03 -> 0x18 0x04 -> 0x21 0x05 -> 0x29 0x06 -> 0x31 0x07 -> 0x39 0x08 -> 0x42 0x09 -> 0x4A 0x0A -> 0x52 0x0B -> 0x5A 0x0C -> 0x63 0x0D -> 0x6B 0x0E -> 0x73 0x0F -> 0x7B 0x10 -> 0x84 0x11 -> 0x8C 0x12 -> 0x94 0x13 -> 0x9C 0x14 -> 0xA5 0x15 -> 0xAD 0x16 -> 0xB5 0x17 -> 0xBD 0x18 -> 0xC6 0x19 -> 0xCE 0x1A -> 0xD6 0x1B -> 0xDE 0x1C -> 0xE7 0x1D -> 0xEF 0x1E -> 0xF7 0x1F -> 0xFF Converting 6 bits to 8 bits: 0x00 -> 0x00 0x01 -> 0x04 0x02 -> 0x08 0x03 -> 0x0C 0x04 -> 0x10 0x05 -> 0x14 0x06 -> 0x18 0x07 -> 0x1C 0x08 -> 0x20 0x09 -> 0x24 0x0A -> 0x28 0x0B -> 0x2C 0x0C -> 0x30 0x0D -> 0x34 0x0E -> 0x38 0x0F -> 0x3C 0x10 -> 0x41 0x11 -> 0x45 0x12 -> 0x49 0x13 -> 0x4D 0x14 -> 0x51 0x15 -> 0x55 0x16 -> 0x59 0x17 -> 0x5D 0x18 -> 0x61 0x19 -> 0x65 0x1A -> 0x69 0x1B -> 0x6D 0x1C -> 0x71 0x1D -> 0x75 0x1E -> 0x79 0x1F -> 0x7D 0x20 -> 0x82 0x21 -> 0x86 0x22 -> 0x8A 0x23 -> 0x8E 0x24 -> 0x92 0x25 -> 0x96 0x26 -> 0x9A 0x27 -> 0x9E 0x28 -> 0xA2 0x29 -> 0xA6 0x2A -> 0xAA 0x2B -> 0xAE 0x2C -> 0xB2 0x2D -> 0xB6 0x2E -> 0xBA 0x2F -> 0xBE 0x30 -> 0xC3 0x31 -> 0xC7 0x32 -> 0xCB 0x33 -> 0xCF 0x34 -> 0xD3 0x35 -> 0xD7 0x36 -> 0xDB 0x37 -> 0xDF 0x38 -> 0xE3 0x39 -> 0xE7 0x3A -> 0xEB 0x3B -> 0xEF 0x3C -> 0xF3 0x3D -> 0xF7 0x3E -> 0xFB 0x3F -> 0xFF Converting 565 to 888: 0x00 -> 0x00 0x00 -> 0x00 0x00 -> 0x00 0x0000 -> 0x000000 0x02 -> 0x10 0x10 -> 0x41 0x12 -> 0x94 0x1212 -> 0x104194 0x1F -> 0xFF 0x3F -> 0xFF 0x1F -> 0xFF 0xFFFF -> 0xFFFFFF ```