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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
// basis_etc.h
// Copyright (C) 2019 Binomial LLC. 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.
#pragma once
#include "transcoder/basisu.h"
#include "basisu_enc.h"
#include <set>

namespace basisu
{
	enum etc_constants
	{
		cETC1BytesPerBlock = 8U,

		cETC1SelectorBits = 2U,
		cETC1SelectorValues = 1U << cETC1SelectorBits,
		cETC1SelectorMask = cETC1SelectorValues - 1U,

		cETC1BlockShift = 2U,
		cETC1BlockSize = 1U << cETC1BlockShift,

		cETC1LSBSelectorIndicesBitOffset = 0,
		cETC1MSBSelectorIndicesBitOffset = 16,

		cETC1FlipBitOffset = 32,
		cETC1DiffBitOffset = 33,

		cETC1IntenModifierNumBits = 3,
		cETC1IntenModifierValues = 1 << cETC1IntenModifierNumBits,
		cETC1RightIntenModifierTableBitOffset = 34,
		cETC1LeftIntenModifierTableBitOffset = 37,

		// Base+Delta encoding (5 bit bases, 3 bit delta)
		cETC1BaseColorCompNumBits = 5,
		cETC1BaseColorCompMax = 1 << cETC1BaseColorCompNumBits,

		cETC1DeltaColorCompNumBits = 3,
		cETC1DeltaColorComp = 1 << cETC1DeltaColorCompNumBits,
		cETC1DeltaColorCompMax = 1 << cETC1DeltaColorCompNumBits,

		cETC1BaseColor5RBitOffset = 59,
		cETC1BaseColor5GBitOffset = 51,
		cETC1BaseColor5BBitOffset = 43,

		cETC1DeltaColor3RBitOffset = 56,
		cETC1DeltaColor3GBitOffset = 48,
		cETC1DeltaColor3BBitOffset = 40,

		// Absolute (non-delta) encoding (two 4-bit per component bases)
		cETC1AbsColorCompNumBits = 4,
		cETC1AbsColorCompMax = 1 << cETC1AbsColorCompNumBits,

		cETC1AbsColor4R1BitOffset = 60,
		cETC1AbsColor4G1BitOffset = 52,
		cETC1AbsColor4B1BitOffset = 44,

		cETC1AbsColor4R2BitOffset = 56,
		cETC1AbsColor4G2BitOffset = 48,
		cETC1AbsColor4B2BitOffset = 40,

		cETC1ColorDeltaMin = -4,
		cETC1ColorDeltaMax = 3,

		// Delta3:
		// 0   1   2   3   4   5   6   7
		// 000 001 010 011 100 101 110 111
		// 0   1   2   3   -4  -3  -2  -1
	};
	
	extern const int g_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues];
	extern const uint8_t g_etc1_to_selector_index[cETC1SelectorValues];
	extern const uint8_t g_selector_index_to_etc1[cETC1SelectorValues];

	struct etc_coord2
	{
		uint8_t m_x, m_y;
	};
	extern const etc_coord2 g_etc1_pixel_coords[2][2][8]; // [flipped][subblock][subblock_pixel]
	extern const uint32_t g_etc1_pixel_indices[2][2][8]; // [flipped][subblock][subblock_pixel]

	struct etc_block
	{
		// big endian uint64:
		// bit ofs:  56  48  40  32  24  16   8   0
		// byte ofs: b0, b1, b2, b3, b4, b5, b6, b7 
		union
		{
			uint64_t m_uint64;

			uint8_t m_bytes[8];
		};

		inline void clear()
		{
			assert(sizeof(*this) == 8);
			clear_obj(*this);
		}

		inline uint64_t get_all_bits() const
		{
			return read_be64(&m_uint64);
		}

		inline uint32_t get_general_bits(uint32_t ofs, uint32_t num) const
		{
			assert((ofs + num) <= 64U);
			assert(num && (num < 32U));
			return (read_be64(&m_uint64) >> ofs) & ((1UL << num) - 1UL);
		}

		inline void set_general_bits(uint32_t ofs, uint32_t num, uint32_t bits)
		{
			assert((ofs + num) <= 64U);
			assert(num && (num < 32U));

			uint64_t x = read_be64(&m_uint64);
			uint64_t msk = ((1ULL << static_cast<uint64_t>(num)) - 1ULL) << static_cast<uint64_t>(ofs);
			x &= ~msk;
			x |= (static_cast<uint64_t>(bits) << static_cast<uint64_t>(ofs));
			write_be64(&m_uint64, x);
		}

		inline uint32_t get_byte_bits(uint32_t ofs, uint32_t num) const
		{
			assert((ofs + num) <= 64U);
			assert(num && (num <= 8U));
			assert((ofs >> 3) == ((ofs + num - 1) >> 3));
			const uint32_t byte_ofs = 7 - (ofs >> 3);
			const uint32_t byte_bit_ofs = ofs & 7;
			return (m_bytes[byte_ofs] >> byte_bit_ofs) & ((1 << num) - 1);
		}

		inline void set_byte_bits(uint32_t ofs, uint32_t num, uint32_t bits)
		{
			assert((ofs + num) <= 64U);
			assert(num && (num < 32U));
			assert((ofs >> 3) == ((ofs + num - 1) >> 3));
			assert(bits < (1U << num));
			const uint32_t byte_ofs = 7 - (ofs >> 3);
			const uint32_t byte_bit_ofs = ofs & 7;
			const uint32_t mask = (1 << num) - 1;
			m_bytes[byte_ofs] &= ~(mask << byte_bit_ofs);
			m_bytes[byte_ofs] |= (bits << byte_bit_ofs);
		}

		// false = left/right subblocks
		// true = upper/lower subblocks
		inline bool get_flip_bit() const
		{
			return (m_bytes[3] & 1) != 0;
		}

		inline void set_flip_bit(bool flip)
		{
			m_bytes[3] &= ~1;
			m_bytes[3] |= static_cast<uint8_t>(flip);
		}

		inline bool get_diff_bit() const
		{
			return (m_bytes[3] & 2) != 0;
		}

		inline void set_diff_bit(bool diff)
		{
			m_bytes[3] &= ~2;
			m_bytes[3] |= (static_cast<uint32_t>(diff) << 1);
		}

		// Returns intensity modifier table (0-7) used by subblock subblock_id.
		// subblock_id=0 left/top (CW 1), 1=right/bottom (CW 2)
		inline uint32_t get_inten_table(uint32_t subblock_id) const
		{
			assert(subblock_id < 2);
			const uint32_t ofs = subblock_id ? 2 : 5;
			return (m_bytes[3] >> ofs) & 7;
		}

		// Sets intensity modifier table (0-7) used by subblock subblock_id (0 or 1)
		inline void set_inten_table(uint32_t subblock_id, uint32_t t)
		{
			assert(subblock_id < 2);
			assert(t < 8);
			const uint32_t ofs = subblock_id ? 2 : 5;
			m_bytes[3] &= ~(7 << ofs);
			m_bytes[3] |= (t << ofs);
		}

		inline void set_inten_tables_etc1s(uint32_t t)
		{
			set_inten_table(0, t);
			set_inten_table(1, t);
		}

		inline bool is_etc1s() const
		{
			if (get_inten_table(0) != get_inten_table(1))
				return false;

			if (get_diff_bit())
			{
				if (get_delta3_color() != 0)
					return false;
			}
			else
			{
				if (get_base4_color(0) != get_base4_color(1))
					return false;
			}

			return true;
		}

		// Returned encoded selector value ranges from 0-3 (this is NOT a direct index into g_etc1_inten_tables, see get_selector())
		inline uint32_t get_raw_selector(uint32_t x, uint32_t y) const
		{
			assert((x | y) < 4);

			const uint32_t bit_index = x * 4 + y;
			const uint32_t byte_bit_ofs = bit_index & 7;
			const uint8_t *p = &m_bytes[7 - (bit_index >> 3)];
			const uint32_t lsb = (p[0] >> byte_bit_ofs) & 1;
			const uint32_t msb = (p[-2] >> byte_bit_ofs) & 1;
			const uint32_t val = lsb | (msb << 1);

			return val;
		}

		// Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
		inline uint32_t get_selector(uint32_t x, uint32_t y) const
		{
			return g_etc1_to_selector_index[get_raw_selector(x, y)];
		}

		// Selector "val" ranges from 0-3 and is a direct index into g_etc1_inten_tables.
		inline void set_selector(uint32_t x, uint32_t y, uint32_t val)
		{
			assert((x | y | val) < 4);
			const uint32_t bit_index = x * 4 + y;

			uint8_t *p = &m_bytes[7 - (bit_index >> 3)];

			const uint32_t byte_bit_ofs = bit_index & 7;
			const uint32_t mask = 1 << byte_bit_ofs;

			const uint32_t etc1_val = g_selector_index_to_etc1[val];

			const uint32_t lsb = etc1_val & 1;
			const uint32_t msb = etc1_val >> 1;

			p[0] &= ~mask;
			p[0] |= (lsb << byte_bit_ofs);

			p[-2] &= ~mask;
			p[-2] |= (msb << byte_bit_ofs);
		}

		inline uint32_t get_raw_selector_bits() const
		{
			return m_bytes[4] | (m_bytes[5] << 8) | (m_bytes[6] << 16) | (m_bytes[7] << 24);
		}

		inline void set_raw_selector_bits(uint32_t bits)
		{
			m_bytes[4] = static_cast<uint8_t>(bits);
			m_bytes[5] = static_cast<uint8_t>(bits >> 8);
			m_bytes[6] = static_cast<uint8_t>(bits >> 16);
			m_bytes[7] = static_cast<uint8_t>(bits >> 24);
		}

		inline void set_raw_selector_bits(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3)
		{
			m_bytes[4] = byte0;
			m_bytes[5] = byte1;
			m_bytes[6] = byte2;
			m_bytes[7] = byte3;
		}

		inline void set_base4_color(uint32_t idx, uint16_t c)
		{
			if (idx)
			{
				set_byte_bits(cETC1AbsColor4R2BitOffset, 4, (c >> 8) & 15);
				set_byte_bits(cETC1AbsColor4G2BitOffset, 4, (c >> 4) & 15);
				set_byte_bits(cETC1AbsColor4B2BitOffset, 4, c & 15);
			}
			else
			{
				set_byte_bits(cETC1AbsColor4R1BitOffset, 4, (c >> 8) & 15);
				set_byte_bits(cETC1AbsColor4G1BitOffset, 4, (c >> 4) & 15);
				set_byte_bits(cETC1AbsColor4B1BitOffset, 4, c & 15);
			}
		}

		inline uint16_t get_base4_color(uint32_t idx) const
		{
			uint32_t r, g, b;
			if (idx)
			{
				r = get_byte_bits(cETC1AbsColor4R2BitOffset, 4);
				g = get_byte_bits(cETC1AbsColor4G2BitOffset, 4);
				b = get_byte_bits(cETC1AbsColor4B2BitOffset, 4);
			}
			else
			{
				r = get_byte_bits(cETC1AbsColor4R1BitOffset, 4);
				g = get_byte_bits(cETC1AbsColor4G1BitOffset, 4);
				b = get_byte_bits(cETC1AbsColor4B1BitOffset, 4);
			}
			return static_cast<uint16_t>(b | (g << 4U) | (r << 8U));
		}

		inline void set_base5_color(uint16_t c)
		{
			set_byte_bits(cETC1BaseColor5RBitOffset, 5, (c >> 10) & 31);
			set_byte_bits(cETC1BaseColor5GBitOffset, 5, (c >> 5) & 31);
			set_byte_bits(cETC1BaseColor5BBitOffset, 5, c & 31);
		}

		inline uint16_t get_base5_color() const
		{
			const uint32_t r = get_byte_bits(cETC1BaseColor5RBitOffset, 5);
			const uint32_t g = get_byte_bits(cETC1BaseColor5GBitOffset, 5);
			const uint32_t b = get_byte_bits(cETC1BaseColor5BBitOffset, 5);
			return static_cast<uint16_t>(b | (g << 5U) | (r << 10U));
		}

		void set_delta3_color(uint16_t c)
		{
			set_byte_bits(cETC1DeltaColor3RBitOffset, 3, (c >> 6) & 7);
			set_byte_bits(cETC1DeltaColor3GBitOffset, 3, (c >> 3) & 7);
			set_byte_bits(cETC1DeltaColor3BBitOffset, 3, c & 7);
		}

		inline uint16_t get_delta3_color() const
		{
			const uint32_t r = get_byte_bits(cETC1DeltaColor3RBitOffset, 3);
			const uint32_t g = get_byte_bits(cETC1DeltaColor3GBitOffset, 3);
			const uint32_t b = get_byte_bits(cETC1DeltaColor3BBitOffset, 3);
			return static_cast<uint16_t>(b | (g << 3U) | (r << 6U));
		}

		uint64_t determine_selectors(const color_rgba* pSource_pixels, bool perceptual, uint32_t begin_subblock = 0, uint32_t end_subblock = 2)
		{
			uint64_t total_error = 0;

			for (uint32_t subblock = begin_subblock; subblock < end_subblock; subblock++)
			{
				color_rgba block_colors[4];
				get_block_colors(block_colors, subblock);

				if (get_flip_bit())
				{
					for (uint32_t y = 0; y < 2; y++)
					{
						for (uint32_t x = 0; x < 4; x++)
						{
							uint32_t best_selector = 0;
							uint64_t best_error = UINT64_MAX;

							for (uint32_t s = 0; s < 4; s++)
							{
								uint64_t err = color_distance(perceptual, block_colors[s], pSource_pixels[x + (subblock * 2 + y) * 4], false);
								if (err < best_error)
								{
									best_error = err;
									best_selector = s;
								}
							}

							set_selector(x, subblock * 2 + y, best_selector);

							total_error += best_error;
						}
					}
				}
				else
				{
					for (uint32_t y = 0; y < 4; y++)
					{
						for (uint32_t x = 0; x < 2; x++)
						{
							uint32_t best_selector = 0;
							uint64_t best_error = UINT64_MAX;

							for (uint32_t s = 0; s < 4; s++)
							{
								uint64_t err = color_distance(perceptual, block_colors[s], pSource_pixels[(subblock * 2) + x + y * 4], false);
								if (err < best_error)
								{
									best_error = err;
									best_selector = s;
								}
							}

							set_selector(subblock * 2 + x, y, best_selector);

							total_error += best_error;
						}
					}
				}
			}

			return total_error;
		}

		color_rgba get_block_color(uint32_t subblock_index, bool scaled) const
		{
			color_rgba b;

			if (get_diff_bit())
			{
				if (subblock_index)
					unpack_color5(b, get_base5_color(), get_delta3_color(), scaled);
				else
					unpack_color5(b, get_base5_color(), scaled);
			}
			else
			{
				b = unpack_color4(get_base4_color(subblock_index), scaled);
			}

			return b;
		}

		uint32_t get_subblock_index(uint32_t x, uint32_t y) const
		{
			if (get_flip_bit())
				return y >= 2;
			else
				return x >= 2;
		}

		bool get_block_colors(color_rgba* pBlock_colors, uint32_t subblock_index) const
		{
			color_rgba b;

			if (get_diff_bit())
			{
				if (subblock_index)
					unpack_color5(b, get_base5_color(), get_delta3_color(), true);
				else
					unpack_color5(b, get_base5_color(), true);
			}
			else
			{
				b = unpack_color4(get_base4_color(subblock_index), true);
			}

			const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];

			bool dc = false;

			pBlock_colors[0].set(clamp255(b.r + pInten_table[0], dc), clamp255(b.g + pInten_table[0], dc), clamp255(b.b + pInten_table[0], dc), 255);
			pBlock_colors[1].set(clamp255(b.r + pInten_table[1], dc), clamp255(b.g + pInten_table[1], dc), clamp255(b.b + pInten_table[1], dc), 255);
			pBlock_colors[2].set(clamp255(b.r + pInten_table[2], dc), clamp255(b.g + pInten_table[2], dc), clamp255(b.b + pInten_table[2], dc), 255);
			pBlock_colors[3].set(clamp255(b.r + pInten_table[3], dc), clamp255(b.g + pInten_table[3], dc), clamp255(b.b + pInten_table[3], dc), 255);

			return dc;
		}

		void get_block_color(color_rgba& color, uint32_t subblock_index, uint32_t selector_index) const
		{
			color_rgba b;

			if (get_diff_bit())
			{
				if (subblock_index)
					unpack_color5(b, get_base5_color(), get_delta3_color(), true);
				else
					unpack_color5(b, get_base5_color(), true);
			}
			else
			{
				b = unpack_color4(get_base4_color(subblock_index), true);
			}

			const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];

			color.set(clamp255(b.r + pInten_table[selector_index]), clamp255(b.g + pInten_table[selector_index]), clamp255(b.b + pInten_table[selector_index]), 255);
		}

		bool get_block_low_high_colors(color_rgba* pBlock_colors, uint32_t subblock_index) const
		{
			color_rgba b;

			if (get_diff_bit())
			{
				if (subblock_index)
					unpack_color5(b, get_base5_color(), get_delta3_color(), true);
				else
					unpack_color5(b, get_base5_color(), true);
			}
			else
			{
				b = unpack_color4(get_base4_color(subblock_index), true);
			}

			const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];

			bool dc = false;

			pBlock_colors[0].set(clamp255(b.r + pInten_table[0], dc), clamp255(b.g + pInten_table[0], dc), clamp255(b.b + pInten_table[0], dc), 255);
			pBlock_colors[1].set(clamp255(b.r + pInten_table[3], dc), clamp255(b.g + pInten_table[3], dc), clamp255(b.b + pInten_table[3], dc), 255);

			return dc;
		}

		static void get_block_colors5(color_rgba *pBlock_colors, const color_rgba &base_color5, uint32_t inten_table, bool scaled = false)
		{
			color_rgba b(base_color5);

			if (!scaled)
			{
				b.r = (b.r << 3) | (b.r >> 2);
				b.g = (b.g << 3) | (b.g >> 2);
				b.b = (b.b << 3) | (b.b >> 2);
			}

			const int* pInten_table = g_etc1_inten_tables[inten_table];

			pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
			pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
			pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
			pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
		}

		static void get_block_colors4(color_rgba *pBlock_colors, const color_rgba &base_color4, uint32_t inten_table, bool scaled = false)
		{
			color_rgba b(base_color4);

			if (!scaled)
			{
				b.r = (b.r << 4) | b.r;
				b.g = (b.g << 4) | b.g;
				b.b = (b.b << 4) | b.b;
			}

			const int* pInten_table = g_etc1_inten_tables[inten_table];

			pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
			pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
			pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
			pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
		}

		uint64_t evaluate_etc1_error(const color_rgba* pBlock_pixels, bool perceptual, int subblock_index = -1) const;
		void get_subblock_pixels(color_rgba* pPixels, int subblock_index = -1) const;

		void get_selector_range(uint32_t& low, uint32_t& high) const
		{
			low = 3;
			high = 0;
			for (uint32_t y = 0; y < 4; y++)
			{
				for (uint32_t x = 0; x < 4; x++)
				{
					const uint32_t s = get_selector(x, y);
					low = minimum(low, s);
					high = maximum(high, s);
				}
			}
		}

		void set_block_color4(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
		{
			set_diff_bit(false);

			set_base4_color(0, pack_color4(c0_unscaled, false));
			set_base4_color(1, pack_color4(c1_unscaled, false));
		}

		void set_block_color5(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
		{
			set_diff_bit(true);

			set_base5_color(pack_color5(c0_unscaled, false));

			int dr = c1_unscaled.r - c0_unscaled.r;
			int dg = c1_unscaled.g - c0_unscaled.g;
			int db = c1_unscaled.b - c0_unscaled.b;

			set_delta3_color(pack_delta3(dr, dg, db));
		}

		void set_block_color5_etc1s(const color_rgba &c_unscaled)
		{
			set_diff_bit(true);
			
			set_base5_color(pack_color5(c_unscaled, false));
			set_delta3_color(pack_delta3(0, 0, 0));
		}

		bool set_block_color5_check(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
		{
			set_diff_bit(true);

			set_base5_color(pack_color5(c0_unscaled, false));

			int dr = c1_unscaled.r - c0_unscaled.r;
			int dg = c1_unscaled.g - c0_unscaled.g;
			int db = c1_unscaled.b - c0_unscaled.b;

			if (((dr < cETC1ColorDeltaMin) || (dr > cETC1ColorDeltaMax)) ||
				((dg < cETC1ColorDeltaMin) || (dg > cETC1ColorDeltaMax)) ||
				((db < cETC1ColorDeltaMin) || (db > cETC1ColorDeltaMax)))
				return false;

			set_delta3_color(pack_delta3(dr, dg, db));

			return true;
		}

		color_rgba get_selector_color(uint32_t x, uint32_t y, uint32_t s) const
		{
			color_rgba block_colors[4];

			get_block_colors(block_colors, get_subblock_index(x, y));

			return block_colors[s];
		}

		// Base color 5
		static uint16_t pack_color5(const color_rgba& color, bool scaled, uint32_t bias = 127U);
		static uint16_t pack_color5(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias = 127U);

		static color_rgba unpack_color5(uint16_t packed_color5, bool scaled, uint32_t alpha = 255U);
		static void unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color, bool scaled);
		static void unpack_color5(color_rgba& result, uint16_t packed_color5, bool scaled);

		static bool unpack_color5(color_rgba& result, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha = 255U);
		static bool unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha = 255U);

		// Delta color 3
		// Inputs range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
		static uint16_t pack_delta3(const color_rgba_i16& color);
		static uint16_t pack_delta3(int r, int g, int b);

		// Results range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
		static color_rgba_i16 unpack_delta3(uint16_t packed_delta3);
		static void unpack_delta3(int& r, int& g, int& b, uint16_t packed_delta3);

		static bool try_pack_color5_delta3(const color_rgba *pColor5_unscaled)
		{
			int dr = pColor5_unscaled[1].r - pColor5_unscaled[0].r;
			int dg = pColor5_unscaled[1].g - pColor5_unscaled[0].g;
			int db = pColor5_unscaled[1].b - pColor5_unscaled[0].b;

			if ((minimum(dr, dg, db) < cETC1ColorDeltaMin) || (maximum(dr, dg, db) > cETC1ColorDeltaMax))
				return false;

			return true;
		}

		// Abs color 4
		static uint16_t pack_color4(const color_rgba& color, bool scaled, uint32_t bias = 127U);
		static uint16_t pack_color4(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias = 127U);

		static color_rgba unpack_color4(uint16_t packed_color4, bool scaled, uint32_t alpha = 255U);
		static void unpack_color4(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color4, bool scaled);

		// subblock colors
		static void get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint32_t table_idx);
		static bool get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint16_t packed_delta3, uint32_t table_idx);
		static void get_abs_subblock_colors(color_rgba* pDst, uint16_t packed_color4, uint32_t table_idx);

		static inline void unscaled_to_scaled_color(color_rgba& dst, const color_rgba& src, bool color4)
		{
			if (color4)
			{
				dst.r = src.r | (src.r << 4);
				dst.g = src.g | (src.g << 4);
				dst.b = src.b | (src.b << 4);
			}
			else
			{
				dst.r = (src.r >> 2) | (src.r << 3);
				dst.g = (src.g >> 2) | (src.g << 3);
				dst.b = (src.b >> 2) | (src.b << 3);
			}
			dst.a = src.a;
		}

	private:
		static uint8_t clamp255(int x, bool &did_clamp)
		{
			if (x < 0)
			{
				did_clamp = true;
				return 0;
			}
			else if (x > 255)
			{
				did_clamp = true;
				return 255;
			}

			return static_cast<uint8_t>(x);
		}

		static uint8_t clamp255(int x)
		{
			if (x < 0)
				return 0;
			else if (x > 255)
				return 255;

			return static_cast<uint8_t>(x);
		}
	};
		
	typedef std::vector<etc_block> etc_block_vec;

	// Returns false if the unpack fails (could be bogus data or ETC2)
	bool unpack_etc1(const etc_block& block, color_rgba *pDst, bool preserve_alpha = false);
		
	enum basis_etc_quality
	{
		cETCQualityFast,
		cETCQualityMedium,
		cETCQualitySlow,
		cETCQualityUber,
		cETCQualityTotal,
	};

	struct basis_etc1_pack_params
	{
		basis_etc_quality m_quality;
		bool m_perceptual;
		bool m_cluster_fit;
		bool m_force_etc1s;
		bool m_use_color4;
		float m_flip_bias;

		inline basis_etc1_pack_params()
		{
			clear();
		}

		void clear()
		{
			m_quality = cETCQualitySlow;
			m_perceptual = true;
			m_cluster_fit = true;
			m_force_etc1s = false;
			m_use_color4 = true;
			m_flip_bias = 0.0f;
		}
	};

	struct etc1_solution_coordinates
	{
		inline etc1_solution_coordinates() :
			m_unscaled_color(0, 0, 0, 0),
			m_inten_table(0),
			m_color4(false)
		{
		}

		inline etc1_solution_coordinates(uint32_t r, uint32_t g, uint32_t b, uint32_t inten_table, bool color4) :
			m_unscaled_color((uint8_t)r, (uint8_t)g, (uint8_t)b, 255),
			m_inten_table((uint8_t)inten_table),
			m_color4(color4)
		{
		}

		inline etc1_solution_coordinates(const color_rgba& c, uint32_t inten_table, bool color4) :
			m_unscaled_color(c),
			m_inten_table(inten_table),
			m_color4(color4)
		{
		}

		inline etc1_solution_coordinates(const etc1_solution_coordinates& other)
		{
			*this = other;
		}

		inline etc1_solution_coordinates& operator= (const etc1_solution_coordinates& rhs)
		{
			m_unscaled_color = rhs.m_unscaled_color;
			m_inten_table = rhs.m_inten_table;
			m_color4 = rhs.m_color4;
			return *this;
		}

		inline void clear()
		{
			m_unscaled_color.clear();
			m_inten_table = 0;
			m_color4 = false;
		}

		inline void init(const color_rgba& c, uint32_t inten_table, bool color4)
		{
			m_unscaled_color = c;
			m_inten_table = inten_table;
			m_color4 = color4;
		}

		inline color_rgba get_scaled_color() const
		{
			int br, bg, bb;
			if (m_color4)
			{
				br = m_unscaled_color.r | (m_unscaled_color.r << 4);
				bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
				bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
			}
			else
			{
				br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
				bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
				bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
			}
			return color_rgba((uint8_t)br, (uint8_t)bg, (uint8_t)bb, 255);
		}

		// returns true if anything was clamped
		inline void get_block_colors(color_rgba* pBlock_colors)
		{
			int br, bg, bb;
			if (m_color4)
			{
				br = m_unscaled_color.r | (m_unscaled_color.r << 4);
				bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
				bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
			}
			else
			{
				br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
				bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
				bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
			}
			const int* pInten_table = g_etc1_inten_tables[m_inten_table];
			pBlock_colors[0].set((uint8_t)(br + pInten_table[0]), (uint8_t)(bg + pInten_table[0]), (uint8_t)(bb + pInten_table[0]), 255);
			pBlock_colors[1].set((uint8_t)(br + pInten_table[1]), (uint8_t)(bg + pInten_table[1]), (uint8_t)(bb + pInten_table[1]), 255);
			pBlock_colors[2].set((uint8_t)(br + pInten_table[2]), (uint8_t)(bg + pInten_table[2]), (uint8_t)(bb + pInten_table[2]), 255);
			pBlock_colors[3].set((uint8_t)(br + pInten_table[3]), (uint8_t)(bg + pInten_table[3]), (uint8_t)(bb + pInten_table[3]), 255);
		}

		color_rgba m_unscaled_color;
		uint32_t m_inten_table;
		bool m_color4;
	};

	class etc1_optimizer
	{
		BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(etc1_optimizer);

	public:
		etc1_optimizer()<--- Member variable 'etc1_optimizer::m_limit' is not initialized in the constructor.<--- Member variable 'etc1_optimizer::m_br' is not initialized in the constructor.<--- Member variable 'etc1_optimizer::m_bg' is not initialized in the constructor.<--- Member variable 'etc1_optimizer::m_bb' is not initialized in the constructor.
		{
			clear();
		}

		void clear()
		{
			m_pParams = nullptr;
			m_pResult = nullptr;
			m_pSorted_luma = nullptr;
			m_pSorted_luma_indices = nullptr;
		}

		struct params;

		typedef bool(*evaluate_solution_override_func)(uint64_t &error, const params &p, const color_rgba* pBlock_colors, const uint8_t* pSelectors, const etc1_solution_coordinates& coords);

		struct params : basis_etc1_pack_params
		{
			params()
			{
				clear();
			}

			params(const basis_etc1_pack_params& base_params)
			{
				clear_optimizer_params();

				*static_cast<basis_etc1_pack_params *>(this) = base_params;
			}

			void clear()
			{
				clear_optimizer_params();
			}

			void clear_optimizer_params()
			{
				basis_etc1_pack_params::clear();

				m_num_src_pixels = 0;
				m_pSrc_pixels = 0;

				m_use_color4 = false;
				static const int s_default_scan_delta[] = { 0 };
				m_pScan_deltas = s_default_scan_delta;
				m_scan_delta_size = 1;

				m_base_color5.clear();
				m_constrain_against_base_color5 = false;

				m_refinement = true;

				m_pForce_selectors = nullptr;

				m_pEval_solution_override = nullptr;
				m_pEval_solution_override_data = nullptr;
			}

			uint32_t m_num_src_pixels;
			const color_rgba* m_pSrc_pixels;

			bool m_use_color4;
			const int* m_pScan_deltas;
			uint32_t m_scan_delta_size;

			color_rgba m_base_color5;
			bool m_constrain_against_base_color5;

			bool m_refinement;

			const uint8_t* m_pForce_selectors;

			evaluate_solution_override_func m_pEval_solution_override;
			void *m_pEval_solution_override_data;
		};

		struct results
		{
			uint64_t m_error;
			color_rgba m_block_color_unscaled;
			uint32_t m_block_inten_table;
			uint32_t m_n;
			uint8_t* m_pSelectors;
			bool m_block_color4;

			inline results& operator= (const results& rhs)
			{
				m_block_color_unscaled = rhs.m_block_color_unscaled;
				m_block_color4 = rhs.m_block_color4;
				m_block_inten_table = rhs.m_block_inten_table;
				m_error = rhs.m_error;
				memcpy(m_pSelectors, rhs.m_pSelectors, minimum(rhs.m_n, m_n));
				return *this;
			}
		};

		void init(const params& params, results& result);
		bool compute();

		const params* get_params() const { return m_pParams; }

	private:
		struct potential_solution
		{
			potential_solution() : m_coords(), m_error(UINT64_MAX), m_valid(false)
			{
			}

			etc1_solution_coordinates  m_coords;
			std::vector<uint8_t>    m_selectors;
			uint64_t                     m_error;
			bool                       m_valid;

			void clear()
			{
				m_coords.clear();
				m_selectors.resize(0);
				m_error = UINT64_MAX;
				m_valid = false;
			}

			bool are_selectors_all_equal() const
			{
				if (!m_selectors.size())
					return false;
				const uint32_t s = m_selectors[0];
				for (uint32_t i = 1; i < m_selectors.size(); i++)
					if (m_selectors[i] != s)
						return false;
				return true;
			}
		};

		const params* m_pParams;
		results* m_pResult;

		int m_limit;

		vec3F m_avg_color;
		int m_br, m_bg, m_bb;
		std::vector<uint16_t> m_luma;
		std::vector<uint32_t> m_sorted_luma;
		std::vector<uint32_t> m_sorted_luma_indices;
		const uint32_t* m_pSorted_luma_indices;
		uint32_t* m_pSorted_luma;

		std::vector<uint8_t> m_selectors;
		std::vector<uint8_t> m_best_selectors;

		potential_solution m_best_solution;
		potential_solution m_trial_solution;
		std::vector<uint8_t> m_temp_selectors;

		std::set<uint32_t> m_solutions_tried;

		void get_nearby_inten_tables(uint32_t idx, int &first_inten_table, int &last_inten_table)<--- Unused private function: 'etc1_optimizer::get_nearby_inten_tables'
		{
			first_inten_table = maximum<int>(idx - 1, 0);
			last_inten_table = minimum<int>(cETC1IntenModifierValues, idx + 1);
		}

		bool evaluate_solution_slow(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
		bool evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);

		inline bool evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
		{
			if (m_pParams->m_quality >= cETCQualitySlow)
				return evaluate_solution_slow(coords, trial_solution, pBest_solution);
			else
				return evaluate_solution_fast(coords, trial_solution, pBest_solution);
		}

		void refine_solution(uint32_t max_refinement_trials);
		void compute_internal_neighborhood(int scan_r, int scan_g, int scan_b);
		void compute_internal_cluster_fit(uint32_t total_perms_to_try);
	};

	struct pack_etc1_block_context
	{
		etc1_optimizer m_optimizer;
	};

} // namespace basisu