BcnDecode.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * The Python Imaging Library
  3. *
  4. * decoder for DXTn-compressed data
  5. *
  6. * Format documentation:
  7. * https://web.archive.org/web/20170802060935/http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_compression_s3tc.txt
  8. *
  9. * The contents of this file are in the public domain (CC0)
  10. * Full text of the CC0 license:
  11. * https://creativecommons.org/publicdomain/zero/1.0/
  12. */
  13. #include "Imaging.h"
  14. #include "Bcn.h"
  15. typedef struct {
  16. UINT8 r, g, b, a;
  17. } rgba;
  18. typedef struct {
  19. UINT8 l;
  20. } lum;
  21. typedef struct {
  22. UINT16 c0, c1;
  23. UINT32 lut;
  24. } bc1_color;
  25. typedef struct {
  26. UINT8 a0, a1;
  27. UINT8 lut[6];
  28. } bc3_alpha;
  29. typedef struct {
  30. INT8 a0, a1;
  31. UINT8 lut[6];
  32. } bc5s_alpha;
  33. #define LOAD16(p) (p)[0] | ((p)[1] << 8)
  34. #define LOAD32(p) (p)[0] | ((p)[1] << 8) | ((p)[2] << 16) | ((p)[3] << 24)
  35. static void
  36. bc1_color_load(bc1_color *dst, const UINT8 *src) {
  37. dst->c0 = LOAD16(src);
  38. dst->c1 = LOAD16(src + 2);
  39. dst->lut = LOAD32(src + 4);
  40. }
  41. static rgba
  42. decode_565(UINT16 x) {
  43. rgba c;
  44. int r, g, b;
  45. r = (x & 0xf800) >> 8;
  46. r |= r >> 5;
  47. c.r = r;
  48. g = (x & 0x7e0) >> 3;
  49. g |= g >> 6;
  50. c.g = g;
  51. b = (x & 0x1f) << 3;
  52. b |= b >> 5;
  53. c.b = b;
  54. c.a = 0xff;
  55. return c;
  56. }
  57. static void
  58. decode_bc1_color(rgba *dst, const UINT8 *src, int separate_alpha) {
  59. bc1_color col;
  60. rgba p[4];
  61. int n, cw;
  62. UINT16 r0, g0, b0, r1, g1, b1;
  63. bc1_color_load(&col, src);
  64. p[0] = decode_565(col.c0);
  65. r0 = p[0].r;
  66. g0 = p[0].g;
  67. b0 = p[0].b;
  68. p[1] = decode_565(col.c1);
  69. r1 = p[1].r;
  70. g1 = p[1].g;
  71. b1 = p[1].b;
  72. /* NOTE: BC2 and BC3 reuse BC1 color blocks but always act like c0 > c1 */
  73. if (col.c0 > col.c1 || separate_alpha) {
  74. p[2].r = (2 * r0 + 1 * r1) / 3;
  75. p[2].g = (2 * g0 + 1 * g1) / 3;
  76. p[2].b = (2 * b0 + 1 * b1) / 3;
  77. p[2].a = 0xff;
  78. p[3].r = (1 * r0 + 2 * r1) / 3;
  79. p[3].g = (1 * g0 + 2 * g1) / 3;
  80. p[3].b = (1 * b0 + 2 * b1) / 3;
  81. p[3].a = 0xff;
  82. } else {
  83. p[2].r = (r0 + r1) / 2;
  84. p[2].g = (g0 + g1) / 2;
  85. p[2].b = (b0 + b1) / 2;
  86. p[2].a = 0xff;
  87. p[3].r = 0;
  88. p[3].g = 0;
  89. p[3].b = 0;
  90. p[3].a = 0;
  91. }
  92. for (n = 0; n < 16; n++) {
  93. cw = 3 & (col.lut >> (2 * n));
  94. dst[n] = p[cw];
  95. }
  96. }
  97. static void
  98. decode_bc3_alpha(char *dst, const UINT8 *src, int stride, int o, int sign) {
  99. UINT16 a0, a1;
  100. UINT8 a[8];
  101. int n, lut1, lut2, aw;
  102. if (sign == 1) {
  103. bc5s_alpha b;
  104. memcpy(&b, src, sizeof(bc5s_alpha));
  105. a0 = b.a0 + 128;
  106. a1 = b.a1 + 128;
  107. lut1 = b.lut[0] | (b.lut[1] << 8) | (b.lut[2] << 16);
  108. lut2 = b.lut[3] | (b.lut[4] << 8) | (b.lut[5] << 16);
  109. } else {
  110. bc3_alpha b;
  111. memcpy(&b, src, sizeof(bc3_alpha));
  112. a0 = b.a0;
  113. a1 = b.a1;
  114. lut1 = b.lut[0] | (b.lut[1] << 8) | (b.lut[2] << 16);
  115. lut2 = b.lut[3] | (b.lut[4] << 8) | (b.lut[5] << 16);
  116. }
  117. a[0] = (UINT8)a0;
  118. a[1] = (UINT8)a1;
  119. if (a0 > a1) {
  120. a[2] = (6 * a0 + 1 * a1) / 7;
  121. a[3] = (5 * a0 + 2 * a1) / 7;
  122. a[4] = (4 * a0 + 3 * a1) / 7;
  123. a[5] = (3 * a0 + 4 * a1) / 7;
  124. a[6] = (2 * a0 + 5 * a1) / 7;
  125. a[7] = (1 * a0 + 6 * a1) / 7;
  126. } else {
  127. a[2] = (4 * a0 + 1 * a1) / 5;
  128. a[3] = (3 * a0 + 2 * a1) / 5;
  129. a[4] = (2 * a0 + 3 * a1) / 5;
  130. a[5] = (1 * a0 + 4 * a1) / 5;
  131. a[6] = 0;
  132. a[7] = 0xff;
  133. }
  134. for (n = 0; n < 8; n++) {
  135. aw = 7 & (lut1 >> (3 * n));
  136. dst[stride * n + o] = a[aw];
  137. }
  138. for (n = 0; n < 8; n++) {
  139. aw = 7 & (lut2 >> (3 * n));
  140. dst[stride * (8 + n) + o] = a[aw];
  141. }
  142. }
  143. static void
  144. decode_bc1_block(rgba *col, const UINT8 *src) {
  145. decode_bc1_color(col, src, 0);
  146. }
  147. static void
  148. decode_bc2_block(rgba *col, const UINT8 *src) {
  149. int n, bitI, byI, av;
  150. decode_bc1_color(col, src + 8, 1);
  151. for (n = 0; n < 16; n++) {
  152. bitI = n * 4;
  153. byI = bitI >> 3;
  154. av = 0xf & (src[byI] >> (bitI & 7));
  155. av = (av << 4) | av;
  156. col[n].a = av;
  157. }
  158. }
  159. static void
  160. decode_bc3_block(rgba *col, const UINT8 *src) {
  161. decode_bc1_color(col, src + 8, 1);
  162. decode_bc3_alpha((char *)col, src, sizeof(col[0]), 3, 0);
  163. }
  164. static void
  165. decode_bc4_block(lum *col, const UINT8 *src) {
  166. decode_bc3_alpha((char *)col, src, sizeof(col[0]), 0, 0);
  167. }
  168. static void
  169. decode_bc5_block(rgba *col, const UINT8 *src, int sign) {
  170. decode_bc3_alpha((char *)col, src, sizeof(col[0]), 0, sign);
  171. decode_bc3_alpha((char *)col, src + 8, sizeof(col[0]), 1, sign);
  172. }
  173. /* BC6 and BC7 are described here:
  174. https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_compression_bptc.txt
  175. */
  176. static UINT8
  177. get_bit(const UINT8 *src, int bit) {
  178. int by = bit >> 3;
  179. bit &= 7;
  180. return (src[by] >> bit) & 1;
  181. }
  182. static UINT8
  183. get_bits(const UINT8 *src, int bit, int count) {
  184. UINT8 v;
  185. int x;
  186. int by = bit >> 3;
  187. bit &= 7;
  188. if (!count) {
  189. return 0;
  190. }
  191. if (bit + count <= 8) {
  192. v = (src[by] >> bit) & ((1 << count) - 1);
  193. } else {
  194. x = src[by] | (src[by + 1] << 8);
  195. v = (x >> bit) & ((1 << count) - 1);
  196. }
  197. return v;
  198. }
  199. /* BC7 */
  200. typedef struct {
  201. char ns;
  202. char pb;
  203. char rb;
  204. char isb;
  205. char cb;
  206. char ab;
  207. char epb;
  208. char spb;
  209. char ib;
  210. char ib2;
  211. } bc7_mode_info;
  212. static const bc7_mode_info bc7_modes[] = {
  213. {3, 4, 0, 0, 4, 0, 1, 0, 3, 0},
  214. {2, 6, 0, 0, 6, 0, 0, 1, 3, 0},
  215. {3, 6, 0, 0, 5, 0, 0, 0, 2, 0},
  216. {2, 6, 0, 0, 7, 0, 1, 0, 2, 0},
  217. {1, 0, 2, 1, 5, 6, 0, 0, 2, 3},
  218. {1, 0, 2, 0, 7, 8, 0, 0, 2, 2},
  219. {1, 0, 0, 0, 7, 7, 1, 0, 4, 0},
  220. {2, 6, 0, 0, 5, 5, 1, 0, 2, 0}};
  221. /* Subset indices:
  222. Table.P2, 1 bit per index */
  223. static const UINT16 bc7_si2[] = {
  224. 0xcccc, 0x8888, 0xeeee, 0xecc8, 0xc880, 0xfeec, 0xfec8, 0xec80, 0xc800, 0xffec,
  225. 0xfe80, 0xe800, 0xffe8, 0xff00, 0xfff0, 0xf000, 0xf710, 0x008e, 0x7100, 0x08ce,
  226. 0x008c, 0x7310, 0x3100, 0x8cce, 0x088c, 0x3110, 0x6666, 0x366c, 0x17e8, 0x0ff0,
  227. 0x718e, 0x399c, 0xaaaa, 0xf0f0, 0x5a5a, 0x33cc, 0x3c3c, 0x55aa, 0x9696, 0xa55a,
  228. 0x73ce, 0x13c8, 0x324c, 0x3bdc, 0x6996, 0xc33c, 0x9966, 0x0660, 0x0272, 0x04e4,
  229. 0x4e40, 0x2720, 0xc936, 0x936c, 0x39c6, 0x639c, 0x9336, 0x9cc6, 0x817e, 0xe718,
  230. 0xccf0, 0x0fcc, 0x7744, 0xee22};
  231. /* Table.P3, 2 bits per index */
  232. static const UINT32 bc7_si3[] = {
  233. 0xaa685050, 0x6a5a5040, 0x5a5a4200, 0x5450a0a8, 0xa5a50000, 0xa0a05050, 0x5555a0a0,
  234. 0x5a5a5050, 0xaa550000, 0xaa555500, 0xaaaa5500, 0x90909090, 0x94949494, 0xa4a4a4a4,
  235. 0xa9a59450, 0x2a0a4250, 0xa5945040, 0x0a425054, 0xa5a5a500, 0x55a0a0a0, 0xa8a85454,
  236. 0x6a6a4040, 0xa4a45000, 0x1a1a0500, 0x0050a4a4, 0xaaa59090, 0x14696914, 0x69691400,
  237. 0xa08585a0, 0xaa821414, 0x50a4a450, 0x6a5a0200, 0xa9a58000, 0x5090a0a8, 0xa8a09050,
  238. 0x24242424, 0x00aa5500, 0x24924924, 0x24499224, 0x50a50a50, 0x500aa550, 0xaaaa4444,
  239. 0x66660000, 0xa5a0a5a0, 0x50a050a0, 0x69286928, 0x44aaaa44, 0x66666600, 0xaa444444,
  240. 0x54a854a8, 0x95809580, 0x96969600, 0xa85454a8, 0x80959580, 0xaa141414, 0x96960000,
  241. 0xaaaa1414, 0xa05050a0, 0xa0a5a5a0, 0x96000000, 0x40804080, 0xa9a8a9a8, 0xaaaaaa44,
  242. 0x2a4a5254};
  243. /* Anchor indices:
  244. Table.A2 */
  245. static const char bc7_ai0[] = {
  246. 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 2, 8, 2, 2, 8,
  247. 8, 15, 2, 8, 2, 2, 8, 8, 2, 2, 15, 15, 6, 8, 2, 8, 15, 15, 2, 8, 2, 2,
  248. 2, 15, 15, 6, 6, 2, 6, 8, 15, 15, 2, 2, 15, 15, 15, 15, 15, 2, 2, 15};
  249. /* Table.A3a */
  250. static const char bc7_ai1[] = {
  251. 3, 3, 15, 15, 8, 3, 15, 15, 8, 8, 6, 6, 6, 5, 3, 3, 3, 3, 8, 15, 3, 3,
  252. 6, 10, 5, 8, 8, 6, 8, 5, 15, 15, 8, 15, 3, 5, 6, 10, 8, 15, 15, 3, 15, 5,
  253. 15, 15, 15, 15, 3, 15, 5, 5, 5, 8, 5, 10, 5, 10, 8, 13, 15, 12, 3, 3};
  254. /* Table.A3b */
  255. static const char bc7_ai2[] = {15, 8, 8, 3, 15, 15, 3, 8, 15, 15, 15, 15, 15,
  256. 15, 15, 8, 15, 8, 15, 3, 15, 8, 15, 8, 3, 15,
  257. 6, 10, 15, 15, 10, 8, 15, 3, 15, 10, 10, 8, 9,
  258. 10, 6, 15, 8, 15, 3, 6, 6, 8, 15, 3, 15, 15,
  259. 15, 15, 15, 15, 15, 15, 15, 15, 3, 15, 15, 8};
  260. /* Interpolation weights */
  261. static const char bc7_weights2[] = {0, 21, 43, 64};
  262. static const char bc7_weights3[] = {0, 9, 18, 27, 37, 46, 55, 64};
  263. static const char bc7_weights4[] = {
  264. 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64};
  265. static const char *
  266. bc7_get_weights(int n) {
  267. if (n == 2) {
  268. return bc7_weights2;
  269. }
  270. if (n == 3) {
  271. return bc7_weights3;
  272. }
  273. return bc7_weights4;
  274. }
  275. static int
  276. bc7_get_subset(int ns, int partition, int n) {
  277. if (ns == 2) {
  278. return 1 & (bc7_si2[partition] >> n);
  279. }
  280. if (ns == 3) {
  281. return 3 & (bc7_si3[partition] >> (2 * n));
  282. }
  283. return 0;
  284. }
  285. static UINT8
  286. expand_quantized(UINT8 v, int bits) {
  287. v = v << (8 - bits);
  288. return v | (v >> bits);
  289. }
  290. static void
  291. bc7_lerp(rgba *dst, const rgba *e, int s0, int s1) {
  292. int t0 = 64 - s0;
  293. int t1 = 64 - s1;
  294. dst->r = (UINT8)((t0 * e[0].r + s0 * e[1].r + 32) >> 6);
  295. dst->g = (UINT8)((t0 * e[0].g + s0 * e[1].g + 32) >> 6);
  296. dst->b = (UINT8)((t0 * e[0].b + s0 * e[1].b + 32) >> 6);
  297. dst->a = (UINT8)((t1 * e[0].a + s1 * e[1].a + 32) >> 6);
  298. }
  299. static void
  300. decode_bc7_block(rgba *col, const UINT8 *src) {
  301. rgba endpoints[6];
  302. int bit = 0, cibit, aibit;
  303. int mode = src[0];
  304. int i, j;
  305. int numep, cb, ab, ib, ib2, i0, i1, s;
  306. UINT8 index_sel, partition, rotation, val;
  307. const char *cw, *aw;
  308. const bc7_mode_info *info;
  309. /* mode is the number of unset bits before the first set bit: */
  310. if (!mode) {
  311. /* degenerate case when no bits set */
  312. for (i = 0; i < 16; i++) {
  313. col[i].r = col[i].g = col[i].b = 0;
  314. col[i].a = 255;
  315. }
  316. return;
  317. }
  318. while (!(mode & (1 << bit++)))
  319. ;
  320. mode = bit - 1;
  321. info = &bc7_modes[mode];
  322. /* color selection bits: {subset}{endpoint} */
  323. cb = info->cb;
  324. ab = info->ab;
  325. cw = bc7_get_weights(info->ib);
  326. aw = bc7_get_weights((ab && info->ib2) ? info->ib2 : info->ib);
  327. #define LOAD(DST, N) \
  328. DST = get_bits(src, bit, N); \
  329. bit += N;
  330. LOAD(partition, info->pb);
  331. LOAD(rotation, info->rb);
  332. LOAD(index_sel, info->isb);
  333. numep = info->ns << 1;
  334. /* red */
  335. for (i = 0; i < numep; i++) {
  336. LOAD(val, cb);
  337. endpoints[i].r = val;
  338. }
  339. /* green */
  340. for (i = 0; i < numep; i++) {
  341. LOAD(val, cb);
  342. endpoints[i].g = val;
  343. }
  344. /* blue */
  345. for (i = 0; i < numep; i++) {
  346. LOAD(val, cb);
  347. endpoints[i].b = val;
  348. }
  349. /* alpha */
  350. for (i = 0; i < numep; i++) {
  351. if (ab) {
  352. LOAD(val, ab);
  353. } else {
  354. val = 255;
  355. }
  356. endpoints[i].a = val;
  357. }
  358. /* p-bits */
  359. #define ASSIGN_P(x) x = (x << 1) | val
  360. if (info->epb) {
  361. /* per endpoint */
  362. cb++;
  363. if (ab) {
  364. ab++;
  365. }
  366. for (i = 0; i < numep; i++) {
  367. LOAD(val, 1);
  368. ASSIGN_P(endpoints[i].r);
  369. ASSIGN_P(endpoints[i].g);
  370. ASSIGN_P(endpoints[i].b);
  371. if (ab) {
  372. ASSIGN_P(endpoints[i].a);
  373. }
  374. }
  375. }
  376. if (info->spb) {
  377. /* per subset */
  378. cb++;
  379. if (ab) {
  380. ab++;
  381. }
  382. for (i = 0; i < numep; i += 2) {
  383. LOAD(val, 1);
  384. for (j = 0; j < 2; j++) {
  385. ASSIGN_P(endpoints[i + j].r);
  386. ASSIGN_P(endpoints[i + j].g);
  387. ASSIGN_P(endpoints[i + j].b);
  388. if (ab) {
  389. ASSIGN_P(endpoints[i + j].a);
  390. }
  391. }
  392. }
  393. }
  394. #undef ASSIGN_P
  395. #define EXPAND(x, b) x = expand_quantized(x, b)
  396. for (i = 0; i < numep; i++) {
  397. EXPAND(endpoints[i].r, cb);
  398. EXPAND(endpoints[i].g, cb);
  399. EXPAND(endpoints[i].b, cb);
  400. if (ab) {
  401. EXPAND(endpoints[i].a, ab);
  402. }
  403. }
  404. #undef EXPAND
  405. #undef LOAD
  406. cibit = bit;
  407. aibit = cibit + 16 * info->ib - info->ns;
  408. for (i = 0; i < 16; i++) {
  409. s = bc7_get_subset(info->ns, partition, i) << 1;
  410. ib = info->ib;
  411. if (i == 0) {
  412. ib--;
  413. } else if (info->ns == 2) {
  414. if (i == bc7_ai0[partition]) {
  415. ib--;
  416. }
  417. } else if (info->ns == 3) {
  418. if (i == bc7_ai1[partition]) {
  419. ib--;
  420. } else if (i == bc7_ai2[partition]) {
  421. ib--;
  422. }
  423. }
  424. i0 = get_bits(src, cibit, ib);
  425. cibit += ib;
  426. if (ab && info->ib2) {
  427. ib2 = info->ib2;
  428. if (ib2 && i == 0) {
  429. ib2--;
  430. }
  431. i1 = get_bits(src, aibit, ib2);
  432. aibit += ib2;
  433. if (index_sel) {
  434. bc7_lerp(&col[i], &endpoints[s], aw[i1], cw[i0]);
  435. } else {
  436. bc7_lerp(&col[i], &endpoints[s], cw[i0], aw[i1]);
  437. }
  438. } else {
  439. bc7_lerp(&col[i], &endpoints[s], cw[i0], cw[i0]);
  440. }
  441. #define ROTATE(x, y) \
  442. val = x; \
  443. x = y; \
  444. y = val
  445. if (rotation == 1) {
  446. ROTATE(col[i].r, col[i].a);
  447. } else if (rotation == 2) {
  448. ROTATE(col[i].g, col[i].a);
  449. } else if (rotation == 3) {
  450. ROTATE(col[i].b, col[i].a);
  451. }
  452. #undef ROTATE
  453. }
  454. }
  455. /* BC6 */
  456. typedef struct {
  457. char ns; /* number of subsets (also called regions) */
  458. char tr; /* whether endpoints are delta-compressed */
  459. char pb; /* partition bits */
  460. char epb; /* endpoint bits */
  461. char rb; /* red bits (delta) */
  462. char gb; /* green bits (delta) */
  463. char bb; /* blue bits (delta) */
  464. } bc6_mode_info;
  465. static const bc6_mode_info bc6_modes[] = {
  466. // 00
  467. {2, 1, 5, 10, 5, 5, 5},
  468. // 01
  469. {2, 1, 5, 7, 6, 6, 6},
  470. // 10
  471. {2, 1, 5, 11, 5, 4, 4},
  472. {2, 1, 5, 11, 4, 5, 4},
  473. {2, 1, 5, 11, 4, 4, 5},
  474. {2, 1, 5, 9, 5, 5, 5},
  475. {2, 1, 5, 8, 6, 5, 5},
  476. {2, 1, 5, 8, 5, 6, 5},
  477. {2, 1, 5, 8, 5, 5, 6},
  478. {2, 0, 5, 6, 6, 6, 6},
  479. // 11
  480. {1, 0, 0, 10, 10, 10, 10},
  481. {1, 1, 0, 11, 9, 9, 9},
  482. {1, 1, 0, 12, 8, 8, 8},
  483. {1, 1, 0, 16, 4, 4, 4}};
  484. /* Table.F, encoded as a sequence of bit indices */
  485. static const UINT8 bc6_bit_packings[][75] = {
  486. {116, 132, 180, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17,
  487. 18, 19, 20, 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38,
  488. 39, 40, 41, 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65,
  489. 66, 67, 68, 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128,
  490. 129, 130, 131, 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179},
  491. {117, 164, 165, 0, 1, 2, 3, 4, 5, 6, 176, 177, 132, 16, 17,
  492. 18, 19, 20, 21, 22, 133, 178, 116, 32, 33, 34, 35, 36, 37, 38,
  493. 179, 181, 180, 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65,
  494. 66, 67, 68, 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128,
  495. 129, 130, 131, 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149},
  496. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20,
  497. 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
  498. 48, 49, 50, 51, 52, 10, 112, 113, 114, 115, 64, 65, 66, 67, 26,
  499. 176, 160, 161, 162, 163, 80, 81, 82, 83, 42, 177, 128, 129, 130, 131,
  500. 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179},
  501. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20,
  502. 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
  503. 48, 49, 50, 51, 10, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68,
  504. 26, 160, 161, 162, 163, 80, 81, 82, 83, 42, 177, 128, 129, 130, 131,
  505. 96, 97, 98, 99, 176, 178, 144, 145, 146, 147, 116, 179},
  506. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20,
  507. 21, 22, 23, 24, 25, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
  508. 48, 49, 50, 51, 10, 132, 112, 113, 114, 115, 64, 65, 66, 67, 26,
  509. 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 42, 128, 129, 130, 131,
  510. 96, 97, 98, 99, 177, 178, 144, 145, 146, 147, 180, 179},
  511. {0, 1, 2, 3, 4, 5, 6, 7, 8, 132, 16, 17, 18, 19, 20,
  512. 21, 22, 23, 24, 116, 32, 33, 34, 35, 36, 37, 38, 39, 40, 180,
  513. 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68,
  514. 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131,
  515. 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179},
  516. {0, 1, 2, 3, 4, 5, 6, 7, 164, 132, 16, 17, 18, 19, 20,
  517. 21, 22, 23, 178, 116, 32, 33, 34, 35, 36, 37, 38, 39, 179, 180,
  518. 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65, 66, 67, 68,
  519. 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131,
  520. 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149},
  521. {0, 1, 2, 3, 4, 5, 6, 7, 176, 132, 16, 17, 18, 19, 20,
  522. 21, 22, 23, 117, 116, 32, 33, 34, 35, 36, 37, 38, 39, 165, 180,
  523. 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68,
  524. 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 177, 128, 129, 130, 131,
  525. 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179},
  526. {0, 1, 2, 3, 4, 5, 6, 7, 177, 132, 16, 17, 18, 19, 20,
  527. 21, 22, 23, 133, 116, 32, 33, 34, 35, 36, 37, 38, 39, 181, 180,
  528. 48, 49, 50, 51, 52, 164, 112, 113, 114, 115, 64, 65, 66, 67, 68,
  529. 176, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128, 129, 130, 131,
  530. 96, 97, 98, 99, 100, 178, 144, 145, 146, 147, 148, 179},
  531. {0, 1, 2, 3, 4, 5, 164, 176, 177, 132, 16, 17, 18, 19, 20,
  532. 21, 117, 133, 178, 116, 32, 33, 34, 35, 36, 37, 165, 179, 181, 180,
  533. 48, 49, 50, 51, 52, 53, 112, 113, 114, 115, 64, 65, 66, 67, 68,
  534. 69, 160, 161, 162, 163, 80, 81, 82, 83, 84, 85, 128, 129, 130, 131,
  535. 96, 97, 98, 99, 100, 101, 144, 145, 146, 147, 148, 149},
  536. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  537. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
  538. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89},
  539. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  540. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 56, 10,
  541. 64, 65, 66, 67, 68, 69, 70, 71, 72, 26, 80, 81, 82, 83, 84, 85, 86, 87, 88, 42},
  542. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  543. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 52, 53, 54, 55, 11, 10,
  544. 64, 65, 66, 67, 68, 69, 70, 71, 27, 26, 80, 81, 82, 83, 84, 85, 86, 87, 43, 42},
  545. {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
  546. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 48, 49, 50, 51, 15, 14, 13, 12, 11, 10,
  547. 64, 65, 66, 67, 31, 30, 29, 28, 27, 26, 80, 81, 82, 83, 47, 46, 45, 44, 43, 42}};
  548. static void
  549. bc6_sign_extend(UINT16 *v, int prec) {
  550. int x = *v;
  551. if (x & (1 << (prec - 1))) {
  552. x |= -1 << prec;
  553. }
  554. *v = (UINT16)x;
  555. }
  556. static int
  557. bc6_unquantize(UINT16 v, int prec, int sign) {
  558. int s = 0;
  559. int x;
  560. if (!sign) {
  561. x = v;
  562. if (prec >= 15) {
  563. return x;
  564. }
  565. if (x == 0) {
  566. return 0;
  567. }
  568. if (x == ((1 << prec) - 1)) {
  569. return 0xffff;
  570. }
  571. return ((x << 15) + 0x4000) >> (prec - 1);
  572. } else {
  573. x = (INT16)v;
  574. if (prec >= 16) {
  575. return x;
  576. }
  577. if (x < 0) {
  578. s = 1;
  579. x = -x;
  580. }
  581. if (x != 0) {
  582. if (x >= ((1 << (prec - 1)) - 1)) {
  583. x = 0x7fff;
  584. } else {
  585. x = ((x << 15) + 0x4000) >> (prec - 1);
  586. }
  587. }
  588. if (s) {
  589. return -x;
  590. }
  591. return x;
  592. }
  593. }
  594. static float
  595. half_to_float(UINT16 h) {
  596. /* https://gist.github.com/rygorous/2144712 */
  597. union {
  598. UINT32 u;
  599. float f;
  600. } o, m;
  601. m.u = 0x77800000;
  602. o.u = (h & 0x7fff) << 13;
  603. o.f *= m.f;
  604. m.u = 0x47800000;
  605. if (o.f >= m.f) {
  606. o.u |= 255 << 23;
  607. }
  608. o.u |= (h & 0x8000) << 16;
  609. return o.f;
  610. }
  611. static float
  612. bc6_finalize(int v, int sign) {
  613. if (sign) {
  614. if (v < 0) {
  615. v = ((-v) * 31) / 32;
  616. return half_to_float((UINT16)(0x8000 | v));
  617. } else {
  618. return half_to_float((UINT16)((v * 31) / 32));
  619. }
  620. } else {
  621. return half_to_float((UINT16)((v * 31) / 64));
  622. }
  623. }
  624. static UINT8
  625. bc6_clamp(float value) {
  626. if (value < 0.0f) {
  627. return 0;
  628. } else if (value > 1.0f) {
  629. return 255;
  630. } else {
  631. return (UINT8) (value * 255.0f);
  632. }
  633. }
  634. static void
  635. bc6_lerp(rgba *col, int *e0, int *e1, int s, int sign) {
  636. int r, g, b;
  637. int t = 64 - s;
  638. r = (e0[0] * t + e1[0] * s) >> 6;
  639. g = (e0[1] * t + e1[1] * s) >> 6;
  640. b = (e0[2] * t + e1[2] * s) >> 6;
  641. col->r = bc6_clamp(bc6_finalize(r, sign));
  642. col->g = bc6_clamp(bc6_finalize(g, sign));
  643. col->b = bc6_clamp(bc6_finalize(b, sign));
  644. }
  645. static void
  646. decode_bc6_block(rgba *col, const UINT8 *src, int sign) {
  647. UINT16 endpoints[12]; /* storage for r0, g0, b0, r1, ... */
  648. int ueps[12];
  649. int i, i0, ib2, di, dw, mask, numep, s;
  650. UINT8 partition;
  651. const bc6_mode_info *info;
  652. const char *cw;
  653. int bit = 5;
  654. int epbits = 75;
  655. int ib = 3;
  656. int mode = src[0] & 0x1f;
  657. if ((mode & 3) == 0 || (mode & 3) == 1) {
  658. mode &= 3;
  659. bit = 2;
  660. } else if ((mode & 3) == 2) {
  661. mode = 2 + (mode >> 2);
  662. epbits = 72;
  663. } else {
  664. mode = 10 + (mode >> 2);
  665. epbits = 60;
  666. ib = 4;
  667. }
  668. if (mode >= 14) {
  669. /* invalid block */
  670. memset(col, 0, 16 * sizeof(col[0]));
  671. return;
  672. }
  673. info = &bc6_modes[mode];
  674. cw = bc7_get_weights(ib);
  675. numep = info->ns == 2 ? 12 : 6;
  676. for (i = 0; i < 12; i++) {
  677. endpoints[i] = 0;
  678. }
  679. for (i = 0; i < epbits; i++) {
  680. di = bc6_bit_packings[mode][i];
  681. dw = di >> 4;
  682. di &= 15;
  683. endpoints[dw] |= (UINT16)get_bit(src, bit + i) << di;
  684. }
  685. bit += epbits;
  686. partition = get_bits(src, bit, info->pb);
  687. bit += info->pb;
  688. mask = (1 << info->epb) - 1;
  689. if (sign) { /* sign-extend e0 if signed */
  690. bc6_sign_extend(&endpoints[0], info->epb);
  691. bc6_sign_extend(&endpoints[1], info->epb);
  692. bc6_sign_extend(&endpoints[2], info->epb);
  693. }
  694. if (sign || info->tr) { /* sign-extend e1,2,3 if signed or deltas */
  695. for (i = 3; i < numep; i += 3) {
  696. bc6_sign_extend(&endpoints[i], info->rb);
  697. bc6_sign_extend(&endpoints[i + 1], info->gb);
  698. bc6_sign_extend(&endpoints[i + 2], info->bb);
  699. }
  700. }
  701. if (info->tr) { /* apply deltas */
  702. for (i = 3; i < numep; i += 3) {
  703. endpoints[i] = (endpoints[i] + endpoints[0]) & mask;
  704. endpoints[i + 1] = (endpoints[i + 1] + endpoints[1]) & mask;
  705. endpoints[i + 2] = (endpoints[i + 2] + endpoints[2]) & mask;
  706. }
  707. }
  708. for (i = 0; i < numep; i++) {
  709. ueps[i] = bc6_unquantize(endpoints[i], info->epb, sign);
  710. }
  711. for (i = 0; i < 16; i++) {
  712. s = bc7_get_subset(info->ns, partition, i) * 6;
  713. ib2 = ib;
  714. if (i == 0) {
  715. ib2--;
  716. } else if (info->ns == 2) {
  717. if (i == bc7_ai0[partition]) {
  718. ib2--;
  719. }
  720. }
  721. i0 = get_bits(src, bit, ib2);
  722. bit += ib2;
  723. bc6_lerp(&col[i], &ueps[s], &ueps[s + 3], cw[i0], sign);
  724. }
  725. }
  726. static void
  727. put_block(Imaging im, ImagingCodecState state, const char *col, int sz, int C) {
  728. int width = state->xsize;
  729. int height = state->ysize;
  730. int xmax = width + state->xoff;
  731. int ymax = height + state->yoff;
  732. int j, i, y, x;
  733. char *dst;
  734. for (j = 0; j < 4; j++) {
  735. y = state->y + j;
  736. if (C) {
  737. if (y >= height) {
  738. continue;
  739. }
  740. if (state->ystep < 0) {
  741. y = state->yoff + ymax - y - 1;
  742. }
  743. dst = im->image[y];
  744. for (i = 0; i < 4; i++) {
  745. x = state->x + i;
  746. if (x >= width) {
  747. continue;
  748. }
  749. memcpy(dst + sz * x, col + sz * (j * 4 + i), sz);
  750. }
  751. } else {
  752. if (state->ystep < 0) {
  753. y = state->yoff + ymax - y - 1;
  754. }
  755. x = state->x;
  756. dst = im->image[y] + sz * x;
  757. memcpy(dst, col + sz * (j * 4), 4 * sz);
  758. }
  759. }
  760. state->x += 4;
  761. if (state->x >= xmax) {
  762. state->y += 4;
  763. state->x = state->xoff;
  764. }
  765. }
  766. static int
  767. decode_bcn(
  768. Imaging im, ImagingCodecState state, const UINT8 *src, int bytes, int N, int C, char *pixel_format) {
  769. int ymax = state->ysize + state->yoff;
  770. const UINT8 *ptr = src;
  771. switch (N) {
  772. #define DECODE_LOOP(NN, SZ, TY, ...) \
  773. case NN: \
  774. while (bytes >= SZ) { \
  775. TY col[16]; \
  776. memset(col, 0, 16 * sizeof(col[0])); \
  777. decode_bc##NN##_block(col, ptr); \
  778. put_block(im, state, (const char *)col, sizeof(col[0]), C); \
  779. ptr += SZ; \
  780. bytes -= SZ; \
  781. if (state->y >= ymax) { \
  782. return -1; \
  783. } \
  784. } \
  785. break
  786. DECODE_LOOP(1, 8, rgba);
  787. DECODE_LOOP(2, 16, rgba);
  788. DECODE_LOOP(3, 16, rgba);
  789. DECODE_LOOP(4, 8, lum);
  790. case 5:
  791. {
  792. int sign = strcmp(pixel_format, "BC5S") == 0 ? 1 : 0;
  793. while (bytes >= 16) {
  794. rgba col[16];
  795. memset(col, sign ? 128 : 0, 16 * sizeof(col[0]));
  796. decode_bc5_block(col, ptr, sign);
  797. put_block(im, state, (const char *)col, sizeof(col[0]), C);
  798. ptr += 16;
  799. bytes -= 16;
  800. if (state->y >= ymax) {
  801. return -1;
  802. }
  803. }
  804. break;
  805. }
  806. case 6:
  807. {
  808. int sign = strcmp(pixel_format, "BC6HS") == 0 ? 1 : 0;
  809. while (bytes >= 16) {
  810. rgba col[16];
  811. decode_bc6_block(col, ptr, sign);
  812. put_block(im, state, (const char *)col, sizeof(col[0]), C);
  813. ptr += 16;
  814. bytes -= 16;
  815. if (state->y >= ymax) {
  816. return -1;
  817. }
  818. }
  819. break;
  820. }
  821. DECODE_LOOP(7, 16, rgba);
  822. #undef DECODE_LOOP
  823. }
  824. return (int)(ptr - src);
  825. }
  826. int
  827. ImagingBcnDecode(Imaging im, ImagingCodecState state, UINT8 *buf, Py_ssize_t bytes) {
  828. int N = state->state & 0xf;
  829. int width = state->xsize;
  830. int height = state->ysize;
  831. int C = (width & 3) | (height & 3) ? 1 : 0;
  832. char *pixel_format = ((BCNSTATE *)state->context)->pixel_format;
  833. return decode_bcn(im, state, buf, bytes, N, C, pixel_format);
  834. }