Decompress.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Resource/Decompress.h"
  5. #include <cstdint>
  6. // ETC2 decompress
  7. typedef unsigned char uint8;
  8. typedef unsigned short uint16;
  9. typedef short int16;
  10. // Implemented by ETCPACK
  11. extern void decompressBlockAlphaC(uint8* data, uint8* img, int width, int height, int ix, int iy, int channels);
  12. extern void decompressBlockETC2c(unsigned int block_part1, unsigned int block_part2, uint8 *img, int width, int height, int startx, int starty, int channels);
  13. extern void setupAlphaTable();
  14. // DXT decompression based on the Squish library, modified for Urho3D
  15. namespace Urho3D
  16. {
  17. /* -----------------------------------------------------------------------------
  18. Copyright (c) 2006 Simon Brown [email protected]
  19. Permission is hereby granted, free of charge, to any person obtaining
  20. a copy of this software and associated documentation files (the
  21. "Software"), to deal in the Software without restriction, including
  22. without limitation the rights to use, copy, modify, merge, publish,
  23. distribute, sublicense, and/or sell copies of the Software, and to
  24. permit persons to whom the Software is furnished to do so, subject to
  25. the following conditions:
  26. The above copyright notice and this permission notice shall be included
  27. in all copies or substantial portions of the Software.
  28. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  29. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  30. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  31. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  32. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  33. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  34. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  35. -------------------------------------------------------------------------- */
  36. static int Unpack565(unsigned char const* packed, unsigned char* colour)
  37. {
  38. // build the packed value
  39. int value = (int)packed[0] | ((int)packed[1] << 8);
  40. // get the components in the stored range
  41. auto red = (unsigned char)((value >> 11) & 0x1f);
  42. auto green = (unsigned char)((value >> 5) & 0x3f);
  43. auto blue = (unsigned char)(value & 0x1f);
  44. // scale up to 8 bits
  45. colour[0] = (red << 3) | (red >> 2);
  46. colour[1] = (green << 2) | (green >> 4);
  47. colour[2] = (blue << 3) | (blue >> 2);
  48. colour[3] = 255;
  49. // return the value
  50. return value;
  51. }
  52. static void DecompressColourDXT(unsigned char* rgba, void const* block, bool isDxt1)
  53. {
  54. // get the block bytes
  55. auto const* bytes = reinterpret_cast< unsigned char const* >( block );
  56. // unpack the endpoints
  57. unsigned char codes[16];
  58. int a = Unpack565(bytes, codes);
  59. int b = Unpack565(bytes + 2, codes + 4);
  60. // generate the midpoints
  61. for (int i = 0; i < 3; ++i)
  62. {
  63. int c = codes[i];
  64. int d = codes[4 + i];
  65. if (isDxt1 && a <= b)
  66. {
  67. codes[8 + i] = (unsigned char)((c + d) / 2);
  68. codes[12 + i] = 0;
  69. }
  70. else
  71. {
  72. codes[8 + i] = (unsigned char)((2 * c + d) / 3);
  73. codes[12 + i] = (unsigned char)((c + 2 * d) / 3);
  74. }
  75. }
  76. // fill in alpha for the intermediate values
  77. codes[8 + 3] = 255;
  78. codes[12 + 3] = (unsigned char)((isDxt1 && a <= b) ? 0 : 255);
  79. // unpack the indices
  80. unsigned char indices[16];
  81. for (int i = 0; i < 4; ++i)
  82. {
  83. unsigned char* ind = indices + 4 * i;
  84. unsigned char packed = bytes[4 + i];
  85. ind[0] = (unsigned char)(packed & 0x3);
  86. ind[1] = (unsigned char)((packed >> 2) & 0x3);
  87. ind[2] = (unsigned char)((packed >> 4) & 0x3);
  88. ind[3] = (unsigned char)((packed >> 6) & 0x3);
  89. }
  90. // store out the colours
  91. for (int i = 0; i < 16; ++i)
  92. {
  93. auto offset = (unsigned char)(4 * indices[i]);
  94. for (int j = 0; j < 4; ++j)
  95. rgba[4 * i + j] = codes[offset + j];
  96. }
  97. }
  98. static void DecompressAlphaDXT3(unsigned char* rgba, void const* block)
  99. {
  100. auto const* bytes = reinterpret_cast< unsigned char const* >( block );
  101. // unpack the alpha values pairwise
  102. for (int i = 0; i < 8; ++i)
  103. {
  104. // quantise down to 4 bits
  105. unsigned char quant = bytes[i];
  106. // unpack the values
  107. auto lo = (unsigned char)(quant & 0x0f);
  108. auto hi = (unsigned char)(quant & 0xf0);
  109. // convert back up to bytes
  110. rgba[8 * i + 3] = lo | (lo << 4);
  111. rgba[8 * i + 7] = hi | (hi >> 4);
  112. }
  113. }
  114. static void DecompressAlphaDXT5(unsigned char* rgba, void const* block)
  115. {
  116. // get the two alpha values
  117. auto const* bytes = reinterpret_cast< unsigned char const* >( block );
  118. int alpha0 = bytes[0];
  119. int alpha1 = bytes[1];
  120. // compare the values to build the codebook
  121. unsigned char codes[8];
  122. codes[0] = (unsigned char)alpha0;
  123. codes[1] = (unsigned char)alpha1;
  124. if (alpha0 <= alpha1)
  125. {
  126. // use 5-alpha codebook
  127. for (int i = 1; i < 5; ++i)
  128. codes[1 + i] = (unsigned char)(((5 - i) * alpha0 + i * alpha1) / 5);
  129. codes[6] = 0;
  130. codes[7] = 255;
  131. }
  132. else
  133. {
  134. // use 7-alpha codebook
  135. for (int i = 1; i < 7; ++i)
  136. codes[1 + i] = (unsigned char)(((7 - i) * alpha0 + i * alpha1) / 7);
  137. }
  138. // decode the indices
  139. unsigned char indices[16];
  140. unsigned char const* src = bytes + 2;
  141. unsigned char* dest = indices;
  142. for (int i = 0; i < 2; ++i)
  143. {
  144. // grab 3 bytes
  145. int value = 0;
  146. for (int j = 0; j < 3; ++j)
  147. {
  148. int byte = *src++;
  149. value |= (byte << 8 * j);
  150. }
  151. // unpack 8 3-bit values from it
  152. for (int j = 0; j < 8; ++j)
  153. {
  154. int index = (value >> 3 * j) & 0x7;
  155. *dest++ = (unsigned char)index;
  156. }
  157. }
  158. // write out the indexed codebook values
  159. for (int i = 0; i < 16; ++i)
  160. rgba[4 * i + 3] = codes[indices[i]];
  161. }
  162. static void DecompressDXT(unsigned char* rgba, const void* block, CompressedFormat format)
  163. {
  164. // get the block locations
  165. void const* colourBlock = block;
  166. void const* alphaBock = block;
  167. if (format == CF_DXT3 || format == CF_DXT5)
  168. colourBlock = reinterpret_cast< unsigned char const* >( block ) + 8;
  169. // decompress colour
  170. DecompressColourDXT(rgba, colourBlock, format == CF_DXT1);
  171. // decompress alpha separately if necessary
  172. if (format == CF_DXT3)
  173. DecompressAlphaDXT3(rgba, alphaBock);
  174. else if (format == CF_DXT5)
  175. DecompressAlphaDXT5(rgba, alphaBock);
  176. }
  177. void DecompressImageDXT(unsigned char* rgba, const void* blocks, int width, int height, int depth, CompressedFormat format)
  178. {
  179. // initialise the block input
  180. auto const* sourceBlock = reinterpret_cast< unsigned char const* >( blocks );
  181. int bytesPerBlock = format == CF_DXT1 ? 8 : 16;
  182. // loop over blocks
  183. for (int z = 0; z < depth; ++z)
  184. {
  185. int sz = width * height * 4 * z;
  186. for (int y = 0; y < height; y += 4)
  187. {
  188. for (int x = 0; x < width; x += 4)
  189. {
  190. // decompress the block
  191. unsigned char targetRgba[4 * 16];
  192. DecompressDXT(targetRgba, sourceBlock, format);
  193. // write the decompressed pixels to the correct image locations
  194. unsigned char const* sourcePixel = targetRgba;
  195. for (int py = 0; py < 4; ++py)
  196. {
  197. for (int px = 0; px < 4; ++px)
  198. {
  199. // get the target location
  200. int sx = x + px;
  201. int sy = y + py;
  202. if (sx < width && sy < height)
  203. {
  204. unsigned char* targetPixel = rgba + sz + 4 * (width * sy + sx);
  205. // copy the rgba value
  206. for (int i = 0; i < 4; ++i)
  207. *targetPixel++ = *sourcePixel++;
  208. }
  209. else
  210. {
  211. // skip this pixel as its outside the image
  212. sourcePixel += 4;
  213. }
  214. }
  215. }
  216. // advance
  217. sourceBlock += bytesPerBlock;
  218. }
  219. }
  220. }
  221. }
  222. // PVRTC decompression based on the Oolong Engine, modified for Urho3D
  223. #define PT_INDEX (2) /*The Punch-through index*/
  224. #define BLK_Y_SIZE (4) /*always 4 for all 2D block types*/
  225. #define BLK_X_MAX (8) /*Max X dimension for blocks*/
  226. #define BLK_X_2BPP (8) /*dimensions for the two formats*/
  227. #define BLK_X_4BPP (4)
  228. #define _MIN(X, Y) (((X)<(Y))? (X):(Y))
  229. #define _MAX(X, Y) (((X)>(Y))? (X):(Y))
  230. #define WRAP_COORD(Val, Size) ((Val) & ((Size)-1))
  231. #define CLAMP(X, lower, upper) (_MIN(_MAX((X),(lower)), (upper)))
  232. #define LIMIT_COORD(Val, Size, AssumeImageTiles) ((AssumeImageTiles)? WRAP_COORD((Val), (Size)): CLAMP((Val), 0, (Size)-1))
  233. using AMTC_BLOCK_STRUCT = struct
  234. {
  235. // Uses 64 bits pre block
  236. unsigned PackedData[2];
  237. };
  238. static void Unpack5554Colour(const AMTC_BLOCK_STRUCT* pBlock, int ABColours[2][4])
  239. {
  240. unsigned RawBits[2];
  241. int i;
  242. // Extract A and B
  243. RawBits[0] = pBlock->PackedData[1] & (0xFFFE); /*15 bits (shifted up by one)*/
  244. RawBits[1] = pBlock->PackedData[1] >> 16; /*16 bits*/
  245. // Step through both colours
  246. for (i = 0; i < 2; i++)
  247. {
  248. // If completely opaque
  249. if (RawBits[i] & (1 << 15))
  250. {
  251. // Extract R and G (both 5 bit)
  252. ABColours[i][0] = (RawBits[i] >> 10) & 0x1F;
  253. ABColours[i][1] = (RawBits[i] >> 5) & 0x1F;
  254. // The precision of Blue depends on A or B. If A then we need to
  255. // replicate the top bit to get 5 bits in total
  256. ABColours[i][2] = RawBits[i] & 0x1F;
  257. if (i == 0)
  258. {
  259. ABColours[0][2] |= ABColours[0][2] >> 4;
  260. }
  261. // Set 4bit alpha fully on...
  262. ABColours[i][3] = 0xF;
  263. }
  264. // Else if colour has variable translucency
  265. else
  266. {
  267. // Extract R and G (both 4 bit).
  268. // (Leave a space on the end for the replication of bits
  269. ABColours[i][0] = (RawBits[i] >> (8 - 1)) & 0x1E;
  270. ABColours[i][1] = (RawBits[i] >> (4 - 1)) & 0x1E;
  271. // Replicate bits to truly expand to 5 bits
  272. ABColours[i][0] |= ABColours[i][0] >> 4;
  273. ABColours[i][1] |= ABColours[i][1] >> 4;
  274. // Grab the 3(+padding) or 4 bits of blue and add an extra padding bit
  275. ABColours[i][2] = (RawBits[i] & 0xF) << 1;
  276. // Expand from 3 to 5 bits if this is from colour A, or 4 to 5 bits if from
  277. // colour B
  278. if (i == 0)
  279. {
  280. ABColours[0][2] |= ABColours[0][2] >> 3;
  281. }
  282. else
  283. {
  284. ABColours[0][2] |= ABColours[0][2] >> 4;
  285. }
  286. // Set the alpha bits to be 3 + a zero on the end
  287. ABColours[i][3] = (RawBits[i] >> 11) & 0xE;
  288. }
  289. }
  290. }
  291. static void UnpackModulations(const AMTC_BLOCK_STRUCT* pBlock, const int Do2bitMode, int ModulationVals[8][16],
  292. int ModulationModes[8][16], int StartX, int StartY)
  293. {
  294. int BlockModMode;
  295. unsigned ModulationBits;
  296. int x, y;
  297. BlockModMode = pBlock->PackedData[1] & 1;
  298. ModulationBits = pBlock->PackedData[0];
  299. // If it's in an interpolated mode
  300. if (Do2bitMode && BlockModMode)
  301. {
  302. // Run through all the pixels in the block. Note we can now treat all the
  303. // "stored" values as if they have 2bits (even when they didn't!)
  304. for (y = 0; y < BLK_Y_SIZE; y++)
  305. {
  306. for (x = 0; x < BLK_X_2BPP; x++)
  307. {
  308. ModulationModes[y + StartY][x + StartX] = BlockModMode;
  309. // If this is a stored value...
  310. if (((x ^ y) & 1) == 0)
  311. {
  312. ModulationVals[y + StartY][x + StartX] = ModulationBits & 3;
  313. ModulationBits >>= 2;
  314. }
  315. }
  316. }
  317. }
  318. // Else if direct encoded 2bit mode - i.e. 1 mode bit per pixel
  319. else if (Do2bitMode)
  320. {
  321. for (y = 0; y < BLK_Y_SIZE; y++)
  322. {
  323. for (x = 0; x < BLK_X_2BPP; x++)
  324. {
  325. ModulationModes[y + StartY][x + StartX] = BlockModMode;
  326. // Double the bits so 0=> 00, and 1=>11
  327. if (ModulationBits & 1)
  328. {
  329. ModulationVals[y + StartY][x + StartX] = 0x3;
  330. }
  331. else
  332. {
  333. ModulationVals[y + StartY][x + StartX] = 0x0;
  334. }
  335. ModulationBits >>= 1;
  336. }
  337. }
  338. }
  339. // Else its the 4bpp mode so each value has 2 bits
  340. else
  341. {
  342. for (y = 0; y < BLK_Y_SIZE; y++)
  343. {
  344. for (x = 0; x < BLK_X_4BPP; x++)
  345. {
  346. ModulationModes[y + StartY][x + StartX] = BlockModMode;
  347. ModulationVals[y + StartY][x + StartX] = ModulationBits & 3;
  348. ModulationBits >>= 2;
  349. }
  350. }
  351. }
  352. }
  353. static void InterpolateColours(const int ColourP[4], const int ColourQ[4], const int ColourR[4], const int ColourS[4],
  354. const int Do2bitMode, const int x, const int y, int Result[4])
  355. {
  356. int u, v, uscale;
  357. int k;
  358. int tmp1, tmp2;
  359. int P[4], Q[4], R[4], S[4];
  360. // Copy the colours
  361. for (k = 0; k < 4; k++)
  362. {
  363. P[k] = ColourP[k];
  364. Q[k] = ColourQ[k];
  365. R[k] = ColourR[k];
  366. S[k] = ColourS[k];
  367. }
  368. // Put the x and y values into the right range
  369. v = (y & 0x3) | ((~y & 0x2) << 1);
  370. if (Do2bitMode)
  371. {
  372. u = (x & 0x7) | ((~x & 0x4) << 1);
  373. }
  374. else
  375. {
  376. u = (x & 0x3) | ((~x & 0x2) << 1);
  377. }
  378. // Get the u and v scale amounts
  379. v = v - BLK_Y_SIZE / 2;
  380. if (Do2bitMode)
  381. {
  382. u = u - BLK_X_2BPP / 2;
  383. uscale = 8;
  384. }
  385. else
  386. {
  387. u = u - BLK_X_4BPP / 2;
  388. uscale = 4;
  389. }
  390. for (k = 0; k < 4; k++)
  391. {
  392. tmp1 = P[k] * uscale + u * (Q[k] - P[k]);
  393. tmp2 = R[k] * uscale + u * (S[k] - R[k]);
  394. tmp1 = tmp1 * 4 + v * (tmp2 - tmp1);
  395. Result[k] = tmp1;
  396. }
  397. // Lop off the appropriate number of bits to get us to 8 bit precision
  398. if (Do2bitMode)
  399. {
  400. // Do RGB
  401. for (k = 0; k < 3; k++)
  402. {
  403. Result[k] >>= 2;
  404. }
  405. Result[3] >>= 1;
  406. }
  407. else
  408. {
  409. // Do RGB (A is ok)
  410. for (k = 0; k < 3; k++)
  411. {
  412. Result[k] >>= 1;
  413. }
  414. }
  415. // Convert from 5554 to 8888
  416. //
  417. // do RGB 5.3 => 8
  418. for (k = 0; k < 3; k++)
  419. {
  420. Result[k] += Result[k] >> 5;
  421. }
  422. Result[3] += Result[3] >> 4;
  423. }
  424. static void GetModulationValue(int x, int y, const int Do2bitMode, const int ModulationVals[8][16],
  425. const int ModulationModes[8][16], int* Mod, int* DoPT)
  426. {
  427. static const int RepVals0[4] = {0, 3, 5, 8};
  428. static const int RepVals1[4] = {0, 4, 4, 8};
  429. int ModVal;
  430. // Map X and Y into the local 2x2 block
  431. y = (y & 0x3) | ((~y & 0x2) << 1);
  432. if (Do2bitMode)
  433. {
  434. x = (x & 0x7) | ((~x & 0x4) << 1);
  435. }
  436. else
  437. {
  438. x = (x & 0x3) | ((~x & 0x2) << 1);
  439. }
  440. // Assume no PT for now
  441. *DoPT = 0;
  442. // Extract the modulation value. If a simple encoding
  443. if (ModulationModes[y][x] == 0)
  444. {
  445. ModVal = RepVals0[ModulationVals[y][x]];
  446. }
  447. else if (Do2bitMode)
  448. {
  449. // If this is a stored value
  450. if (((x ^ y) & 1) == 0)
  451. {
  452. ModVal = RepVals0[ModulationVals[y][x]];
  453. }
  454. // Else average from the neighbours
  455. //
  456. // If H&V interpolation...
  457. else if (ModulationModes[y][x] == 1)
  458. {
  459. ModVal = (RepVals0[ModulationVals[y - 1][x]] +
  460. RepVals0[ModulationVals[y + 1][x]] +
  461. RepVals0[ModulationVals[y][x - 1]] +
  462. RepVals0[ModulationVals[y][x + 1]] + 2) / 4;
  463. }
  464. // Else if H-Only
  465. else if (ModulationModes[y][x] == 2)
  466. {
  467. ModVal = (RepVals0[ModulationVals[y][x - 1]] +
  468. RepVals0[ModulationVals[y][x + 1]] + 1) / 2;
  469. }
  470. // Else it's V-Only
  471. else
  472. {
  473. ModVal = (RepVals0[ModulationVals[y - 1][x]] +
  474. RepVals0[ModulationVals[y + 1][x]] + 1) / 2;
  475. }
  476. }
  477. // Else it's 4BPP and PT encoding
  478. else
  479. {
  480. ModVal = RepVals1[ModulationVals[y][x]];
  481. *DoPT = ModulationVals[y][x] == PT_INDEX;
  482. }
  483. *Mod = ModVal;
  484. }
  485. static unsigned TwiddleUV(unsigned YSize, unsigned XSize, unsigned YPos, unsigned XPos)
  486. {
  487. unsigned Twiddled;
  488. unsigned MinDimension;
  489. unsigned MaxValue;
  490. unsigned SrcBitPos;
  491. unsigned DstBitPos;
  492. int ShiftCount;
  493. if (YSize < XSize)
  494. {
  495. MinDimension = YSize;
  496. MaxValue = XPos;
  497. }
  498. else
  499. {
  500. MinDimension = XSize;
  501. MaxValue = YPos;
  502. }
  503. // Step through all the bits in the "minimum" dimension
  504. SrcBitPos = 1;
  505. DstBitPos = 1;
  506. Twiddled = 0;
  507. ShiftCount = 0;
  508. while (SrcBitPos < MinDimension)
  509. {
  510. if (YPos & SrcBitPos)
  511. {
  512. Twiddled |= DstBitPos;
  513. }
  514. if (XPos & SrcBitPos)
  515. {
  516. Twiddled |= (DstBitPos << 1);
  517. }
  518. SrcBitPos <<= 1;
  519. DstBitPos <<= 2;
  520. ShiftCount += 1;
  521. }
  522. // Prepend any unused bits
  523. MaxValue >>= ShiftCount;
  524. Twiddled |= (MaxValue << (2 * ShiftCount));
  525. return Twiddled;
  526. }
  527. void DecompressImagePVRTC(unsigned char* rgba, const void* blocks, int width, int height, CompressedFormat format)
  528. {
  529. auto* pCompressedData = (AMTC_BLOCK_STRUCT*)blocks;
  530. int AssumeImageTiles = 1;
  531. int Do2bitMode = format == CF_PVRTC_RGB_2BPP || format == CF_PVRTC_RGBA_2BPP;
  532. int x, y;
  533. int i, j;
  534. int BlkX, BlkY;
  535. int BlkXp1, BlkYp1;
  536. int XBlockSize;
  537. int BlkXDim, BlkYDim;
  538. int StartX, StartY;
  539. int ModulationVals[8][16];
  540. int ModulationModes[8][16];
  541. int Mod, DoPT;
  542. unsigned uPosition;
  543. // Local neighbourhood of blocks
  544. AMTC_BLOCK_STRUCT* pBlocks[2][2];
  545. AMTC_BLOCK_STRUCT* pPrevious[2][2] = {{nullptr, nullptr},
  546. {nullptr, nullptr}};
  547. // Low precision colours extracted from the blocks
  548. struct
  549. {
  550. int Reps[2][4];
  551. } Colours5554[2][2];
  552. // Interpolated A and B colours for the pixel
  553. int ASig[4], BSig[4];
  554. int Result[4];
  555. if (Do2bitMode)
  556. {
  557. XBlockSize = BLK_X_2BPP;
  558. }
  559. else
  560. {
  561. XBlockSize = BLK_X_4BPP;
  562. }
  563. // For MBX don't allow the sizes to get too small
  564. BlkXDim = _MAX(2, width / XBlockSize);
  565. BlkYDim = _MAX(2, height / BLK_Y_SIZE);
  566. // Step through the pixels of the image decompressing each one in turn
  567. //
  568. // Note that this is a hideously inefficient way to do this!
  569. for (y = 0; y < height; y++)
  570. {
  571. for (x = 0; x < width; x++)
  572. {
  573. // Map this pixel to the top left neighbourhood of blocks
  574. BlkX = (x - XBlockSize / 2);
  575. BlkY = (y - BLK_Y_SIZE / 2);
  576. BlkX = LIMIT_COORD(BlkX, width, AssumeImageTiles);
  577. BlkY = LIMIT_COORD(BlkY, height, AssumeImageTiles);
  578. BlkX /= XBlockSize;
  579. BlkY /= BLK_Y_SIZE;
  580. // Compute the positions of the other 3 blocks
  581. BlkXp1 = LIMIT_COORD(BlkX + 1, BlkXDim, AssumeImageTiles);
  582. BlkYp1 = LIMIT_COORD(BlkY + 1, BlkYDim, AssumeImageTiles);
  583. // Map to block memory locations
  584. pBlocks[0][0] = pCompressedData + TwiddleUV((unsigned)BlkYDim, (unsigned)BlkXDim, (unsigned)BlkY, (unsigned)BlkX);
  585. pBlocks[0][1] = pCompressedData + TwiddleUV((unsigned)BlkYDim, (unsigned)BlkXDim, (unsigned)BlkY, (unsigned)BlkXp1);
  586. pBlocks[1][0] = pCompressedData + TwiddleUV((unsigned)BlkYDim, (unsigned)BlkXDim, (unsigned)BlkYp1, (unsigned)BlkX);
  587. pBlocks[1][1] = pCompressedData + TwiddleUV((unsigned)BlkYDim, (unsigned)BlkXDim, (unsigned)BlkYp1, (unsigned)BlkXp1);
  588. // Extract the colours and the modulation information IF the previous values
  589. // have changed.
  590. if (memcmp(pPrevious, pBlocks, 4 * sizeof(void*)) != 0)
  591. {
  592. StartY = 0;
  593. for (i = 0; i < 2; i++)
  594. {
  595. StartX = 0;
  596. for (j = 0; j < 2; j++)
  597. {
  598. Unpack5554Colour(pBlocks[i][j], Colours5554[i][j].Reps);
  599. UnpackModulations(pBlocks[i][j],
  600. Do2bitMode,
  601. ModulationVals,
  602. ModulationModes,
  603. StartX, StartY);
  604. StartX += XBlockSize;
  605. }
  606. StartY += BLK_Y_SIZE;
  607. }
  608. // Make a copy of the new pointers
  609. memcpy(pPrevious, pBlocks, 4 * sizeof(void*));
  610. }
  611. // Decompress the pixel. First compute the interpolated A and B signals
  612. InterpolateColours(Colours5554[0][0].Reps[0],
  613. Colours5554[0][1].Reps[0],
  614. Colours5554[1][0].Reps[0],
  615. Colours5554[1][1].Reps[0],
  616. Do2bitMode, x, y,
  617. ASig);
  618. InterpolateColours(Colours5554[0][0].Reps[1],
  619. Colours5554[0][1].Reps[1],
  620. Colours5554[1][0].Reps[1],
  621. Colours5554[1][1].Reps[1],
  622. Do2bitMode, x, y,
  623. BSig);
  624. GetModulationValue(x, y, Do2bitMode, (const int (*)[16])ModulationVals, (const int (*)[16])ModulationModes,
  625. &Mod, &DoPT);
  626. // Compute the modulated colour
  627. for (i = 0; i < 4; i++)
  628. {
  629. Result[i] = ASig[i] * 8 + Mod * (BSig[i] - ASig[i]);
  630. Result[i] >>= 3;
  631. }
  632. if (DoPT)
  633. {
  634. Result[3] = 0;
  635. }
  636. // Store the result in the output image
  637. uPosition = (unsigned)((x + y * width) << 2);
  638. rgba[uPosition + 0] = (unsigned char)Result[0];
  639. rgba[uPosition + 1] = (unsigned char)Result[1];
  640. rgba[uPosition + 2] = (unsigned char)Result[2];
  641. rgba[uPosition + 3] = (unsigned char)Result[3];
  642. }
  643. }
  644. }
  645. void FlipBlockVertical(unsigned char* dest, const unsigned char* src, CompressedFormat format)
  646. {
  647. switch (format)
  648. {
  649. case CF_RGBA:
  650. for (unsigned i = 0; i < 4; ++i)
  651. dest[i] = src[i];
  652. break;
  653. case CF_DXT1:
  654. for (unsigned i = 0; i < 4; ++i)
  655. {
  656. dest[i] = src[i];
  657. dest[i + 4] = src[7 - i];
  658. }
  659. break;
  660. case CF_DXT3:
  661. for (unsigned i = 0; i < 8; i += 2)
  662. {
  663. dest[i] = src[6 - i];
  664. dest[i + 1] = src[6 - i + 1];
  665. }
  666. for (unsigned i = 0; i < 4; ++i)
  667. {
  668. dest[i + 8] = src[i + 8];
  669. dest[i + 12] = src[15 - i];
  670. }
  671. break;
  672. case CF_DXT5:
  673. dest[0] = src[0];
  674. dest[1] = src[1];
  675. {
  676. unsigned a1 = src[2] | ((unsigned)src[3] << 8) | ((unsigned)src[4] << 16);
  677. unsigned a2 = src[5] | ((unsigned)src[6] << 8) | ((unsigned)src[7] << 16);
  678. unsigned b1 = ((a1 & 0x000fff) << 12) | (a1 & 0xfff000) >> 12;
  679. unsigned b2 = ((a2 & 0x000fff) << 12) | (a2 & 0xfff000) >> 12;
  680. dest[2] = (unsigned char)(b2 & 0xff);
  681. dest[3] = (unsigned char)((b2 >> 8) & 0xff);
  682. dest[4] = (unsigned char)((b2 >> 16) & 0xff);
  683. dest[5] = (unsigned char)(b1 & 0xff);
  684. dest[6] = (unsigned char)((b1 >> 8) & 0xff);
  685. dest[7] = (unsigned char)((b1 >> 16) & 0xff);
  686. }
  687. for (unsigned i = 0; i < 4; ++i)
  688. {
  689. dest[i + 8] = src[i + 8];
  690. dest[i + 12] = src[15 - i];
  691. }
  692. break;
  693. default:
  694. // ETC1 & PVRTC not yet implemented
  695. break;
  696. }
  697. }
  698. static unsigned char FlipDXT1Horizontal(unsigned char src)
  699. {
  700. return (unsigned char)(((src & 0x3) << 6) | ((src & 0xc) << 2) | ((src & 0x30) >> 2) | ((src & 0xc0) >> 6));
  701. }
  702. static unsigned FlipDXT5AlphaHorizontal(unsigned src)
  703. {
  704. // Works on 2 lines at a time
  705. return ((src & 0x7) << 9) | ((src & 0x38) << 3) | ((src & 0x1c0) >> 3) | ((src & 0xe00) >> 9) |
  706. ((src & 0x7000) << 9) | ((src & 0x38000) << 3) | ((src & 0x1c0000) >> 3) | ((src & 0xe00000) >> 9);
  707. }
  708. void FlipBlockHorizontal(unsigned char* dest, const unsigned char* src, CompressedFormat format)
  709. {
  710. switch (format)
  711. {
  712. case CF_DXT1:
  713. for (unsigned i = 0; i < 4; ++i)
  714. {
  715. dest[i] = src[i];
  716. dest[i + 4] = FlipDXT1Horizontal(src[i + 4]);
  717. }
  718. break;
  719. case CF_DXT3:
  720. for (unsigned i = 0; i < 8; i += 2)
  721. {
  722. dest[i] = (unsigned char)(((src[i + 1] & 0xf0) >> 4) | ((src[i + 1] & 0xf) << 4));
  723. dest[i + 1] = (unsigned char)(((src[i] & 0xf0) >> 4) | ((src[i] & 0xf) << 4));
  724. }
  725. for (unsigned i = 0; i < 4; ++i)
  726. {
  727. dest[i + 8] = src[i + 8];
  728. dest[i + 12] = FlipDXT1Horizontal(src[i + 12]);
  729. }
  730. break;
  731. case CF_DXT5:
  732. dest[0] = src[0];
  733. dest[1] = src[1];
  734. {
  735. unsigned a1 = src[2] | ((unsigned)src[3] << 8) | ((unsigned)src[4] << 16);
  736. unsigned a2 = src[5] | ((unsigned)src[6] << 8) | ((unsigned)src[7] << 16);
  737. unsigned b1 = FlipDXT5AlphaHorizontal(a1);
  738. unsigned b2 = FlipDXT5AlphaHorizontal(a2);
  739. dest[2] = (unsigned char)(b1 & 0xff);
  740. dest[3] = (unsigned char)((b1 >> 8) & 0xff);
  741. dest[4] = (unsigned char)((b1 >> 16) & 0xff);
  742. dest[5] = (unsigned char)(b2 & 0xff);
  743. dest[6] = (unsigned char)((b2 >> 8) & 0xff);
  744. dest[7] = (unsigned char)((b2 >> 16) & 0xff);
  745. }
  746. for (unsigned i = 0; i < 4; ++i)
  747. {
  748. dest[i + 8] = src[i + 8];
  749. dest[i + 12] = FlipDXT1Horizontal(src[i + 12]);
  750. }
  751. break;
  752. default:
  753. // ETC1 & PVRTC not yet implemented
  754. break;
  755. }
  756. }
  757. static void ReadBigEndian4byteWord(uint32_t* pBlock, const unsigned char *s)
  758. {
  759. *pBlock = (s[0] << 24) | (s[1] << 16) | (s[2] << 8) | s[3];
  760. }
  761. // Use ETCPACK to decompress ETC texture.
  762. void DecompressImageETC(unsigned char* dstImage, const void* blocks, int width, int height, bool hasAlpha)
  763. {
  764. // ETCPACK initialization.
  765. static const bool placeholder = []() { setupAlphaTable(); return true; }();
  766. const int channelCount = hasAlpha ? 4 : 3;
  767. unsigned char* src = (unsigned char*)blocks;
  768. unsigned int blockPart1, blockPart2;
  769. // ETCPACK write 4x4 blocks, so it needs padding.
  770. int w4 = ((width + 3) / 4);
  771. int h4 = ((height + 3) / 4);
  772. unsigned char buffer4x4[4 * 4 * 4];
  773. for (int y = 0; y < h4; ++y)
  774. {
  775. for (int x = 0; x < w4; ++x)
  776. {
  777. memset(&buffer4x4[0], 0xFF, 4 * 4 * 4);
  778. if (hasAlpha)
  779. {
  780. decompressBlockAlphaC(src, &buffer4x4[3], 4, 4, 0, 0, channelCount);
  781. src += 8;
  782. }
  783. ReadBigEndian4byteWord(&blockPart1, src);
  784. src += 4;
  785. ReadBigEndian4byteWord(&blockPart2, src);
  786. src += 4;
  787. decompressBlockETC2c(blockPart1, blockPart2, &buffer4x4[0], 4, 4, 0, 0, 4);
  788. int wbuf = Min(width - x * 4, 4);
  789. int hbuf = Min(height - y * 4, 4);
  790. for(int dy = 0; dy < hbuf; ++dy)
  791. {
  792. for (int dx = 0; dx < wbuf; ++dx)
  793. {
  794. int idst = ((y * 4 + dy) * width + x * 4 + dx) * 4;
  795. int ibuf = (dy * 4 + dx) * 4;
  796. dstImage[idst] = buffer4x4[ibuf];
  797. dstImage[idst + 1] = buffer4x4[ibuf + 1];
  798. dstImage[idst + 2] = buffer4x4[ibuf + 2];
  799. dstImage[idst + 3] = buffer4x4[ibuf + 3];
  800. }
  801. }
  802. }
  803. }
  804. }
  805. }