bcdec.h 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. /* bcdec.h - v0.97
  2. provides functions to decompress blocks of BC compressed images
  3. written by Sergii "iOrange" Kudlai in 2022
  4. This library does not allocate memory and is trying to use as less stack as possible
  5. The library was never optimized specifically for speed but for the overall size
  6. it has zero external dependencies and is not using any runtime functions
  7. Supported BC formats:
  8. BC1 (also known as DXT1) + it's "binary alpha" variant BC1A (DXT1A)
  9. BC2 (also known as DXT3)
  10. BC3 (also known as DXT5)
  11. BC4 (also known as ATI1N)
  12. BC5 (also known as ATI2N)
  13. BC6H (HDR format)
  14. BC7
  15. BC1/BC2/BC3/BC7 are expected to decompress into 4*4 RGBA blocks 8bit per component (32bit pixel)
  16. BC4/BC5 are expected to decompress into 4*4 R/RG blocks 8bit per component (8bit and 16bit pixel)
  17. BC6H is expected to decompress into 4*4 RGB blocks of either 32bit float or 16bit "half" per
  18. component (96bit or 48bit pixel)
  19. For more info, issues and suggestions please visit https://github.com/iOrange/bcdec
  20. CREDITS:
  21. Aras Pranckevicius (@aras-p) - BC1/BC3 decoders optimizations (up to 3x the speed)
  22. - BC6H/BC7 bits pulling routines optimizations
  23. - optimized BC6H by moving unquantize out of the loop
  24. - Split BC6H decompression function into 'half' and
  25. 'float' variants
  26. Michael Schmidt (@RunDevelopment) - Found better "magic" coefficients for integer interpolation
  27. of reference colors in BC1 color block, that match with
  28. the floating point interpolation. This also made it faster
  29. than integer division by 3!
  30. bugfixes:
  31. @linkmauve
  32. LICENSE: See end of file for license information.
  33. */
  34. #ifndef BCDEC_HEADER_INCLUDED
  35. #define BCDEC_HEADER_INCLUDED
  36. #define BCDEC_VERSION_MAJOR 0
  37. #define BCDEC_VERSION_MINOR 97
  38. /* if BCDEC_STATIC causes problems, try defining BCDECDEF to 'inline' or 'static inline' */
  39. #ifndef BCDECDEF
  40. #ifdef BCDEC_STATIC
  41. #define BCDECDEF static
  42. #else
  43. #ifdef __cplusplus
  44. #define BCDECDEF extern "C"
  45. #else
  46. #define BCDECDEF extern
  47. #endif
  48. #endif
  49. #endif
  50. /* Used information sources:
  51. https://docs.microsoft.com/en-us/windows/win32/direct3d10/d3d10-graphics-programming-guide-resources-block-compression
  52. https://docs.microsoft.com/en-us/windows/win32/direct3d11/bc6h-format
  53. https://docs.microsoft.com/en-us/windows/win32/direct3d11/bc7-format
  54. https://docs.microsoft.com/en-us/windows/win32/direct3d11/bc7-format-mode-reference
  55. ! WARNING ! Khronos's BPTC partitions tables contain mistakes, do not use them!
  56. https://www.khronos.org/registry/DataFormat/specs/1.1/dataformat.1.1.html#BPTC
  57. ! Use tables from here instead !
  58. https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_compression_bptc.txt
  59. Leaving it here as it's a nice read
  60. https://fgiesen.wordpress.com/2021/10/04/gpu-bcn-decoding/
  61. Fast half to float function from here
  62. https://gist.github.com/rygorous/2144712
  63. */
  64. #define BCDEC_BC1_BLOCK_SIZE 8
  65. #define BCDEC_BC2_BLOCK_SIZE 16
  66. #define BCDEC_BC3_BLOCK_SIZE 16
  67. #define BCDEC_BC4_BLOCK_SIZE 8
  68. #define BCDEC_BC5_BLOCK_SIZE 16
  69. #define BCDEC_BC6H_BLOCK_SIZE 16
  70. #define BCDEC_BC7_BLOCK_SIZE 16
  71. #define BCDEC_BC1_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC1_BLOCK_SIZE)
  72. #define BCDEC_BC2_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC2_BLOCK_SIZE)
  73. #define BCDEC_BC3_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC3_BLOCK_SIZE)
  74. #define BCDEC_BC4_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC4_BLOCK_SIZE)
  75. #define BCDEC_BC5_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC5_BLOCK_SIZE)
  76. #define BCDEC_BC6H_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC6H_BLOCK_SIZE)
  77. #define BCDEC_BC7_COMPRESSED_SIZE(w, h) ((((w)>>2)*((h)>>2))*BCDEC_BC7_BLOCK_SIZE)
  78. BCDECDEF void bcdec_bc1(const void* compressedBlock, void* decompressedBlock, int destinationPitch);
  79. BCDECDEF void bcdec_bc2(const void* compressedBlock, void* decompressedBlock, int destinationPitch);
  80. BCDECDEF void bcdec_bc3(const void* compressedBlock, void* decompressedBlock, int destinationPitch);
  81. BCDECDEF void bcdec_bc4(const void* compressedBlock, void* decompressedBlock, int destinationPitch);
  82. BCDECDEF void bcdec_bc5(const void* compressedBlock, void* decompressedBlock, int destinationPitch);
  83. BCDECDEF void bcdec_bc6h_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned);
  84. BCDECDEF void bcdec_bc6h_half(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned);
  85. BCDECDEF void bcdec_bc7(const void* compressedBlock, void* decompressedBlock, int destinationPitch);
  86. #endif /* BCDEC_HEADER_INCLUDED */
  87. #ifdef BCDEC_IMPLEMENTATION
  88. static void bcdec__color_block(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int onlyOpaqueMode) {
  89. unsigned short c0, c1;
  90. unsigned int refColors[4]; /* 0xAABBGGRR */
  91. unsigned char* dstColors;
  92. unsigned int colorIndices;
  93. int i, j, idx;
  94. unsigned int r0, g0, b0, r1, g1, b1, r, g, b;
  95. c0 = ((unsigned short*)compressedBlock)[0];
  96. c1 = ((unsigned short*)compressedBlock)[1];
  97. /* Unpack 565 ref colors */
  98. r0 = (c0 >> 11) & 0x1F;
  99. g0 = (c0 >> 5) & 0x3F;
  100. b0 = c0 & 0x1F;
  101. r1 = (c1 >> 11) & 0x1F;
  102. g1 = (c1 >> 5) & 0x3F;
  103. b1 = c1 & 0x1F;
  104. /* Expand 565 ref colors to 888 */
  105. r = (r0 * 527 + 23) >> 6;
  106. g = (g0 * 259 + 33) >> 6;
  107. b = (b0 * 527 + 23) >> 6;
  108. refColors[0] = 0xFF000000 | (b << 16) | (g << 8) | r;
  109. r = (r1 * 527 + 23) >> 6;
  110. g = (g1 * 259 + 33) >> 6;
  111. b = (b1 * 527 + 23) >> 6;
  112. refColors[1] = 0xFF000000 | (b << 16) | (g << 8) | r;
  113. if (c0 > c1 || onlyOpaqueMode) { /* Standard BC1 mode (also BC3 color block uses ONLY this mode) */
  114. /* color_2 = 2/3*color_0 + 1/3*color_1
  115. color_3 = 1/3*color_0 + 2/3*color_1 */
  116. r = ((2 * r0 + r1) * 351 + 61) >> 7;
  117. g = ((2 * g0 + g1) * 2763 + 1039) >> 11;
  118. b = ((2 * b0 + b1) * 351 + 61) >> 7;
  119. refColors[2] = 0xFF000000 | (b << 16) | (g << 8) | r;
  120. r = ((r0 + r1 * 2) * 351 + 61) >> 7;
  121. g = ((g0 + g1 * 2) * 2763 + 1039) >> 11;
  122. b = ((b0 + b1 * 2) * 351 + 61) >> 7;
  123. refColors[3] = 0xFF000000 | (b << 16) | (g << 8) | r;
  124. } else { /* Quite rare BC1A mode */
  125. /* color_2 = 1/2*color_0 + 1/2*color_1;
  126. color_3 = 0; */
  127. r = ((r0 + r1) * 1053 + 125) >> 8;
  128. g = ((g0 + g1) * 4145 + 1019) >> 11;
  129. b = ((b0 + b1) * 1053 + 125) >> 8;
  130. refColors[2] = 0xFF000000 | (b << 16) | (g << 8) | r;
  131. refColors[3] = 0x00000000;
  132. }
  133. colorIndices = ((unsigned int*)compressedBlock)[1];
  134. /* Fill out the decompressed color block */
  135. dstColors = (unsigned char*)decompressedBlock;
  136. for (i = 0; i < 4; ++i) {
  137. for (j = 0; j < 4; ++j) {
  138. idx = colorIndices & 0x03;
  139. ((unsigned int*)dstColors)[j] = refColors[idx];
  140. colorIndices >>= 2;
  141. }
  142. dstColors += destinationPitch;
  143. }
  144. }
  145. static void bcdec__sharp_alpha_block(const void* compressedBlock, void* decompressedBlock, int destinationPitch) {
  146. unsigned short* alpha;
  147. unsigned char* decompressed;
  148. int i, j;
  149. alpha = (unsigned short*)compressedBlock;
  150. decompressed = (unsigned char*)decompressedBlock;
  151. for (i = 0; i < 4; ++i) {
  152. for (j = 0; j < 4; ++j) {
  153. decompressed[j * 4] = ((alpha[i] >> (4 * j)) & 0x0F) * 17;
  154. }
  155. decompressed += destinationPitch;
  156. }
  157. }
  158. static void bcdec__smooth_alpha_block(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int pixelSize) {
  159. unsigned char* decompressed;
  160. unsigned char alpha[8];
  161. int i, j;
  162. unsigned long long block, indices;
  163. block = *(unsigned long long*)compressedBlock;
  164. decompressed = (unsigned char*)decompressedBlock;
  165. alpha[0] = block & 0xFF;
  166. alpha[1] = (block >> 8) & 0xFF;
  167. if (alpha[0] > alpha[1]) {
  168. /* 6 interpolated alpha values. */
  169. alpha[2] = (6 * alpha[0] + alpha[1] + 1) / 7; /* 6/7*alpha_0 + 1/7*alpha_1 */
  170. alpha[3] = (5 * alpha[0] + 2 * alpha[1] + 1) / 7; /* 5/7*alpha_0 + 2/7*alpha_1 */
  171. alpha[4] = (4 * alpha[0] + 3 * alpha[1] + 1) / 7; /* 4/7*alpha_0 + 3/7*alpha_1 */
  172. alpha[5] = (3 * alpha[0] + 4 * alpha[1] + 1) / 7; /* 3/7*alpha_0 + 4/7*alpha_1 */
  173. alpha[6] = (2 * alpha[0] + 5 * alpha[1] + 1) / 7; /* 2/7*alpha_0 + 5/7*alpha_1 */
  174. alpha[7] = ( alpha[0] + 6 * alpha[1] + 1) / 7; /* 1/7*alpha_0 + 6/7*alpha_1 */
  175. }
  176. else {
  177. /* 4 interpolated alpha values. */
  178. alpha[2] = (4 * alpha[0] + alpha[1] + 1) / 5; /* 4/5*alpha_0 + 1/5*alpha_1 */
  179. alpha[3] = (3 * alpha[0] + 2 * alpha[1] + 1) / 5; /* 3/5*alpha_0 + 2/5*alpha_1 */
  180. alpha[4] = (2 * alpha[0] + 3 * alpha[1] + 1) / 5; /* 2/5*alpha_0 + 3/5*alpha_1 */
  181. alpha[5] = ( alpha[0] + 4 * alpha[1] + 1) / 5; /* 1/5*alpha_0 + 4/5*alpha_1 */
  182. alpha[6] = 0x00;
  183. alpha[7] = 0xFF;
  184. }
  185. indices = block >> 16;
  186. for (i = 0; i < 4; ++i) {
  187. for (j = 0; j < 4; ++j) {
  188. decompressed[j * pixelSize] = alpha[indices & 0x07];
  189. indices >>= 3;
  190. }
  191. decompressed += destinationPitch;
  192. }
  193. }
  194. typedef struct bcdec__bitstream {
  195. unsigned long long low;
  196. unsigned long long high;
  197. } bcdec__bitstream_t;
  198. static int bcdec__bitstream_read_bits(bcdec__bitstream_t* bstream, int numBits) {
  199. unsigned int mask = (1 << numBits) - 1;
  200. /* Read the low N bits */
  201. unsigned int bits = (bstream->low & mask);
  202. bstream->low >>= numBits;
  203. /* Put the low N bits of "high" into the high 64-N bits of "low". */
  204. bstream->low |= (bstream->high & mask) << (sizeof(bstream->high) * 8 - numBits);
  205. bstream->high >>= numBits;
  206. return bits;
  207. }
  208. static int bcdec__bitstream_read_bit(bcdec__bitstream_t* bstream) {
  209. return bcdec__bitstream_read_bits(bstream, 1);
  210. }
  211. /* reversed bits pulling, used in BC6H decoding
  212. why ?? just why ??? */
  213. static int bcdec__bitstream_read_bits_r(bcdec__bitstream_t* bstream, int numBits) {
  214. int bits = bcdec__bitstream_read_bits(bstream, numBits);
  215. /* Reverse the bits. */
  216. int result = 0;
  217. while (numBits--) {
  218. result <<= 1;
  219. result |= (bits & 1);
  220. bits >>= 1;
  221. }
  222. return result;
  223. }
  224. BCDECDEF void bcdec_bc1(const void* compressedBlock, void* decompressedBlock, int destinationPitch) {
  225. bcdec__color_block(compressedBlock, decompressedBlock, destinationPitch, 0);
  226. }
  227. BCDECDEF void bcdec_bc2(const void* compressedBlock, void* decompressedBlock, int destinationPitch) {
  228. bcdec__color_block(((char*)compressedBlock) + 8, decompressedBlock, destinationPitch, 1);
  229. bcdec__sharp_alpha_block(compressedBlock, ((char*)decompressedBlock) + 3, destinationPitch);
  230. }
  231. BCDECDEF void bcdec_bc3(const void* compressedBlock, void* decompressedBlock, int destinationPitch) {
  232. bcdec__color_block(((char*)compressedBlock) + 8, decompressedBlock, destinationPitch, 1);
  233. bcdec__smooth_alpha_block(compressedBlock, ((char*)decompressedBlock) + 3, destinationPitch, 4);
  234. }
  235. BCDECDEF void bcdec_bc4(const void* compressedBlock, void* decompressedBlock, int destinationPitch) {
  236. bcdec__smooth_alpha_block(compressedBlock, decompressedBlock, destinationPitch, 1);
  237. }
  238. BCDECDEF void bcdec_bc5(const void* compressedBlock, void* decompressedBlock, int destinationPitch) {
  239. bcdec__smooth_alpha_block(compressedBlock, decompressedBlock, destinationPitch, 2);
  240. bcdec__smooth_alpha_block(((char*)compressedBlock) + 8, ((char*)decompressedBlock) + 1, destinationPitch, 2);
  241. }
  242. /* http://graphics.stanford.edu/~seander/bithacks.html#VariableSignExtend */
  243. static int bcdec__extend_sign(int val, int bits) {
  244. return (val << (32 - bits)) >> (32 - bits);
  245. }
  246. static int bcdec__transform_inverse(int val, int a0, int bits, int isSigned) {
  247. /* If the precision of A0 is "p" bits, then the transform algorithm is:
  248. B0 = (B0 + A0) & ((1 << p) - 1) */
  249. val = (val + a0) & ((1 << bits) - 1);
  250. if (isSigned) {
  251. val = bcdec__extend_sign(val, bits);
  252. }
  253. return val;
  254. }
  255. /* pretty much copy-paste from documentation */
  256. static int bcdec__unquantize(int val, int bits, int isSigned) {
  257. int unq, s = 0;
  258. if (!isSigned) {
  259. if (bits >= 15) {
  260. unq = val;
  261. } else if (!val) {
  262. unq = 0;
  263. } else if (val == ((1 << bits) - 1)) {
  264. unq = 0xFFFF;
  265. } else {
  266. unq = ((val << 16) + 0x8000) >> bits;
  267. }
  268. } else {
  269. if (bits >= 16) {
  270. unq = val;
  271. } else {
  272. if (val < 0) {
  273. s = 1;
  274. val = -val;
  275. }
  276. if (val == 0) {
  277. unq = 0;
  278. } else if (val >= ((1 << (bits - 1)) - 1)) {
  279. unq = 0x7FFF;
  280. } else {
  281. unq = ((val << 15) + 0x4000) >> (bits - 1);
  282. }
  283. if (s) {
  284. unq = -unq;
  285. }
  286. }
  287. }
  288. return unq;
  289. }
  290. static int bcdec__interpolate(int a, int b, int* weights, int index) {
  291. return (a * (64 - weights[index]) + b * weights[index] + 32) >> 6;
  292. }
  293. static unsigned short bcdec__finish_unquantize(int val, int isSigned) {
  294. int s;
  295. if (!isSigned) {
  296. return (unsigned short)((val * 31) >> 6); /* scale the magnitude by 31 / 64 */
  297. } else {
  298. val = (val < 0) ? -(((-val) * 31) >> 5) : (val * 31) >> 5; /* scale the magnitude by 31 / 32 */
  299. s = 0;
  300. if (val < 0) {
  301. s = 0x8000;
  302. val = -val;
  303. }
  304. return (unsigned short)(s | val);
  305. }
  306. }
  307. /* modified half_to_float_fast4 from https://gist.github.com/rygorous/2144712 */
  308. static float bcdec__half_to_float_quick(unsigned short half) {
  309. typedef union {
  310. unsigned int u;
  311. float f;
  312. } FP32;
  313. static const FP32 magic = { 113 << 23 };
  314. static const unsigned int shifted_exp = 0x7c00 << 13; /* exponent mask after shift */
  315. FP32 o;
  316. unsigned int exp;
  317. o.u = (half & 0x7fff) << 13; /* exponent/mantissa bits */
  318. exp = shifted_exp & o.u; /* just the exponent */
  319. o.u += (127 - 15) << 23; /* exponent adjust */
  320. /* handle exponent special cases */
  321. if (exp == shifted_exp) { /* Inf/NaN? */
  322. o.u += (128 - 16) << 23; /* extra exp adjust */
  323. } else if (exp == 0) { /* Zero/Denormal? */
  324. o.u += 1 << 23; /* extra exp adjust */
  325. o.f -= magic.f; /* renormalize */
  326. }
  327. o.u |= (half & 0x8000) << 16; /* sign bit */
  328. return o.f;
  329. }
  330. BCDECDEF void bcdec_bc6h_half(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned) {
  331. static char actual_bits_count[4][14] = {
  332. { 10, 7, 11, 11, 11, 9, 8, 8, 8, 6, 10, 11, 12, 16 }, /* W */
  333. { 5, 6, 5, 4, 4, 5, 6, 5, 5, 6, 10, 9, 8, 4 }, /* dR */
  334. { 5, 6, 4, 5, 4, 5, 5, 6, 5, 6, 10, 9, 8, 4 }, /* dG */
  335. { 5, 6, 4, 4, 5, 5, 5, 5, 6, 6, 10, 9, 8, 4 } /* dB */
  336. };
  337. /* There are 32 possible partition sets for a two-region tile.
  338. Each 4x4 block represents a single shape.
  339. Here also every fix-up index has MSB bit set. */
  340. static unsigned char partition_sets[32][4][4] = {
  341. { {128, 0, 1, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 0, 1, 129} }, /* 0 */
  342. { {128, 0, 0, 1}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 0, 129} }, /* 1 */
  343. { {128, 1, 1, 1}, {0, 1, 1, 1}, { 0, 1, 1, 1}, {0, 1, 1, 129} }, /* 2 */
  344. { {128, 0, 0, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 1, 1, 129} }, /* 3 */
  345. { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 1, 129} }, /* 4 */
  346. { {128, 0, 1, 1}, {0, 1, 1, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 5 */
  347. { {128, 0, 0, 1}, {0, 0, 1, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 6 */
  348. { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 0, 1, 1}, {0, 1, 1, 129} }, /* 7 */
  349. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 1}, {0, 0, 1, 129} }, /* 8 */
  350. { {128, 0, 1, 1}, {0, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 9 */
  351. { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 10 */
  352. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 1}, {0, 1, 1, 129} }, /* 11 */
  353. { {128, 0, 0, 1}, {0, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 12 */
  354. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 13 */
  355. { {128, 0, 0, 0}, {1, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 14 */
  356. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 0}, {1, 1, 1, 129} }, /* 15 */
  357. { {128, 0, 0, 0}, {1, 0, 0, 0}, { 1, 1, 1, 0}, {1, 1, 1, 129} }, /* 16 */
  358. { {128, 1, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 0}, {0, 0, 0, 0} }, /* 17 */
  359. { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 1, 0} }, /* 18 */
  360. { {128, 1, 129, 1}, {0, 0, 1, 1}, { 0, 0, 0, 1}, {0, 0, 0, 0} }, /* 19 */
  361. { {128, 0, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 0}, {0, 0, 0, 0} }, /* 20 */
  362. { {128, 0, 0, 0}, {1, 0, 0, 0}, {129, 1, 0, 0}, {1, 1, 1, 0} }, /* 21 */
  363. { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 0, 0} }, /* 22 */
  364. { {128, 1, 1, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 0, 0, 129} }, /* 23 */
  365. { {128, 0, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 0, 0} }, /* 24 */
  366. { {128, 0, 0, 0}, {1, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 0, 0} }, /* 25 */
  367. { {128, 1, 129, 0}, {0, 1, 1, 0}, { 0, 1, 1, 0}, {0, 1, 1, 0} }, /* 26 */
  368. { {128, 0, 129, 1}, {0, 1, 1, 0}, { 0, 1, 1, 0}, {1, 1, 0, 0} }, /* 27 */
  369. { {128, 0, 0, 1}, {0, 1, 1, 1}, {129, 1, 1, 0}, {1, 0, 0, 0} }, /* 28 */
  370. { {128, 0, 0, 0}, {1, 1, 1, 1}, {129, 1, 1, 1}, {0, 0, 0, 0} }, /* 29 */
  371. { {128, 1, 129, 1}, {0, 0, 0, 1}, { 1, 0, 0, 0}, {1, 1, 1, 0} }, /* 30 */
  372. { {128, 0, 129, 1}, {1, 0, 0, 1}, { 1, 0, 0, 1}, {1, 1, 0, 0} } /* 31 */
  373. };
  374. static int aWeight3[8] = { 0, 9, 18, 27, 37, 46, 55, 64 };
  375. static int aWeight4[16] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
  376. bcdec__bitstream_t bstream;
  377. int mode, partition, numPartitions, i, j, partitionSet, indexBits, index, ep_i, actualBits0Mode;
  378. int r[4], g[4], b[4]; /* wxyz */
  379. unsigned short* decompressed;
  380. int* weights;
  381. decompressed = (unsigned short*)decompressedBlock;
  382. bstream.low = ((unsigned long long*)compressedBlock)[0];
  383. bstream.high = ((unsigned long long*)compressedBlock)[1];
  384. r[0] = r[1] = r[2] = r[3] = 0;
  385. g[0] = g[1] = g[2] = g[3] = 0;
  386. b[0] = b[1] = b[2] = b[3] = 0;
  387. mode = bcdec__bitstream_read_bits(&bstream, 2);
  388. if (mode > 1) {
  389. mode |= (bcdec__bitstream_read_bits(&bstream, 3) << 2);
  390. }
  391. /* modes >= 11 (10 in my code) are using 0 one, others will read it from the bitstream */
  392. partition = 0;
  393. switch (mode) {
  394. /* mode 1 */
  395. case 0b00: {
  396. /* Partitition indices: 46 bits
  397. Partition: 5 bits
  398. Color Endpoints: 75 bits (10.555, 10.555, 10.555) */
  399. g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */
  400. b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */
  401. b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */
  402. r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */
  403. g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */
  404. b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */
  405. r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* rx[4:0] */
  406. g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */
  407. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  408. g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */
  409. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  410. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */
  411. b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */
  412. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  413. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  414. r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */
  415. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  416. r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */
  417. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  418. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  419. mode = 0;
  420. } break;
  421. /* mode 2 */
  422. case 0b01: {
  423. /* Partitition indices: 46 bits
  424. Partition: 5 bits
  425. Color Endpoints: 75 bits (7666, 7666, 7666) */
  426. g[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gy[5] */
  427. g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */
  428. g[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gz[5] */
  429. r[0] |= bcdec__bitstream_read_bits(&bstream, 7); /* rw[6:0] */
  430. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  431. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  432. b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */
  433. g[0] |= bcdec__bitstream_read_bits(&bstream, 7); /* gw[6:0] */
  434. b[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* by[5] */
  435. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  436. g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */
  437. b[0] |= bcdec__bitstream_read_bits(&bstream, 7); /* bw[6:0] */
  438. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  439. b[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* bz[5] */
  440. b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */
  441. r[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* rx[5:0] */
  442. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  443. g[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* gx[5:0] */
  444. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */
  445. b[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* bx[5:0] */
  446. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  447. r[2] |= bcdec__bitstream_read_bits(&bstream, 6); /* ry[5:0] */
  448. r[3] |= bcdec__bitstream_read_bits(&bstream, 6); /* rz[5:0] */
  449. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  450. mode = 1;
  451. } break;
  452. /* mode 3 */
  453. case 0b00010: {
  454. /* Partitition indices: 46 bits
  455. Partition: 5 bits
  456. Color Endpoints: 72 bits (11.555, 11.444, 11.444) */
  457. r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */
  458. g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */
  459. b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */
  460. r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* rx[4:0] */
  461. r[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* rw[10] */
  462. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  463. g[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* gx[3:0] */
  464. g[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* gw[10] */
  465. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  466. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */
  467. b[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* bx[3:0] */
  468. b[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* bw[10] */
  469. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  470. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  471. r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */
  472. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  473. r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */
  474. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  475. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  476. mode = 2;
  477. } break;
  478. /* mode 4 */
  479. case 0b00110: {
  480. /* Partitition indices: 46 bits
  481. Partition: 5 bits
  482. Color Endpoints: 72 bits (11.444, 11.555, 11.444) */
  483. r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */
  484. g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */
  485. b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */
  486. r[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* rx[3:0] */
  487. r[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* rw[10] */
  488. g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */
  489. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  490. g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */
  491. g[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* gw[10] */
  492. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */
  493. b[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* bx[3:0] */
  494. b[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* bw[10] */
  495. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  496. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  497. r[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* ry[3:0] */
  498. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  499. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  500. r[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* rz[3:0] */
  501. g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */
  502. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  503. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  504. mode = 3;
  505. } break;
  506. /* mode 5 */
  507. case 0b01010: {
  508. /* Partitition indices: 46 bits
  509. Partition: 5 bits
  510. Color Endpoints: 72 bits (11.444, 11.444, 11.555) */
  511. r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */
  512. g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */
  513. b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */
  514. r[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* rx[3:0] */
  515. r[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* rw[10] */
  516. b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */
  517. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  518. g[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* gx[3:0] */
  519. g[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* gw[10] */
  520. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  521. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */
  522. b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */
  523. b[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* bw[10] */
  524. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  525. r[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* ry[3:0] */
  526. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  527. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  528. r[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* rz[3:0] */
  529. b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */
  530. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  531. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  532. mode = 4;
  533. } break;
  534. /* mode 6 */
  535. case 0b01110: {
  536. /* Partitition indices: 46 bits
  537. Partition: 5 bits
  538. Color Endpoints: 72 bits (9555, 9555, 9555) */
  539. r[0] |= bcdec__bitstream_read_bits(&bstream, 9); /* rw[8:0] */
  540. b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */
  541. g[0] |= bcdec__bitstream_read_bits(&bstream, 9); /* gw[8:0] */
  542. g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */
  543. b[0] |= bcdec__bitstream_read_bits(&bstream, 9); /* bw[8:0] */
  544. b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */
  545. r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* rx[4:0] */
  546. g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */
  547. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  548. g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */
  549. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  550. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gx[3:0] */
  551. b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */
  552. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  553. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  554. r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */
  555. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  556. r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */
  557. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  558. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  559. mode = 5;
  560. } break;
  561. /* mode 7 */
  562. case 0b10010: {
  563. /* Partitition indices: 46 bits
  564. Partition: 5 bits
  565. Color Endpoints: 72 bits (8666, 8555, 8555) */
  566. r[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* rw[7:0] */
  567. g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */
  568. b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */
  569. g[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* gw[7:0] */
  570. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  571. g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */
  572. b[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* bw[7:0] */
  573. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  574. b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */
  575. r[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* rx[5:0] */
  576. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  577. g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */
  578. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  579. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */
  580. b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */
  581. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  582. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  583. r[2] |= bcdec__bitstream_read_bits(&bstream, 6); /* ry[5:0] */
  584. r[3] |= bcdec__bitstream_read_bits(&bstream, 6); /* rz[5:0] */
  585. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  586. mode = 6;
  587. } break;
  588. /* mode 8 */
  589. case 0b10110: {
  590. /* Partitition indices: 46 bits
  591. Partition: 5 bits
  592. Color Endpoints: 72 bits (8555, 8666, 8555) */
  593. r[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* rw[7:0] */
  594. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  595. b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */
  596. g[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* gw[7:0] */
  597. g[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gy[5] */
  598. g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */
  599. b[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* bw[7:0] */
  600. g[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gz[5] */
  601. b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */
  602. r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* rx[4:0] */
  603. g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */
  604. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  605. g[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* gx[5:0] */
  606. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* zx[3:0] */
  607. b[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bx[4:0] */
  608. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  609. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  610. r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */
  611. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  612. r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */
  613. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  614. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  615. mode = 7;
  616. } break;
  617. /* mode 9 */
  618. case 0b11010: {
  619. /* Partitition indices: 46 bits
  620. Partition: 5 bits
  621. Color Endpoints: 72 bits (8555, 8555, 8666) */
  622. r[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* rw[7:0] */
  623. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  624. b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */
  625. g[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* gw[7:0] */
  626. b[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* by[5] */
  627. g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */
  628. b[0] |= bcdec__bitstream_read_bits(&bstream, 8); /* bw[7:0] */
  629. b[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* bz[5] */
  630. b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */
  631. r[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* bw[4:0] */
  632. g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */
  633. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  634. g[1] |= bcdec__bitstream_read_bits(&bstream, 5); /* gx[4:0] */
  635. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  636. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */
  637. b[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* bx[5:0] */
  638. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  639. r[2] |= bcdec__bitstream_read_bits(&bstream, 5); /* ry[4:0] */
  640. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  641. r[3] |= bcdec__bitstream_read_bits(&bstream, 5); /* rz[4:0] */
  642. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  643. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  644. mode = 8;
  645. } break;
  646. /* mode 10 */
  647. case 0b11110: {
  648. /* Partitition indices: 46 bits
  649. Partition: 5 bits
  650. Color Endpoints: 72 bits (6666, 6666, 6666) */
  651. r[0] |= bcdec__bitstream_read_bits(&bstream, 6); /* rw[5:0] */
  652. g[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gz[4] */
  653. b[3] |= bcdec__bitstream_read_bit(&bstream); /* bz[0] */
  654. b[3] |= bcdec__bitstream_read_bit(&bstream) << 1; /* bz[1] */
  655. b[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* by[4] */
  656. g[0] |= bcdec__bitstream_read_bits(&bstream, 6); /* gw[5:0] */
  657. g[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gy[5] */
  658. b[2] |= bcdec__bitstream_read_bit(&bstream) << 5; /* by[5] */
  659. b[3] |= bcdec__bitstream_read_bit(&bstream) << 2; /* bz[2] */
  660. g[2] |= bcdec__bitstream_read_bit(&bstream) << 4; /* gy[4] */
  661. b[0] |= bcdec__bitstream_read_bits(&bstream, 6); /* bw[5:0] */
  662. g[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* gz[5] */
  663. b[3] |= bcdec__bitstream_read_bit(&bstream) << 3; /* bz[3] */
  664. b[3] |= bcdec__bitstream_read_bit(&bstream) << 5; /* bz[5] */
  665. b[3] |= bcdec__bitstream_read_bit(&bstream) << 4; /* bz[4] */
  666. r[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* rx[5:0] */
  667. g[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* gy[3:0] */
  668. g[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* gx[5:0] */
  669. g[3] |= bcdec__bitstream_read_bits(&bstream, 4); /* gz[3:0] */
  670. b[1] |= bcdec__bitstream_read_bits(&bstream, 6); /* bx[5:0] */
  671. b[2] |= bcdec__bitstream_read_bits(&bstream, 4); /* by[3:0] */
  672. r[2] |= bcdec__bitstream_read_bits(&bstream, 6); /* ry[5:0] */
  673. r[3] |= bcdec__bitstream_read_bits(&bstream, 6); /* rz[5:0] */
  674. partition = bcdec__bitstream_read_bits(&bstream, 5); /* d[4:0] */
  675. mode = 9;
  676. } break;
  677. /* mode 11 */
  678. case 0b00011: {
  679. /* Partitition indices: 63 bits
  680. Partition: 0 bits
  681. Color Endpoints: 60 bits (10.10, 10.10, 10.10) */
  682. r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */
  683. g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */
  684. b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */
  685. r[1] |= bcdec__bitstream_read_bits(&bstream, 10); /* rx[9:0] */
  686. g[1] |= bcdec__bitstream_read_bits(&bstream, 10); /* gx[9:0] */
  687. b[1] |= bcdec__bitstream_read_bits(&bstream, 10); /* bx[9:0] */
  688. mode = 10;
  689. } break;
  690. /* mode 12 */
  691. case 0b00111: {
  692. /* Partitition indices: 63 bits
  693. Partition: 0 bits
  694. Color Endpoints: 60 bits (11.9, 11.9, 11.9) */
  695. r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */
  696. g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */
  697. b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */
  698. r[1] |= bcdec__bitstream_read_bits(&bstream, 9); /* rx[8:0] */
  699. r[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* rw[10] */
  700. g[1] |= bcdec__bitstream_read_bits(&bstream, 9); /* gx[8:0] */
  701. g[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* gw[10] */
  702. b[1] |= bcdec__bitstream_read_bits(&bstream, 9); /* bx[8:0] */
  703. b[0] |= bcdec__bitstream_read_bit(&bstream) << 10; /* bw[10] */
  704. mode = 11;
  705. } break;
  706. /* mode 13 */
  707. case 0b01011: {
  708. /* Partitition indices: 63 bits
  709. Partition: 0 bits
  710. Color Endpoints: 60 bits (12.8, 12.8, 12.8) */
  711. r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */
  712. g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */
  713. b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */
  714. r[1] |= bcdec__bitstream_read_bits(&bstream, 8); /* rx[7:0] */
  715. r[0] |= bcdec__bitstream_read_bits_r(&bstream, 2) << 10;/* rx[10:11] */
  716. g[1] |= bcdec__bitstream_read_bits(&bstream, 8); /* gx[7:0] */
  717. g[0] |= bcdec__bitstream_read_bits_r(&bstream, 2) << 10;/* gx[10:11] */
  718. b[1] |= bcdec__bitstream_read_bits(&bstream, 8); /* bx[7:0] */
  719. b[0] |= bcdec__bitstream_read_bits_r(&bstream, 2) << 10;/* bx[10:11] */
  720. mode = 12;
  721. } break;
  722. /* mode 14 */
  723. case 0b01111: {
  724. /* Partitition indices: 63 bits
  725. Partition: 0 bits
  726. Color Endpoints: 60 bits (16.4, 16.4, 16.4) */
  727. r[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* rw[9:0] */
  728. g[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* gw[9:0] */
  729. b[0] |= bcdec__bitstream_read_bits(&bstream, 10); /* bw[9:0] */
  730. r[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* rx[3:0] */
  731. r[0] |= bcdec__bitstream_read_bits_r(&bstream, 6) << 10;/* rw[10:15] */
  732. g[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* gx[3:0] */
  733. g[0] |= bcdec__bitstream_read_bits_r(&bstream, 6) << 10;/* gw[10:15] */
  734. b[1] |= bcdec__bitstream_read_bits(&bstream, 4); /* bx[3:0] */
  735. b[0] |= bcdec__bitstream_read_bits_r(&bstream, 6) << 10;/* bw[10:15] */
  736. mode = 13;
  737. } break;
  738. default: {
  739. /* Modes 10011, 10111, 11011, and 11111 (not shown) are reserved.
  740. Do not use these in your encoder. If the hardware is passed blocks
  741. with one of these modes specified, the resulting decompressed block
  742. must contain all zeroes in all channels except for the alpha channel. */
  743. for (i = 0; i < 4; ++i) {
  744. for (j = 0; j < 4; ++j) {
  745. decompressed[j * 3 + 0] = 0;
  746. decompressed[j * 3 + 1] = 0;
  747. decompressed[j * 3 + 2] = 0;
  748. }
  749. decompressed += destinationPitch;
  750. }
  751. return;
  752. }
  753. }
  754. numPartitions = (mode >= 10) ? 0 : 1;
  755. actualBits0Mode = actual_bits_count[0][mode];
  756. if (isSigned) {
  757. r[0] = bcdec__extend_sign(r[0], actualBits0Mode);
  758. g[0] = bcdec__extend_sign(g[0], actualBits0Mode);
  759. b[0] = bcdec__extend_sign(b[0], actualBits0Mode);
  760. }
  761. /* Mode 11 (like Mode 10) does not use delta compression,
  762. and instead stores both color endpoints explicitly. */
  763. if ((mode != 9 && mode != 10) || isSigned) {
  764. for (i = 1; i < (numPartitions + 1) * 2; ++i) {
  765. r[i] = bcdec__extend_sign(r[i], actual_bits_count[1][mode]);
  766. g[i] = bcdec__extend_sign(g[i], actual_bits_count[2][mode]);
  767. b[i] = bcdec__extend_sign(b[i], actual_bits_count[3][mode]);
  768. }
  769. }
  770. if (mode != 9 && mode != 10) {
  771. for (i = 1; i < (numPartitions + 1) * 2; ++i) {
  772. r[i] = bcdec__transform_inverse(r[i], r[0], actualBits0Mode, isSigned);
  773. g[i] = bcdec__transform_inverse(g[i], g[0], actualBits0Mode, isSigned);
  774. b[i] = bcdec__transform_inverse(b[i], b[0], actualBits0Mode, isSigned);
  775. }
  776. }
  777. for (i = 0; i < (numPartitions + 1) * 2; ++i) {
  778. r[i] = bcdec__unquantize(r[i], actualBits0Mode, isSigned);
  779. g[i] = bcdec__unquantize(g[i], actualBits0Mode, isSigned);
  780. b[i] = bcdec__unquantize(b[i], actualBits0Mode, isSigned);
  781. }
  782. weights = (mode >= 10) ? aWeight4 : aWeight3;
  783. for (i = 0; i < 4; ++i) {
  784. for (j = 0; j < 4; ++j) {
  785. partitionSet = (mode >= 10) ? ((i|j) ? 0 : 128) : partition_sets[partition][i][j];
  786. indexBits = (mode >= 10) ? 4 : 3;
  787. /* fix-up index is specified with one less bit */
  788. /* The fix-up index for subset 0 is always index 0 */
  789. if (partitionSet & 0x80) {
  790. indexBits--;
  791. }
  792. partitionSet &= 0x01;
  793. index = bcdec__bitstream_read_bits(&bstream, indexBits);
  794. ep_i = partitionSet * 2;
  795. decompressed[j * 3 + 0] = bcdec__finish_unquantize(
  796. bcdec__interpolate(r[ep_i], r[ep_i+1], weights, index), isSigned);
  797. decompressed[j * 3 + 1] = bcdec__finish_unquantize(
  798. bcdec__interpolate(g[ep_i], g[ep_i+1], weights, index), isSigned);
  799. decompressed[j * 3 + 2] = bcdec__finish_unquantize(
  800. bcdec__interpolate(b[ep_i], b[ep_i+1], weights, index), isSigned);
  801. }
  802. decompressed += destinationPitch;
  803. }
  804. }
  805. BCDECDEF void bcdec_bc6h_float(const void* compressedBlock, void* decompressedBlock, int destinationPitch, int isSigned) {
  806. unsigned short block[16*3];
  807. float* decompressed;
  808. const unsigned short* b;
  809. int i, j;
  810. bcdec_bc6h_half(compressedBlock, block, 4*3, isSigned);
  811. b = block;
  812. decompressed = (float*)decompressedBlock;
  813. for (i = 0; i < 4; ++i) {
  814. for (j = 0; j < 4; ++j) {
  815. decompressed[j * 3 + 0] = bcdec__half_to_float_quick(*b++);
  816. decompressed[j * 3 + 1] = bcdec__half_to_float_quick(*b++);
  817. decompressed[j * 3 + 2] = bcdec__half_to_float_quick(*b++);
  818. }
  819. decompressed += destinationPitch;
  820. }
  821. }
  822. static void bcdec__swap_values(int* a, int* b) {
  823. a[0] ^= b[0], b[0] ^= a[0], a[0] ^= b[0];
  824. }
  825. BCDECDEF void bcdec_bc7(const void* compressedBlock, void* decompressedBlock, int destinationPitch) {
  826. static char actual_bits_count[2][8] = {
  827. { 4, 6, 5, 7, 5, 7, 7, 5 }, /* RGBA */
  828. { 0, 0, 0, 0, 6, 8, 7, 5 }, /* Alpha */
  829. };
  830. /* There are 64 possible partition sets for a two-region tile.
  831. Each 4x4 block represents a single shape.
  832. Here also every fix-up index has MSB bit set. */
  833. static unsigned char partition_sets[2][64][4][4] = {
  834. { /* Partition table for 2-subset BPTC */
  835. { {128, 0, 1, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 0, 1, 129} }, /* 0 */
  836. { {128, 0, 0, 1}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 0, 129} }, /* 1 */
  837. { {128, 1, 1, 1}, {0, 1, 1, 1}, { 0, 1, 1, 1}, {0, 1, 1, 129} }, /* 2 */
  838. { {128, 0, 0, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 1, 1, 129} }, /* 3 */
  839. { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 1, 129} }, /* 4 */
  840. { {128, 0, 1, 1}, {0, 1, 1, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 5 */
  841. { {128, 0, 0, 1}, {0, 0, 1, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 6 */
  842. { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 0, 1, 1}, {0, 1, 1, 129} }, /* 7 */
  843. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 1}, {0, 0, 1, 129} }, /* 8 */
  844. { {128, 0, 1, 1}, {0, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 9 */
  845. { {128, 0, 0, 0}, {0, 0, 0, 1}, { 0, 1, 1, 1}, {1, 1, 1, 129} }, /* 10 */
  846. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 1}, {0, 1, 1, 129} }, /* 11 */
  847. { {128, 0, 0, 1}, {0, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 12 */
  848. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 13 */
  849. { {128, 0, 0, 0}, {1, 1, 1, 1}, { 1, 1, 1, 1}, {1, 1, 1, 129} }, /* 14 */
  850. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 0}, {1, 1, 1, 129} }, /* 15 */
  851. { {128, 0, 0, 0}, {1, 0, 0, 0}, { 1, 1, 1, 0}, {1, 1, 1, 129} }, /* 16 */
  852. { {128, 1, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 0}, {0, 0, 0, 0} }, /* 17 */
  853. { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 1, 0} }, /* 18 */
  854. { {128, 1, 129, 1}, {0, 0, 1, 1}, { 0, 0, 0, 1}, {0, 0, 0, 0} }, /* 19 */
  855. { {128, 0, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 0}, {0, 0, 0, 0} }, /* 20 */
  856. { {128, 0, 0, 0}, {1, 0, 0, 0}, {129, 1, 0, 0}, {1, 1, 1, 0} }, /* 21 */
  857. { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 0, 0} }, /* 22 */
  858. { {128, 1, 1, 1}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {0, 0, 0, 129} }, /* 23 */
  859. { {128, 0, 129, 1}, {0, 0, 0, 1}, { 0, 0, 0, 1}, {0, 0, 0, 0} }, /* 24 */
  860. { {128, 0, 0, 0}, {1, 0, 0, 0}, {129, 0, 0, 0}, {1, 1, 0, 0} }, /* 25 */
  861. { {128, 1, 129, 0}, {0, 1, 1, 0}, { 0, 1, 1, 0}, {0, 1, 1, 0} }, /* 26 */
  862. { {128, 0, 129, 1}, {0, 1, 1, 0}, { 0, 1, 1, 0}, {1, 1, 0, 0} }, /* 27 */
  863. { {128, 0, 0, 1}, {0, 1, 1, 1}, {129, 1, 1, 0}, {1, 0, 0, 0} }, /* 28 */
  864. { {128, 0, 0, 0}, {1, 1, 1, 1}, {129, 1, 1, 1}, {0, 0, 0, 0} }, /* 29 */
  865. { {128, 1, 129, 1}, {0, 0, 0, 1}, { 1, 0, 0, 0}, {1, 1, 1, 0} }, /* 30 */
  866. { {128, 0, 129, 1}, {1, 0, 0, 1}, { 1, 0, 0, 1}, {1, 1, 0, 0} }, /* 31 */
  867. { {128, 1, 0, 1}, {0, 1, 0, 1}, { 0, 1, 0, 1}, {0, 1, 0, 129} }, /* 32 */
  868. { {128, 0, 0, 0}, {1, 1, 1, 1}, { 0, 0, 0, 0}, {1, 1, 1, 129} }, /* 33 */
  869. { {128, 1, 0, 1}, {1, 0, 129, 0}, { 0, 1, 0, 1}, {1, 0, 1, 0} }, /* 34 */
  870. { {128, 0, 1, 1}, {0, 0, 1, 1}, {129, 1, 0, 0}, {1, 1, 0, 0} }, /* 35 */
  871. { {128, 0, 129, 1}, {1, 1, 0, 0}, { 0, 0, 1, 1}, {1, 1, 0, 0} }, /* 36 */
  872. { {128, 1, 0, 1}, {0, 1, 0, 1}, {129, 0, 1, 0}, {1, 0, 1, 0} }, /* 37 */
  873. { {128, 1, 1, 0}, {1, 0, 0, 1}, { 0, 1, 1, 0}, {1, 0, 0, 129} }, /* 38 */
  874. { {128, 1, 0, 1}, {1, 0, 1, 0}, { 1, 0, 1, 0}, {0, 1, 0, 129} }, /* 39 */
  875. { {128, 1, 129, 1}, {0, 0, 1, 1}, { 1, 1, 0, 0}, {1, 1, 1, 0} }, /* 40 */
  876. { {128, 0, 0, 1}, {0, 0, 1, 1}, {129, 1, 0, 0}, {1, 0, 0, 0} }, /* 41 */
  877. { {128, 0, 129, 1}, {0, 0, 1, 0}, { 0, 1, 0, 0}, {1, 1, 0, 0} }, /* 42 */
  878. { {128, 0, 129, 1}, {1, 0, 1, 1}, { 1, 1, 0, 1}, {1, 1, 0, 0} }, /* 43 */
  879. { {128, 1, 129, 0}, {1, 0, 0, 1}, { 1, 0, 0, 1}, {0, 1, 1, 0} }, /* 44 */
  880. { {128, 0, 1, 1}, {1, 1, 0, 0}, { 1, 1, 0, 0}, {0, 0, 1, 129} }, /* 45 */
  881. { {128, 1, 1, 0}, {0, 1, 1, 0}, { 1, 0, 0, 1}, {1, 0, 0, 129} }, /* 46 */
  882. { {128, 0, 0, 0}, {0, 1, 129, 0}, { 0, 1, 1, 0}, {0, 0, 0, 0} }, /* 47 */
  883. { {128, 1, 0, 0}, {1, 1, 129, 0}, { 0, 1, 0, 0}, {0, 0, 0, 0} }, /* 48 */
  884. { {128, 0, 129, 0}, {0, 1, 1, 1}, { 0, 0, 1, 0}, {0, 0, 0, 0} }, /* 49 */
  885. { {128, 0, 0, 0}, {0, 0, 129, 0}, { 0, 1, 1, 1}, {0, 0, 1, 0} }, /* 50 */
  886. { {128, 0, 0, 0}, {0, 1, 0, 0}, {129, 1, 1, 0}, {0, 1, 0, 0} }, /* 51 */
  887. { {128, 1, 1, 0}, {1, 1, 0, 0}, { 1, 0, 0, 1}, {0, 0, 1, 129} }, /* 52 */
  888. { {128, 0, 1, 1}, {0, 1, 1, 0}, { 1, 1, 0, 0}, {1, 0, 0, 129} }, /* 53 */
  889. { {128, 1, 129, 0}, {0, 0, 1, 1}, { 1, 0, 0, 1}, {1, 1, 0, 0} }, /* 54 */
  890. { {128, 0, 129, 1}, {1, 0, 0, 1}, { 1, 1, 0, 0}, {0, 1, 1, 0} }, /* 55 */
  891. { {128, 1, 1, 0}, {1, 1, 0, 0}, { 1, 1, 0, 0}, {1, 0, 0, 129} }, /* 56 */
  892. { {128, 1, 1, 0}, {0, 0, 1, 1}, { 0, 0, 1, 1}, {1, 0, 0, 129} }, /* 57 */
  893. { {128, 1, 1, 1}, {1, 1, 1, 0}, { 1, 0, 0, 0}, {0, 0, 0, 129} }, /* 58 */
  894. { {128, 0, 0, 1}, {1, 0, 0, 0}, { 1, 1, 1, 0}, {0, 1, 1, 129} }, /* 59 */
  895. { {128, 0, 0, 0}, {1, 1, 1, 1}, { 0, 0, 1, 1}, {0, 0, 1, 129} }, /* 60 */
  896. { {128, 0, 129, 1}, {0, 0, 1, 1}, { 1, 1, 1, 1}, {0, 0, 0, 0} }, /* 61 */
  897. { {128, 0, 129, 0}, {0, 0, 1, 0}, { 1, 1, 1, 0}, {1, 1, 1, 0} }, /* 62 */
  898. { {128, 1, 0, 0}, {0, 1, 0, 0}, { 0, 1, 1, 1}, {0, 1, 1, 129} } /* 63 */
  899. },
  900. { /* Partition table for 3-subset BPTC */
  901. { {128, 0, 1, 129}, {0, 0, 1, 1}, { 0, 2, 2, 1}, { 2, 2, 2, 130} }, /* 0 */
  902. { {128, 0, 0, 129}, {0, 0, 1, 1}, {130, 2, 1, 1}, { 2, 2, 2, 1} }, /* 1 */
  903. { {128, 0, 0, 0}, {2, 0, 0, 1}, {130, 2, 1, 1}, { 2, 2, 1, 129} }, /* 2 */
  904. { {128, 2, 2, 130}, {0, 0, 2, 2}, { 0, 0, 1, 1}, { 0, 1, 1, 129} }, /* 3 */
  905. { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 1, 2, 2}, { 1, 1, 2, 130} }, /* 4 */
  906. { {128, 0, 1, 129}, {0, 0, 1, 1}, { 0, 0, 2, 2}, { 0, 0, 2, 130} }, /* 5 */
  907. { {128, 0, 2, 130}, {0, 0, 2, 2}, { 1, 1, 1, 1}, { 1, 1, 1, 129} }, /* 6 */
  908. { {128, 0, 1, 1}, {0, 0, 1, 1}, {130, 2, 1, 1}, { 2, 2, 1, 129} }, /* 7 */
  909. { {128, 0, 0, 0}, {0, 0, 0, 0}, {129, 1, 1, 1}, { 2, 2, 2, 130} }, /* 8 */
  910. { {128, 0, 0, 0}, {1, 1, 1, 1}, {129, 1, 1, 1}, { 2, 2, 2, 130} }, /* 9 */
  911. { {128, 0, 0, 0}, {1, 1, 129, 1}, { 2, 2, 2, 2}, { 2, 2, 2, 130} }, /* 10 */
  912. { {128, 0, 1, 2}, {0, 0, 129, 2}, { 0, 0, 1, 2}, { 0, 0, 1, 130} }, /* 11 */
  913. { {128, 1, 1, 2}, {0, 1, 129, 2}, { 0, 1, 1, 2}, { 0, 1, 1, 130} }, /* 12 */
  914. { {128, 1, 2, 2}, {0, 129, 2, 2}, { 0, 1, 2, 2}, { 0, 1, 2, 130} }, /* 13 */
  915. { {128, 0, 1, 129}, {0, 1, 1, 2}, { 1, 1, 2, 2}, { 1, 2, 2, 130} }, /* 14 */
  916. { {128, 0, 1, 129}, {2, 0, 0, 1}, {130, 2, 0, 0}, { 2, 2, 2, 0} }, /* 15 */
  917. { {128, 0, 0, 129}, {0, 0, 1, 1}, { 0, 1, 1, 2}, { 1, 1, 2, 130} }, /* 16 */
  918. { {128, 1, 1, 129}, {0, 0, 1, 1}, {130, 0, 0, 1}, { 2, 2, 0, 0} }, /* 17 */
  919. { {128, 0, 0, 0}, {1, 1, 2, 2}, {129, 1, 2, 2}, { 1, 1, 2, 130} }, /* 18 */
  920. { {128, 0, 2, 130}, {0, 0, 2, 2}, { 0, 0, 2, 2}, { 1, 1, 1, 129} }, /* 19 */
  921. { {128, 1, 1, 129}, {0, 1, 1, 1}, { 0, 2, 2, 2}, { 0, 2, 2, 130} }, /* 20 */
  922. { {128, 0, 0, 129}, {0, 0, 0, 1}, {130, 2, 2, 1}, { 2, 2, 2, 1} }, /* 21 */
  923. { {128, 0, 0, 0}, {0, 0, 129, 1}, { 0, 1, 2, 2}, { 0, 1, 2, 130} }, /* 22 */
  924. { {128, 0, 0, 0}, {1, 1, 0, 0}, {130, 2, 129, 0}, { 2, 2, 1, 0} }, /* 23 */
  925. { {128, 1, 2, 130}, {0, 129, 2, 2}, { 0, 0, 1, 1}, { 0, 0, 0, 0} }, /* 24 */
  926. { {128, 0, 1, 2}, {0, 0, 1, 2}, {129, 1, 2, 2}, { 2, 2, 2, 130} }, /* 25 */
  927. { {128, 1, 1, 0}, {1, 2, 130, 1}, {129, 2, 2, 1}, { 0, 1, 1, 0} }, /* 26 */
  928. { {128, 0, 0, 0}, {0, 1, 129, 0}, { 1, 2, 130, 1}, { 1, 2, 2, 1} }, /* 27 */
  929. { {128, 0, 2, 2}, {1, 1, 0, 2}, {129, 1, 0, 2}, { 0, 0, 2, 130} }, /* 28 */
  930. { {128, 1, 1, 0}, {0, 129, 1, 0}, { 2, 0, 0, 2}, { 2, 2, 2, 130} }, /* 29 */
  931. { {128, 0, 1, 1}, {0, 1, 2, 2}, { 0, 1, 130, 2}, { 0, 0, 1, 129} }, /* 30 */
  932. { {128, 0, 0, 0}, {2, 0, 0, 0}, {130, 2, 1, 1}, { 2, 2, 2, 129} }, /* 31 */
  933. { {128, 0, 0, 0}, {0, 0, 0, 2}, {129, 1, 2, 2}, { 1, 2, 2, 130} }, /* 32 */
  934. { {128, 2, 2, 130}, {0, 0, 2, 2}, { 0, 0, 1, 2}, { 0, 0, 1, 129} }, /* 33 */
  935. { {128, 0, 1, 129}, {0, 0, 1, 2}, { 0, 0, 2, 2}, { 0, 2, 2, 130} }, /* 34 */
  936. { {128, 1, 2, 0}, {0, 129, 2, 0}, { 0, 1, 130, 0}, { 0, 1, 2, 0} }, /* 35 */
  937. { {128, 0, 0, 0}, {1, 1, 129, 1}, { 2, 2, 130, 2}, { 0, 0, 0, 0} }, /* 36 */
  938. { {128, 1, 2, 0}, {1, 2, 0, 1}, {130, 0, 129, 2}, { 0, 1, 2, 0} }, /* 37 */
  939. { {128, 1, 2, 0}, {2, 0, 1, 2}, {129, 130, 0, 1}, { 0, 1, 2, 0} }, /* 38 */
  940. { {128, 0, 1, 1}, {2, 2, 0, 0}, { 1, 1, 130, 2}, { 0, 0, 1, 129} }, /* 39 */
  941. { {128, 0, 1, 1}, {1, 1, 130, 2}, { 2, 2, 0, 0}, { 0, 0, 1, 129} }, /* 40 */
  942. { {128, 1, 0, 129}, {0, 1, 0, 1}, { 2, 2, 2, 2}, { 2, 2, 2, 130} }, /* 41 */
  943. { {128, 0, 0, 0}, {0, 0, 0, 0}, {130, 1, 2, 1}, { 2, 1, 2, 129} }, /* 42 */
  944. { {128, 0, 2, 2}, {1, 129, 2, 2}, { 0, 0, 2, 2}, { 1, 1, 2, 130} }, /* 43 */
  945. { {128, 0, 2, 130}, {0, 0, 1, 1}, { 0, 0, 2, 2}, { 0, 0, 1, 129} }, /* 44 */
  946. { {128, 2, 2, 0}, {1, 2, 130, 1}, { 0, 2, 2, 0}, { 1, 2, 2, 129} }, /* 45 */
  947. { {128, 1, 0, 1}, {2, 2, 130, 2}, { 2, 2, 2, 2}, { 0, 1, 0, 129} }, /* 46 */
  948. { {128, 0, 0, 0}, {2, 1, 2, 1}, {130, 1, 2, 1}, { 2, 1, 2, 129} }, /* 47 */
  949. { {128, 1, 0, 129}, {0, 1, 0, 1}, { 0, 1, 0, 1}, { 2, 2, 2, 130} }, /* 48 */
  950. { {128, 2, 2, 130}, {0, 1, 1, 1}, { 0, 2, 2, 2}, { 0, 1, 1, 129} }, /* 49 */
  951. { {128, 0, 0, 2}, {1, 129, 1, 2}, { 0, 0, 0, 2}, { 1, 1, 1, 130} }, /* 50 */
  952. { {128, 0, 0, 0}, {2, 129, 1, 2}, { 2, 1, 1, 2}, { 2, 1, 1, 130} }, /* 51 */
  953. { {128, 2, 2, 2}, {0, 129, 1, 1}, { 0, 1, 1, 1}, { 0, 2, 2, 130} }, /* 52 */
  954. { {128, 0, 0, 2}, {1, 1, 1, 2}, {129, 1, 1, 2}, { 0, 0, 0, 130} }, /* 53 */
  955. { {128, 1, 1, 0}, {0, 129, 1, 0}, { 0, 1, 1, 0}, { 2, 2, 2, 130} }, /* 54 */
  956. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 2, 1, 129, 2}, { 2, 1, 1, 130} }, /* 55 */
  957. { {128, 1, 1, 0}, {0, 129, 1, 0}, { 2, 2, 2, 2}, { 2, 2, 2, 130} }, /* 56 */
  958. { {128, 0, 2, 2}, {0, 0, 1, 1}, { 0, 0, 129, 1}, { 0, 0, 2, 130} }, /* 57 */
  959. { {128, 0, 2, 2}, {1, 1, 2, 2}, {129, 1, 2, 2}, { 0, 0, 2, 130} }, /* 58 */
  960. { {128, 0, 0, 0}, {0, 0, 0, 0}, { 0, 0, 0, 0}, { 2, 129, 1, 130} }, /* 59 */
  961. { {128, 0, 0, 130}, {0, 0, 0, 1}, { 0, 0, 0, 2}, { 0, 0, 0, 129} }, /* 60 */
  962. { {128, 2, 2, 2}, {1, 2, 2, 2}, { 0, 2, 2, 2}, {129, 2, 2, 130} }, /* 61 */
  963. { {128, 1, 0, 129}, {2, 2, 2, 2}, { 2, 2, 2, 2}, { 2, 2, 2, 130} }, /* 62 */
  964. { {128, 1, 1, 129}, {2, 0, 1, 1}, {130, 2, 0, 1}, { 2, 2, 2, 0} } /* 63 */
  965. }
  966. };
  967. static int aWeight2[] = { 0, 21, 43, 64 };
  968. static int aWeight3[] = { 0, 9, 18, 27, 37, 46, 55, 64 };
  969. static int aWeight4[] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
  970. static unsigned char sModeHasPBits = 0b11001011;
  971. bcdec__bitstream_t bstream;
  972. int mode, partition, numPartitions, numEndpoints, i, j, k, rotation, partitionSet;
  973. int indexSelectionBit, indexBits, indexBits2, index, index2;
  974. int endpoints[6][4];
  975. char indices[4][4];
  976. int r, g, b, a;
  977. int* weights, * weights2;
  978. unsigned char* decompressed;
  979. decompressed = (unsigned char*)decompressedBlock;
  980. bstream.low = ((unsigned long long*)compressedBlock)[0];
  981. bstream.high = ((unsigned long long*)compressedBlock)[1];
  982. for (mode = 0; mode < 8 && (0 == bcdec__bitstream_read_bit(&bstream)); ++mode);
  983. /* unexpected mode, clear the block (transparent black) */
  984. if (mode >= 8) {
  985. for (i = 0; i < 4; ++i) {
  986. for (j = 0; j < 4; ++j) {
  987. decompressed[j * 4 + 0] = 0;
  988. decompressed[j * 4 + 1] = 0;
  989. decompressed[j * 4 + 2] = 0;
  990. decompressed[j * 4 + 3] = 0;
  991. }
  992. decompressed += destinationPitch;
  993. }
  994. return;
  995. }
  996. partition = 0;
  997. numPartitions = 1;
  998. rotation = 0;
  999. indexSelectionBit = 0;
  1000. if (mode == 0 || mode == 1 || mode == 2 || mode == 3 || mode == 7) {
  1001. numPartitions = (mode == 0 || mode == 2) ? 3 : 2;
  1002. partition = bcdec__bitstream_read_bits(&bstream, (mode == 0) ? 4 : 6);
  1003. }
  1004. numEndpoints = numPartitions * 2;
  1005. if (mode == 4 || mode == 5) {
  1006. rotation = bcdec__bitstream_read_bits(&bstream, 2);
  1007. if (mode == 4) {
  1008. indexSelectionBit = bcdec__bitstream_read_bit(&bstream);
  1009. }
  1010. }
  1011. /* Extract endpoints */
  1012. /* RGB */
  1013. for (i = 0; i < 3; ++i) {
  1014. for (j = 0; j < numEndpoints; ++j) {
  1015. endpoints[j][i] = bcdec__bitstream_read_bits(&bstream, actual_bits_count[0][mode]);
  1016. }
  1017. }
  1018. /* Alpha (if any) */
  1019. if (actual_bits_count[1][mode] > 0) {
  1020. for (j = 0; j < numEndpoints; ++j) {
  1021. endpoints[j][3] = bcdec__bitstream_read_bits(&bstream, actual_bits_count[1][mode]);
  1022. }
  1023. }
  1024. /* Fully decode endpoints */
  1025. /* First handle modes that have P-bits */
  1026. if (mode == 0 || mode == 1 || mode == 3 || mode == 6 || mode == 7) {
  1027. for (i = 0; i < numEndpoints; ++i) {
  1028. /* component-wise left-shift */
  1029. for (j = 0; j < 4; ++j) {
  1030. endpoints[i][j] <<= 1;
  1031. }
  1032. }
  1033. /* if P-bit is shared */
  1034. if (mode == 1) {
  1035. i = bcdec__bitstream_read_bit(&bstream);
  1036. j = bcdec__bitstream_read_bit(&bstream);
  1037. /* rgb component-wise insert pbits */
  1038. for (k = 0; k < 3; ++k) {
  1039. endpoints[0][k] |= i;
  1040. endpoints[1][k] |= i;
  1041. endpoints[2][k] |= j;
  1042. endpoints[3][k] |= j;
  1043. }
  1044. } else if (sModeHasPBits & (1 << mode)) {
  1045. /* unique P-bit per endpoint */
  1046. for (i = 0; i < numEndpoints; ++i) {
  1047. j = bcdec__bitstream_read_bit(&bstream);
  1048. for (k = 0; k < 4; ++k) {
  1049. endpoints[i][k] |= j;
  1050. }
  1051. }
  1052. }
  1053. }
  1054. for (i = 0; i < numEndpoints; ++i) {
  1055. /* get color components precision including pbit */
  1056. j = actual_bits_count[0][mode] + ((sModeHasPBits >> mode) & 1);
  1057. for (k = 0; k < 3; ++k) {
  1058. /* left shift endpoint components so that their MSB lies in bit 7 */
  1059. endpoints[i][k] = endpoints[i][k] << (8 - j);
  1060. /* Replicate each component's MSB into the LSBs revealed by the left-shift operation above */
  1061. endpoints[i][k] = endpoints[i][k] | (endpoints[i][k] >> j);
  1062. }
  1063. /* get alpha component precision including pbit */
  1064. j = actual_bits_count[1][mode] + ((sModeHasPBits >> mode) & 1);
  1065. /* left shift endpoint components so that their MSB lies in bit 7 */
  1066. endpoints[i][3] = endpoints[i][3] << (8 - j);
  1067. /* Replicate each component's MSB into the LSBs revealed by the left-shift operation above */
  1068. endpoints[i][3] = endpoints[i][3] | (endpoints[i][3] >> j);
  1069. }
  1070. /* If this mode does not explicitly define the alpha component */
  1071. /* set alpha equal to 1.0 */
  1072. if (!actual_bits_count[1][mode]) {
  1073. for (j = 0; j < numEndpoints; ++j) {
  1074. endpoints[j][3] = 0xFF;
  1075. }
  1076. }
  1077. /* Determine weights tables */
  1078. indexBits = (mode == 0 || mode == 1) ? 3 : ((mode == 6) ? 4 : 2);
  1079. indexBits2 = (mode == 4) ? 3 : ((mode == 5) ? 2 : 0);
  1080. weights = (indexBits == 2) ? aWeight2 : ((indexBits == 3) ? aWeight3 : aWeight4);
  1081. weights2 = (indexBits2 == 2) ? aWeight2 : aWeight3;
  1082. /* Quite inconvenient that indices aren't interleaved so we have to make 2 passes here */
  1083. /* Pass #1: collecting color indices */
  1084. for (i = 0; i < 4; ++i) {
  1085. for (j = 0; j < 4; ++j) {
  1086. partitionSet = (numPartitions == 1) ? ((i | j) ? 0 : 128) : partition_sets[numPartitions - 2][partition][i][j];
  1087. indexBits = (mode == 0 || mode == 1) ? 3 : ((mode == 6) ? 4 : 2);
  1088. /* fix-up index is specified with one less bit */
  1089. /* The fix-up index for subset 0 is always index 0 */
  1090. if (partitionSet & 0x80) {
  1091. indexBits--;
  1092. }
  1093. indices[i][j] = bcdec__bitstream_read_bits(&bstream, indexBits);
  1094. }
  1095. }
  1096. /* Pass #2: reading alpha indices (if any) and interpolating & rotating */
  1097. for (i = 0; i < 4; ++i) {
  1098. for (j = 0; j < 4; ++j) {
  1099. partitionSet = (numPartitions == 1) ? ((i|j) ? 0 : 128) : partition_sets[numPartitions - 2][partition][i][j];
  1100. partitionSet &= 0x03;
  1101. index = indices[i][j];
  1102. if (!indexBits2) {
  1103. r = bcdec__interpolate(endpoints[partitionSet * 2][0], endpoints[partitionSet * 2 + 1][0], weights, index);
  1104. g = bcdec__interpolate(endpoints[partitionSet * 2][1], endpoints[partitionSet * 2 + 1][1], weights, index);
  1105. b = bcdec__interpolate(endpoints[partitionSet * 2][2], endpoints[partitionSet * 2 + 1][2], weights, index);
  1106. a = bcdec__interpolate(endpoints[partitionSet * 2][3], endpoints[partitionSet * 2 + 1][3], weights, index);
  1107. } else {
  1108. index2 = bcdec__bitstream_read_bits(&bstream, (i|j) ? indexBits2 : (indexBits2 - 1));
  1109. /* The index value for interpolating color comes from the secondary index bits for the texel
  1110. if the mode has an index selection bit and its value is one, and from the primary index bits otherwise.
  1111. The alpha index comes from the secondary index bits if the block has a secondary index and
  1112. the block either doesn’t have an index selection bit or that bit is zero, and from the primary index bits otherwise. */
  1113. if (!indexSelectionBit) {
  1114. r = bcdec__interpolate(endpoints[partitionSet * 2][0], endpoints[partitionSet * 2 + 1][0], weights, index);
  1115. g = bcdec__interpolate(endpoints[partitionSet * 2][1], endpoints[partitionSet * 2 + 1][1], weights, index);
  1116. b = bcdec__interpolate(endpoints[partitionSet * 2][2], endpoints[partitionSet * 2 + 1][2], weights, index);
  1117. a = bcdec__interpolate(endpoints[partitionSet * 2][3], endpoints[partitionSet * 2 + 1][3], weights2, index2);
  1118. } else {
  1119. r = bcdec__interpolate(endpoints[partitionSet * 2][0], endpoints[partitionSet * 2 + 1][0], weights2, index2);
  1120. g = bcdec__interpolate(endpoints[partitionSet * 2][1], endpoints[partitionSet * 2 + 1][1], weights2, index2);
  1121. b = bcdec__interpolate(endpoints[partitionSet * 2][2], endpoints[partitionSet * 2 + 1][2], weights2, index2);
  1122. a = bcdec__interpolate(endpoints[partitionSet * 2][3], endpoints[partitionSet * 2 + 1][3], weights, index);
  1123. }
  1124. }
  1125. switch (rotation) {
  1126. case 1: { /* 01 – Block format is Scalar(R) Vector(AGB) - swap A and R */
  1127. bcdec__swap_values(&a, &r);
  1128. } break;
  1129. case 2: { /* 10 – Block format is Scalar(G) Vector(RAB) - swap A and G */
  1130. bcdec__swap_values(&a, &g);
  1131. } break;
  1132. case 3: { /* 11 - Block format is Scalar(B) Vector(RGA) - swap A and B */
  1133. bcdec__swap_values(&a, &b);
  1134. } break;
  1135. }
  1136. decompressed[j * 4 + 0] = r;
  1137. decompressed[j * 4 + 1] = g;
  1138. decompressed[j * 4 + 2] = b;
  1139. decompressed[j * 4 + 3] = a;
  1140. }
  1141. decompressed += destinationPitch;
  1142. }
  1143. }
  1144. #endif /* BCDEC_IMPLEMENTATION */
  1145. /* LICENSE:
  1146. This software is available under 2 licenses -- choose whichever you prefer.
  1147. ------------------------------------------------------------------------------
  1148. ALTERNATIVE A - MIT License
  1149. Copyright (c) 2022 Sergii Kudlai
  1150. Permission is hereby granted, free of charge, to any person obtaining a copy of
  1151. this software and associated documentation files (the "Software"), to deal in
  1152. the Software without restriction, including without limitation the rights to
  1153. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  1154. of the Software, and to permit persons to whom the Software is furnished to do
  1155. so, subject to the following conditions:
  1156. The above copyright notice and this permission notice shall be included in all
  1157. copies or substantial portions of the Software.
  1158. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1159. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1160. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1161. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1162. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1163. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1164. SOFTWARE.
  1165. ------------------------------------------------------------------------------
  1166. ALTERNATIVE B - The Unlicense
  1167. This is free and unencumbered software released into the public domain.
  1168. Anyone is free to copy, modify, publish, use, compile, sell, or
  1169. distribute this software, either in source code form or as a compiled
  1170. binary, for any purpose, commercial or non-commercial, and by any
  1171. means.
  1172. In jurisdictions that recognize copyright laws, the author or authors
  1173. of this software dedicate any and all copyright interest in the
  1174. software to the public domain. We make this dedication for the benefit
  1175. of the public at large and to the detriment of our heirs and
  1176. successors. We intend this dedication to be an overt act of
  1177. relinquishment in perpetuity of all present and future rights to this
  1178. software under copyright law.
  1179. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  1180. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  1181. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  1182. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  1183. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  1184. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  1185. OTHER DEALINGS IN THE SOFTWARE.
  1186. For more information, please refer to <https://unlicense.org>
  1187. */