BcnDecode.c 22 KB

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