basisu_etc.h 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  1. // basis_etc.h
  2. // Copyright (C) 2019 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #pragma once
  16. #include "transcoder/basisu.h"
  17. #include "basisu_enc.h"
  18. #include <set>
  19. namespace basisu
  20. {
  21. enum etc_constants
  22. {
  23. cETC1BytesPerBlock = 8U,
  24. cETC1SelectorBits = 2U,
  25. cETC1SelectorValues = 1U << cETC1SelectorBits,
  26. cETC1SelectorMask = cETC1SelectorValues - 1U,
  27. cETC1BlockShift = 2U,
  28. cETC1BlockSize = 1U << cETC1BlockShift,
  29. cETC1LSBSelectorIndicesBitOffset = 0,
  30. cETC1MSBSelectorIndicesBitOffset = 16,
  31. cETC1FlipBitOffset = 32,
  32. cETC1DiffBitOffset = 33,
  33. cETC1IntenModifierNumBits = 3,
  34. cETC1IntenModifierValues = 1 << cETC1IntenModifierNumBits,
  35. cETC1RightIntenModifierTableBitOffset = 34,
  36. cETC1LeftIntenModifierTableBitOffset = 37,
  37. // Base+Delta encoding (5 bit bases, 3 bit delta)
  38. cETC1BaseColorCompNumBits = 5,
  39. cETC1BaseColorCompMax = 1 << cETC1BaseColorCompNumBits,
  40. cETC1DeltaColorCompNumBits = 3,
  41. cETC1DeltaColorComp = 1 << cETC1DeltaColorCompNumBits,
  42. cETC1DeltaColorCompMax = 1 << cETC1DeltaColorCompNumBits,
  43. cETC1BaseColor5RBitOffset = 59,
  44. cETC1BaseColor5GBitOffset = 51,
  45. cETC1BaseColor5BBitOffset = 43,
  46. cETC1DeltaColor3RBitOffset = 56,
  47. cETC1DeltaColor3GBitOffset = 48,
  48. cETC1DeltaColor3BBitOffset = 40,
  49. // Absolute (non-delta) encoding (two 4-bit per component bases)
  50. cETC1AbsColorCompNumBits = 4,
  51. cETC1AbsColorCompMax = 1 << cETC1AbsColorCompNumBits,
  52. cETC1AbsColor4R1BitOffset = 60,
  53. cETC1AbsColor4G1BitOffset = 52,
  54. cETC1AbsColor4B1BitOffset = 44,
  55. cETC1AbsColor4R2BitOffset = 56,
  56. cETC1AbsColor4G2BitOffset = 48,
  57. cETC1AbsColor4B2BitOffset = 40,
  58. cETC1ColorDeltaMin = -4,
  59. cETC1ColorDeltaMax = 3,
  60. // Delta3:
  61. // 0 1 2 3 4 5 6 7
  62. // 000 001 010 011 100 101 110 111
  63. // 0 1 2 3 -4 -3 -2 -1
  64. };
  65. extern const int g_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues];
  66. extern const uint8_t g_etc1_to_selector_index[cETC1SelectorValues];
  67. extern const uint8_t g_selector_index_to_etc1[cETC1SelectorValues];
  68. struct etc_coord2
  69. {
  70. uint8_t m_x, m_y;
  71. };
  72. extern const etc_coord2 g_etc1_pixel_coords[2][2][8]; // [flipped][subblock][subblock_pixel]
  73. extern const uint32_t g_etc1_pixel_indices[2][2][8]; // [flipped][subblock][subblock_pixel]
  74. struct etc_block
  75. {
  76. // big endian uint64:
  77. // bit ofs: 56 48 40 32 24 16 8 0
  78. // byte ofs: b0, b1, b2, b3, b4, b5, b6, b7
  79. union
  80. {
  81. uint64_t m_uint64;
  82. uint8_t m_bytes[8];
  83. };
  84. inline void clear()
  85. {
  86. assert(sizeof(*this) == 8);
  87. clear_obj(*this);
  88. }
  89. inline uint64_t get_all_bits() const
  90. {
  91. return read_be64(&m_uint64);
  92. }
  93. inline uint32_t get_general_bits(uint32_t ofs, uint32_t num) const
  94. {
  95. assert((ofs + num) <= 64U);
  96. assert(num && (num < 32U));
  97. return (read_be64(&m_uint64) >> ofs) & ((1UL << num) - 1UL);
  98. }
  99. inline void set_general_bits(uint32_t ofs, uint32_t num, uint32_t bits)
  100. {
  101. assert((ofs + num) <= 64U);
  102. assert(num && (num < 32U));
  103. uint64_t x = read_be64(&m_uint64);
  104. uint64_t msk = ((1ULL << static_cast<uint64_t>(num)) - 1ULL) << static_cast<uint64_t>(ofs);
  105. x &= ~msk;
  106. x |= (static_cast<uint64_t>(bits) << static_cast<uint64_t>(ofs));
  107. write_be64(&m_uint64, x);
  108. }
  109. inline uint32_t get_byte_bits(uint32_t ofs, uint32_t num) const
  110. {
  111. assert((ofs + num) <= 64U);
  112. assert(num && (num <= 8U));
  113. assert((ofs >> 3) == ((ofs + num - 1) >> 3));
  114. const uint32_t byte_ofs = 7 - (ofs >> 3);
  115. const uint32_t byte_bit_ofs = ofs & 7;
  116. return (m_bytes[byte_ofs] >> byte_bit_ofs) & ((1 << num) - 1);
  117. }
  118. inline void set_byte_bits(uint32_t ofs, uint32_t num, uint32_t bits)
  119. {
  120. assert((ofs + num) <= 64U);
  121. assert(num && (num < 32U));
  122. assert((ofs >> 3) == ((ofs + num - 1) >> 3));
  123. assert(bits < (1U << num));
  124. const uint32_t byte_ofs = 7 - (ofs >> 3);
  125. const uint32_t byte_bit_ofs = ofs & 7;
  126. const uint32_t mask = (1 << num) - 1;
  127. m_bytes[byte_ofs] &= ~(mask << byte_bit_ofs);
  128. m_bytes[byte_ofs] |= (bits << byte_bit_ofs);
  129. }
  130. // false = left/right subblocks
  131. // true = upper/lower subblocks
  132. inline bool get_flip_bit() const
  133. {
  134. return (m_bytes[3] & 1) != 0;
  135. }
  136. inline void set_flip_bit(bool flip)
  137. {
  138. m_bytes[3] &= ~1;
  139. m_bytes[3] |= static_cast<uint8_t>(flip);
  140. }
  141. inline bool get_diff_bit() const
  142. {
  143. return (m_bytes[3] & 2) != 0;
  144. }
  145. inline void set_diff_bit(bool diff)
  146. {
  147. m_bytes[3] &= ~2;
  148. m_bytes[3] |= (static_cast<uint32_t>(diff) << 1);
  149. }
  150. // Returns intensity modifier table (0-7) used by subblock subblock_id.
  151. // subblock_id=0 left/top (CW 1), 1=right/bottom (CW 2)
  152. inline uint32_t get_inten_table(uint32_t subblock_id) const
  153. {
  154. assert(subblock_id < 2);
  155. const uint32_t ofs = subblock_id ? 2 : 5;
  156. return (m_bytes[3] >> ofs) & 7;
  157. }
  158. // Sets intensity modifier table (0-7) used by subblock subblock_id (0 or 1)
  159. inline void set_inten_table(uint32_t subblock_id, uint32_t t)
  160. {
  161. assert(subblock_id < 2);
  162. assert(t < 8);
  163. const uint32_t ofs = subblock_id ? 2 : 5;
  164. m_bytes[3] &= ~(7 << ofs);
  165. m_bytes[3] |= (t << ofs);
  166. }
  167. inline void set_inten_tables_etc1s(uint32_t t)
  168. {
  169. set_inten_table(0, t);
  170. set_inten_table(1, t);
  171. }
  172. inline bool is_etc1s() const
  173. {
  174. if (get_inten_table(0) != get_inten_table(1))
  175. return false;
  176. if (get_diff_bit())
  177. {
  178. if (get_delta3_color() != 0)
  179. return false;
  180. }
  181. else
  182. {
  183. if (get_base4_color(0) != get_base4_color(1))
  184. return false;
  185. }
  186. return true;
  187. }
  188. // Returned encoded selector value ranges from 0-3 (this is NOT a direct index into g_etc1_inten_tables, see get_selector())
  189. inline uint32_t get_raw_selector(uint32_t x, uint32_t y) const
  190. {
  191. assert((x | y) < 4);
  192. const uint32_t bit_index = x * 4 + y;
  193. const uint32_t byte_bit_ofs = bit_index & 7;
  194. const uint8_t *p = &m_bytes[7 - (bit_index >> 3)];
  195. const uint32_t lsb = (p[0] >> byte_bit_ofs) & 1;
  196. const uint32_t msb = (p[-2] >> byte_bit_ofs) & 1;
  197. const uint32_t val = lsb | (msb << 1);
  198. return val;
  199. }
  200. // Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  201. inline uint32_t get_selector(uint32_t x, uint32_t y) const
  202. {
  203. return g_etc1_to_selector_index[get_raw_selector(x, y)];
  204. }
  205. // Selector "val" ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  206. inline void set_selector(uint32_t x, uint32_t y, uint32_t val)
  207. {
  208. assert((x | y | val) < 4);
  209. const uint32_t bit_index = x * 4 + y;
  210. uint8_t *p = &m_bytes[7 - (bit_index >> 3)];
  211. const uint32_t byte_bit_ofs = bit_index & 7;
  212. const uint32_t mask = 1 << byte_bit_ofs;
  213. const uint32_t etc1_val = g_selector_index_to_etc1[val];
  214. const uint32_t lsb = etc1_val & 1;
  215. const uint32_t msb = etc1_val >> 1;
  216. p[0] &= ~mask;
  217. p[0] |= (lsb << byte_bit_ofs);
  218. p[-2] &= ~mask;
  219. p[-2] |= (msb << byte_bit_ofs);
  220. }
  221. inline uint32_t get_raw_selector_bits() const
  222. {
  223. return m_bytes[4] | (m_bytes[5] << 8) | (m_bytes[6] << 16) | (m_bytes[7] << 24);
  224. }
  225. inline void set_raw_selector_bits(uint32_t bits)
  226. {
  227. m_bytes[4] = static_cast<uint8_t>(bits);
  228. m_bytes[5] = static_cast<uint8_t>(bits >> 8);
  229. m_bytes[6] = static_cast<uint8_t>(bits >> 16);
  230. m_bytes[7] = static_cast<uint8_t>(bits >> 24);
  231. }
  232. inline void set_raw_selector_bits(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3)
  233. {
  234. m_bytes[4] = byte0;
  235. m_bytes[5] = byte1;
  236. m_bytes[6] = byte2;
  237. m_bytes[7] = byte3;
  238. }
  239. inline void set_base4_color(uint32_t idx, uint16_t c)
  240. {
  241. if (idx)
  242. {
  243. set_byte_bits(cETC1AbsColor4R2BitOffset, 4, (c >> 8) & 15);
  244. set_byte_bits(cETC1AbsColor4G2BitOffset, 4, (c >> 4) & 15);
  245. set_byte_bits(cETC1AbsColor4B2BitOffset, 4, c & 15);
  246. }
  247. else
  248. {
  249. set_byte_bits(cETC1AbsColor4R1BitOffset, 4, (c >> 8) & 15);
  250. set_byte_bits(cETC1AbsColor4G1BitOffset, 4, (c >> 4) & 15);
  251. set_byte_bits(cETC1AbsColor4B1BitOffset, 4, c & 15);
  252. }
  253. }
  254. inline uint16_t get_base4_color(uint32_t idx) const
  255. {
  256. uint32_t r, g, b;
  257. if (idx)
  258. {
  259. r = get_byte_bits(cETC1AbsColor4R2BitOffset, 4);
  260. g = get_byte_bits(cETC1AbsColor4G2BitOffset, 4);
  261. b = get_byte_bits(cETC1AbsColor4B2BitOffset, 4);
  262. }
  263. else
  264. {
  265. r = get_byte_bits(cETC1AbsColor4R1BitOffset, 4);
  266. g = get_byte_bits(cETC1AbsColor4G1BitOffset, 4);
  267. b = get_byte_bits(cETC1AbsColor4B1BitOffset, 4);
  268. }
  269. return static_cast<uint16_t>(b | (g << 4U) | (r << 8U));
  270. }
  271. inline void set_base5_color(uint16_t c)
  272. {
  273. set_byte_bits(cETC1BaseColor5RBitOffset, 5, (c >> 10) & 31);
  274. set_byte_bits(cETC1BaseColor5GBitOffset, 5, (c >> 5) & 31);
  275. set_byte_bits(cETC1BaseColor5BBitOffset, 5, c & 31);
  276. }
  277. inline uint16_t get_base5_color() const
  278. {
  279. const uint32_t r = get_byte_bits(cETC1BaseColor5RBitOffset, 5);
  280. const uint32_t g = get_byte_bits(cETC1BaseColor5GBitOffset, 5);
  281. const uint32_t b = get_byte_bits(cETC1BaseColor5BBitOffset, 5);
  282. return static_cast<uint16_t>(b | (g << 5U) | (r << 10U));
  283. }
  284. void set_delta3_color(uint16_t c)
  285. {
  286. set_byte_bits(cETC1DeltaColor3RBitOffset, 3, (c >> 6) & 7);
  287. set_byte_bits(cETC1DeltaColor3GBitOffset, 3, (c >> 3) & 7);
  288. set_byte_bits(cETC1DeltaColor3BBitOffset, 3, c & 7);
  289. }
  290. inline uint16_t get_delta3_color() const
  291. {
  292. const uint32_t r = get_byte_bits(cETC1DeltaColor3RBitOffset, 3);
  293. const uint32_t g = get_byte_bits(cETC1DeltaColor3GBitOffset, 3);
  294. const uint32_t b = get_byte_bits(cETC1DeltaColor3BBitOffset, 3);
  295. return static_cast<uint16_t>(b | (g << 3U) | (r << 6U));
  296. }
  297. uint64_t determine_selectors(const color_rgba* pSource_pixels, bool perceptual, uint32_t begin_subblock = 0, uint32_t end_subblock = 2)
  298. {
  299. uint64_t total_error = 0;
  300. for (uint32_t subblock = begin_subblock; subblock < end_subblock; subblock++)
  301. {
  302. color_rgba block_colors[4];
  303. get_block_colors(block_colors, subblock);
  304. if (get_flip_bit())
  305. {
  306. for (uint32_t y = 0; y < 2; y++)
  307. {
  308. for (uint32_t x = 0; x < 4; x++)
  309. {
  310. uint32_t best_selector = 0;
  311. uint64_t best_error = UINT64_MAX;
  312. for (uint32_t s = 0; s < 4; s++)
  313. {
  314. uint64_t err = color_distance(perceptual, block_colors[s], pSource_pixels[x + (subblock * 2 + y) * 4], false);
  315. if (err < best_error)
  316. {
  317. best_error = err;
  318. best_selector = s;
  319. }
  320. }
  321. set_selector(x, subblock * 2 + y, best_selector);
  322. total_error += best_error;
  323. }
  324. }
  325. }
  326. else
  327. {
  328. for (uint32_t y = 0; y < 4; y++)
  329. {
  330. for (uint32_t x = 0; x < 2; x++)
  331. {
  332. uint32_t best_selector = 0;
  333. uint64_t best_error = UINT64_MAX;
  334. for (uint32_t s = 0; s < 4; s++)
  335. {
  336. uint64_t err = color_distance(perceptual, block_colors[s], pSource_pixels[(subblock * 2) + x + y * 4], false);
  337. if (err < best_error)
  338. {
  339. best_error = err;
  340. best_selector = s;
  341. }
  342. }
  343. set_selector(subblock * 2 + x, y, best_selector);
  344. total_error += best_error;
  345. }
  346. }
  347. }
  348. }
  349. return total_error;
  350. }
  351. color_rgba get_block_color(uint32_t subblock_index, bool scaled) const
  352. {
  353. color_rgba b;
  354. if (get_diff_bit())
  355. {
  356. if (subblock_index)
  357. unpack_color5(b, get_base5_color(), get_delta3_color(), scaled);
  358. else
  359. unpack_color5(b, get_base5_color(), scaled);
  360. }
  361. else
  362. {
  363. b = unpack_color4(get_base4_color(subblock_index), scaled);
  364. }
  365. return b;
  366. }
  367. uint32_t get_subblock_index(uint32_t x, uint32_t y) const
  368. {
  369. if (get_flip_bit())
  370. return y >= 2;
  371. else
  372. return x >= 2;
  373. }
  374. bool get_block_colors(color_rgba* pBlock_colors, uint32_t subblock_index) const
  375. {
  376. color_rgba b;
  377. if (get_diff_bit())
  378. {
  379. if (subblock_index)
  380. unpack_color5(b, get_base5_color(), get_delta3_color(), true);
  381. else
  382. unpack_color5(b, get_base5_color(), true);
  383. }
  384. else
  385. {
  386. b = unpack_color4(get_base4_color(subblock_index), true);
  387. }
  388. const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];
  389. bool dc = false;
  390. 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);
  391. 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);
  392. 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);
  393. 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);
  394. return dc;
  395. }
  396. void get_block_color(color_rgba& color, uint32_t subblock_index, uint32_t selector_index) const
  397. {
  398. color_rgba b;
  399. if (get_diff_bit())
  400. {
  401. if (subblock_index)
  402. unpack_color5(b, get_base5_color(), get_delta3_color(), true);
  403. else
  404. unpack_color5(b, get_base5_color(), true);
  405. }
  406. else
  407. {
  408. b = unpack_color4(get_base4_color(subblock_index), true);
  409. }
  410. const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];
  411. color.set(clamp255(b.r + pInten_table[selector_index]), clamp255(b.g + pInten_table[selector_index]), clamp255(b.b + pInten_table[selector_index]), 255);
  412. }
  413. bool get_block_low_high_colors(color_rgba* pBlock_colors, uint32_t subblock_index) const
  414. {
  415. color_rgba b;
  416. if (get_diff_bit())
  417. {
  418. if (subblock_index)
  419. unpack_color5(b, get_base5_color(), get_delta3_color(), true);
  420. else
  421. unpack_color5(b, get_base5_color(), true);
  422. }
  423. else
  424. {
  425. b = unpack_color4(get_base4_color(subblock_index), true);
  426. }
  427. const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];
  428. bool dc = false;
  429. 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);
  430. 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);
  431. return dc;
  432. }
  433. static void get_block_colors5(color_rgba *pBlock_colors, const color_rgba &base_color5, uint32_t inten_table, bool scaled = false)
  434. {
  435. color_rgba b(base_color5);
  436. if (!scaled)
  437. {
  438. b.r = (b.r << 3) | (b.r >> 2);
  439. b.g = (b.g << 3) | (b.g >> 2);
  440. b.b = (b.b << 3) | (b.b >> 2);
  441. }
  442. const int* pInten_table = g_etc1_inten_tables[inten_table];
  443. pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
  444. pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
  445. pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
  446. pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
  447. }
  448. static void get_block_colors4(color_rgba *pBlock_colors, const color_rgba &base_color4, uint32_t inten_table, bool scaled = false)
  449. {
  450. color_rgba b(base_color4);
  451. if (!scaled)
  452. {
  453. b.r = (b.r << 4) | b.r;
  454. b.g = (b.g << 4) | b.g;
  455. b.b = (b.b << 4) | b.b;
  456. }
  457. const int* pInten_table = g_etc1_inten_tables[inten_table];
  458. pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
  459. pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
  460. pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
  461. pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
  462. }
  463. uint64_t evaluate_etc1_error(const color_rgba* pBlock_pixels, bool perceptual, int subblock_index = -1) const;
  464. void get_subblock_pixels(color_rgba* pPixels, int subblock_index = -1) const;
  465. void get_selector_range(uint32_t& low, uint32_t& high) const
  466. {
  467. low = 3;
  468. high = 0;
  469. for (uint32_t y = 0; y < 4; y++)
  470. {
  471. for (uint32_t x = 0; x < 4; x++)
  472. {
  473. const uint32_t s = get_selector(x, y);
  474. low = minimum(low, s);
  475. high = maximum(high, s);
  476. }
  477. }
  478. }
  479. void set_block_color4(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
  480. {
  481. set_diff_bit(false);
  482. set_base4_color(0, pack_color4(c0_unscaled, false));
  483. set_base4_color(1, pack_color4(c1_unscaled, false));
  484. }
  485. void set_block_color5(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
  486. {
  487. set_diff_bit(true);
  488. set_base5_color(pack_color5(c0_unscaled, false));
  489. int dr = c1_unscaled.r - c0_unscaled.r;
  490. int dg = c1_unscaled.g - c0_unscaled.g;
  491. int db = c1_unscaled.b - c0_unscaled.b;
  492. set_delta3_color(pack_delta3(dr, dg, db));
  493. }
  494. void set_block_color5_etc1s(const color_rgba &c_unscaled)
  495. {
  496. set_diff_bit(true);
  497. set_base5_color(pack_color5(c_unscaled, false));
  498. set_delta3_color(pack_delta3(0, 0, 0));
  499. }
  500. bool set_block_color5_check(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
  501. {
  502. set_diff_bit(true);
  503. set_base5_color(pack_color5(c0_unscaled, false));
  504. int dr = c1_unscaled.r - c0_unscaled.r;
  505. int dg = c1_unscaled.g - c0_unscaled.g;
  506. int db = c1_unscaled.b - c0_unscaled.b;
  507. if (((dr < cETC1ColorDeltaMin) || (dr > cETC1ColorDeltaMax)) ||
  508. ((dg < cETC1ColorDeltaMin) || (dg > cETC1ColorDeltaMax)) ||
  509. ((db < cETC1ColorDeltaMin) || (db > cETC1ColorDeltaMax)))
  510. return false;
  511. set_delta3_color(pack_delta3(dr, dg, db));
  512. return true;
  513. }
  514. color_rgba get_selector_color(uint32_t x, uint32_t y, uint32_t s) const
  515. {
  516. color_rgba block_colors[4];
  517. get_block_colors(block_colors, get_subblock_index(x, y));
  518. return block_colors[s];
  519. }
  520. // Base color 5
  521. static uint16_t pack_color5(const color_rgba& color, bool scaled, uint32_t bias = 127U);
  522. static uint16_t pack_color5(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias = 127U);
  523. static color_rgba unpack_color5(uint16_t packed_color5, bool scaled, uint32_t alpha = 255U);
  524. static void unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color, bool scaled);
  525. static void unpack_color5(color_rgba& result, uint16_t packed_color5, bool scaled);
  526. static bool unpack_color5(color_rgba& result, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha = 255U);
  527. 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);
  528. // Delta color 3
  529. // Inputs range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
  530. static uint16_t pack_delta3(const color_rgba_i16& color);
  531. static uint16_t pack_delta3(int r, int g, int b);
  532. // Results range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
  533. static color_rgba_i16 unpack_delta3(uint16_t packed_delta3);
  534. static void unpack_delta3(int& r, int& g, int& b, uint16_t packed_delta3);
  535. static bool try_pack_color5_delta3(const color_rgba *pColor5_unscaled)
  536. {
  537. int dr = pColor5_unscaled[1].r - pColor5_unscaled[0].r;
  538. int dg = pColor5_unscaled[1].g - pColor5_unscaled[0].g;
  539. int db = pColor5_unscaled[1].b - pColor5_unscaled[0].b;
  540. if ((minimum(dr, dg, db) < cETC1ColorDeltaMin) || (maximum(dr, dg, db) > cETC1ColorDeltaMax))
  541. return false;
  542. return true;
  543. }
  544. // Abs color 4
  545. static uint16_t pack_color4(const color_rgba& color, bool scaled, uint32_t bias = 127U);
  546. static uint16_t pack_color4(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias = 127U);
  547. static color_rgba unpack_color4(uint16_t packed_color4, bool scaled, uint32_t alpha = 255U);
  548. static void unpack_color4(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color4, bool scaled);
  549. // subblock colors
  550. static void get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint32_t table_idx);
  551. static bool get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint16_t packed_delta3, uint32_t table_idx);
  552. static void get_abs_subblock_colors(color_rgba* pDst, uint16_t packed_color4, uint32_t table_idx);
  553. static inline void unscaled_to_scaled_color(color_rgba& dst, const color_rgba& src, bool color4)
  554. {
  555. if (color4)
  556. {
  557. dst.r = src.r | (src.r << 4);
  558. dst.g = src.g | (src.g << 4);
  559. dst.b = src.b | (src.b << 4);
  560. }
  561. else
  562. {
  563. dst.r = (src.r >> 2) | (src.r << 3);
  564. dst.g = (src.g >> 2) | (src.g << 3);
  565. dst.b = (src.b >> 2) | (src.b << 3);
  566. }
  567. dst.a = src.a;
  568. }
  569. private:
  570. static uint8_t clamp255(int x, bool &did_clamp)
  571. {
  572. if (x < 0)
  573. {
  574. did_clamp = true;
  575. return 0;
  576. }
  577. else if (x > 255)
  578. {
  579. did_clamp = true;
  580. return 255;
  581. }
  582. return static_cast<uint8_t>(x);
  583. }
  584. static uint8_t clamp255(int x)
  585. {
  586. if (x < 0)
  587. return 0;
  588. else if (x > 255)
  589. return 255;
  590. return static_cast<uint8_t>(x);
  591. }
  592. };
  593. typedef std::vector<etc_block> etc_block_vec;
  594. // Returns false if the unpack fails (could be bogus data or ETC2)
  595. bool unpack_etc1(const etc_block& block, color_rgba *pDst, bool preserve_alpha = false);
  596. enum basis_etc_quality
  597. {
  598. cETCQualityFast,
  599. cETCQualityMedium,
  600. cETCQualitySlow,
  601. cETCQualityUber,
  602. cETCQualityTotal,
  603. };
  604. struct basis_etc1_pack_params
  605. {
  606. basis_etc_quality m_quality;
  607. bool m_perceptual;
  608. bool m_cluster_fit;
  609. bool m_force_etc1s;
  610. bool m_use_color4;
  611. float m_flip_bias;
  612. inline basis_etc1_pack_params()
  613. {
  614. clear();
  615. }
  616. void clear()
  617. {
  618. m_quality = cETCQualitySlow;
  619. m_perceptual = true;
  620. m_cluster_fit = true;
  621. m_force_etc1s = false;
  622. m_use_color4 = true;
  623. m_flip_bias = 0.0f;
  624. }
  625. };
  626. struct etc1_solution_coordinates
  627. {
  628. inline etc1_solution_coordinates() :
  629. m_unscaled_color(0, 0, 0, 0),
  630. m_inten_table(0),
  631. m_color4(false)
  632. {
  633. }
  634. inline etc1_solution_coordinates(uint32_t r, uint32_t g, uint32_t b, uint32_t inten_table, bool color4) :
  635. m_unscaled_color((uint8_t)r, (uint8_t)g, (uint8_t)b, 255),
  636. m_inten_table((uint8_t)inten_table),
  637. m_color4(color4)
  638. {
  639. }
  640. inline etc1_solution_coordinates(const color_rgba& c, uint32_t inten_table, bool color4) :
  641. m_unscaled_color(c),
  642. m_inten_table(inten_table),
  643. m_color4(color4)
  644. {
  645. }
  646. inline etc1_solution_coordinates(const etc1_solution_coordinates& other)
  647. {
  648. *this = other;
  649. }
  650. inline etc1_solution_coordinates& operator= (const etc1_solution_coordinates& rhs)
  651. {
  652. m_unscaled_color = rhs.m_unscaled_color;
  653. m_inten_table = rhs.m_inten_table;
  654. m_color4 = rhs.m_color4;
  655. return *this;
  656. }
  657. inline void clear()
  658. {
  659. m_unscaled_color.clear();
  660. m_inten_table = 0;
  661. m_color4 = false;
  662. }
  663. inline void init(const color_rgba& c, uint32_t inten_table, bool color4)
  664. {
  665. m_unscaled_color = c;
  666. m_inten_table = inten_table;
  667. m_color4 = color4;
  668. }
  669. inline color_rgba get_scaled_color() const
  670. {
  671. int br, bg, bb;
  672. if (m_color4)
  673. {
  674. br = m_unscaled_color.r | (m_unscaled_color.r << 4);
  675. bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
  676. bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
  677. }
  678. else
  679. {
  680. br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
  681. bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
  682. bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
  683. }
  684. return color_rgba((uint8_t)br, (uint8_t)bg, (uint8_t)bb, 255);
  685. }
  686. // returns true if anything was clamped
  687. inline void get_block_colors(color_rgba* pBlock_colors)
  688. {
  689. int br, bg, bb;
  690. if (m_color4)
  691. {
  692. br = m_unscaled_color.r | (m_unscaled_color.r << 4);
  693. bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
  694. bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
  695. }
  696. else
  697. {
  698. br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
  699. bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
  700. bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
  701. }
  702. const int* pInten_table = g_etc1_inten_tables[m_inten_table];
  703. pBlock_colors[0].set((uint8_t)(br + pInten_table[0]), (uint8_t)(bg + pInten_table[0]), (uint8_t)(bb + pInten_table[0]), 255);
  704. pBlock_colors[1].set((uint8_t)(br + pInten_table[1]), (uint8_t)(bg + pInten_table[1]), (uint8_t)(bb + pInten_table[1]), 255);
  705. pBlock_colors[2].set((uint8_t)(br + pInten_table[2]), (uint8_t)(bg + pInten_table[2]), (uint8_t)(bb + pInten_table[2]), 255);
  706. pBlock_colors[3].set((uint8_t)(br + pInten_table[3]), (uint8_t)(bg + pInten_table[3]), (uint8_t)(bb + pInten_table[3]), 255);
  707. }
  708. color_rgba m_unscaled_color;
  709. uint32_t m_inten_table;
  710. bool m_color4;
  711. };
  712. class etc1_optimizer
  713. {
  714. BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(etc1_optimizer);
  715. public:
  716. etc1_optimizer()
  717. {
  718. clear();
  719. }
  720. void clear()
  721. {
  722. m_pParams = nullptr;
  723. m_pResult = nullptr;
  724. m_pSorted_luma = nullptr;
  725. m_pSorted_luma_indices = nullptr;
  726. }
  727. struct params;
  728. 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);
  729. struct params : basis_etc1_pack_params
  730. {
  731. params()
  732. {
  733. clear();
  734. }
  735. params(const basis_etc1_pack_params& base_params)
  736. {
  737. clear_optimizer_params();
  738. *static_cast<basis_etc1_pack_params *>(this) = base_params;
  739. }
  740. void clear()
  741. {
  742. clear_optimizer_params();
  743. }
  744. void clear_optimizer_params()
  745. {
  746. basis_etc1_pack_params::clear();
  747. m_num_src_pixels = 0;
  748. m_pSrc_pixels = 0;
  749. m_use_color4 = false;
  750. static const int s_default_scan_delta[] = { 0 };
  751. m_pScan_deltas = s_default_scan_delta;
  752. m_scan_delta_size = 1;
  753. m_base_color5.clear();
  754. m_constrain_against_base_color5 = false;
  755. m_refinement = true;
  756. m_pForce_selectors = nullptr;
  757. m_pEval_solution_override = nullptr;
  758. m_pEval_solution_override_data = nullptr;
  759. }
  760. uint32_t m_num_src_pixels;
  761. const color_rgba* m_pSrc_pixels;
  762. bool m_use_color4;
  763. const int* m_pScan_deltas;
  764. uint32_t m_scan_delta_size;
  765. color_rgba m_base_color5;
  766. bool m_constrain_against_base_color5;
  767. bool m_refinement;
  768. const uint8_t* m_pForce_selectors;
  769. evaluate_solution_override_func m_pEval_solution_override;
  770. void *m_pEval_solution_override_data;
  771. };
  772. struct results
  773. {
  774. uint64_t m_error;
  775. color_rgba m_block_color_unscaled;
  776. uint32_t m_block_inten_table;
  777. uint32_t m_n;
  778. uint8_t* m_pSelectors;
  779. bool m_block_color4;
  780. inline results& operator= (const results& rhs)
  781. {
  782. m_block_color_unscaled = rhs.m_block_color_unscaled;
  783. m_block_color4 = rhs.m_block_color4;
  784. m_block_inten_table = rhs.m_block_inten_table;
  785. m_error = rhs.m_error;
  786. memcpy(m_pSelectors, rhs.m_pSelectors, minimum(rhs.m_n, m_n));
  787. return *this;
  788. }
  789. };
  790. void init(const params& params, results& result);
  791. bool compute();
  792. const params* get_params() const { return m_pParams; }
  793. private:
  794. struct potential_solution
  795. {
  796. potential_solution() : m_coords(), m_error(UINT64_MAX), m_valid(false)
  797. {
  798. }
  799. etc1_solution_coordinates m_coords;
  800. std::vector<uint8_t> m_selectors;
  801. uint64_t m_error;
  802. bool m_valid;
  803. void clear()
  804. {
  805. m_coords.clear();
  806. m_selectors.resize(0);
  807. m_error = UINT64_MAX;
  808. m_valid = false;
  809. }
  810. bool are_selectors_all_equal() const
  811. {
  812. if (!m_selectors.size())
  813. return false;
  814. const uint32_t s = m_selectors[0];
  815. for (uint32_t i = 1; i < m_selectors.size(); i++)
  816. if (m_selectors[i] != s)
  817. return false;
  818. return true;
  819. }
  820. };
  821. const params* m_pParams;
  822. results* m_pResult;
  823. int m_limit;
  824. vec3F m_avg_color;
  825. int m_br, m_bg, m_bb;
  826. std::vector<uint16_t> m_luma;
  827. std::vector<uint32_t> m_sorted_luma;
  828. std::vector<uint32_t> m_sorted_luma_indices;
  829. const uint32_t* m_pSorted_luma_indices;
  830. uint32_t* m_pSorted_luma;
  831. std::vector<uint8_t> m_selectors;
  832. std::vector<uint8_t> m_best_selectors;
  833. potential_solution m_best_solution;
  834. potential_solution m_trial_solution;
  835. std::vector<uint8_t> m_temp_selectors;
  836. std::set<uint32_t> m_solutions_tried;
  837. void get_nearby_inten_tables(uint32_t idx, int &first_inten_table, int &last_inten_table)
  838. {
  839. first_inten_table = maximum<int>(idx - 1, 0);
  840. last_inten_table = minimum<int>(cETC1IntenModifierValues, idx + 1);
  841. }
  842. bool evaluate_solution_slow(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
  843. bool evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
  844. inline bool evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
  845. {
  846. if (m_pParams->m_quality >= cETCQualitySlow)
  847. return evaluate_solution_slow(coords, trial_solution, pBest_solution);
  848. else
  849. return evaluate_solution_fast(coords, trial_solution, pBest_solution);
  850. }
  851. void refine_solution(uint32_t max_refinement_trials);
  852. void compute_internal_neighborhood(int scan_r, int scan_g, int scan_b);
  853. void compute_internal_cluster_fit(uint32_t total_perms_to_try);
  854. };
  855. struct pack_etc1_block_context
  856. {
  857. etc1_optimizer m_optimizer;
  858. };
  859. } // namespace basisu