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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
| static const uint8_t dither_tresshold_r[64] = { 1, 7, 3, 5, 0, 8, 2, 6, 7, 1, 5, 3, 8, 0, 6, 2, 3, 5, 0, 8, 2, 6, 1, 7, 5, 3, 8, 0, 6, 2, 7, 1,
0, 8, 2, 6, 1, 7, 3, 5, 8, 0, 6, 2, 7, 1, 5, 3, 2, 6, 1, 7, 3, 5, 0, 8, 6, 2, 7, 1, 5, 3, 8, 0 };
static const uint8_t dither_tresshold_g[64] = { 1, 3, 2, 2, 3, 1, 2, 2, 2, 2, 0, 4, 2, 2, 4, 0, 3, 1, 2, 2, 1, 3, 2, 2, 2, 2, 4, 0, 2, 2, 0, 4,
1, 3, 2, 2, 3, 1, 2, 2, 2, 2, 0, 4, 2, 2, 4, 0, 3, 1, 2, 2, 1, 3, 2, 2, 2, 2, 4, 0, 2, 2, 0, 4 };
static const uint8_t dither_tresshold_b[64] = { 5, 3, 8, 0, 6, 2, 7, 1, 3, 5, 0, 8, 2, 6, 1, 7, 8, 0, 6, 2, 7, 1, 5, 3, 0, 8, 2, 6, 1, 7, 3, 5,
6, 2, 7, 1, 5, 3, 8, 0, 2, 6, 1, 7, 3, 5, 0, 8, 7, 1, 5, 3, 8, 0, 6, 2, 1, 7, 3, 5, 0, 8, 2, 6 };
uint8_t closest_rb(uint8_t c) { return (c >> 3 << 3); } uint8_t closest_g(uint8_t c) { return (c >> 2 << 2); }
uint16_t RGB16BIT(uint8_t r, uint8_t g, uint8_t b) { return ((uint16_t)((r>>3)<<11)|((g>>2)<<5)|(b>>3)); }
#define MIN(x, y) ( (x <= y) ? (x) : (y) )
uint16_t dither_xy( int x, int y, uint8_t r, uint8_t g, uint8_t b ){ uint8_t tresshold_id = ((y & 7) << 3) + (x & 7);
r = closest_rb( MIN(r + dither_tresshold_r[tresshold_id], 0xff) ); g = closest_g( MIN(g + dither_tresshold_g[tresshold_id], 0xff) ); b = closest_rb( MIN(b + dither_tresshold_b[tresshold_id], 0xff) ); return RGB16BIT(r, g, b); }
uint16_t dither_color_xy(int x, int y, uint32_t col) { return dither_xy(x, y, (col >> 16) & 0xFF , ((col) >> 8) & 0xFF, ((col) & 0xFF)); }
void ExampleDither1(uint16_t * dest, uint32_t * src, int width, int height){ int x, y; for (y=0; y<height; y++){ for (x=0; x<width; x++){ int pos = y * width + x; dest[pos] = dither_color_xy(x,y,src[pos]); } } }
void ExampleDither2(uint16_t * dest, uint8_t * src, int width, int height){ int x, y; for (y=0; y<height; y++){ for (x=0; x<width; x++){ int pos = y * width + x; dest[pos] = dither_xy(x,y,src[pos*3],src[pos*3+1],src[pos*3+2]); } } }
typedef union { struct { uint8_t blue; uint8_t green; uint8_t red; uint8_t alpha; } ch; uint32_t full; } color32_t;
typedef union { struct { uint16_t blue : 5; uint16_t green : 6; uint16_t red : 5; } ch; uint16_t full; } color16_t;
void argb8888_to_rgb565(uint8_t *in_argb8888, uint8_t *out_rgb565, uint16_t w, uint16_t h) { color32_t *col_32 = (color32_t *)in_argb8888; color16_t *col_16 = (color16_t *)out_rgb565;
uint8_t tresshold_id; uint8_t r; uint8_t g; uint8_t b;
for (uint16_t y=0; y<h; y++) { for (uint16_t x=0; x<w; x++) { tresshold_id = ((y & 7) << 3) + (x & 7); r = closest_rb(MIN(col_32->ch.red + dither_tresshold_r[tresshold_id], 0xff)); g = closest_g (MIN(col_32->ch.green + dither_tresshold_g[tresshold_id], 0xff)); b = closest_rb(MIN(col_32->ch.blue + dither_tresshold_b[tresshold_id], 0xff));
col_16->full = ((uint16_t)((r>>3)<<11)|((g>>2)<<5)|(b>>3));
col_32++; col_16++; } }
}
|