-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrdopng.cpp
4769 lines (3935 loc) · 130 KB
/
rdopng.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
// rdopng.cpp
// Copyright (C) 2022 Richard Geldreich, Jr. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Normal map specific references:
// "Survey of Efficient Representations for Independent Unit Vectors" by Cigolle, Donow, et al
// https://jcgt.org/published/0003/02/01/
// "Objective Image Quality Assessment of Texture Compression" by Griffin, Olano
// https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=914900
#if _MSC_VER
// For sprintf(), strcpy()
#define _CRT_SECURE_NO_WARNINGS (1)
#endif
#include "encoder/basisu.h"
#include "encoder/basisu_enc.h"
#define MINIZ_HEADER_FILE_ONLY
#define MINIZ_NO_ZLIB_COMPATIBLE_NAMES
#include "encoder/basisu_miniz.h"
#include "encoder/lodepng.h"
// Set BASISU_CATCH_EXCEPTIONS if you want exceptions to crash the app, otherwise main() catches them.
#ifndef BASISU_CATCH_EXCEPTIONS
#define BASISU_CATCH_EXCEPTIONS 1
#endif
#define RDO_PNG_VERSION "v1.10"
#define RDO_PNG_USE_APPROX_ACOS (1)
const float DEF_MAX_SMOOTH_STD_DEV = 35.0f;
const float DEF_SMOOTH_MAX_MSE_SCALE = 250.0f;
const float DEF_MAX_ULTRA_SMOOTH_STD_DEV = 5.0F;
const float DEF_ULTRA_SMOOTH_MAX_MSE_SCALE = 1500.0F;
const float QOI_DEF_SMOOTH_MAX_MSE_SCALE = 2500.0f;
const float QOI_DEF_ULTRA_SMOOTH_MAX_MSE_SCALE = 5000.0f;
const float LZ4I_DEF_SMOOTH_MAX_MSE_SCALE = 8000.0f;
const float LZ4I_DEF_ULTRA_SMOOTH_MAX_MSE_SCALE = 10000.0f;
using namespace basisu;
using namespace buminiz;
extern bool g_use_miniz;
namespace buminiz
{
extern uint64_t g_defl_freq[2][TDEFL_MAX_HUFF_SYMBOLS];
}
const float RAD_TO_DEG = 57.29577951f;
const uint32_t MAX_DELTA_COLORS = 12;
enum
{
PNG_NO_FILTER = 0,
PNG_PREV_PIXEL_FILTER = 1,
PNG_PREV_SCANLINE_FILTER = 2,
PNG_AVG_FILTER = 3,
PNG_PAETH_FILTER = 4
};
struct match_order
{
uint8_t v[MAX_DELTA_COLORS + 1];
};
static const match_order g_match_order8[] =
{
{1, 8,0,0,0,0,0,0,0},
{2, 7,1,0,0,0,0,0,0},
{2, 1,7,0,0,0,0,0,0},
{2, 6,2,0,0,0,0,0,0},
{2, 2,6,0,0,0,0,0,0},
{2, 5,3,0,0,0,0,0,0},
{2, 3,5,0,0,0,0,0,0}
};
const uint32_t NUM_MATCH_ORDER_8 = sizeof(g_match_order8) / sizeof(g_match_order8[0]);
static const match_order g_match_order4[] =
{
{ 1, 4 },
{ 2, 1, 3 },
{ 2, 3, 1 },
{ 2, 2, 2 },
{ 3, 1, 2, 1 },
{ 3, 2, 1, 1 },
{ 3, 1, 1, 2 },
{ 4, 1, 1, 1, 1 }
};
const uint32_t NUM_MATCH_ORDER_4 = sizeof(g_match_order4) / sizeof(g_match_order4[0]);
static const match_order g_match_order12[] =
{
{1, 12,0,0,0,0,0,0,0},
{2, 11,1,0,0,0,0,0,0},
{2, 1,11,0,0,0,0,0,0},
{2, 10,2,0,0,0,0,0,0},
{2, 2,10,0,0,0,0,0,0},
{2, 9,3,0,0,0,0,0,0},
{2, 3,9,0,0,0,0,0,0},
{2, 8,4,0,0,0,0,0,0},
{2, 4,8,0,0,0,0,0,0},
{2, 7,5,0,0,0,0,0,0},
{2, 5,7,0,0,0,0,0,0},
{3, 6,3,3,0,0,0,0,0},
{3, 3,3,6,0,0,0,0,0}
};
const uint32_t NUM_MATCH_ORDER_12 = sizeof(g_match_order12) / sizeof(g_match_order12[0]);
static const match_order g_match_order6[] =
{
{ 1, 6 },
{ 2, 1, 5 },
{ 2, 5, 1 },
{ 2, 3, 3 },
{ 3, 2, 2, 2 },
{ 2, 2, 4 },
{ 2, 4, 2 },
{ 3, 1, 1, 4 },
{ 3, 4, 1, 1 },
{ 3, 1, 2, 3 },
{ 3, 2, 1, 3 },
{ 3, 3, 1, 2 },
{ 3, 3, 2, 1 },
{ 4, 1, 1, 1, 3 },
{ 4, 3, 1, 1, 1 },
{ 4, 1, 2, 1, 2 },
{ 4, 2, 1, 1, 2 },
{ 4, 1, 2, 2, 1 },
{ 4, 2, 2, 1, 1 },
{ 4, 1, 1, 2, 2 },
{ 6, 1, 1, 1, 1, 1, 1 },
};
const uint32_t NUM_MATCH_ORDER_6 = sizeof(g_match_order6) / sizeof(g_match_order6[0]);
static const match_order g_match_order6c[] =
{
{1, 6,0,0,0,0,0},
{2, 5,1,0,0,0,0},
{2, 4,2,0,0,0,0},
{2, 3,3,0,0,0,0},
{2, 2,4,0,0,0,0},
{2, 1,5,0,0,0,0},
{3, 4,1,1,0,0,0},
{3, 3,2,1,0,0,0},
{3, 2,3,1,0,0,0},
{3, 1,4,1,0,0,0},
{3, 3,1,2,0,0,0},
{3, 2,2,2,0,0,0},
{3, 1,3,2,0,0,0},
{3, 2,1,3,0,0,0},
{3, 1,2,3,0,0,0},
{3, 1,1,4,0,0,0},
{4, 3,1,1,1,0,0},
{4, 2,2,1,1,0,0},
{4, 1,3,1,1,0,0},
{4, 2,1,2,1,0,0},
{4, 1,2,2,1,0,0},
{4, 1,1,3,1,0,0},
{4, 2,1,1,2,0,0},
{4, 1,2,1,2,0,0},
{4, 1,1,2,2,0,0},
{4, 1,1,1,3,0,0},
{5, 2,1,1,1,1,0},
{5, 1,2,1,1,1,0},
{5, 1,1,2,1,1,0},
{5, 1,1,1,2,1,0},
{5, 1,1,1,1,2,0},
{6, 1,1,1,1,1,1},
};
const uint32_t NUM_MATCH_ORDER_6C = sizeof(g_match_order6c) / sizeof(g_match_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1,1,6,1,1,1,1,0,0,0,0,0 },
{ 7, 1,1,1,6,1,1,1,0,0,0,0,0 },
{ 7, 1,1,1,1,6,1,1,0,0,0,0,0 },
{ 7, 1,1,1,1,1,6,1,0,0,0,0,0 },
{ 7, 1,1,1,1,1,1,6,0,0,0,0,0 },
{ 8, 5,1,1,1,1,1,1,1,0,0,0,0 },
{ 8, 1,5,1,1,1,1,1,1,0,0,0,0 },
{ 8, 1,1,5,1,1,1,1,1,0,0,0,0 },
{ 8, 1,1,1,5,1,1,1,1,0,0,0,0 },
{ 8, 1,1,1,1,5,1,1,1,0,0,0,0 },
{ 8, 1,1,1,1,1,5,1,1,0,0,0,0 },
{ 8, 1,1,1,1,1,1,5,1,0,0,0,0 },
{ 8, 1,1,1,1,1,1,1,5,0,0,0,0 },
{ 9, 4,1,1,1,1,1,1,1,1,0,0,0 },
{ 9, 1,4,1,1,1,1,1,1,1,0,0,0 },
{ 9, 1,1,4,1,1,1,1,1,1,0,0,0 },
{ 9, 1,1,1,4,1,1,1,1,1,0,0,0 },
{ 9, 1,1,1,1,4,1,1,1,1,0,0,0 },
{ 9, 1,1,1,1,1,4,1,1,1,0,0,0 },
{ 9, 1,1,1,1,1,1,4,1,1,0,0,0 },
{ 9, 1,1,1,1,1,1,1,4,1,0,0,0 },
{ 9, 1,1,1,1,1,1,1,1,4,0,0,0 },
{ 12, 1,1,1,1,1,1,1,1,1,1,1,1 }
};
const uint32_t NUM_LZ4_MATCH_ORDER_12 = sizeof(g_lz4_match_order_12_bytes) / sizeof(g_lz4_match_order_12_bytes[0]);
enum speed_mode
{
cNormalSpeed,
cFasterSpeed,
cFastestSpeed
};
struct rdo_png_params
{
rdo_png_params()
{
clear();
}
void clear()
{
m_orig_img.clear();
m_output_file_data.clear();
m_lambda = 300.0f;
m_level = 0;
m_psnr = 0;
m_angular_rms_error = 0;
m_y_psnr = 0;
m_bpp = 0;
m_print_debug_output = false;
m_debug_images = false;
m_print_progress = false;
m_print_stats = false;
m_use_chan_weights = false;
m_chan_weights[0] = 1;
m_chan_weights[1] = 1;
m_chan_weights[2] = 1;
m_chan_weights[3] = 1;
{
float LW = 2;
float AW = 1.5;
float BW = 1;
float l = sqrtf(LW * LW + AW * AW + BW * BW);
LW /= l;
AW /= l;
BW /= l;
m_chan_weights_lab[0] = LW; // L
m_chan_weights_lab[1] = AW; // a
m_chan_weights_lab[2] = BW; // b
m_chan_weights_lab[3] = 1.5f; // alpha
}
m_use_reject_thresholds = true;
m_reject_thresholds[0] = 32;
m_reject_thresholds[1] = 32;
m_reject_thresholds[2] = 32;
m_reject_thresholds[3] = 32;
m_reject_thresholds_lab[0] = .05f;
//m_reject_thresholds_lab[1] = .075f;
m_reject_thresholds_lab[1] = .05f;
m_transparent_reject_test = false;
m_perceptual_error = true;
m_match_only = false;
m_two_pass = false;
m_alpha_is_opacity = true;
m_speed_mode = cFastestSpeed;
m_normal_map = false;
m_snorm8 = false;
m_print_normal_map_metrics = false;
m_max_smooth_std_dev = DEF_MAX_SMOOTH_STD_DEV;
m_smooth_max_mse_scale = DEF_SMOOTH_MAX_MSE_SCALE;
m_max_ultra_smooth_std_dev = DEF_MAX_ULTRA_SMOOTH_STD_DEV;
m_ultra_smooth_max_mse_scale = DEF_ULTRA_SMOOTH_MAX_MSE_SCALE;
m_no_mse_scaling = false;
}
void print()
{
printf("orig image: %ux%u has alpha: %u\n", m_orig_img.get_width(), m_orig_img.get_height(), m_orig_img.has_alpha());
printf("lambda: %f\n", m_lambda);
printf("level: %u\n", m_level);
printf("chan weights: %u %u %u %u\n", m_chan_weights[0], m_chan_weights[1], m_chan_weights[2], m_chan_weights[3]);
printf("use chan weights: %u\n", m_use_chan_weights);
printf("chan weights lab: %f %f %f %f\n", m_chan_weights_lab[0], m_chan_weights_lab[1], m_chan_weights_lab[2], m_chan_weights_lab[3]);
printf("reject thresholds: %u %u %u %u\n", m_reject_thresholds[0], m_reject_thresholds[1], m_reject_thresholds[2], m_reject_thresholds[3]);
printf("reject thresholds lab: %f %f\n", m_reject_thresholds_lab[0], m_reject_thresholds_lab[1]);
printf("use reject thresholds: %u\n", m_use_reject_thresholds);
printf("transparent reject test: %u\n", m_transparent_reject_test);
printf("print debug output: %u\n", m_print_debug_output);
printf("debug images: %u\n", m_debug_images);
printf("print progress: %u\n", m_print_progress);
printf("print stats: %u\n", m_print_stats);
printf("perceptual error: %u\n", m_perceptual_error);
printf("match only: %u\n", m_match_only);
printf("two pass: %u\n", m_two_pass);
printf("alpha is opacity: %u\n", m_alpha_is_opacity);
printf("speed mode: %u\n", (uint32_t)m_speed_mode);
printf("normal map: %u\n", m_normal_map);
printf("snorm8: %u\n", m_snorm8);
printf("print normal map metrics: %u\n", m_print_normal_map_metrics);
printf("max smooth std dev: %f\n", m_max_smooth_std_dev);
printf("smooth max mse scale: %f\n", m_smooth_max_mse_scale);
printf("max ultra smooth std dev: %f\n", m_max_ultra_smooth_std_dev);
printf("ultra smooth max mse scale: %f\n", m_ultra_smooth_max_mse_scale);
printf("no MSE scaling: %u\n", m_no_mse_scaling);
}
// TODO: results - move
float m_psnr;
float m_angular_rms_error;
float m_y_psnr;
float m_bpp;
// This is the output image data, but note for PNG you can't save it at the right size without the scanline predictor values.
image m_output_image;
image m_orig_img;
uint8_vec m_output_file_data;
float m_lambda;
uint32_t m_level;
uint32_t m_chan_weights[4];
float m_chan_weights_lab[4];
bool m_use_chan_weights;
uint32_t m_reject_thresholds[4];
float m_reject_thresholds_lab[2];
bool m_use_reject_thresholds;
bool m_transparent_reject_test;
bool m_print_debug_output;
bool m_debug_images;
bool m_print_progress;
bool m_print_stats;
bool m_perceptual_error;
bool m_match_only;
bool m_two_pass;
bool m_alpha_is_opacity;
speed_mode m_speed_mode;
bool m_normal_map;
bool m_snorm8;
bool m_print_normal_map_metrics;
float m_max_smooth_std_dev;
float m_smooth_max_mse_scale;
float m_max_ultra_smooth_std_dev;
float m_ultra_smooth_max_mse_scale;
bool m_no_mse_scaling;
};
struct rdo_png_level
{
int m_num_scanlines_to_check;
uint32_t m_first_filter;
uint32_t m_last_filter;
bool m_double_width;
uint32_t m_M;
int m_search_dist;
bool m_exhaustive_search;
const uint32_t m_num_match_order_a;
const match_order* m_pMatch_order_a;
const uint32_t m_num_match_order_b;
const match_order* m_pMatch_order_b;
};
static const rdo_png_level g_levels[30] =
{
// 4 pixels wide
// 0-1
{ 1, 3, 3, false, 4, 16, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
{ 1, 3, 3, false, 4, 32, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
// 2-3
{ 2, 3, 3, false, 4, 32, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
{ 2, 3, 4, false, 4, 32, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
// 4-5
{ 2, 3, 4, false, 4, 64, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
{ 4, 3, 4, false, 4, 64, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
// 6-7
{ 4, 3, 4, false, 4, 128, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
{ 4, 3, 4, false, 4, 256, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
// 8-9
{ 6, 3, 4, false, 4, 256, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
{ 8, 3, 4, false, 4, 256, false, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
// 6 pixels wide - greater compression
// 10-11
{ 1, 3, 3, false, 6, 16, false, NUM_MATCH_ORDER_6, g_match_order6, 0, nullptr },
{ 1, 3, 4, false, 6, 32, false, NUM_MATCH_ORDER_6, g_match_order6, 0, nullptr },
// 12-13
{ 2, 3, 4, false, 6, 32, false, NUM_MATCH_ORDER_6C, g_match_order6c, 0, nullptr },
{ 4, 3, 4, false, 6, 64, false, NUM_MATCH_ORDER_6C, g_match_order6c, 0, nullptr },
// 14-15
{ 4, 3, 4, false, 6, 128, false, NUM_MATCH_ORDER_6C, g_match_order6c, 0, nullptr },
{ 4, 3, 4, false, 6, 256, false, NUM_MATCH_ORDER_6C, g_match_order6c, 0, nullptr },
// 16-17
{ 8, 3, 4, false, 6, 256, false, NUM_MATCH_ORDER_6C, g_match_order6c, 0, nullptr },
{ 8, 1, 4, false, 6, 256, false, NUM_MATCH_ORDER_6C, g_match_order6c, 0, nullptr },
// double matching, 6 or 12 pixels wide
// 18-19
{ 1, 3, 3, true, 6, 16, false, NUM_MATCH_ORDER_6, g_match_order6, NUM_MATCH_ORDER_12, g_match_order12 },
{ 1, 3, 4, true, 6, 32, false, NUM_MATCH_ORDER_6C, g_match_order6c, NUM_MATCH_ORDER_12, g_match_order12 },
// 20-21
{ 4, 3, 4, true, 6, 64, false, NUM_MATCH_ORDER_6, g_match_order6, NUM_MATCH_ORDER_12, g_match_order12 },
{ 4, 3, 4, true, 6, 128, false, NUM_MATCH_ORDER_6C, g_match_order6c, NUM_MATCH_ORDER_12, g_match_order12 },
// 22-23
{ 4, 3, 4, true, 6, 256, false, NUM_MATCH_ORDER_6C, g_match_order6c, NUM_MATCH_ORDER_12, g_match_order12 },
{ 8, 3, 4, true, 6, 256, false, NUM_MATCH_ORDER_6C, g_match_order6c, NUM_MATCH_ORDER_12, g_match_order12 },
// Exhaustive searching (for tiny images/testing)
// 24-25
{ 4, 1, 4, false, 4, 256, true, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
{ 8, 1, 4, false, 4, 256, true, NUM_MATCH_ORDER_4, g_match_order4, 0, nullptr },
// 26-27
{ 4, 1, 4, false, 6, 256, true, NUM_MATCH_ORDER_6C, g_match_order6c, 0, nullptr },
{ 8, 1, 4, false, 6, 256, true, NUM_MATCH_ORDER_6C, g_match_order6c, 0, nullptr },
// 28-29
{ 4, 1, 4, false, 6, 256, true, NUM_MATCH_ORDER_6, g_match_order6, 0, nullptr },
{ 8, 1, 4, false, 6, 256, true, NUM_MATCH_ORDER_6, g_match_order6, 0, nullptr },
};
const uint32_t MAX_LEVELS = sizeof(g_levels) / sizeof(g_levels[0]);
static const uint16_t g_tdefl_len_sym[256] = {
257,258,259,260,261,262,263,264,265,265,266,266,267,267,268,268,269,269,269,269,270,270,270,270,271,271,271,271,272,272,272,272,
273,273,273,273,273,273,273,273,274,274,274,274,274,274,274,274,275,275,275,275,275,275,275,275,276,276,276,276,276,276,276,276,
277,277,277,277,277,277,277,277,277,277,277,277,277,277,277,277,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,278,
279,279,279,279,279,279,279,279,279,279,279,279,279,279,279,279,280,280,280,280,280,280,280,280,280,280,280,280,280,280,280,280,
281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,281,
282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,282,
283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,283,
284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,284,285 };
static const uint8_t g_tdefl_len_extra[256] = {
0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,0 };
static const uint8_t g_tdefl_small_dist_sym[512] = {
0,1,2,3,4,4,5,5,6,6,6,6,7,7,7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,
11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,
13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,
14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,
14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,
15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,16,16,16,16,16,16,16,16,16,16,16,16,16,
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,
17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17,17 };
static const uint8_t g_tdefl_large_dist_sym[128] = {
0,0,18,19,20,20,21,21,22,22,22,22,23,23,23,23,24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,26,26,26,26,26,
26,26,26,26,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,28,
28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29,29 };
static const uint8_t g_tdefl_small_dist_extra[512] =
{
0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6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11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13
};
static inline float square(float f)
{
return f * f;
}
static inline uint32_t byteswap_32(uint32_t v)
{
return ((v & 0xFF) << 24) | (((v >> 8) & 0xFF) << 16) | (((v >> 16) & 0xFF) << 8) | ((v >> 24) & 0xFF);
}
class tracked_stat
{
public:
tracked_stat() { clear(); }
inline void clear() { m_num = 0; m_total = 0; m_total2 = 0; }
inline void update(uint32_t val) { m_num++; m_total += val; m_total2 += val * val; }
inline tracked_stat& operator += (uint32_t val) { update(val); return *this; }
inline uint32_t get_number_of_values() { return m_num; }
inline uint64_t get_total() const { return m_total; }
inline uint64_t get_total2() const { return m_total2; }
inline float get_average() const { return m_num ? (float)m_total / m_num : 0.0f; };
inline float get_std_dev() const { return m_num ? sqrtf((float)(m_num * m_total2 - m_total * m_total)) / m_num : 0.0f; }
inline float get_variance() const { float s = get_std_dev(); return s * s; }
private:
uint32_t m_num;
uint64_t m_total;
uint64_t m_total2;
};
static inline vec3F decode_normal(const color_rgba& c, const rdo_png_params& params)
{
if (params.m_snorm8)
{
// snomr8 - supported by GPU's. Zero can be represented exactly, two values for -1.
return vec3F(
clamp((float)(c.r - 128) * (1.0f / 127.0f), -1.0f, 1.0f),
clamp((float)(c.g - 128) * (1.0f / 127.0f), -1.0f, 1.0f),
clamp((float)(c.b - 128) * (1.0f / 127.0f), -1.0f, 1.0f));
}
else
{
// unorm8 - zero cannot be represented exactly
return vec3F((c.r * (1.0f / 255.0f)) * 2.0f - 1.0f, (c.g * (1.0f / 255.0f)) * 2.0f - 1.0f, (c.b * (1.0f / 255.0f)) * 2.0f - 1.0f);
}
}
static inline color_rgba encode_normal(const vec3F& v, int alpha, const rdo_png_params& params)
{
color_rgba result;
if (params.m_snorm8)
{
result.set((int)std::round(v[0] * 127.0f) + 128, (int)std::round(v[1] * 127.0f) + 128, (int)std::round(v[2] * 127.0f) + 128, alpha);
}
else
{
result.set(
(int)std::round(((v[0] * .5f) + .5f) * 255.0f),
(int)std::round(((v[1] * .5f) + .5f) * 255.0f),
(int)std::round(((v[2] * .5f) + .5f) * 255.0f),
alpha);
}
return result;
}
static color_rgba encode_normal_exhaustive(const vec3F& v, int alpha, const rdo_png_params& params)
{
color_rgba result;
float best_dot = -1e+9f;
color_rgba best_color(0);
for (uint32_t i = 0; i < 8; i++)
{
if (params.m_snorm8)
{
result.set(
(int)((i & 1) ? floorf : ceilf)(v[0] * 127.0f) + 128,
(int)((i & 2) ? floorf : ceilf)(v[1] * 127.0f) + 128,
(int)((i & 4) ? floorf : ceilf)(v[2] * 127.0f) + 128, alpha);
}
else
{
result.set(
(int)((i & 1) ? floorf : ceilf)(((v[0] * .5f) + .5f) * 255.0f),
(int)((i & 2) ? floorf : ceilf)(((v[1] * .5f) + .5f) * 255.0f),
(int)((i & 4) ? floorf : ceilf)(((v[2] * .5f) + .5f) * 255.0f), alpha);
color_rgba result2(
(int)((i & 1) ? floorf : ceilf)(((v[0] * .5f) + .5f) * 255.0f),
(int)((i & 2) ? floorf : ceilf)(((v[1] * .5f) + .5f) * 255.0f),
(int)((i & 4) ? floorf : ceilf)(((v[2] * .5f) + .5f) * 255.0f), alpha);
}
vec3F decoded_v(decode_normal(result, params));
decoded_v.normalize_in_place();
float dot = decoded_v.dot(v);
if (dot > best_dot)
{
best_dot = dot;
best_color = result;
}
}
return best_color;
}
static inline uint32_t compute_match_cost(uint32_t dist, uint32_t match_len_in_bytes, const huffman_encoding_table& lit_tab, const huffman_encoding_table& dist_tab)
{
assert(match_len_in_bytes >= 3 && match_len_in_bytes <= 258);
assert(dist >= 1 && dist <= 32768);
uint32_t len_sym = g_tdefl_len_sym[match_len_in_bytes - 3];
uint32_t len_cost = lit_tab.get_code_sizes()[len_sym] + g_tdefl_len_extra[match_len_in_bytes - 3];
assert(lit_tab.get_code_sizes()[len_sym]);
uint32_t adj_dist = dist - 1;
uint32_t dist_cost;
if (adj_dist < 512)
{
dist_cost = dist_tab.get_code_sizes()[g_tdefl_small_dist_sym[adj_dist]];
dist_cost += g_tdefl_small_dist_extra[adj_dist];
}
else
{
dist_cost = dist_tab.get_code_sizes()[g_tdefl_large_dist_sym[adj_dist >> 8]];
dist_cost += g_tdefl_large_dist_extra[adj_dist >> 8];
}
return len_cost + dist_cost;
}
// c b
// a x
static inline uint8_t paeth(int a, int b, int c)
{
int p = a + b - c, pa = abs(p - a), pb = abs(p - b), pc = abs(p - c);
if (pa <= pb && pa <= pc)
return (uint8_t)(a);
if (pb <= pc)
return (uint8_t)(b);
return (uint8_t)(c);
}
static inline uint8_t avg(int a, int b, int c)
{
return (uint8_t)((a + b) / 2);
}
static inline color_rgba png_predict(const color_rgba& trial_c, uint32_t x, uint32_t y, const image& coded_img, uint32_t filter, uint32_t num_comps)
{
assert(filter);
const color_rgba ca(x ? coded_img(x - 1, y) : g_black_color);
const color_rgba cb(y ? coded_img(x, y - 1) : g_black_color);
const color_rgba cc((x && y) ? coded_img(x - 1, y - 1) : g_black_color);
color_rgba res;
for (uint32_t c = 0; c < num_comps; c++)
{
uint32_t pa = ca[c];
uint32_t pb = cb[c];
uint32_t pc = cc[c];
uint32_t d;
if (filter == PNG_PAETH_FILTER)
d = paeth(pa, pb, pc);
else if (filter == PNG_AVG_FILTER)
d = avg(pa, pb, pc);
else if (filter == PNG_PREV_SCANLINE_FILTER)
d = pb;
else
{
assert(filter == PNG_PREV_PIXEL_FILTER);
d = pa;
}
res[c] = (uint8_t)(trial_c[c] - d);
}
if (num_comps == 3)
res[3] = 255;
return res;
}
static inline color_rgba png_unpredict(const color_rgba& delta_c, uint32_t x, uint32_t y, const image& coded_img, uint32_t filter, uint32_t num_comps)
{
color_rgba res;
const color_rgba ca(x ? coded_img(x - 1, y) : g_black_color);
const color_rgba cb(y ? coded_img(x, y - 1) : g_black_color);
const color_rgba cc((x && y) ? coded_img(x - 1, y - 1) : g_black_color);
for (uint32_t c = 0; c < num_comps; c++)
{
uint32_t pa = ca[c];
uint32_t pb = cb[c];
uint32_t pc = cc[c];
uint32_t d;
if (filter == 4)
d = paeth(pa, pb, pc);
else if (filter == 3)
d = avg(pa, pb, pc);
else if (filter == 2)
d = pb;
else
{
assert(filter == PNG_PREV_PIXEL_FILTER);
d = pa;
}
res[c] = (uint8_t)(delta_c[c] + d);
}
if (num_comps == 3)
res[3] = 255;
return res;
}
struct Lab { float L; float a; float b; };
struct RGB { float r; float g; float b; };
static inline Lab linear_srgb_to_oklab(RGB c)
{
float l = 0.4122214708f * c.r + 0.5363325363f * c.g + 0.0514459929f * c.b;
float m = 0.2119034982f * c.r + 0.6806995451f * c.g + 0.1073969566f * c.b;
float s = 0.0883024619f * c.r + 0.2817188376f * c.g + 0.6299787005f * c.b;
float l_ = std::cbrtf(l);
float m_ = std::cbrtf(m);
float s_ = std::cbrtf(s);
return
{
0.2104542553f * l_ + 0.7936177850f * m_ - 0.0040720468f * s_,
1.9779984951f * l_ - 2.4285922050f * m_ + 0.4505937099f * s_,
0.0259040371f * l_ + 0.7827717662f * m_ - 0.8086757660f * s_,
};
}
static float g_srgb_to_linear[256];
static float f_inv(float x)
{
if (x <= 0.04045f)
return x / 12.92f;
else
return powf(((x + 0.055f) / 1.055f), 2.4f);
}
static void init_srgb_to_linear()
{
for (uint32_t i = 0; i < 256; i++)
g_srgb_to_linear[i] = f_inv(i / 255.0f);
}
#pragma pack(push, 1)
struct Lab16
{
uint16_t m_L, m_a, m_b;
};
#pragma pack(pop)
basisu::vector<Lab16> g_srgb_to_oklab16;
const float SCALE_L = 1.0f / 65535.0f;
const float SCALE_A = (1.0f / 65535.0f) * (0.276216f - (-0.233887f));
const float OFS_A = -0.233887f;
const float SCALE_B = (1.0f / 65535.0f) * (0.198570f - (-0.311528f));
const float OFS_B = -0.311528f;
const float MIN_L = 0.000000f, MAX_L = 1.000000f;
const float MIN_A = -0.233888f, MAX_A = 0.276217f;
const float MIN_B = -0.311529f, MAX_B = 0.198570f;
static inline Lab srgb_to_oklab(const color_rgba &c)
{
const Lab16 &l = g_srgb_to_oklab16[c.r + c.g * 256 + c.b * 65536];
Lab res;
res.L = l.m_L * SCALE_L;
res.a = l.m_a * SCALE_A + OFS_A;
res.b = l.m_b * SCALE_B + OFS_B;
return res;
}
static inline Lab srgb_to_oklab_norm(const color_rgba& c)
{
const Lab16& l = g_srgb_to_oklab16[c.r + c.g * 256 + c.b * 65536];
Lab res;
res.L = l.m_L * SCALE_L;
res.a = l.m_a * SCALE_L;
res.b = l.m_b * SCALE_L;
return res;
}
static void init_oklab_table(const char *pExec, bool quiet, bool caching_enabled)
{
g_srgb_to_oklab16.resize(256 * 256 * 256);
std::string path(pExec);
if (caching_enabled)
{
string_get_pathname(pExec, path);
path += "oklab.bin";
uint8_vec file_data;
if (read_file_to_vec(path.c_str(), file_data))
{
if (file_data.size() == 256 * 256 * 256 * 6)
{
memcpy(g_srgb_to_oklab16.data(), file_data.data(), file_data.size_in_bytes());
if (!quiet)
printf("Read Oklab table data from file %s\n", path.c_str());
return;
}
}
}
if (!quiet)
printf("Computing Oklab table\n");
for (uint32_t r = 0; r <= 255; r++)
{
//printf("%u\n", r);
for (uint32_t g = 0; g <= 255; g++)
{
for (uint32_t b = 0; b <= 255; b++)
{
color_rgba c(r, g, b, 255);
Lab l(linear_srgb_to_oklab({ g_srgb_to_linear[c.r], g_srgb_to_linear[c.g], g_srgb_to_linear[c.b] }));
assert(l.L >= MIN_L && l.L <= MAX_L);
assert(l.a >= MIN_A && l.a <= MAX_A);
assert(l.b >= MIN_B && l.b <= MAX_B);
float lL = std::round(((l.L - MIN_L) / (MAX_L - MIN_L)) * 65535.0f);
float la = std::round(((l.a - MIN_A) / (MAX_A - MIN_A)) * 65535.0f);
float lb = std::round(((l.b - MIN_B) / (MAX_B - MIN_B)) * 65535.0f);
lL = clamp(lL, 0.0f, 65535.0f);
la = clamp(la, 0.0f, 65535.0f);
lb = clamp(lb, 0.0f, 65535.0f);
Lab16& v = g_srgb_to_oklab16[r + g * 256 + b * 65536];
v.m_L = (uint16_t)lL;
v.m_a = (uint16_t)la;
v.m_b = (uint16_t)lb;
Lab cl = srgb_to_oklab(c);
//printf("%f %f %f, %f %f %f\n", l.L, l.a, l.b, cl.L, cl.a, cl.b);
}
}