Decompress.cpp 31 KB

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