Decompress.cpp 31 KB

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