-
Notifications
You must be signed in to change notification settings - Fork 6
/
s2tc_algorithm.cpp
1465 lines (1384 loc) · 34.6 KB
/
s2tc_algorithm.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2011 Rudolf Polzer All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* RUDOLF POLZER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define S2TC_LICENSE_IDENTIFIER s2tc_algorithm_license
#include "s2tc_license.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include "s2tc_algorithm.h"
#include "s2tc_common.h"
namespace
{
template<class T> void swap(T& a, T& b)
{
T h = a;
a = b;
b = h;
}
template<class T> struct color_type_info
{
};
template<> struct color_type_info<unsigned char>
{
static const unsigned char min_value = 0;
static const unsigned char max_value = 255;
};
struct color_t
{
signed char r, g, b;
};
inline color_t make_color_t()
{
return (color_t) {0, 0, 0};
}
inline color_t make_color_t(signed char r_, signed char g_, signed char b_)
{
return (color_t) {r_, g_, b_};
}
inline color_t make_color_t(int i)
{
return (color_t) {(signed char)(i >> 3), (signed char)(i >> 2), (signed char)(i >> 3)};
}
inline bool operator==(const color_t &a, const color_t &b)
{
return a.r == b.r && a.g == b.g && a.b == b.b;
}
inline bool operator<(const color_t &a, const color_t &b)
{
signed char d;
d = a.r - b.r;
if(d)
return d < 0;
d = a.g - b.g;
if(d)
return d < 0;
d = a.b - b.b;
return d < 0;
}
inline color_t &operator--(color_t &c)
{
if(c.b > 0)
{
--c.b;
}
else if(c.g > 0)
{
c.b = 31;
--c.g;
}
else if(c.r > 0)
{
c.b = 31;
c.g = 63;
--c.r;
}
else
{
c.b = 31;
c.g = 63;
c.r = 31;
}
return c;
}
inline color_t &operator++(color_t &c)
{
if(c.b < 31)
{
++c.b;
}
else if(c.g < 63)
{
c.b = 0;
++c.g;
}
else if(c.r < 31)
{
c.b = 0;
c.g = 0;
++c.r;
}
else
{
c.b = 0;
c.g = 0;
c.r = 0;
}
return c;
}
template<> struct color_type_info<color_t>
{
static const color_t min_value;
static const color_t max_value;
};
const color_t color_type_info<color_t>::min_value = { 0, 0, 0 };
const color_t color_type_info<color_t>::max_value = { 31, 63, 31 };
struct bigcolor_t
{
int r, g, b;
inline bigcolor_t(): r(0), g(0), b(0)
{
}
inline bigcolor_t &operator+=(const color_t &c)
{
r += c.r;
g += c.g;
b += c.b;
return *this;
}
inline bigcolor_t &operator+=(int v)
{
r += v;
g += v;
b += v;
return *this;
}
inline bigcolor_t operator+(int v)
{
bigcolor_t out = *this;
out += v;
return out;
}
inline bigcolor_t &operator/=(int v)
{
r /= v;
g /= v;
b /= v;
return *this;
}
inline bigcolor_t operator/(int v)
{
bigcolor_t out = *this;
out /= v;
return out;
}
inline bigcolor_t &operator<<=(int v)
{
r <<= v;
g <<= v;
b <<= v;
return *this;
}
inline bigcolor_t operator<<(int v)
{
bigcolor_t out = *this;
out <<= v;
return out;
}
inline operator color_t()
{
color_t out;
out.r = r & 31;
out.g = g & 63;
out.b = b & 31;
return out;
}
};
// 16 differences must fit in int
// i.e. a difference must be lower than 2^27
// shift right, rounded
#define SHRR(a,n) (((a) + (1 << ((n)-1))) >> (n))
inline int color_dist_avg(const color_t &a, const color_t &b)
{
int dr = a.r - b.r; // multiplier: 31 (-1..1)
int dg = a.g - b.g; // multiplier: 63 (-1..1)
int db = a.b - b.b; // multiplier: 31 (-1..1)
return ((dr*dr) << 2) + dg*dg + ((db*db) << 2);
}
inline int color_dist_w0avg(const color_t &a, const color_t &b)
{
int dr = a.r - b.r; // multiplier: 31 (-1..1)
int dg = a.g - b.g; // multiplier: 63 (-1..1)
int db = a.b - b.b; // multiplier: 31 (-1..1)
return dr*dr + dg*dg + db*db;
// weighted 1:4:1
}
inline int color_dist_wavg(const color_t &a, const color_t &b)
{
int dr = a.r - b.r; // multiplier: 31 (-1..1)
int dg = a.g - b.g; // multiplier: 63 (-1..1)
int db = a.b - b.b; // multiplier: 31 (-1..1)
return ((dr*dr) << 2) + ((dg*dg) << 2) + (db*db);
// weighted 4:16:1
}
inline int color_dist_yuv(const color_t &a, const color_t &b)
{
int dr = a.r - b.r; // multiplier: 31 (-1..1)
int dg = a.g - b.g; // multiplier: 63 (-1..1)
int db = a.b - b.b; // multiplier: 31 (-1..1)
int y = dr * 30*2 + dg * 59 + db * 11*2; // multiplier: 6259
int u = dr * 202 - y; // * 0.5 / (1 - 0.30)
int v = db * 202 - y; // * 0.5 / (1 - 0.11)
return ((y*y) << 1) + SHRR(u*u, 3) + SHRR(v*v, 4);
// weight for u: sqrt(2^-4) / (0.5 / (1 - 0.30)) = 0.350
// weight for v: sqrt(2^-5) / (0.5 / (1 - 0.11)) = 0.315
}
inline int color_dist_rgb(const color_t &a, const color_t &b)
{
int dr = a.r - b.r; // multiplier: 31 (-1..1)
int dg = a.g - b.g; // multiplier: 63 (-1..1)
int db = a.b - b.b; // multiplier: 31 (-1..1)
int y = dr * 21*2 + dg * 72 + db * 7*2; // multiplier: 6272
int u = dr * 202 - y; // * 0.5 / (1 - 0.21)
int v = db * 202 - y; // * 0.5 / (1 - 0.07)
return ((y*y) << 1) + SHRR(u*u, 3) + SHRR(v*v, 4);
// weight for u: sqrt(2^-4) / (0.5 / (1 - 0.21)) = 0.395
// weight for v: sqrt(2^-5) / (0.5 / (1 - 0.07)) = 0.328
}
inline int color_dist_srgb(const color_t &a, const color_t &b)
{
int dr = a.r * (int) a.r - b.r * (int) b.r; // multiplier: 31*31
int dg = a.g * (int) a.g - b.g * (int) b.g; // multiplier: 63*63
int db = a.b * (int) a.b - b.b * (int) b.b; // multiplier: 31*31
int y = dr * 21*2*2 + dg * 72 + db * 7*2*2; // multiplier: 393400
int u = dr * 409 - y; // * 0.5 / (1 - 0.30)
int v = db * 409 - y; // * 0.5 / (1 - 0.11)
int sy = SHRR(y, 3) * SHRR(y, 4);
int su = SHRR(u, 3) * SHRR(u, 4);
int sv = SHRR(v, 3) * SHRR(v, 4);
return SHRR(sy, 4) + SHRR(su, 8) + SHRR(sv, 9);
// weight for u: sqrt(2^-4) / (0.5 / (1 - 0.30)) = 0.350
// weight for v: sqrt(2^-5) / (0.5 / (1 - 0.11)) = 0.315
}
inline int srgb_get_y(const color_t &a)
{
// convert to linear
int r = a.r * (int) a.r;
int g = a.g * (int) a.g;
int b = a.b * (int) a.b;
// find luminance
int y = 37 * (r * 21*2*2 + g * 72 + b * 7*2*2); // multiplier: 14555800
// square root it (!)
y = sqrtf(y) + 0.5f; // now in range 0 to 3815
return y;
}
inline int color_dist_srgb_mixed(const color_t &a, const color_t &b)
{
// get Y
int ay = srgb_get_y(a);
int by = srgb_get_y(b);
// get UV
int au = a.r * 191 - ay;
int av = a.b * 191 - ay;
int bu = b.r * 191 - by;
int bv = b.b * 191 - by;
// get differences
int y = ay - by;
int u = au - bu;
int v = av - bv;
return ((y*y) << 3) + SHRR(u*u, 1) + SHRR(v*v, 2);
// weight for u: ???
// weight for v: ???
}
inline int color_dist_normalmap(const color_t &a, const color_t &b)
{
float ca[3], cb[3], n;
ca[0] = a.r / 31.0f * 2 - 1;
ca[1] = a.g / 63.0f * 2 - 1;
ca[2] = a.b / 31.0f * 2 - 1;
cb[0] = b.r / 31.0f * 2 - 1;
cb[1] = b.g / 63.0f * 2 - 1;
cb[2] = b.b / 31.0f * 2 - 1;
n = ca[0] * ca[0] + ca[1] * ca[1] + ca[2] * ca[2];
if(n > 0)
{
n = 1.0f / sqrtf(n);
ca[0] *= n;
ca[1] *= n;
ca[2] *= n;
}
n = cb[0] * cb[0] + cb[1] * cb[1] + cb[2] * cb[2];
if(n > 0)
{
n = 1.0f / sqrtf(n);
cb[0] *= n;
cb[1] *= n;
cb[2] *= n;
}
return
100000 *
(
(cb[0] - ca[0]) * (cb[0] - ca[0])
+
(cb[1] - ca[1]) * (cb[1] - ca[1])
+
(cb[2] - ca[2]) * (cb[2] - ca[2])
)
;
// max value: 1000 * (4 + 4 + 4) = 6000
}
typedef int ColorDistFunc(const color_t &a, const color_t &b);
inline int alpha_dist(unsigned char a, unsigned char b)
{
return (a - (int) b) * (a - (int) b);
}
template <class T, class F>
// n: input count
// m: total color count (including non-counted inputs)
// m >= n
inline void reduce_colors_inplace(T *c, int n, int m, F dist)
{
int i, j, k;
int bestsum = -1;
int besti = 0;
int bestj = 1;
int dists[m][n];
// first the square
for(i = 0; i < n; ++i)
{
dists[i][i] = 0;
for(j = i+1; j < n; ++j)
{
int d = dist(c[i], c[j]);
dists[i][j] = dists[j][i] = d;
}
}
// then the box
for(; i < m; ++i)
{
for(j = 0; j < n; ++j)
{
int d = dist(c[i], c[j]);
dists[i][j] = d;
}
}
for(i = 0; i < m; ++i)
for(j = i+1; j < m; ++j)
{
int sum = 0;
for(k = 0; k < n; ++k)
{
int di = dists[i][k];
int dj = dists[j][k];
int m = min(di, dj);
sum += m;
}
if(bestsum < 0 || sum < bestsum)
{
bestsum = sum;
besti = i;
bestj = j;
}
}
T c0 = c[besti];
c[1] = c[bestj];
c[0] = c0;
}
template <class T, class F>
inline void reduce_colors_inplace_2fixpoints(T *c, int n, int m, F dist, const T &fix0, const T &fix1)
{
// TODO fix this for ramp encoding!
int i, j, k;
int bestsum = -1;
int besti = 0;
int bestj = 1;
int dists[m+2][n];
// first the square
for(i = 0; i < n; ++i)
{
dists[i][i] = 0;
for(j = i+1; j < n; ++j)
{
int d = dist(c[i], c[j]);
dists[i][j] = dists[j][i] = d;
}
}
// then the box
for(; i < m; ++i)
{
for(j = 0; j < n; ++j)
{
int d = dist(c[i], c[j]);
dists[i][j] = d;
}
}
// then the two extra rows
for(j = 0; j < n; ++j)
{
int d = dist(fix0, c[j]);
dists[m][j] = d;
}
for(j = 0; j < n; ++j)
{
int d = dist(fix1, c[j]);
dists[m+1][j] = d;
}
for(i = 0; i < m; ++i)
for(j = i+1; j < m; ++j)
{
int sum = 0;
for(k = 0; k < n; ++k)
{
int di = dists[i][k];
int dj = dists[j][k];
int d0 = dists[m][k];
int d1 = dists[m+1][k];
int m = min(min(di, dj), min(d0, d1));
sum += m;
}
if(bestsum < 0 || sum < bestsum)
{
bestsum = sum;
besti = i;
bestj = j;
}
}
if(besti != 0)
c[0] = c[besti];
if(bestj != 1)
c[1] = c[bestj];
}
enum CompressionMode
{
MODE_NORMAL,
MODE_FAST
};
template<ColorDistFunc ColorDist> inline int refine_component_encode(int comp)
{
return comp;
}
template<> inline int refine_component_encode<color_dist_srgb>(int comp)
{
return comp * comp;
}
template<> inline int refine_component_encode<color_dist_srgb_mixed>(int comp)
{
return comp * comp;
}
template<ColorDistFunc ColorDist> inline int refine_component_decode(int comp)
{
return comp;
}
template<> inline int refine_component_decode<color_dist_srgb>(int comp)
{
return sqrtf(comp) + 0.5f;
}
template<> inline int refine_component_decode<color_dist_srgb_mixed>(int comp)
{
return sqrtf(comp) + 0.5f;
}
template <class T, class Big, int scale_l>
struct s2tc_evaluate_colors_result_t;
template <class T, class Big>
struct s2tc_evaluate_colors_result_t<T, Big, 1>
{
// uses:
// Big << int
// Big / int
// Big + int
// Big += T
int n0, n1;
Big S0, S1;
inline s2tc_evaluate_colors_result_t():
n0(), n1(), S0(), S1()
{
}
inline void add(int l, T a)
{
if(l)
{
++n1;
S1 += a;
}
else
{
++n0;
S0 += a;
}
}
inline bool evaluate(T &a, T &b)
{
if(!n0 && !n1)
return false;
if(n0)
a = ((S0 << 1) + n0) / (n0 << 1);
if(n1)
b = ((S1 << 1) + n1) / (n1 << 1);
return true;
}
};
template <class T, class Big, int scale_l>
struct s2tc_evaluate_colors_result_t
{
// a possible implementation of inferred color/alpha values
// refining would go here
};
template <class T>
struct s2tc_evaluate_colors_result_null_t
{
inline void add(int l, T a)
{
}
};
template<class T> T get(const unsigned char *buf)
{
T c;
c.r = buf[0];
c.g = buf[1];
c.b = buf[2];
return c;
}
template<> unsigned char get<unsigned char>(const unsigned char *buf)
{
return buf[3]; // extract alpha
}
template<class T, class Big, int bpp, bool have_trans, bool have_0_255, int n_input, class Dist, class Eval, class Arr>
inline unsigned int s2tc_try_encode_block(
Arr &out,
Eval &res,
Dist ColorDist,
const unsigned char *in, int iw, int w, int h,
const T colors_ref[])
{
unsigned int score = 0;
for(int x = 0; x < w; ++x) for(int y = 0; y < h; ++y)
{
int i = y * 4 + x;
const unsigned char *pix = &in[(y * iw + x) * 4];
if(have_trans)
{
if(pix[3] == 0)
{
out.do_or(i, (1 << bpp) - 1);
continue;
}
}
T color(get<T>(pix));
int best = 0;
int bestdist = ColorDist(color, colors_ref[0]);
for(int k = 1; k < n_input; ++k)
{
int dist = ColorDist(color, colors_ref[k]);
if(dist < bestdist)
{
bestdist = dist;
best = k;
}
}
if(have_0_255)
{
int dist_0 = ColorDist(color, color_type_info<T>::min_value);
if(dist_0 <= bestdist)
{
bestdist = dist_0;
out.do_or(i, (1 << bpp) - 2);
score += bestdist;
continue;
}
int dist_255 = ColorDist(color, color_type_info<T>::max_value);
if(dist_255 <= bestdist)
{
bestdist = dist_255;
out.do_or(i, (1 << bpp) - 1);
score += bestdist;
continue;
}
}
// record
res.add(best, color);
out.do_or(i, best);
score += bestdist;
}
return score;
}
// REFINE_LOOP: refine, take result over only if score improved, loop until it did not
inline void s2tc_dxt5_encode_alpha_refine_loop(bitarray<uint64_t, 16, 3> &out, const unsigned char *in, int iw, int w, int h, unsigned char &a0, unsigned char &a1)
{
bitarray<uint64_t, 16, 3> out2;
unsigned char a0next = a0, a1next = a1;
unsigned int s = 0x7FFFFFFF;
for(;;)
{
unsigned char ramp[2] = {
a0next,
a1next
};
s2tc_evaluate_colors_result_t<unsigned char, int, 1> r2;
unsigned int s2 = s2tc_try_encode_block<unsigned char, int, 3, false, true, 2>(out2, r2, alpha_dist, in, iw, w, h, ramp);
if(s2 < s)
{
out = out2;
s = s2;
a0 = a0next;
a1 = a1next;
if(!r2.evaluate(a0next, a1next))
break;
}
else
break;
out2.clear();
}
if(a1 == a0)
{
if(a0 == 255)
--a1;
else
++a1;
for(int i = 0; i < 16; ++i) switch(out.get(i))
{
case 1:
out.set(i, 0);
break;
}
}
if(a1 < a0)
{
swap(a0, a1);
for(int i = 0; i < 16; ++i) switch(out.get(i))
{
case 0:
out.set(i, 1);
break;
case 1:
out.set(i, 0);
break;
case 6:
case 7:
break;
default:
out.set(i, 7 - out.get(i));
break;
}
}
}
// REFINE_ALWAYS: refine, do not check
inline void s2tc_dxt5_encode_alpha_refine_always(bitarray<uint64_t, 16, 3> &out, const unsigned char *in, int iw, int w, int h, unsigned char &a0, unsigned char &a1)
{
unsigned char ramp[2] = {
a0,
a1
};
s2tc_evaluate_colors_result_t<unsigned char, int, 1> r2;
s2tc_try_encode_block<unsigned char, int, 3, false, true, 2>(out, r2, alpha_dist, in, iw, w, h, ramp);
r2.evaluate(a0, a1);
if(a1 == a0)
{
if(a0 == 255)
--a1;
else
++a1;
for(int i = 0; i < 16; ++i) switch(out.get(i))
{
case 1:
out.set(i, 0);
break;
}
}
if(a1 < a0)
{
swap(a0, a1);
for(int i = 0; i < 16; ++i) switch(out.get(i))
{
case 0:
out.set(i, 1);
break;
case 1:
out.set(i, 0);
break;
case 6:
case 7:
break;
default:
out.set(i, 7 - out.get(i));
break;
}
}
}
// REFINE_NEVER: do not refine
inline void s2tc_dxt5_encode_alpha_refine_never(bitarray<uint64_t, 16, 3> &out, const unsigned char *in, int iw, int w, int h, unsigned char &a0, unsigned char &a1)
{
if(a1 < a0)
swap(a0, a1);
unsigned char ramp[6] = {
a0,
a1
};
s2tc_evaluate_colors_result_null_t<unsigned char> r2;
s2tc_try_encode_block<unsigned char, int, 3, false, true, 2>(out, r2, alpha_dist, in, iw, w, h, ramp);
}
// REFINE_LOOP: refine, take result over only if score improved, loop until it did not
template<ColorDistFunc ColorDist, bool have_trans>
inline void s2tc_dxt1_encode_color_refine_loop(bitarray<uint32_t, 16, 2> &out, const unsigned char *in, int iw, int w, int h, color_t &c0, color_t &c1)
{
bitarray<uint32_t, 16, 2> out2;
color_t c0next = c0, c1next = c1;
unsigned int s = 0x7FFFFFFF;
for(;;)
{
color_t ramp[2] = {
c0next,
c1next
};
s2tc_evaluate_colors_result_t<color_t, bigcolor_t, 1> r2;
unsigned int s2 = s2tc_try_encode_block<color_t, bigcolor_t, 2, have_trans, false, 2>(out2, r2, ColorDist, in, iw, w, h, ramp);
if(s2 < s)
{
out = out2;
s = s2;
c0 = c0next;
c1 = c1next;
if(!r2.evaluate(c0next, c1next))
break;
}
else
break;
out2.clear();
}
if(c0 == c1)
{
if(c0 == color_type_info<color_t>::max_value)
--c1;
else
++c1;
for(int i = 0; i < 16; ++i)
if(!(out.get(i) == 1))
out.set(i, 0);
}
if(have_trans ? c1 < c0 : c0 < c1)
{
swap(c0, c1);
for(int i = 0; i < 16; ++i)
if(!(out.get(i) & 2))
out.do_xor(i, 1);
}
}
// REFINE_ALWAYS: refine, do not check
template<ColorDistFunc ColorDist, bool have_trans>
inline void s2tc_dxt1_encode_color_refine_always(bitarray<uint32_t, 16, 2> &out, const unsigned char *in, int iw, int w, int h, color_t &c0, color_t &c1)
{
color_t ramp[2] = {
c0,
c1
};
s2tc_evaluate_colors_result_t<color_t, bigcolor_t, 1> r2;
s2tc_try_encode_block<color_t, bigcolor_t, 2, have_trans, false, 2>(out, r2, ColorDist, in, iw, w, h, ramp);
r2.evaluate(c0, c1);
if(c0 == c1)
{
if(c0 == color_type_info<color_t>::max_value)
--c1;
else
++c1;
for(int i = 0; i < 16; ++i)
if(!(out.get(i) == 1))
out.set(i, 0);
}
if(have_trans ? c1 < c0 : c0 < c1)
{
swap(c0, c1);
for(int i = 0; i < 16; ++i)
if(!(out.get(i) & 2))
out.do_xor(i, 1);
}
}
// REFINE_NEVER: do not refine
template<ColorDistFunc ColorDist, bool have_trans>
inline void s2tc_dxt1_encode_color_refine_never(bitarray<uint32_t, 16, 2> &out, const unsigned char *in, int iw, int w, int h, color_t &c0, color_t &c1)
{
if(have_trans ? c1 < c0 : c0 < c1)
swap(c0, c1);
color_t ramp[2] = {
c0,
c1
};
s2tc_evaluate_colors_result_null_t<color_t> r2;
s2tc_try_encode_block<color_t, bigcolor_t, 2, have_trans, false, 2>(out, r2, ColorDist, in, iw, w, h, ramp);
}
inline void s2tc_dxt3_encode_alpha(bitarray<uint64_t, 16, 4> &out, const unsigned char *in, int iw, int w, int h)
{
for(int x = 0; x < w; ++x) for(int y = 0; y < h; ++y)
{
int i = y * 4 + x;
const unsigned char *pix = &in[(y * iw + x) * 4];
out.do_or(i, pix[3]);
}
}
template<DxtMode dxt, ColorDistFunc ColorDist, CompressionMode mode, RefinementMode refine>
inline void s2tc_encode_block(unsigned char *out, const unsigned char *rgba, int iw, int w, int h, int nrandom)
{
color_t c[16 + (nrandom >= 0 ? nrandom : 0)];
unsigned char ca[16 + (nrandom >= 0 ? nrandom : 0)];
int x, y;
if(mode == MODE_FAST)
{
// FAST: trick from libtxc_dxtn: just get brightest and darkest colors, and encode using these
color_t c0 = make_color_t(0, 0, 0);
// dummy values because we don't know whether the first pixel will write
c[0].r = 31;
c[0].g = 63;
c[0].b = 31;
c[1].r = 0;
c[1].g = 0;
c[1].b = 0;
int dmin = 0x7FFFFFFF;
int dmax = 0;
if(dxt == DXT5)
{
ca[0] = rgba[3];
ca[1] = ca[0];
}
for(x = 0; x < w; ++x)
for(y = 0; y < h; ++y)
{
c[2].r = rgba[(x + y * iw) * 4 + 0];
c[2].g = rgba[(x + y * iw) * 4 + 1];
c[2].b = rgba[(x + y * iw) * 4 + 2];
ca[2] = rgba[(x + y * iw) * 4 + 3];
if (dxt == DXT1)
if(ca[2] == 0)
continue;
// MODE_FAST doesn't work for normalmaps, so this works
int d = ColorDist(c[2], c0);
if(d > dmax)
{
dmax = d;
c[1] = c[2];
}
if(d < dmin)
{
dmin = d;
c[0] = c[2];
}
if(dxt == DXT5)
{
if(ca[2] != 255)
{
if(ca[2] > ca[1])
ca[1] = ca[2];
if(ca[2] < ca[0])
ca[0] = ca[2];
}
}
}
}
else
{
int n = 0, m = 0;
for(x = 0; x < w; ++x)
for(y = 0; y < h; ++y)
{
c[n].r = rgba[(x + y * iw) * 4 + 0];
c[n].g = rgba[(x + y * iw) * 4 + 1];
c[n].b = rgba[(x + y * iw) * 4 + 2];
ca[n] = rgba[(x + y * iw) * 4 + 3];
if (dxt == DXT1)
if(ca[n] == 0)
continue;
++n;
}
if(n == 0)
{
n = 1;
c[0].r = 0;
c[0].g = 0;
c[0].b = 0;
ca[0] = 0;
}
m = n;
if(nrandom > 0)
{
color_t mins = c[0];
color_t maxs = c[0];
unsigned char mina = (dxt == DXT5) ? ca[0] : 0;
unsigned char maxa = (dxt == DXT5) ? ca[0] : 0;
for(x = 1; x < n; ++x)
{
mins.r = min(mins.r, c[x].r);
mins.g = min(mins.g, c[x].g);
mins.b = min(mins.b, c[x].b);
maxs.r = max(maxs.r, c[x].r);
maxs.g = max(maxs.g, c[x].g);
maxs.b = max(maxs.b, c[x].b);
if(dxt == DXT5)
{
mina = min(mina, ca[x]);
maxa = max(maxa, ca[x]);
}
}
color_t len = make_color_t(maxs.r - mins.r + 1, maxs.g - mins.g + 1, maxs.b - mins.b + 1);
int lena = (dxt == DXT5) ? (maxa - (int) mina + 1) : 0;
for(x = 0; x < nrandom; ++x)
{
c[m].r = mins.r + rand() % len.r;
c[m].g = mins.g + rand() % len.g;
c[m].b = mins.b + rand() % len.b;
if(dxt == DXT5)
ca[m] = mina + rand() % lena;
++m;
}
}
else
{
// hack for last miplevel
if(n == 1)
{
c[1] = c[0];
m = n = 2;