Decompress.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. //
  2. // Copyright (c) 2008-2015 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 "../Resource/Decompress.h"
  23. // DXT decompression based on the Squish library, modified for Urho3D
  24. namespace Urho3D
  25. {
  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, int depth, 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 z = 0; z < depth; ++z )
  193. {
  194. int sz = width*height*4*z;
  195. for( int y = 0; y < height; y += 4 )
  196. {
  197. for( int x = 0; x < width; x += 4 )
  198. {
  199. // decompress the block
  200. unsigned char targetRgba[4*16];
  201. DecompressDXT( targetRgba, sourceBlock, format );
  202. // write the decompressed pixels to the correct image locations
  203. unsigned char const* sourcePixel = targetRgba;
  204. for( int py = 0; py < 4; ++py )
  205. {
  206. for( int px = 0; px < 4; ++px )
  207. {
  208. // get the target location
  209. int sx = x + px;
  210. int sy = y + py;
  211. if( sx < width && sy < height )
  212. {
  213. unsigned char* targetPixel = rgba + sz + 4*( width*sy + sx );
  214. // copy the rgba value
  215. for( int i = 0; i < 4; ++i )
  216. *targetPixel++ = *sourcePixel++;
  217. }
  218. else
  219. {
  220. // skip this pixel as its outside the image
  221. sourcePixel += 4;
  222. }
  223. }
  224. }
  225. // advance
  226. sourceBlock += bytesPerBlock;
  227. }
  228. }
  229. }
  230. }
  231. // ETC and PVRTC decompression based on the Oolong Engine, modified for Urho3D
  232. /*
  233. Oolong Engine for the iPhone / iPod touch
  234. Copyright (c) 2007-2008 Wolfgang Engel http://code.google.com/p/oolongengine/
  235. This software is provided 'as-is', without any express or implied warranty
  236. In no event will the authors be held liable for any damages arising from the
  237. use of this software. Permission is granted to anyone to use this software for
  238. any purpose, including commercial applications, and to alter it and
  239. redistribute it freely, subject to the following restrictions:
  240. 1. The origin of this software must not be misrepresented; you must not claim
  241. that you wrote the original software. If you use this software in a product, an
  242. acknowledgment in the product documentation would be appreciated but is not
  243. required.
  244. 2. Altered source versions must be plainly marked as such, and must not be
  245. misrepresented as being the original software.
  246. 3. This notice may not be removed or altered from any source distribution.
  247. */
  248. #define _CLAMP_(X,Xmin,Xmax) ( (X)<(Xmax) ? ( (X)<(Xmin)?(Xmin):(X) ) : (Xmax) )
  249. unsigned ETC_FLIP = 0x01000000;
  250. unsigned ETC_DIFF = 0x02000000;
  251. const int mod[8][4]={{2, 8,-2,-8},
  252. {5, 17, -5, -17},
  253. {9, 29, -9, -29},
  254. {13, 42, -13, -42},
  255. {18, 60, -18, -60},
  256. {24, 80, -24, -80},
  257. {33, 106, -33, -106},
  258. {47, 183, -47, -183}};
  259. // lsb: hgfedcba ponmlkji msb: hgfedcba ponmlkji due to endianness
  260. static unsigned long ModifyPixel(int red, int green, int blue, int x, int y, unsigned long modBlock, int modTable)
  261. {
  262. int index = x*4+y, pixelMod;
  263. unsigned long mostSig = modBlock<<1;
  264. if (index<8) //hgfedcba
  265. pixelMod = mod[modTable][((modBlock>>(index+24))&0x1)+((mostSig>>(index+8))&0x2)];
  266. else // ponmlkj
  267. pixelMod = mod[modTable][((modBlock>>(index+8))&0x1)+((mostSig>>(index-8))&0x2)];
  268. red = _CLAMP_(red+pixelMod,0,255);
  269. green = _CLAMP_(green+pixelMod,0,255);
  270. blue = _CLAMP_(blue+pixelMod,0,255);
  271. return ((blue<<16) + (green<<8) + red)|0xff000000;
  272. }
  273. static void DecompressETC(unsigned char* pDestData, const void* pSrcData)
  274. {
  275. unsigned long blockTop, blockBot, *input = (unsigned long*)pSrcData, *output;
  276. unsigned char red1, green1, blue1, red2, green2, blue2;
  277. bool bFlip, bDiff;
  278. int modtable1,modtable2;
  279. blockTop = *(input++);
  280. blockBot = *(input++);
  281. output = (unsigned long*)pDestData;
  282. // check flipbit
  283. bFlip = (blockTop & ETC_FLIP) != 0;
  284. bDiff = (blockTop & ETC_DIFF) != 0;
  285. if(bDiff)
  286. { // differential mode 5 colour bits + 3 difference bits
  287. // get base colour for subblock 1
  288. blue1 = (unsigned char)((blockTop&0xf80000)>>16);
  289. green1 = (unsigned char)((blockTop&0xf800)>>8);
  290. red1 = (unsigned char)(blockTop&0xf8);
  291. // get differential colour for subblock 2
  292. signed char blues = (signed char)(blue1>>3) + ((signed char) ((blockTop & 0x70000) >> 11)>>5);
  293. signed char greens = (signed char)(green1>>3) + ((signed char)((blockTop & 0x700) >>3)>>5);
  294. signed char reds = (signed char)(red1>>3) + ((signed char)((blockTop & 0x7)<<5)>>5);
  295. blue2 = (unsigned char)blues;
  296. green2 = (unsigned char)greens;
  297. red2 = (unsigned char)reds;
  298. red1 = red1 +(red1>>5); // copy bits to lower sig
  299. green1 = green1 + (green1>>5); // copy bits to lower sig
  300. blue1 = blue1 + (blue1>>5); // copy bits to lower sig
  301. red2 = (red2<<3) +(red2>>2); // copy bits to lower sig
  302. green2 = (green2<<3) + (green2>>2); // copy bits to lower sig
  303. blue2 = (blue2<<3) + (blue2>>2); // copy bits to lower sig
  304. }
  305. else
  306. { // individual mode 4 + 4 colour bits
  307. // get base colour for subblock 1
  308. blue1 = (unsigned char)((blockTop&0xf00000)>>16);
  309. blue1 = blue1 +(blue1>>4); // copy bits to lower sig
  310. green1 = (unsigned char)((blockTop&0xf000)>>8);
  311. green1 = green1 + (green1>>4); // copy bits to lower sig
  312. red1 = (unsigned char)(blockTop&0xf0);
  313. red1 = red1 + (red1>>4); // copy bits to lower sig
  314. // get base colour for subblock 2
  315. blue2 = (unsigned char)((blockTop&0xf0000)>>12);
  316. blue2 = blue2 +(blue2>>4); // copy bits to lower sig
  317. green2 = (unsigned char)((blockTop&0xf00)>>4);
  318. green2 = green2 + (green2>>4); // copy bits to lower sig
  319. red2 = (unsigned char)((blockTop&0xf)<<4);
  320. red2 = red2 + (red2>>4); // copy bits to lower sig
  321. }
  322. // get the modtables for each subblock
  323. modtable1 = (blockTop>>29)&0x7;
  324. modtable2 = (blockTop>>26)&0x7;
  325. if(!bFlip)
  326. { // 2 2x4 blocks side by side
  327. for(int j=0;j<4;j++) // vertical
  328. {
  329. for(int k=0;k<2;k++) // horizontal
  330. {
  331. *(output+j*4+k) = ModifyPixel(red1,green1,blue1,k,j,blockBot,modtable1);
  332. *(output+j*4+k+2) = ModifyPixel(red2,green2,blue2,k+2,j,blockBot,modtable2);
  333. }
  334. }
  335. }
  336. else
  337. { // 2 4x2 blocks on top of each other
  338. for(int j=0;j<2;j++)
  339. {
  340. for(int k=0;k<4;k++)
  341. {
  342. *(output+j*4+k) = ModifyPixel(red1,green1,blue1,k,j,blockBot,modtable1);
  343. *(output+(j+2)*4+k) = ModifyPixel(red2,green2,blue2,k,j+2,blockBot,modtable2);
  344. }
  345. }
  346. }
  347. }
  348. void DecompressImageETC( unsigned char* rgba, const void* blocks, int width, int height )
  349. {
  350. // initialise the block input
  351. unsigned char const* sourceBlock = reinterpret_cast< unsigned char const* >( blocks );
  352. int bytesPerBlock = 8;
  353. // loop over blocks
  354. for( int y = 0; y < height; y += 4 )
  355. {
  356. for( int x = 0; x < width; x += 4 )
  357. {
  358. // decompress the block
  359. unsigned char targetRgba[4*16];
  360. DecompressETC( targetRgba, sourceBlock );
  361. // write the decompressed pixels to the correct image locations
  362. unsigned char const* sourcePixel = targetRgba;
  363. for( int py = 0; py < 4; ++py )
  364. {
  365. for( int px = 0; px < 4; ++px )
  366. {
  367. // get the target location
  368. int sx = x + px;
  369. int sy = y + py;
  370. if( sx < width && sy < height )
  371. {
  372. unsigned char* targetPixel = rgba + 4*( width*sy + sx );
  373. // copy the rgba value
  374. for( int i = 0; i < 4; ++i )
  375. *targetPixel++ = *sourcePixel++;
  376. }
  377. else
  378. {
  379. // skip this pixel as its outside the image
  380. sourcePixel += 4;
  381. }
  382. }
  383. }
  384. // advance
  385. sourceBlock += bytesPerBlock;
  386. }
  387. }
  388. }
  389. #define PT_INDEX (2) /*The Punch-through index*/
  390. #define BLK_Y_SIZE (4) /*always 4 for all 2D block types*/
  391. #define BLK_X_MAX (8) /*Max X dimension for blocks*/
  392. #define BLK_X_2BPP (8) /*dimensions for the two formats*/
  393. #define BLK_X_4BPP (4)
  394. #define _MIN(X,Y) (((X)<(Y))? (X):(Y))
  395. #define _MAX(X,Y) (((X)>(Y))? (X):(Y))
  396. #define WRAP_COORD(Val, Size) ((Val) & ((Size)-1))
  397. #define CLAMP(X, lower, upper) (_MIN(_MAX((X),(lower)), (upper)))
  398. #define LIMIT_COORD(Val, Size, AssumeImageTiles) ((AssumeImageTiles)? WRAP_COORD((Val), (Size)): CLAMP((Val), 0, (Size)-1))
  399. typedef struct
  400. {
  401. // Uses 64 bits pre block
  402. unsigned PackedData[2];
  403. } AMTC_BLOCK_STRUCT;
  404. static void Unpack5554Colour(const AMTC_BLOCK_STRUCT *pBlock, int ABColours[2][4])
  405. {
  406. unsigned RawBits[2];
  407. int i;
  408. // Extract A and B
  409. RawBits[0] = pBlock->PackedData[1] & (0xFFFE); /*15 bits (shifted up by one)*/
  410. RawBits[1] = pBlock->PackedData[1] >> 16; /*16 bits*/
  411. // Step through both colours
  412. for(i = 0; i < 2; i++)
  413. {
  414. // If completely opaque
  415. if(RawBits[i] & (1<<15))
  416. {
  417. // Extract R and G (both 5 bit)
  418. ABColours[i][0] = (RawBits[i] >> 10) & 0x1F;
  419. ABColours[i][1] = (RawBits[i] >> 5) & 0x1F;
  420. // The precision of Blue depends on A or B. If A then we need to
  421. // replicate the top bit to get 5 bits in total
  422. ABColours[i][2] = RawBits[i] & 0x1F;
  423. if(i==0)
  424. {
  425. ABColours[0][2] |= ABColours[0][2] >> 4;
  426. }
  427. // Set 4bit alpha fully on...
  428. ABColours[i][3] = 0xF;
  429. }
  430. // Else if colour has variable translucency
  431. else
  432. {
  433. // Extract R and G (both 4 bit).
  434. // (Leave a space on the end for the replication of bits
  435. ABColours[i][0] = (RawBits[i] >> (8-1)) & 0x1E;
  436. ABColours[i][1] = (RawBits[i] >> (4-1)) & 0x1E;
  437. // Replicate bits to truly expand to 5 bits
  438. ABColours[i][0] |= ABColours[i][0] >> 4;
  439. ABColours[i][1] |= ABColours[i][1] >> 4;
  440. // Grab the 3(+padding) or 4 bits of blue and add an extra padding bit
  441. ABColours[i][2] = (RawBits[i] & 0xF) << 1;
  442. // Expand from 3 to 5 bits if this is from colour A, or 4 to 5 bits if from
  443. // colour B
  444. if(i==0)
  445. {
  446. ABColours[0][2] |= ABColours[0][2] >> 3;
  447. }
  448. else
  449. {
  450. ABColours[0][2] |= ABColours[0][2] >> 4;
  451. }
  452. // Set the alpha bits to be 3 + a zero on the end
  453. ABColours[i][3] = (RawBits[i] >> 11) & 0xE;
  454. }
  455. }
  456. }
  457. static void UnpackModulations(const AMTC_BLOCK_STRUCT *pBlock, const int Do2bitMode, int ModulationVals[8][16], int ModulationModes[8][16], int StartX, int StartY)
  458. {
  459. int BlockModMode;
  460. unsigned ModulationBits;
  461. int x, y;
  462. BlockModMode= pBlock->PackedData[1] & 1;
  463. ModulationBits = pBlock->PackedData[0];
  464. // If it's in an interpolated mode
  465. if(Do2bitMode && BlockModMode)
  466. {
  467. // Run through all the pixels in the block. Note we can now treat all the
  468. // "stored" values as if they have 2bits (even when they didn't!)
  469. for(y = 0; y < BLK_Y_SIZE; y++)
  470. {
  471. for(x = 0; x < BLK_X_2BPP; x++)
  472. {
  473. ModulationModes[y+StartY][x+StartX] = BlockModMode;
  474. // If this is a stored value...
  475. if(((x^y)&1) == 0)
  476. {
  477. ModulationVals[y+StartY][x+StartX] = ModulationBits & 3;
  478. ModulationBits >>= 2;
  479. }
  480. }
  481. }
  482. }
  483. // Else if direct encoded 2bit mode - i.e. 1 mode bit per pixel
  484. else if(Do2bitMode)
  485. {
  486. for(y = 0; y < BLK_Y_SIZE; y++)
  487. {
  488. for(x = 0; x < BLK_X_2BPP; x++)
  489. {
  490. ModulationModes[y+StartY][x+StartX] = BlockModMode;
  491. // Double the bits so 0=> 00, and 1=>11
  492. if(ModulationBits & 1)
  493. {
  494. ModulationVals[y+StartY][x+StartX] = 0x3;
  495. }
  496. else
  497. {
  498. ModulationVals[y+StartY][x+StartX] = 0x0;
  499. }
  500. ModulationBits >>= 1;
  501. }
  502. }
  503. }
  504. // Else its the 4bpp mode so each value has 2 bits
  505. else
  506. {
  507. for(y = 0; y < BLK_Y_SIZE; y++)
  508. {
  509. for(x = 0; x < BLK_X_4BPP; x++)
  510. {
  511. ModulationModes[y+StartY][x+StartX] = BlockModMode;
  512. ModulationVals[y+StartY][x+StartX] = ModulationBits & 3;
  513. ModulationBits >>= 2;
  514. }
  515. }
  516. }
  517. }
  518. 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])
  519. {
  520. int u, v, uscale;
  521. int k;
  522. int tmp1, tmp2;
  523. int P[4], Q[4], R[4], S[4];
  524. // Copy the colours
  525. for(k = 0; k < 4; k++)
  526. {
  527. P[k] = ColourP[k];
  528. Q[k] = ColourQ[k];
  529. R[k] = ColourR[k];
  530. S[k] = ColourS[k];
  531. }
  532. // Put the x and y values into the right range
  533. v = (y & 0x3) | ((~y & 0x2) << 1);
  534. if(Do2bitMode)
  535. {
  536. u = (x & 0x7) | ((~x & 0x4) << 1);
  537. }
  538. else
  539. {
  540. u = (x & 0x3) | ((~x & 0x2) << 1);
  541. }
  542. // Get the u and v scale amounts
  543. v = v - BLK_Y_SIZE/2;
  544. if(Do2bitMode)
  545. {
  546. u = u - BLK_X_2BPP/2;
  547. uscale = 8;
  548. }
  549. else
  550. {
  551. u = u - BLK_X_4BPP/2;
  552. uscale = 4;
  553. }
  554. for(k = 0; k < 4; k++)
  555. {
  556. tmp1 = P[k] * uscale + u * (Q[k] - P[k]);
  557. tmp2 = R[k] * uscale + u * (S[k] - R[k]);
  558. tmp1 = tmp1 * 4 + v * (tmp2 - tmp1);
  559. Result[k] = tmp1;
  560. }
  561. // Lop off the appropriate number of bits to get us to 8 bit precision
  562. if(Do2bitMode)
  563. {
  564. // Do RGB
  565. for(k = 0; k < 3; k++)
  566. {
  567. Result[k] >>= 2;
  568. }
  569. Result[3] >>= 1;
  570. }
  571. else
  572. {
  573. // Do RGB (A is ok)
  574. for(k = 0; k < 3; k++)
  575. {
  576. Result[k] >>= 1;
  577. }
  578. }
  579. // Convert from 5554 to 8888
  580. //
  581. // do RGB 5.3 => 8
  582. for(k = 0; k < 3; k++)
  583. {
  584. Result[k] += Result[k] >> 5;
  585. }
  586. Result[3] += Result[3] >> 4;
  587. }
  588. static void GetModulationValue(int x, int y, const int Do2bitMode, const int ModulationVals[8][16], const int ModulationModes[8][16], int *Mod, int *DoPT)
  589. {
  590. static const int RepVals0[4] = {0, 3, 5, 8};
  591. static const int RepVals1[4] = {0, 4, 4, 8};
  592. int ModVal;
  593. // Map X and Y into the local 2x2 block
  594. y = (y & 0x3) | ((~y & 0x2) << 1);
  595. if(Do2bitMode)
  596. {
  597. x = (x & 0x7) | ((~x & 0x4) << 1);
  598. }
  599. else
  600. {
  601. x = (x & 0x3) | ((~x & 0x2) << 1);
  602. }
  603. // Assume no PT for now
  604. *DoPT = 0;
  605. // Extract the modulation value. If a simple encoding
  606. if(ModulationModes[y][x]==0)
  607. {
  608. ModVal = RepVals0[ModulationVals[y][x]];
  609. }
  610. else if(Do2bitMode)
  611. {
  612. // If this is a stored value
  613. if(((x^y)&1)==0)
  614. {
  615. ModVal = RepVals0[ModulationVals[y][x]];
  616. }
  617. // Else average from the neighbours
  618. //
  619. // If H&V interpolation...
  620. else if(ModulationModes[y][x] == 1)
  621. {
  622. ModVal = (RepVals0[ModulationVals[y-1][x]] +
  623. RepVals0[ModulationVals[y+1][x]] +
  624. RepVals0[ModulationVals[y][x-1]] +
  625. RepVals0[ModulationVals[y][x+1]] + 2) / 4;
  626. }
  627. // Else if H-Only
  628. else if(ModulationModes[y][x] == 2)
  629. {
  630. ModVal = (RepVals0[ModulationVals[y][x-1]] +
  631. RepVals0[ModulationVals[y][x+1]] + 1) / 2;
  632. }
  633. // Else it's V-Only
  634. else
  635. {
  636. ModVal = (RepVals0[ModulationVals[y-1][x]] +
  637. RepVals0[ModulationVals[y+1][x]] + 1) / 2;
  638. }
  639. }
  640. // Else it's 4BPP and PT encoding
  641. else
  642. {
  643. ModVal = RepVals1[ModulationVals[y][x]];
  644. *DoPT = ModulationVals[y][x] == PT_INDEX;
  645. }
  646. *Mod = ModVal;
  647. }
  648. static unsigned TwiddleUV(unsigned YSize, unsigned XSize, unsigned YPos, unsigned XPos)
  649. {
  650. unsigned Twiddled;
  651. unsigned MinDimension;
  652. unsigned MaxValue;
  653. unsigned SrcBitPos;
  654. unsigned DstBitPos;
  655. int ShiftCount;
  656. if (YSize < XSize)
  657. {
  658. MinDimension = YSize;
  659. MaxValue = XPos;
  660. }
  661. else
  662. {
  663. MinDimension = XSize;
  664. MaxValue = YPos;
  665. }
  666. // Step through all the bits in the "minimum" dimension
  667. SrcBitPos = 1;
  668. DstBitPos = 1;
  669. Twiddled = 0;
  670. ShiftCount = 0;
  671. while(SrcBitPos < MinDimension)
  672. {
  673. if(YPos & SrcBitPos)
  674. {
  675. Twiddled |= DstBitPos;
  676. }
  677. if(XPos & SrcBitPos)
  678. {
  679. Twiddled |= (DstBitPos << 1);
  680. }
  681. SrcBitPos <<= 1;
  682. DstBitPos <<= 2;
  683. ShiftCount += 1;
  684. }
  685. // Prepend any unused bits
  686. MaxValue >>= ShiftCount;
  687. Twiddled |= (MaxValue << (2*ShiftCount));
  688. return Twiddled;
  689. }
  690. void DecompressImagePVRTC(unsigned char* dest, const void *blocks, int width, int height, CompressedFormat format)
  691. {
  692. AMTC_BLOCK_STRUCT* pCompressedData = (AMTC_BLOCK_STRUCT*)blocks;
  693. int AssumeImageTiles = 1;
  694. int Do2bitMode = format == CF_PVRTC_RGB_2BPP || format == CF_PVRTC_RGBA_2BPP;
  695. int x, y;
  696. int i, j;
  697. int BlkX, BlkY;
  698. int BlkXp1, BlkYp1;
  699. int XBlockSize;
  700. int BlkXDim, BlkYDim;
  701. int StartX, StartY;
  702. int ModulationVals[8][16];
  703. int ModulationModes[8][16];
  704. int Mod, DoPT;
  705. unsigned uPosition;
  706. // Local neighbourhood of blocks
  707. AMTC_BLOCK_STRUCT *pBlocks[2][2];
  708. AMTC_BLOCK_STRUCT *pPrevious[2][2] = {{NULL, NULL}, {NULL, NULL}};
  709. // Low precision colours extracted from the blocks
  710. struct
  711. {
  712. int Reps[2][4];
  713. } Colours5554[2][2];
  714. // Interpolated A and B colours for the pixel
  715. int ASig[4], BSig[4];
  716. int Result[4];
  717. if(Do2bitMode)
  718. {
  719. XBlockSize = BLK_X_2BPP;
  720. }
  721. else
  722. {
  723. XBlockSize = BLK_X_4BPP;
  724. }
  725. // For MBX don't allow the sizes to get too small
  726. BlkXDim = _MAX(2, width / XBlockSize);
  727. BlkYDim = _MAX(2, height / BLK_Y_SIZE);
  728. // Step through the pixels of the image decompressing each one in turn
  729. //
  730. // Note that this is a hideously inefficient way to do this!
  731. for(y = 0; y < height; y++)
  732. {
  733. for(x = 0; x < width; x++)
  734. {
  735. // Map this pixel to the top left neighbourhood of blocks
  736. BlkX = (x - XBlockSize/2);
  737. BlkY = (y - BLK_Y_SIZE/2);
  738. BlkX = LIMIT_COORD(BlkX, width, AssumeImageTiles);
  739. BlkY = LIMIT_COORD(BlkY, height, AssumeImageTiles);
  740. BlkX /= XBlockSize;
  741. BlkY /= BLK_Y_SIZE;
  742. // Compute the positions of the other 3 blocks
  743. BlkXp1 = LIMIT_COORD(BlkX+1, BlkXDim, AssumeImageTiles);
  744. BlkYp1 = LIMIT_COORD(BlkY+1, BlkYDim, AssumeImageTiles);
  745. // Map to block memory locations
  746. pBlocks[0][0] = pCompressedData + TwiddleUV(BlkYDim, BlkXDim, BlkY, BlkX);
  747. pBlocks[0][1] = pCompressedData + TwiddleUV(BlkYDim, BlkXDim, BlkY, BlkXp1);
  748. pBlocks[1][0] = pCompressedData + TwiddleUV(BlkYDim, BlkXDim, BlkYp1, BlkX);
  749. pBlocks[1][1] = pCompressedData + TwiddleUV(BlkYDim, BlkXDim, BlkYp1, BlkXp1);
  750. // Extract the colours and the modulation information IF the previous values
  751. // have changed.
  752. if(memcmp(pPrevious, pBlocks, 4*sizeof(void*)) != 0)
  753. {
  754. StartY = 0;
  755. for(i = 0; i < 2; i++)
  756. {
  757. StartX = 0;
  758. for(j = 0; j < 2; j++)
  759. {
  760. Unpack5554Colour(pBlocks[i][j], Colours5554[i][j].Reps);
  761. UnpackModulations(pBlocks[i][j],
  762. Do2bitMode,
  763. ModulationVals,
  764. ModulationModes,
  765. StartX, StartY);
  766. StartX += XBlockSize;
  767. }
  768. StartY += BLK_Y_SIZE;
  769. }
  770. // Make a copy of the new pointers
  771. memcpy(pPrevious, pBlocks, 4*sizeof(void*));
  772. }
  773. // Decompress the pixel. First compute the interpolated A and B signals
  774. InterpolateColours(Colours5554[0][0].Reps[0],
  775. Colours5554[0][1].Reps[0],
  776. Colours5554[1][0].Reps[0],
  777. Colours5554[1][1].Reps[0],
  778. Do2bitMode, x, y,
  779. ASig);
  780. InterpolateColours(Colours5554[0][0].Reps[1],
  781. Colours5554[0][1].Reps[1],
  782. Colours5554[1][0].Reps[1],
  783. Colours5554[1][1].Reps[1],
  784. Do2bitMode, x, y,
  785. BSig);
  786. GetModulationValue(x,y, Do2bitMode, (const int (*)[16])ModulationVals, (const int (*)[16])ModulationModes,
  787. &Mod, &DoPT);
  788. // Compute the modulated colour
  789. for(i = 0; i < 4; i++)
  790. {
  791. Result[i] = ASig[i] * 8 + Mod * (BSig[i] - ASig[i]);
  792. Result[i] >>= 3;
  793. }
  794. if(DoPT)
  795. {
  796. Result[3] = 0;
  797. }
  798. // Store the result in the output image
  799. uPosition = (x+y*width)<<2;
  800. dest[uPosition+0] = (unsigned char)Result[0];
  801. dest[uPosition+1] = (unsigned char)Result[1];
  802. dest[uPosition+2] = (unsigned char)Result[2];
  803. dest[uPosition+3] = (unsigned char)Result[3];
  804. }
  805. }
  806. }
  807. void FlipBlockVertical(unsigned char* dest, unsigned char* src, CompressedFormat format)
  808. {
  809. switch (format)
  810. {
  811. case CF_RGBA:
  812. for (unsigned i = 0; i < 4; ++i)
  813. dest[i] = src[i];
  814. break;
  815. case CF_DXT1:
  816. for (unsigned i = 0; i < 4; ++i)
  817. {
  818. dest[i] = src[i];
  819. dest[i + 4] = src[7 - i];
  820. }
  821. break;
  822. case CF_DXT3:
  823. for (unsigned i = 0; i < 8; i += 2)
  824. {
  825. dest[i] = src[6 - i];
  826. dest[i + 1] = src[6 - i + 1];
  827. }
  828. for (unsigned i = 0; i < 4; ++i)
  829. {
  830. dest[i + 8] = src[i + 8];
  831. dest[i + 12] = src[15 - i];
  832. }
  833. break;
  834. case CF_DXT5:
  835. dest[0] = src[0];
  836. dest[1] = src[1];
  837. {
  838. unsigned a1 = src[2] | ((unsigned)src[3] << 8) | ((unsigned)src[4] << 16);
  839. unsigned a2 = src[5] | ((unsigned)src[6] << 8) | ((unsigned)src[7] << 16);
  840. unsigned b1 = ((a1 & 0x000fff) << 12) | (a1 & 0xfff000) >> 12;
  841. unsigned b2 = ((a2 & 0x000fff) << 12) | (a2 & 0xfff000) >> 12;
  842. dest[2] = b2 & 0xff;
  843. dest[3] = (b2 >> 8) & 0xff;
  844. dest[4] = (b2 >> 16) & 0xff;
  845. dest[5] = b1 & 0xff;
  846. dest[6] = (b1 >> 8) & 0xff;
  847. dest[7] = (b1 >> 16) & 0xff;
  848. }
  849. for (unsigned i = 0; i < 4; ++i)
  850. {
  851. dest[i + 8] = src[i + 8];
  852. dest[i + 12] = src[15 - i];
  853. }
  854. break;
  855. default:
  856. /// ETC1 & PVRTC not yet implemented
  857. break;
  858. }
  859. }
  860. static unsigned char FlipDXT1Horizontal(unsigned char src)
  861. {
  862. return ((src & 0x3) << 6) | ((src & 0xc) << 2) | ((src & 0x30) >> 2) | ((src & 0xc0) >> 6);
  863. }
  864. static unsigned FlipDXT5AlphaHorizontal(unsigned src)
  865. {
  866. // Works on 2 lines at a time
  867. return ((src & 0x7) << 9) | ((src & 0x38) << 3) | ((src & 0x1c0) >> 3) | ((src & 0xe00) >> 9) |
  868. ((src & 0x7000) << 9) | ((src & 0x38000) << 3) | ((src & 0x1c0000) >> 3) | ((src & 0xe00000) >> 9);
  869. }
  870. void FlipBlockHorizontal(unsigned char* dest, unsigned char* src, CompressedFormat format)
  871. {
  872. switch (format)
  873. {
  874. case CF_DXT1:
  875. for (unsigned i = 0; i < 4; ++i)
  876. {
  877. dest[i] = src[i];
  878. dest[i + 4] = FlipDXT1Horizontal(src[i + 4]);
  879. }
  880. break;
  881. case CF_DXT3:
  882. for (unsigned i = 0; i < 8; i += 2)
  883. {
  884. dest[i] = ((src[i+1] & 0xf0) >> 4) | ((src[i+1] & 0xf) << 4);
  885. dest[i+1] = ((src[i] & 0xf0) >> 4) | ((src[i] & 0xf) << 4);
  886. }
  887. for (unsigned i = 0; i < 4; ++i)
  888. {
  889. dest[i + 8] = src[i + 8];
  890. dest[i + 12] = FlipDXT1Horizontal(src[i + 12]);
  891. }
  892. break;
  893. case CF_DXT5:
  894. dest[0] = src[0];
  895. dest[1] = src[1];
  896. {
  897. unsigned a1 = src[2] | ((unsigned)src[3] << 8) | ((unsigned)src[4] << 16);
  898. unsigned a2 = src[5] | ((unsigned)src[6] << 8) | ((unsigned)src[7] << 16);
  899. unsigned b1 = FlipDXT5AlphaHorizontal(a1);
  900. unsigned b2 = FlipDXT5AlphaHorizontal(a2);
  901. dest[2] = b1 & 0xff;
  902. dest[3] = (b1 >> 8) & 0xff;
  903. dest[4] = (b1 >> 16) & 0xff;
  904. dest[5] = b2 & 0xff;
  905. dest[6] = (b2 >> 8) & 0xff;
  906. dest[7] = (b2 >> 16) & 0xff;
  907. }
  908. for (unsigned i = 0; i < 4; ++i)
  909. {
  910. dest[i + 8] = src[i + 8];
  911. dest[i + 12] = FlipDXT1Horizontal(src[i + 12]);
  912. }
  913. break;
  914. default:
  915. /// ETC1 & PVRTC not yet implemented
  916. break;
  917. }
  918. }
  919. }