1 static const uint8_t bayer_matrix[16] =
2 {
3      0,  4,  1, 5,
4      4,  0,  5, 1,
5      3,  7,  2, 6,
6      7,  3,  6, 2
7 };
8 
9 
10 static const uint8_t magic_matrix[16] =
11 {
12      0,  6,  1, 7,
13      4,  2,  5, 3,
14      3,  5,  2, 4,
15      7,  1,  6, 0
16 };
17 
rgb_dither(int rgb_dither_sel,int * r,int * g,int * b,int dith)18 static STRICTINLINE void rgb_dither(int rgb_dither_sel, int* r, int* g, int* b, int dith)
19 {
20 
21     int32_t newr = *r, newg = *g, newb = *b;
22     int32_t rcomp, gcomp, bcomp;
23 
24 
25     if (newr > 247)
26         newr = 255;
27     else
28         newr = (newr & 0xf8) + 8;
29     if (newg > 247)
30         newg = 255;
31     else
32         newg = (newg & 0xf8) + 8;
33     if (newb > 247)
34         newb = 255;
35     else
36         newb = (newb & 0xf8) + 8;
37 
38     if (rgb_dither_sel != 2)
39         rcomp = gcomp = bcomp = dith;
40     else
41     {
42         rcomp = dith & 7;
43         gcomp = (dith >> 3) & 7;
44         bcomp = (dith >> 6) & 7;
45     }
46 
47 
48 
49 
50 
51     int32_t replacesign = (rcomp - (*r & 7)) >> 31;
52 
53     int32_t ditherdiff = newr - *r;
54     *r = *r + (ditherdiff & replacesign);
55 
56     replacesign = (gcomp - (*g & 7)) >> 31;
57     ditherdiff = newg - *g;
58     *g = *g + (ditherdiff & replacesign);
59 
60     replacesign = (bcomp - (*b & 7)) >> 31;
61     ditherdiff = newb - *b;
62     *b = *b + (ditherdiff & replacesign);
63 }
64 
get_dither_noise(uint32_t wid,int x,int y,int * cdith,int * adith)65 static STRICTINLINE void get_dither_noise(uint32_t wid, int x, int y, int* cdith, int* adith)
66 {
67     if (!state[wid].other_modes.f.getditherlevel)
68         state[wid].noise = ((irand(&state[wid].rseed) & 7) << 6) | 0x20;
69 
70     y >>= state[wid].scfield;
71 
72     int dithindex;
73     switch(state[wid].other_modes.f.rgb_alpha_dither)
74     {
75     case 0:
76         dithindex = ((y & 3) << 2) | (x & 3);
77         *adith = *cdith = magic_matrix[dithindex];
78         break;
79     case 1:
80         dithindex = ((y & 3) << 2) | (x & 3);
81         *cdith = magic_matrix[dithindex];
82         *adith = (~(*cdith)) & 7;
83         break;
84     case 2:
85         dithindex = ((y & 3) << 2) | (x & 3);
86         *cdith = magic_matrix[dithindex];
87         *adith = (state[wid].noise >> 6) & 7;
88         break;
89     case 3:
90         dithindex = ((y & 3) << 2) | (x & 3);
91         *cdith = magic_matrix[dithindex];
92         *adith = 0;
93         break;
94     case 4:
95         dithindex = ((y & 3) << 2) | (x & 3);
96         *adith = *cdith = bayer_matrix[dithindex];
97         break;
98     case 5:
99         dithindex = ((y & 3) << 2) | (x & 3);
100         *cdith = bayer_matrix[dithindex];
101         *adith = (~(*cdith)) & 7;
102         break;
103     case 6:
104         dithindex = ((y & 3) << 2) | (x & 3);
105         *cdith = bayer_matrix[dithindex];
106         *adith = (state[wid].noise >> 6) & 7;
107         break;
108     case 7:
109         dithindex = ((y & 3) << 2) | (x & 3);
110         *cdith = bayer_matrix[dithindex];
111         *adith = 0;
112         break;
113     case 8:
114         dithindex = ((y & 3) << 2) | (x & 3);
115         *cdith = irand(&state[wid].rseed);
116         *adith = magic_matrix[dithindex];
117         break;
118     case 9:
119         dithindex = ((y & 3) << 2) | (x & 3);
120         *cdith = irand(&state[wid].rseed);
121         *adith = (~magic_matrix[dithindex]) & 7;
122         break;
123     case 10:
124         *cdith = irand(&state[wid].rseed);
125         *adith = (state[wid].noise >> 6) & 7;
126         break;
127     case 11:
128         *cdith = irand(&state[wid].rseed);
129         *adith = 0;
130         break;
131     case 12:
132         dithindex = ((y & 3) << 2) | (x & 3);
133         *cdith = 7;
134         *adith = bayer_matrix[dithindex];
135         break;
136     case 13:
137         dithindex = ((y & 3) << 2) | (x & 3);
138         *cdith = 7;
139         *adith = (~bayer_matrix[dithindex]) & 7;
140         break;
141     case 14:
142         *cdith = 7;
143         *adith = (state[wid].noise >> 6) & 7;
144         break;
145     case 15:
146         *cdith = 7;
147         *adith = 0;
148         break;
149     }
150 }
151