Decompress.cpp 31 KB

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