OSDN Git Service

fix another bug in alpha refinement
[android-x86/external-s2tc.git] / s2tc_compressor.cpp
1 #include <math.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdio.h>
5
6 #include "s2tc_compressor.h"
7 #include "s2tc_common.h"
8
9 namespace
10 {
11         typedef struct
12         {
13                 signed char r, g, b;
14         }
15         color_t;
16
17         inline bool operator<(const color_t &a, const color_t &b)
18         {
19                 signed char d;
20                 d = a.r - b.r;
21                 if(d)
22                         return d < 0;
23                 d = a.g - b.g;
24                 if(d)
25                         return d < 0;
26                 d = a.b - b.b;
27                 return d < 0;
28         }
29         // 16 differences must fit in int
30         // i.e. a difference must be lower than 2^27
31
32         // shift right, rounded
33 #define SHRR(a,n) (((a) + (1 << ((n)-1))) >> (n))
34
35         inline int color_dist_avg(const color_t &a, const color_t &b)
36         {
37                 int dr = a.r - b.r; // multiplier: 31 (-1..1)
38                 int dg = a.g - b.g; // multiplier: 63 (-1..1)
39                 int db = a.b - b.b; // multiplier: 31 (-1..1)
40                 return ((dr*dr) << 2) + dg*dg + ((db*db) << 2);
41         }
42
43         inline int color_dist_wavg(const color_t &a, const color_t &b)
44         {
45                 int dr = a.r - b.r; // multiplier: 31 (-1..1)
46                 int dg = a.g - b.g; // multiplier: 63 (-1..1)
47                 int db = a.b - b.b; // multiplier: 31 (-1..1)
48                 return ((dr*dr) << 2) + ((dg*dg) << 2) + (db*db);
49                 // weighted 4:16:1
50         }
51
52         inline int color_dist_yuv(const color_t &a, const color_t &b)
53         {
54                 int dr = a.r - b.r; // multiplier: 31 (-1..1)
55                 int dg = a.g - b.g; // multiplier: 63 (-1..1)
56                 int db = a.b - b.b; // multiplier: 31 (-1..1)
57                 int y = dr * 30*2 + dg * 59 + db * 11*2; // multiplier: 6259
58                 int u = dr * 202 - y; // * 0.5 / (1 - 0.30)
59                 int v = db * 202 - y; // * 0.5 / (1 - 0.11)
60                 return ((y*y) << 1) + SHRR(u*u, 3) + SHRR(v*v, 4);
61                 // weight for u: sqrt(2^-4) / (0.5 / (1 - 0.30)) = 0.350
62                 // weight for v: sqrt(2^-5) / (0.5 / (1 - 0.11)) = 0.315
63         }
64
65         inline int color_dist_rgb(const color_t &a, const color_t &b)
66         {
67                 int dr = a.r - b.r; // multiplier: 31 (-1..1)
68                 int dg = a.g - b.g; // multiplier: 63 (-1..1)
69                 int db = a.b - b.b; // multiplier: 31 (-1..1)
70                 int y = dr * 21*2 + dg * 72 + db * 7*2; // multiplier: 6272
71                 int u = dr * 202 - y; // * 0.5 / (1 - 0.21)
72                 int v = db * 202 - y; // * 0.5 / (1 - 0.07)
73                 return ((y*y) << 1) + SHRR(u*u, 3) + SHRR(v*v, 4);
74                 // weight for u: sqrt(2^-4) / (0.5 / (1 - 0.21)) = 0.395
75                 // weight for v: sqrt(2^-5) / (0.5 / (1 - 0.07)) = 0.328
76         }
77
78         inline int color_dist_srgb(const color_t &a, const color_t &b)
79         {
80                 int dr = a.r * (int) a.r - b.r * (int) b.r; // multiplier: 31*31
81                 int dg = a.g * (int) a.g - b.g * (int) b.g; // multiplier: 63*63
82                 int db = a.b * (int) a.b - b.b * (int) b.b; // multiplier: 31*31
83                 int y = dr * 21*2*2 + dg * 72 + db * 7*2*2; // multiplier: 393400
84                 int u = dr * 409 - y; // * 0.5 / (1 - 0.30)
85                 int v = db * 409 - y; // * 0.5 / (1 - 0.11)
86                 int sy = SHRR(y, 3) * SHRR(y, 4);
87                 int su = SHRR(u, 3) * SHRR(u, 4);
88                 int sv = SHRR(v, 3) * SHRR(v, 4);
89                 return SHRR(sy, 4) + SHRR(su, 8) + SHRR(sv, 9);
90                 // weight for u: sqrt(2^-4) / (0.5 / (1 - 0.30)) = 0.350
91                 // weight for v: sqrt(2^-5) / (0.5 / (1 - 0.11)) = 0.315
92         }
93
94         inline int srgb_get_y(const color_t &a)
95         {
96                 // convert to linear
97                 int r = a.r * (int) a.r;
98                 int g = a.g * (int) a.g;
99                 int b = a.b * (int) a.b;
100                 // find luminance
101                 int y = 37 * (r * 21*2*2 + g * 72 + b * 7*2*2); // multiplier: 14555800
102                 // square root it (!)
103                 y = sqrtf(y) + 0.5f; // now in range 0 to 3815
104                 return y;
105         }
106
107         inline int color_dist_srgb_mixed(const color_t &a, const color_t &b)
108         {
109                 // get Y
110                 int ay = srgb_get_y(a);
111                 int by = srgb_get_y(b);
112                 // get UV
113                 int au = a.r * 191 - ay;
114                 int av = a.b * 191 - ay;
115                 int bu = b.r * 191 - by;
116                 int bv = b.b * 191 - by;
117                 // get differences
118                 int y = ay - by;
119                 int u = au - bu;
120                 int v = av - bv;
121                 return ((y*y) << 3) + SHRR(u*u, 1) + SHRR(v*v, 2);
122                 // weight for u: ???
123                 // weight for v: ???
124         }
125
126         // FIXME this is likely broken
127         inline int color_dist_lab_srgb(const color_t &a, const color_t &b)
128         {
129                 // undo sRGB
130                 float ar = powf(a.r / 31.0f, 2.4f);
131                 float ag = powf(a.g / 63.0f, 2.4f);
132                 float ab = powf(a.b / 31.0f, 2.4f);
133                 float br = powf(b.r / 31.0f, 2.4f);
134                 float bg = powf(b.g / 63.0f, 2.4f);
135                 float bb = powf(b.b / 31.0f, 2.4f);
136                 // convert to CIE XYZ
137                 float aX = 0.4124f * ar + 0.3576f * ag + 0.1805f * ab;
138                 float aY = 0.2126f * ar + 0.7152f * ag + 0.0722f * ab;
139                 float aZ = 0.0193f * ar + 0.1192f * ag + 0.9505f * ab;
140                 float bX = 0.4124f * br + 0.3576f * bg + 0.1805f * bb;
141                 float bY = 0.2126f * br + 0.7152f * bg + 0.0722f * bb;
142                 float bZ = 0.0193f * br + 0.1192f * bg + 0.9505f * bb;
143                 // convert to CIE Lab
144                 float Xn = 0.3127f;
145                 float Yn = 0.3290f;
146                 float Zn = 0.3583f;
147                 float aL = 116 * cbrtf(aY / Yn) - 16;
148                 float aA = 500 * (cbrtf(aX / Xn) - cbrtf(aY / Yn));
149                 float aB = 200 * (cbrtf(aY / Yn) - cbrtf(aZ / Zn));
150                 float bL = 116 * cbrtf(bY / Yn) - 16;
151                 float bA = 500 * (cbrtf(bX / Xn) - cbrtf(bY / Yn));
152                 float bB = 200 * (cbrtf(bY / Yn) - cbrtf(bZ / Zn));
153                 // euclidean distance, but moving weight away from A and B
154                 return 1000 * ((aL - bL) * (aL - bL) + (aA - bA) * (aA - bA) + (aB - bB) * (aB - bB));
155         }
156
157         inline int color_dist_normalmap(const color_t &a, const color_t &b)
158         {
159                 float ca[3], cb[3], n;
160                 ca[0] = a.r / 31.0f * 2 - 1;
161                 ca[1] = a.g / 63.0f * 2 - 1;
162                 ca[2] = a.b / 31.0f * 2 - 1;
163                 cb[0] = b.r / 31.0f * 2 - 1;
164                 cb[1] = b.g / 63.0f * 2 - 1;
165                 cb[2] = b.b / 31.0f * 2 - 1;
166                 n = sqrt(ca[0] * ca[0] + ca[1] * ca[1] + ca[2] * ca[2]);
167                 if(n > 0)
168                 {
169                         n = 1.0f / n;
170                         ca[0] *= n;
171                         ca[1] *= n;
172                         ca[2] *= n;
173                 }
174                 n = sqrt(cb[0] * cb[0] + cb[1] * cb[1] + cb[2] * cb[2]);
175                 if(n > 0)
176                 {
177                         n = 1.0f / n;
178                         cb[0] *= n;
179                         cb[1] *= n;
180                         cb[2] *= n;
181                 }
182
183                 return
184                         1000 *
185                         (
186                                 (cb[0] - ca[0]) * (cb[0] - ca[0])
187                                 +
188                                 (cb[1] - ca[1]) * (cb[1] - ca[1])
189                                 +
190                                 (cb[2] - ca[2]) * (cb[2] - ca[2])
191                         )
192                         ;
193                 // max value: 1000 * (4 + 4 + 4) = 6000
194         }
195
196         typedef int ColorDistFunc(const color_t &a, const color_t &b);
197
198         inline int alpha_dist(unsigned char a, unsigned char b)
199         {
200                 return (a - (int) b) * (a - (int) b);
201         }
202
203         template <class T, class F>
204         // n: input count
205         // m: total color count (including non-counted inputs)
206         // m >= n
207         inline void reduce_colors_inplace(T *c, int n, int m, F dist)
208         {
209                 int i, j, k;
210                 int bestsum = -1;
211                 int besti = 0;
212                 int bestj = 1;
213                 int dists[m][n];
214                 // first the square
215                 for(i = 0; i < n; ++i)
216                 {
217                         dists[i][i] = 0;
218                         for(j = i+1; j < n; ++j)
219                         {
220                                 int d = dist(c[i], c[j]);
221                                 dists[i][j] = dists[j][i] = d;
222                         }
223                 }
224                 // then the box
225                 for(; i < m; ++i)
226                 {
227                         for(j = 0; j < n; ++j)
228                         {
229                                 int d = dist(c[i], c[j]);
230                                 dists[i][j] = d;
231                         }
232                 }
233                 for(i = 0; i < m; ++i)
234                         for(j = i+1; j < m; ++j)
235                         {
236                                 int sum = 0;
237                                 for(k = 0; k < n; ++k)
238                                 {
239                                         int di = dists[i][k];
240                                         int dj = dists[j][k];
241                                         int m  = min(di, dj);
242                                         sum += m;
243                                 }
244                                 if(bestsum < 0 || sum < bestsum)
245                                 {
246                                         bestsum = sum;
247                                         besti = i;
248                                         bestj = j;
249                                 }
250                         }
251                 if(besti != 0)
252                         c[0] = c[besti];
253                 if(bestj != 1)
254                         c[1] = c[bestj];
255         }
256         template <class T, class F>
257         inline void reduce_colors_inplace_2fixpoints(T *c, int n, int m, F dist, const T &fix0, const T &fix1)
258         {
259                 int i, j, k;
260                 int bestsum = -1;
261                 int besti = 0;
262                 int bestj = 1;
263                 int dists[m+2][n];
264                 // first the square
265                 for(i = 0; i < n; ++i)
266                 {
267                         dists[i][i] = 0;
268                         for(j = i+1; j < n; ++j)
269                         {
270                                 int d = dist(c[i], c[j]);
271                                 dists[i][j] = dists[j][i] = d;
272                         }
273                 }
274                 // then the box
275                 for(; i < m; ++i)
276                 {
277                         for(j = 0; j < n; ++j)
278                         {
279                                 int d = dist(c[i], c[j]);
280                                 dists[i][j] = d;
281                         }
282                 }
283                 // then the two extra rows
284                 for(j = 0; j < n; ++j)
285                 {
286                         int d = dist(fix0, c[j]);
287                         dists[m][j] = d;
288                 }
289                 for(j = 0; j < n; ++j)
290                 {
291                         int d = dist(fix1, c[j]);
292                         dists[m+1][j] = d;
293                 }
294                 for(i = 0; i < m; ++i)
295                         for(j = i+1; j < m; ++j)
296                         {
297                                 int sum = 0;
298                                 for(k = 0; k < n; ++k)
299                                 {
300                                         int di = dists[i][k];
301                                         int dj = dists[j][k];
302                                         int d0 = dists[m][k];
303                                         int d1 = dists[m+1][k];
304                                         int m  = min(min(di, dj), min(d0, d1));
305                                         sum += m;
306                                 }
307                                 if(bestsum < 0 || sum < bestsum)
308                                 {
309                                         bestsum = sum;
310                                         besti = i;
311                                         bestj = j;
312                                 }
313                         }
314                 if(besti != 0)
315                         c[0] = c[besti];
316                 if(bestj != 1)
317                         c[1] = c[bestj];
318         }
319
320         enum CompressionMode
321         {
322                 MODE_NORMAL,
323                 MODE_RANDOM,
324                 MODE_FAST
325         };
326
327         template<ColorDistFunc ColorDist> inline int refine_component_encode(int comp)
328         {
329                 return comp;
330         }
331         template<> inline int refine_component_encode<color_dist_srgb>(int comp)
332         {
333                 return comp * comp;
334         }
335         template<> inline int refine_component_encode<color_dist_srgb_mixed>(int comp)
336         {
337                 return comp * comp;
338         }
339         template<> inline int refine_component_encode<color_dist_lab_srgb>(int comp)
340         {
341                 return comp * comp;
342         }
343
344         template<ColorDistFunc ColorDist> inline int refine_component_decode(int comp)
345         {
346                 return comp;
347         }
348         template<> inline int refine_component_decode<color_dist_srgb>(int comp)
349         {
350                 return sqrtf(comp) + 0.5f;
351         }
352         template<> inline int refine_component_decode<color_dist_srgb_mixed>(int comp)
353         {
354                 return sqrtf(comp) + 0.5f;
355         }
356         template<> inline int refine_component_decode<color_dist_lab_srgb>(int comp)
357         {
358                 return sqrtf(comp) + 0.5f;
359         }
360
361         template<DxtMode dxt, ColorDistFunc ColorDist, CompressionMode mode, RefinementMode refine>
362         inline void s2tc_encode_block(unsigned char *out, const unsigned char *rgba, int iw, int w, int h, int nrandom)
363         {
364                 color_t c[16 + (mode == MODE_RANDOM ? nrandom : 0)];
365                 unsigned char ca[16 + (mode == MODE_RANDOM ? nrandom : 0)];
366                 int n = 0, m = 0;
367                 int x, y;
368
369                 if(mode == MODE_FAST)
370                 {
371                         // FAST: trick from libtxc_dxtn: just get brightest and darkest colors, and encode using these
372
373                         color_t c0 = {0, 0, 0};
374
375                         c[0].r = rgba[2];
376                         c[0].g = rgba[1];
377                         c[0].b = rgba[0];
378                         c[1] = c[0];
379                         int dmin = ColorDist(c[0], c0);
380                         int dmax = dmin;
381                         if(dxt == DXT5)
382                         {
383                                 ca[0] = rgba[3];
384                                 ca[1] = ca[0];
385                         }
386
387                         for(x = 0; x < w; ++x)
388                                 for(y = !x; y < h; ++y)
389                                 {
390                                         c[2].r = rgba[(x + y * iw) * 4 + 2];
391                                         c[2].g = rgba[(x + y * iw) * 4 + 1];
392                                         c[2].b = rgba[(x + y * iw) * 4 + 0];
393
394                                         int d = ColorDist(c[2], c0);
395                                         if(d > dmax)
396                                         {
397                                                 dmax = d;
398                                                 c[1] = c[2];
399                                         }
400                                         if(d < dmin)
401                                         {
402                                                 dmin = d;
403                                                 c[0] = c[2];
404                                         }
405
406                                         if(dxt == DXT5)
407                                         {
408                                                 ca[2]  = rgba[(x + y * iw) * 4 + 3];
409                                                 if(ca[2] > ca[1])
410                                                         ca[1] = ca[2];
411                                                 if(ca[2] < ca[0])
412                                                         ca[0] = ca[2];
413                                         }
414                                 }
415
416                         m = n = 2;
417                 }
418                 else
419                 {
420                         for(x = 0; x < w; ++x)
421                                 for(y = 0; y < h; ++y)
422                                 {
423                                         c[n].r = rgba[(x + y * iw) * 4 + 2];
424                                         c[n].g = rgba[(x + y * iw) * 4 + 1];
425                                         c[n].b = rgba[(x + y * iw) * 4 + 0];
426                                         if(dxt == DXT5)
427                                                 ca[n]  = rgba[(x + y * iw) * 4 + 3];
428                                         ++n;
429                                 }
430                         m = n;
431
432                         if(mode == MODE_RANDOM)
433                         {
434                                 color_t mins = c[0];
435                                 color_t maxs = c[0];
436                                 unsigned char mina = (dxt == DXT5) ? ca[0] : 0;
437                                 unsigned char maxa = (dxt == DXT5) ? ca[0] : 0;
438                                 for(x = 1; x < n; ++x)
439                                 {
440                                         mins.r = min(mins.r, c[x].r);
441                                         mins.g = min(mins.g, c[x].g);
442                                         mins.b = min(mins.b, c[x].b);
443                                         maxs.r = max(maxs.r, c[x].r);
444                                         maxs.g = max(maxs.g, c[x].g);
445                                         maxs.b = max(maxs.b, c[x].b);
446                                         if(dxt == DXT5)
447                                         {
448                                                 mina = min(mina, ca[x]);
449                                                 maxa = max(maxa, ca[x]);
450                                         }
451                                 }
452                                 color_t len = { maxs.r - mins.r + 1, maxs.g - mins.g + 1, maxs.b - mins.b + 1 };
453                                 int lena = (dxt == DXT5) ? (maxa - (int) mina + 1) : 0;
454                                 for(x = 0; x < nrandom; ++x)
455                                 {
456                                         c[m].r = mins.r + rand() % len.r;
457                                         c[m].g = mins.g + rand() % len.g;
458                                         c[m].b = mins.b + rand() % len.b;
459                                         if(dxt == DXT5)
460                                                 ca[m] = mina + rand() % lena;
461                                         ++m;
462                                 }
463                         }
464                         else
465                         {
466                                 // hack for last miplevel
467                                 if(n == 1)
468                                 {
469                                         c[1] = c[0];
470                                         m = n = 2;
471                                 }
472                         }
473
474                         reduce_colors_inplace(c, n, m, ColorDist);
475                         if(dxt == DXT5)
476                                 reduce_colors_inplace_2fixpoints(ca, n, m, alpha_dist, (unsigned char) 0, (unsigned char) 255);
477                 }
478
479                 if(refine == REFINE_NEVER)
480                 {
481                         if(dxt == DXT5)
482                         {
483                                 if(ca[1] < ca[0])
484                                 {
485                                         ca[2] = ca[0];
486                                         ca[0] = ca[1];
487                                         ca[1] = ca[2];
488                                 }
489                         }
490                         if(c[1] < c[0])
491                         {
492                                 c[2] = c[0];
493                                 c[0] = c[1];
494                                 c[1] = c[2];
495                         }
496                 }
497
498                 bool refined;
499                 do
500                 {
501                         int nc0 = 0, na0 = 0, sc0r = 0, sc0g = 0, sc0b = 0, sa0 = 0;
502                         int nc1 = 0, na1 = 0, sc1r = 0, sc1g = 0, sc1b = 0, sa1 = 0;
503                         if(refine == REFINE_LOOP)
504                                 refined = false;
505
506                         memset(out, 0, (dxt == DXT1) ? 8 : 16);
507                         for(x = 0; x < w; ++x)
508                                 for(y = 0; y < h; ++y)
509                                 {
510                                         int pindex = (x+y*4);
511                                         c[2].r = rgba[(x + y * iw) * 4 + 2];
512                                         c[2].g = rgba[(x + y * iw) * 4 + 1];
513                                         c[2].b = rgba[(x + y * iw) * 4 + 0];
514                                         ca[2]  = rgba[(x + y * iw) * 4 + 3];
515                                         switch(dxt)
516                                         {
517                                                 case DXT5:
518                                                         {
519                                                                 int da[4];
520                                                                 int bitindex = pindex * 3;
521                                                                 da[0] = alpha_dist(ca[0], ca[2]);
522                                                                 da[1] = alpha_dist(ca[1], ca[2]);
523                                                                 da[2] = alpha_dist(0, ca[2]);
524                                                                 da[3] = alpha_dist(255, ca[2]);
525                                                                 if(da[2] <= da[0] && da[2] <= da[1] && da[2] <= da[3])
526                                                                 {
527                                                                         // 6
528                                                                         ++bitindex;
529                                                                         out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
530                                                                         ++bitindex;
531                                                                         out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
532                                                                 }
533                                                                 else if(da[3] <= da[0] && da[3] <= da[1])
534                                                                 {
535                                                                         // 7
536                                                                         out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
537                                                                         ++bitindex;
538                                                                         out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
539                                                                         ++bitindex;
540                                                                         out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
541                                                                 }
542                                                                 else if(da[0] <= da[1])
543                                                                 {
544                                                                         // 0
545                                                                         if(refine != REFINE_NEVER)
546                                                                         {
547                                                                                 ++na0;
548                                                                                 sa0 += ca[2];
549                                                                         }
550                                                                 }
551                                                                 else
552                                                                 {
553                                                                         // 1
554                                                                         out[bitindex / 8 + 2] |= (1 << (bitindex % 8));
555                                                                         if(refine != REFINE_NEVER)
556                                                                         {
557                                                                                 ++na1;
558                                                                                 sa1 += ca[2];
559                                                                         }
560                                                                 }
561                                                         }
562                                                         if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
563                                                         {
564                                                                 int bitindex = pindex * 2;
565                                                                 out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
566                                                                 if(refine != REFINE_NEVER)
567                                                                 {
568                                                                         ++nc1;
569                                                                         sc1r += refine_component_encode<ColorDist>(c[2].r);
570                                                                         sc1g += refine_component_encode<ColorDist>(c[2].g);
571                                                                         sc1b += refine_component_encode<ColorDist>(c[2].b);
572                                                                 }
573                                                         }
574                                                         else
575                                                         {
576                                                                 if(refine != REFINE_NEVER)
577                                                                 {
578                                                                         ++nc0;
579                                                                         sc0r += refine_component_encode<ColorDist>(c[2].r);
580                                                                         sc0g += refine_component_encode<ColorDist>(c[2].g);
581                                                                         sc0b += refine_component_encode<ColorDist>(c[2].b);
582                                                                 }
583                                                         }
584                                                         break;
585                                                 case DXT3:
586                                                         {
587                                                                 int bitindex = pindex * 4;
588                                                                 out[bitindex / 8 + 0] |= (ca[2] << (bitindex % 8));
589                                                         }
590                                                         if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
591                                                         {
592                                                                 int bitindex = pindex * 2;
593                                                                 out[bitindex / 8 + 12] |= (1 << (bitindex % 8));
594                                                                 if(refine != REFINE_NEVER)
595                                                                 {
596                                                                         ++nc1;
597                                                                         sc1r += refine_component_encode<ColorDist>(c[2].r);
598                                                                         sc1g += refine_component_encode<ColorDist>(c[2].g);
599                                                                         sc1b += refine_component_encode<ColorDist>(c[2].b);
600                                                                 }
601                                                         }
602                                                         else
603                                                         {
604                                                                 if(refine != REFINE_NEVER)
605                                                                 {
606                                                                         ++nc0;
607                                                                         sc0r += refine_component_encode<ColorDist>(c[2].r);
608                                                                         sc0g += refine_component_encode<ColorDist>(c[2].g);
609                                                                         sc0b += refine_component_encode<ColorDist>(c[2].b);
610                                                                 }
611                                                         }
612                                                         break;
613                                                 case DXT1:
614                                                         {
615                                                                 int bitindex = pindex * 2;
616                                                                 if(!ca[2])
617                                                                         out[bitindex / 8 + 4] |= (3 << (bitindex % 8));
618                                                                 else if(ColorDist(c[0], c[2]) > ColorDist(c[1], c[2]))
619                                                                 {
620                                                                         out[bitindex / 8 + 4] |= (1 << (bitindex % 8));
621                                                                         if(refine != REFINE_NEVER)
622                                                                         {
623                                                                                 ++nc1;
624                                                                                 sc1r += refine_component_encode<ColorDist>(c[2].r);
625                                                                                 sc1g += refine_component_encode<ColorDist>(c[2].g);
626                                                                                 sc1b += refine_component_encode<ColorDist>(c[2].b);
627                                                                         }
628                                                                 }
629                                                                 else
630                                                                 {
631                                                                         if(refine != REFINE_NEVER)
632                                                                         {
633                                                                                 ++nc0;
634                                                                                 sc0r += refine_component_encode<ColorDist>(c[2].r);
635                                                                                 sc0g += refine_component_encode<ColorDist>(c[2].g);
636                                                                                 sc0b += refine_component_encode<ColorDist>(c[2].b);
637                                                                         }
638                                                                 }
639                                                         }
640                                                         break;
641                                         }
642                                 }
643                         if(refine != REFINE_NEVER)
644                         {
645                                 // REFINEMENT: trick from libtxc_dxtn: reassign the colors to an average of the colors encoded with that value
646
647                                 if(dxt == DXT5)
648                                 {
649                                         if(na0)
650                                                 ca[0] = (2 * sa0 + na0) / (2 * na0);
651                                         if(na1)
652                                                 ca[1] = (2 * sa1 + na1) / (2 * na1);
653                                 }
654                                 if(refine == REFINE_CHECK || refine == REFINE_LOOP)
655                                 {
656                                         c[2] = c[0];
657                                         c[3] = c[1];
658                                 }
659                                 if(nc0)
660                                 {
661                                         c[0].r = refine_component_decode<ColorDist>((2 * sc0r + nc0) / (2 * nc0));
662                                         c[0].g = refine_component_decode<ColorDist>((2 * sc0g + nc0) / (2 * nc0));
663                                         c[0].b = refine_component_decode<ColorDist>((2 * sc0b + nc0) / (2 * nc0));
664                                 }
665                                 if(nc1)
666                                 {
667                                         c[1].r = refine_component_decode<ColorDist>((2 * sc1r + nc1) / (2 * nc1));
668                                         c[1].g = refine_component_decode<ColorDist>((2 * sc1g + nc1) / (2 * nc1));
669                                         c[1].b = refine_component_decode<ColorDist>((2 * sc1b + nc1) / (2 * nc1));
670                                 }
671
672                                 if(refine == REFINE_CHECK || refine == REFINE_LOOP)
673                                 {
674                                         int score_01 = 0;
675                                         int score_23 = 0;
676                                         for(x = 0; x < w; ++x)
677                                                 for(y = 0; y < h; ++y)
678                                                 {
679                                                         int pindex = (x+y*4);
680                                                         c[4].r = rgba[(x + y * iw) * 4 + 2];
681                                                         c[4].g = rgba[(x + y * iw) * 4 + 1];
682                                                         c[4].b = rgba[(x + y * iw) * 4 + 0];
683                                                         ca[4]  = rgba[(x + y * iw) * 4 + 3];
684                                                         if(dxt == DXT1 && !ca[4])
685                                                                 continue;
686                                                         int bitindex = pindex * 2;
687                                                         if(out[bitindex / 8 + (dxt == DXT1 ? 4 : 12)] & (1 << (bitindex % 8)))
688                                                         {
689                                                                 // we picked an 1
690                                                                 score_01 += ColorDist(c[1], c[4]);
691                                                                 score_23 += ColorDist(c[3], c[4]);
692                                                         }
693                                                         else
694                                                         {
695                                                                 // we picked a 0
696                                                                 score_01 += ColorDist(c[0], c[4]);
697                                                                 score_23 += ColorDist(c[2], c[4]);
698                                                         }
699                                                 }
700
701                                         if(score_23 <= score_01)
702                                         {
703                                                 // refinement was BAD
704                                                 c[0] = c[2];
705                                                 c[1] = c[3];
706                                         }
707                                         else if(refine == REFINE_LOOP)
708                                                 refined = true;
709
710                                         // alpha refinement is always good and doesn't
711                                         // need to be checked because alpha is linear
712                                 }
713
714                                 if(dxt == DXT5)
715                                 {
716                                         if(ca[1] < ca[0])
717                                         {
718                                                 ca[2] = ca[0];
719                                                 ca[0] = ca[1];
720                                                 ca[1] = ca[2];
721                                                 // swap the alphas
722                                                 for(int pindex = 0; pindex < 16; ++pindex)
723                                                 {
724                                                         int bitindex_set = pindex * 3;
725                                                         int bitindex_test = bitindex_set + 2;
726                                                         if(!(out[bitindex_test / 8 + 2] & (1 << (bitindex_test % 8))))
727                                                                 out[bitindex_set / 8 + 2] ^= (1 << (bitindex_set % 8));
728                                                 }
729                                         }
730                                 }
731                                 if(c[1] < c[0])
732                                 {
733                                         c[2] = c[0];
734                                         c[0] = c[1];
735                                         c[1] = c[2];
736                                         // swap the colors
737                                         if(dxt == DXT1)
738                                         {
739                                                 out[4] ^= 0x55 & ~(out[4] >> 1);
740                                                 out[5] ^= 0x55 & ~(out[5] >> 1);
741                                                 out[6] ^= 0x55 & ~(out[6] >> 1);
742                                                 out[7] ^= 0x55 & ~(out[7] >> 1);
743                                         }
744                                         else
745                                         {
746                                                 out[12] ^= 0x55 & ~(out[12] >> 1);
747                                                 out[13] ^= 0x55 & ~(out[13] >> 1);
748                                                 out[14] ^= 0x55 & ~(out[14] >> 1);
749                                                 out[15] ^= 0x55 & ~(out[15] >> 1);
750                                         }
751                                 }
752                         }
753                 }
754                 while(refine == REFINE_LOOP && refined);
755
756                 switch(dxt)
757                 {
758                         case DXT5:
759                                 out[0] = ca[0];
760                                 out[1] = ca[1];
761                         case DXT3:
762                                 out[8] = ((c[0].g & 0x07) << 5) | c[0].b;
763                                 out[9] = (c[0].r << 3) | (c[0].g >> 3);
764                                 out[10] = ((c[1].g & 0x07) << 5) | c[1].b;
765                                 out[11] = (c[1].r << 3) | (c[1].g >> 3);
766                                 break;
767                         case DXT1:
768                                 out[0] = ((c[0].g & 0x07) << 5) | c[0].b;
769                                 out[1] = (c[0].r << 3) | (c[0].g >> 3);
770                                 out[2] = ((c[1].g & 0x07) << 5) | c[1].b;
771                                 out[3] = (c[1].r << 3) | (c[1].g >> 3);
772                                 break;
773                 }
774         }
775
776         // compile time dispatch magic
777         template<DxtMode dxt, ColorDistFunc ColorDist, CompressionMode mode>
778         inline s2tc_encode_block_func_t s2tc_encode_block_func(RefinementMode refine)
779         {
780                 switch(refine)
781                 {
782                         case REFINE_NEVER:
783                                 return s2tc_encode_block<dxt, ColorDist, mode, REFINE_NEVER>;
784                         case REFINE_CHECK:
785                                 // these color dist functions do not need the refinement check, as they always improve the situation
786                                 if(ColorDist != color_dist_avg && ColorDist != color_dist_wavg)
787                                         return s2tc_encode_block<dxt, ColorDist, mode, REFINE_CHECK>;
788                         case REFINE_LOOP:
789                                 // these color dist functions do not need the refinement check, as they always improve the situation
790                                 if(ColorDist != color_dist_avg && ColorDist != color_dist_wavg)
791                                         return s2tc_encode_block<dxt, ColorDist, mode, REFINE_CHECK>;
792                         default:
793                         case REFINE_ALWAYS:
794                                 return s2tc_encode_block<dxt, ColorDist, mode, REFINE_ALWAYS>;
795                 }
796         }
797
798         template<DxtMode dxt, ColorDistFunc ColorDist>
799         inline s2tc_encode_block_func_t s2tc_encode_block_func(int nrandom, RefinementMode refine)
800         {
801                 if(nrandom > 0)
802                         return s2tc_encode_block_func<dxt, ColorDist, MODE_RANDOM>(refine);
803                 else if(nrandom == 0 || ColorDist == color_dist_normalmap) // MODE_FAST not supported for normalmaps, sorry
804                         return s2tc_encode_block_func<dxt, ColorDist, MODE_NORMAL>(refine);
805                 else
806                         return s2tc_encode_block_func<dxt, ColorDist, MODE_FAST>(refine);
807         }
808
809         template<ColorDistFunc ColorDist>
810         inline s2tc_encode_block_func_t s2tc_encode_block_func(DxtMode dxt, int nrandom, RefinementMode refine)
811         {
812                 switch(dxt)
813                 {
814                         case DXT1:
815                                 return s2tc_encode_block_func<DXT1, ColorDist>(nrandom, refine);
816                                 break;
817                         case DXT3:
818                                 return s2tc_encode_block_func<DXT3, ColorDist>(nrandom, refine);
819                                 break;
820                         default:
821                         case DXT5:
822                                 return s2tc_encode_block_func<DXT5, ColorDist>(nrandom, refine);
823                                 break;
824                 }
825         }
826 };
827
828 s2tc_encode_block_func_t s2tc_encode_block_func(DxtMode dxt, ColorDistMode cd, int nrandom, RefinementMode refine)
829 {
830         switch(cd)
831         {
832                 case RGB:
833                         return s2tc_encode_block_func<color_dist_rgb>(dxt, nrandom, refine);
834                         break;
835                 case YUV:
836                         return s2tc_encode_block_func<color_dist_yuv>(dxt, nrandom, refine);
837                         break;
838                 case SRGB:
839                         return s2tc_encode_block_func<color_dist_srgb>(dxt, nrandom, refine);
840                         break;
841                 case SRGB_MIXED:
842                         return s2tc_encode_block_func<color_dist_srgb_mixed>(dxt, nrandom, refine);
843                         break;
844                 case LAB:
845                         return s2tc_encode_block_func<color_dist_lab_srgb>(dxt, nrandom, refine);
846                         break;
847                 case AVG:
848                         return s2tc_encode_block_func<color_dist_avg>(dxt, nrandom, refine);
849                         break;
850                 default:
851                 case WAVG:
852                         return s2tc_encode_block_func<color_dist_wavg>(dxt, nrandom, refine);
853                         break;
854                 case NORMALMAP:
855                         return s2tc_encode_block_func<color_dist_normalmap>(dxt, nrandom, refine);
856                         break;
857         }
858 }
859
860 namespace
861 {
862         inline int diffuse(int *diff, int src, int shift)
863         {
864                 int maxval = (1 << (8 - shift)) - 1;
865                 src += *diff;
866                 int ret = max(0, min(src >> shift, maxval));
867                 // simulate decoding ("loop filter")
868                 int loop = (ret << shift) | (ret >> (8 - 2 * shift));
869                 *diff = src - loop;
870                 return ret;
871         }
872         inline int diffuse1(int *diff, int src)
873         {
874                 src += *diff;
875                 int ret = (src >= 128);
876                 int loop = ret ? 255 : 0;
877                 *diff = src - loop;
878                 return ret;
879         }
880 };
881
882 void rgb565_image(unsigned char *out, const unsigned char *rgba, int w, int h, int srccomps, int bgr, int alphabits)
883 {
884         int x, y;
885         int diffuse_r = 0;
886         int diffuse_g = 0;
887         int diffuse_b = 0;
888         int diffuse_a = 0;
889         if(bgr)
890         {
891                 for(y = 0; y < h; ++y)
892                         for(x = 0; x < w; ++x)
893                         {
894                                 out[(x + y * w) * 4 + 2] = diffuse(&diffuse_r, rgba[(x + y * w) * srccomps + 2], 3);
895                                 out[(x + y * w) * 4 + 1] = diffuse(&diffuse_g, rgba[(x + y * w) * srccomps + 1], 2);
896                                 out[(x + y * w) * 4 + 0] = diffuse(&diffuse_b, rgba[(x + y * w) * srccomps + 0], 3);
897                         }
898         }
899         else
900         {
901                 for(y = 0; y < h; ++y)
902                         for(x = 0; x < w; ++x)
903                         {
904                                 out[(x + y * w) * 4 + 2] = diffuse(&diffuse_r, rgba[(x + y * w) * srccomps + 0], 3);
905                                 out[(x + y * w) * 4 + 1] = diffuse(&diffuse_g, rgba[(x + y * w) * srccomps + 1], 2);
906                                 out[(x + y * w) * 4 + 0] = diffuse(&diffuse_b, rgba[(x + y * w) * srccomps + 2], 3);
907                         }
908         }
909         if(srccomps == 4)
910         {
911                 if(alphabits == 1)
912                 {
913                         for(y = 0; y < h; ++y)
914                                 for(x = 0; x < w; ++x)
915                                         out[(x + y * w) * 4 + 3] = diffuse1(&diffuse_a, rgba[(x + y * w) * srccomps + 3]);
916                 }
917                 else if(alphabits == 8)
918                 {
919                         for(y = 0; y < h; ++y)
920                                 for(x = 0; x < w; ++x)
921                                         out[(x + y * w) * 4 + 3] = rgba[(x + y * w) * srccomps + 3]; // no conversion
922                 }
923                 else
924                 {
925                         int alphadiffuse = 8 - alphabits;
926                         for(y = 0; y < h; ++y)
927                                 for(x = 0; x < w; ++x)
928                                         out[(x + y * w) * 4 + 3] = diffuse(&diffuse_a, rgba[(x + y * w) * srccomps + 3], alphadiffuse);
929                 }
930         }
931         else
932         {
933                 int alpharange = (1 << alphabits) - 1;
934                 for(y = 0; y < h; ++y)
935                         for(x = 0; x < w; ++x)
936                                 out[(x + y * w) * 4 + 3] = alpharange;
937         }
938 }
939