DXTDecoder.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. /*
  21. ================================================================================================
  22. Contains the DxtDecoder implementation.
  23. ================================================================================================
  24. */
  25. #pragma hdrstop
  26. #include "DXTCodec_local.h"
  27. #include "DXTCodec.h"
  28. /*
  29. ========================
  30. idDxtDecoder::EmitBlock
  31. ========================
  32. */
  33. void idDxtDecoder::EmitBlock( byte *outPtr, int x, int y, const byte *colorBlock ) {
  34. outPtr += ( y * width + x ) * 4;
  35. for ( int j = 0; j < 4; j++ ) {
  36. memcpy( outPtr, &colorBlock[j*4*4], 4*4 );
  37. outPtr += width * 4;
  38. }
  39. }
  40. /*
  41. ========================
  42. idDxtDecoder::DecodeAlphaValues
  43. ========================
  44. */
  45. void idDxtDecoder::DecodeAlphaValues( byte *colorBlock, const int offset ) {
  46. int i;
  47. unsigned int indexes;
  48. byte alphas[8];
  49. alphas[0] = ReadByte();
  50. alphas[1] = ReadByte();
  51. if ( alphas[0] > alphas[1] ) {
  52. alphas[2] = ( 6 * alphas[0] + 1 * alphas[1] ) / 7;
  53. alphas[3] = ( 5 * alphas[0] + 2 * alphas[1] ) / 7;
  54. alphas[4] = ( 4 * alphas[0] + 3 * alphas[1] ) / 7;
  55. alphas[5] = ( 3 * alphas[0] + 4 * alphas[1] ) / 7;
  56. alphas[6] = ( 2 * alphas[0] + 5 * alphas[1] ) / 7;
  57. alphas[7] = ( 1 * alphas[0] + 6 * alphas[1] ) / 7;
  58. } else {
  59. alphas[2] = ( 4 * alphas[0] + 1 * alphas[1] ) / 5;
  60. alphas[3] = ( 3 * alphas[0] + 2 * alphas[1] ) / 5;
  61. alphas[4] = ( 2 * alphas[0] + 3 * alphas[1] ) / 5;
  62. alphas[5] = ( 1 * alphas[0] + 4 * alphas[1] ) / 5;
  63. alphas[6] = 0;
  64. alphas[7] = 255;
  65. }
  66. colorBlock += offset;
  67. indexes = (int)ReadByte() | ( (int)ReadByte() << 8 ) | ( (int)ReadByte() << 16 );
  68. for ( i = 0; i < 8; i++ ) {
  69. colorBlock[i*4] = alphas[indexes & 7];
  70. indexes >>= 3;
  71. }
  72. indexes = (int)ReadByte() | ( (int)ReadByte() << 8 ) | ( (int)ReadByte() << 16 );
  73. for ( i = 8; i < 16; i++ ) {
  74. colorBlock[i*4] = alphas[indexes & 7];
  75. indexes >>= 3;
  76. }
  77. }
  78. /*
  79. ========================
  80. idDxtDecoder::DecodeColorValues
  81. ========================
  82. */
  83. void idDxtDecoder::DecodeColorValues( byte *colorBlock, bool noBlack, bool writeAlpha ) {
  84. byte colors[4][4];
  85. unsigned short color0 = ReadUShort();
  86. unsigned short color1 = ReadUShort();
  87. ColorFrom565( color0, colors[0] );
  88. ColorFrom565( color1, colors[1] );
  89. colors[0][3] = 255;
  90. colors[1][3] = 255;
  91. if ( noBlack || color0 > color1 ) {
  92. colors[2][0] = ( 2 * colors[0][0] + 1 * colors[1][0] ) / 3;
  93. colors[2][1] = ( 2 * colors[0][1] + 1 * colors[1][1] ) / 3;
  94. colors[2][2] = ( 2 * colors[0][2] + 1 * colors[1][2] ) / 3;
  95. colors[2][3] = 255;
  96. colors[3][0] = ( 1 * colors[0][0] + 2 * colors[1][0] ) / 3;
  97. colors[3][1] = ( 1 * colors[0][1] + 2 * colors[1][1] ) / 3;
  98. colors[3][2] = ( 1 * colors[0][2] + 2 * colors[1][2] ) / 3;
  99. colors[3][3] = 255;
  100. } else {
  101. colors[2][0] = ( 1 * colors[0][0] + 1 * colors[1][0] ) / 2;
  102. colors[2][1] = ( 1 * colors[0][1] + 1 * colors[1][1] ) / 2;
  103. colors[2][2] = ( 1 * colors[0][2] + 1 * colors[1][2] ) / 2;
  104. colors[2][3] = 255;
  105. colors[3][0] = 0;
  106. colors[3][1] = 0;
  107. colors[3][2] = 0;
  108. colors[3][3] = 0;
  109. }
  110. unsigned int indexes = ReadUInt();
  111. for ( int i = 0; i < 16; i++ ) {
  112. colorBlock[i*4+0] = colors[indexes & 3][0];
  113. colorBlock[i*4+1] = colors[indexes & 3][1];
  114. colorBlock[i*4+2] = colors[indexes & 3][2];
  115. if ( writeAlpha ) {
  116. colorBlock[i*4+3] = colors[indexes & 3][3];
  117. }
  118. indexes >>= 2;
  119. }
  120. }
  121. /*
  122. ========================
  123. idDxtDecoder::DecodeCTX1Values
  124. ========================
  125. */
  126. void idDxtDecoder::DecodeCTX1Values( byte *colorBlock ) {
  127. byte colors[4][2];
  128. colors[0][0] = ReadByte();
  129. colors[0][1] = ReadByte();
  130. colors[1][0] = ReadByte();
  131. colors[1][1] = ReadByte();
  132. colors[2][0] = ( 2 * colors[0][0] + 1 * colors[1][0] ) / 3;
  133. colors[2][1] = ( 2 * colors[0][1] + 1 * colors[1][1] ) / 3;
  134. colors[3][0] = ( 1 * colors[0][0] + 2 * colors[1][0] ) / 3;
  135. colors[3][1] = ( 1 * colors[0][1] + 2 * colors[1][1] ) / 3;
  136. unsigned int indexes = ReadUInt();
  137. for ( int i = 0; i < 16; i++ ) {
  138. colorBlock[i*4+0] = colors[indexes & 3][0];
  139. colorBlock[i*4+1] = colors[indexes & 3][1];
  140. indexes >>= 2;
  141. }
  142. }
  143. /*
  144. ========================
  145. idDxtDecoder::DecompressImageDXT1
  146. ========================
  147. */
  148. void idDxtDecoder::DecompressImageDXT1( const byte *inBuf, byte *outBuf, int width, int height ) {
  149. byte block[64];
  150. this->width = width;
  151. this->height = height;
  152. this->inData = inBuf;
  153. for ( int j = 0; j < height; j += 4 ) {
  154. for ( int i = 0; i < width; i += 4 ) {
  155. DecodeColorValues( block, false, true );
  156. EmitBlock( outBuf, i, j, block );
  157. }
  158. }
  159. }
  160. /*
  161. ========================
  162. idDxtDecoder::DecompressImageDXT5
  163. ========================
  164. */
  165. void idDxtDecoder::DecompressImageDXT5( const byte *inBuf, byte *outBuf, int width, int height ) {
  166. byte block[64];
  167. this->width = width;
  168. this->height = height;
  169. this->inData = inBuf;
  170. for ( int j = 0; j < height; j += 4 ) {
  171. for ( int i = 0; i < width; i += 4 ) {
  172. DecodeAlphaValues( block, 3 );
  173. DecodeColorValues( block, true, false );
  174. EmitBlock( outBuf, i, j, block );
  175. }
  176. }
  177. }
  178. /*
  179. ========================
  180. idDxtDecoder::DecompressImageDXT5_nVidia7x
  181. ========================
  182. */
  183. void idDxtDecoder::DecompressImageDXT5_nVidia7x( const byte *inBuf, byte *outBuf, int width, int height ) {
  184. byte block[64];
  185. this->width = width;
  186. this->height = height;
  187. this->inData = inBuf;
  188. for ( int j = 0; j < height; j += 4 ) {
  189. for ( int i = 0; i < width; i += 4 ) {
  190. DecodeAlphaValues( block, 3 );
  191. DecodeColorValues( block, false, false );
  192. EmitBlock( outBuf, i, j, block );
  193. }
  194. }
  195. }
  196. /*
  197. ========================
  198. idDxtDecoder::DecompressYCoCgDXT5
  199. ========================
  200. */
  201. void idDxtDecoder::DecompressYCoCgDXT5( const byte *inBuf, byte *outBuf, int width, int height ) {
  202. DecompressImageDXT5_nVidia7x( inBuf, outBuf, width, height );
  203. // descale the CoCg values and set the scale factor effectively to 1
  204. for ( int i = 0; i < width * height; i++ ) {
  205. int scale = ( outBuf[i*4+2] >> 3 ) + 1;
  206. outBuf[i*4+0] = byte( ( outBuf[i*4+0] - 128 ) / scale + 128 );
  207. outBuf[i*4+1] = byte( ( outBuf[i*4+1] - 128 ) / scale + 128 );
  208. outBuf[i*4+2] = 0; // this translates to a scale factor of 1 for uncompressed
  209. }
  210. }
  211. /*
  212. ========================
  213. idDxtDecoder::DecompressYCoCgCTX1DXT5A
  214. ========================
  215. */
  216. void idDxtDecoder::DecompressYCoCgCTX1DXT5A( const byte *inBuf, byte *outBuf, int width, int height ) {
  217. byte block[64];
  218. this->width = width;
  219. this->height = height;
  220. this->inData = inBuf;
  221. for ( int j = 0; j < height; j += 4 ) {
  222. for ( int i = 0; i < width; i += 4 ) {
  223. DecodeAlphaValues( block, 3 );
  224. DecodeCTX1Values( block );
  225. EmitBlock( outBuf, i, j, block );
  226. }
  227. }
  228. }
  229. /*
  230. ========================
  231. idDxtDecoder::DecodeNormalYValues
  232. ========================
  233. */
  234. void idDxtDecoder::DecodeNormalYValues( byte *normalBlock, const int offsetY, byte &c0, byte &c1 ) {
  235. int i;
  236. unsigned int indexes;
  237. unsigned short normal0, normal1;
  238. byte normalsY[4];
  239. normal0 = ReadUShort();
  240. normal1 = ReadUShort();
  241. assert( normal0 >= normal1 );
  242. normalsY[0] = NormalYFrom565( normal0 );
  243. normalsY[1] = NormalYFrom565( normal1 );
  244. normalsY[2] = ( 2 * normalsY[0] + 1 * normalsY[1] ) / 3;
  245. normalsY[3] = ( 1 * normalsY[0] + 2 * normalsY[1] ) / 3;
  246. c0 = NormalBiasFrom565( normal0 );
  247. c1 = NormalScaleFrom565( normal0 );
  248. byte *normalYPtr = normalBlock + offsetY;
  249. indexes = ReadUInt();
  250. for ( i = 0; i < 16; i++ ) {
  251. normalYPtr[i*4] = normalsY[indexes & 3];
  252. indexes >>= 2;
  253. }
  254. }
  255. /*
  256. ========================
  257. UShortSqrt
  258. ========================
  259. */
  260. byte UShortSqrt( unsigned short s ) {
  261. #if 1
  262. int t, b, r, x;
  263. r = 0;
  264. for ( b = 0x10000000; b != 0; b >>= 2 ) {
  265. t = r + b;
  266. r >>= 1;
  267. x = -( t <= s );
  268. s = s - (unsigned short)( t & x );
  269. r += b & x;
  270. }
  271. return byte( r );
  272. #else
  273. int t, b, r;
  274. r = 0;
  275. for ( b = 0x10000000; b != 0; b >>= 2 ) {
  276. t = r + b;
  277. r >>= 1;
  278. if ( t <= s ) {
  279. s -= t;
  280. r += b;
  281. }
  282. }
  283. return r;
  284. #endif
  285. }
  286. /*
  287. ========================
  288. idDxtDecoder::DeriveNormalZValues
  289. ========================
  290. */
  291. void idDxtDecoder::DeriveNormalZValues( byte *normalBlock ) {
  292. int i;
  293. for ( i = 0; i < 16; i++ ) {
  294. int x = normalBlock[i*4+0] - 127;
  295. int y = normalBlock[i*4+1] - 127;
  296. normalBlock[i*4+2] = 128 + UShortSqrt( (unsigned short)( 16383 - x * x - y * y ) );
  297. }
  298. }
  299. /*
  300. ========================
  301. idDxtDecoder::UnRotateNormals
  302. ========================
  303. */
  304. void UnRotateNormals( const byte *block, float *normals, byte c0, byte c1 ) {
  305. int rotation = c0;
  306. float angle = -( rotation / 255.0f ) * idMath::PI;
  307. float s = sin( angle );
  308. float c = cos( angle );
  309. int scale = ( c1 >> 3 ) + 1;
  310. for ( int i = 0; i < 16; i++ ) {
  311. float x = block[i*4+0] / 255.0f * 2.0f - 1.0f;
  312. float y = ( ( block[i*4+1] - 128 ) / scale + 128 ) / 255.0f * 2.0f - 1.0f;
  313. float rx = c * x - s * y;
  314. float ry = s * x + c * y;
  315. normals[i*4+0] = rx;
  316. normals[i*4+1] = ry;
  317. }
  318. }
  319. /*
  320. ========================
  321. idDxtDecoder::DecompressNormalMapDXT1
  322. ========================
  323. */
  324. void idDxtDecoder::DecompressNormalMapDXT1( const byte *inBuf, byte *outBuf, int width, int height ) {
  325. byte block[64];
  326. this->width = width;
  327. this->height = height;
  328. this->inData = inBuf;
  329. for ( int j = 0; j < height; j += 4 ) {
  330. for ( int i = 0; i < width; i += 4 ) {
  331. DecodeColorValues( block, false, true );
  332. #if 1
  333. float normals[16*4];
  334. /*
  335. for ( int k = 0; k < 16; k++ ) {
  336. normals[k*4+0] = block[k*4+0] / 255.0f * 2.0f - 1.0f;
  337. normals[k*4+1] = block[k*4+1] / 255.0f * 2.0f - 1.0f;
  338. }
  339. */
  340. UnRotateNormals( block, normals, block[0*4+2], 0 );
  341. for ( int k = 0; k < 16; k++ ) {
  342. float x = normals[k*4+0];
  343. float y = normals[k*4+1];
  344. float z = 1.0f - x * x - y * y;
  345. if ( z < 0.0f ) z = 0.0f;
  346. normals[k*4+2] = sqrt( z );
  347. }
  348. for ( int k = 0; k < 16; k++ ) {
  349. block[k*4+0] = idMath::Ftob( ( normals[k*4+0] + 1.0f ) / 2.0f * 255.0f );
  350. block[k*4+1] = idMath::Ftob( ( normals[k*4+1] + 1.0f ) / 2.0f * 255.0f );
  351. block[k*4+2] = idMath::Ftob( ( normals[k*4+2] + 1.0f ) / 2.0f * 255.0f );
  352. }
  353. #else
  354. DeriveNormalZValues( block );
  355. #endif
  356. EmitBlock( outBuf, i, j, block );
  357. }
  358. }
  359. }
  360. /*
  361. ========================
  362. idDxtDecoder::DecompressNormalMapDXT1Renormalize
  363. ========================
  364. */
  365. void idDxtDecoder::DecompressNormalMapDXT1Renormalize( const byte *inBuf, byte *outBuf, int width, int height ) {
  366. byte block[64];
  367. this->width = width;
  368. this->height = height;
  369. this->inData = inBuf;
  370. for ( int j = 0; j < height; j += 4 ) {
  371. for ( int i = 0; i < width; i += 4 ) {
  372. DecodeColorValues( block, false, true );
  373. for ( int k = 0; k < 16; k++ ) {
  374. float normal[3];
  375. normal[0] = block[k*4+0] / 255.0f * 2.0f - 1.0f;
  376. normal[1] = block[k*4+1] / 255.0f * 2.0f - 1.0f;
  377. normal[2] = block[k*4+2] / 255.0f * 2.0f - 1.0f;
  378. float rsq = idMath::InvSqrt( normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2] );
  379. normal[0] *= rsq;
  380. normal[1] *= rsq;
  381. normal[2] *= rsq;
  382. block[k*4+0] = idMath::Ftob( ( normal[0] + 1.0f ) / 2.0f * 255.0f + 0.5f );
  383. block[k*4+1] = idMath::Ftob( ( normal[1] + 1.0f ) / 2.0f * 255.0f + 0.5f );
  384. block[k*4+2] = idMath::Ftob( ( normal[2] + 1.0f ) / 2.0f * 255.0f + 0.5f );
  385. }
  386. EmitBlock( outBuf, i, j, block );
  387. }
  388. }
  389. }
  390. /*
  391. ========================
  392. idDxtDecoder::DecompressNormalMapDXT5Renormalize
  393. ========================
  394. */
  395. void idDxtDecoder::DecompressNormalMapDXT5Renormalize( const byte *inBuf, byte *outBuf, int width, int height ) {
  396. byte block[64];
  397. this->width = width;
  398. this->height = height;
  399. this->inData = inBuf;
  400. for ( int j = 0; j < height; j += 4 ) {
  401. for ( int i = 0; i < width; i += 4 ) {
  402. DecodeAlphaValues( block, 3 );
  403. DecodeColorValues( block, false, false );
  404. for ( int k = 0; k < 16; k++ ) {
  405. float normal[3];
  406. #if 0 // object-space
  407. normal[0] = block[k*4+0] / 255.0f * 2.0f - 1.0f;
  408. normal[1] = block[k*4+1] / 255.0f * 2.0f - 1.0f;
  409. normal[2] = block[k*4+3] / 255.0f * 2.0f - 1.0f;
  410. #else
  411. normal[0] = block[k*4+3] / 255.0f * 2.0f - 1.0f;
  412. normal[1] = block[k*4+1] / 255.0f * 2.0f - 1.0f;
  413. normal[2] = block[k*4+2] / 255.0f * 2.0f - 1.0f;
  414. #endif
  415. float rsq = idMath::InvSqrt( normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2] );
  416. normal[0] *= rsq;
  417. normal[1] *= rsq;
  418. normal[2] *= rsq;
  419. block[k*4+0] = idMath::Ftob( ( normal[0] + 1.0f ) / 2.0f * 255.0f + 0.5f );
  420. block[k*4+1] = idMath::Ftob( ( normal[1] + 1.0f ) / 2.0f * 255.0f + 0.5f );
  421. block[k*4+2] = idMath::Ftob( ( normal[2] + 1.0f ) / 2.0f * 255.0f + 0.5f );
  422. }
  423. EmitBlock( outBuf, i, j, block );
  424. }
  425. }
  426. }
  427. /*
  428. ========================
  429. idDxtDecoder::BiasScaleNormalY
  430. ========================
  431. */
  432. void BiasScaleNormalY( byte *normals, const int offsetY, const byte c0, const byte c1 ) {
  433. int bias = c0 - 4;
  434. int scale = ( c1 >> 3 ) + 1;
  435. for ( int i = 0; i < 16; i++ ) {
  436. normals[i*4+offsetY] = byte( ( normals[i*4+offsetY] - 128 ) / scale + bias );
  437. }
  438. }
  439. /*
  440. ========================
  441. idDxtDecoder::BiasScaleNormals
  442. ========================
  443. */
  444. void BiasScaleNormals( const byte *block, float *normals, const byte c0, const byte c1 ) {
  445. int bias = c0 - 4;
  446. int scale = ( c1 >> 3 ) + 1;
  447. for ( int i = 0; i < 16; i++ ) {
  448. normals[i*4+0] = block[i*4+0] / 255.0f * 2.0f - 1.0f;
  449. normals[i*4+1] = ( ( block[i*4+1] - 128.0f ) / scale + bias ) / 255.0f * 2.0f - 1.0f;
  450. }
  451. }
  452. /*
  453. ========================
  454. idDxtDecoder::DecompressNormalMapDXT5
  455. ========================
  456. */
  457. void idDxtDecoder::DecompressNormalMapDXT5( const byte *inBuf, byte *outBuf, int width, int height ) {
  458. byte block[64];
  459. byte c0, c1;
  460. this->width = width;
  461. this->height = height;
  462. this->inData = inBuf;
  463. for ( int j = 0; j < height; j += 4 ) {
  464. for ( int i = 0; i < width; i += 4 ) {
  465. DecodeAlphaValues( block, 0 );
  466. DecodeNormalYValues( block, 1, c0, c1 );
  467. #if 1
  468. float normals[16*4];
  469. //BiasScaleNormals( block, normals, c0, c1 );
  470. UnRotateNormals( block, normals, c0, c1 );
  471. for ( int k = 0; k < 16; k++ ) {
  472. float x = normals[k*4+0];
  473. float y = normals[k*4+1];
  474. float z = 1.0f - x * x - y * y;
  475. if ( z < 0.0f ) z = 0.0f;
  476. normals[k*4+2] = sqrt( z );
  477. }
  478. for ( int k = 0; k < 16; k++ ) {
  479. block[k*4+0] = idMath::Ftob( ( normals[k*4+0] + 1.0f ) / 2.0f * 255.0f );
  480. block[k*4+1] = idMath::Ftob( ( normals[k*4+1] + 1.0f ) / 2.0f * 255.0f );
  481. block[k*4+2] = idMath::Ftob( ( normals[k*4+2] + 1.0f ) / 2.0f * 255.0f );
  482. }
  483. #else
  484. BiasScaleNormalY( block, 1, c0, c1 );
  485. DeriveNormalZValues( block );
  486. #endif
  487. EmitBlock( outBuf, i, j, block );
  488. }
  489. }
  490. }
  491. /*
  492. ========================
  493. idDxtDecoder::DecompressNormalMapDXN2
  494. ========================
  495. */
  496. void idDxtDecoder::DecompressNormalMapDXN2( const byte *inBuf, byte *outBuf, int width, int height ) {
  497. byte block[64];
  498. this->width = width;
  499. this->height = height;
  500. this->inData = inBuf;
  501. for ( int j = 0; j < height; j += 4 ) {
  502. for ( int i = 0; i < width; i += 4 ) {
  503. DecodeAlphaValues( block, 0 );
  504. DecodeAlphaValues( block, 1 );
  505. #if 1
  506. float normals[16*4];
  507. for ( int k = 0; k < 16; k++ ) {
  508. normals[k*4+0] = block[k*4+0] / 255.0f * 2.0f - 1.0f;
  509. normals[k*4+1] = block[k*4+1] / 255.0f * 2.0f - 1.0f;
  510. }
  511. for ( int k = 0; k < 16; k++ ) {
  512. float x = normals[k*4+0];
  513. float y = normals[k*4+1];
  514. float z = 1.0f - x * x - y * y;
  515. if ( z < 0.0f ) z = 0.0f;
  516. normals[k*4+2] = sqrt( z );
  517. }
  518. for ( int k = 0; k < 16; k++ ) {
  519. block[k*4+0] = idMath::Ftob( ( normals[k*4+0] + 1.0f ) / 2.0f * 255.0f );
  520. block[k*4+1] = idMath::Ftob( ( normals[k*4+1] + 1.0f ) / 2.0f * 255.0f );
  521. block[k*4+2] = idMath::Ftob( ( normals[k*4+2] + 1.0f ) / 2.0f * 255.0f );
  522. }
  523. #else
  524. DeriveNormalZValues( block );
  525. #endif
  526. EmitBlock( outBuf, i, j, block );
  527. }
  528. }
  529. }
  530. /*
  531. ========================
  532. idDxtDecoder::DecomposeColorBlock
  533. ========================
  534. */
  535. void idDxtDecoder::DecomposeColorBlock( byte colors[2][4], byte colorIndices[16], bool noBlack ) {
  536. int i;
  537. unsigned int indices;
  538. unsigned short color0, color1;
  539. int colorRemap1[] = { 3, 0, 2, 1 };
  540. int colorRemap2[] = { 1, 3, 2, 0 };
  541. int *crm;
  542. color0 = ReadUShort();
  543. color1 = ReadUShort();
  544. ColorFrom565( color0, colors[0] );
  545. ColorFrom565( color1, colors[1] );
  546. if ( noBlack || color0 > color1 ) {
  547. crm = colorRemap1;
  548. } else {
  549. crm = colorRemap2;
  550. }
  551. indices = ReadUInt();
  552. for ( i = 0; i < 16; i++ ) {
  553. colorIndices[i] = (byte)crm[ indices & 3 ];
  554. indices >>= 2;
  555. }
  556. }
  557. /*
  558. ========================
  559. idDxtDecoder::DecomposeAlphaBlock
  560. ========================
  561. */
  562. void idDxtDecoder::DecomposeAlphaBlock( byte colors[2][4], byte alphaIndices[16] ) {
  563. int i;
  564. unsigned char alpha0, alpha1;
  565. unsigned int indices;
  566. int alphaRemap1[] = { 7, 0, 6, 5, 4, 3, 2, 1 };
  567. int alphaRemap2[] = { 1, 6, 2, 3, 4, 5, 0, 7 };
  568. int *arm;
  569. alpha0 = ReadByte();
  570. alpha1 = ReadByte();
  571. colors[0][3] = alpha0;
  572. colors[1][3] = alpha1;
  573. if ( alpha0 > alpha1 ) {
  574. arm = alphaRemap1;
  575. } else {
  576. arm = alphaRemap2;
  577. }
  578. indices = (int)ReadByte() | ( (int)ReadByte() << 8 ) | ( (int)ReadByte() << 16 );
  579. for ( i = 0; i < 8; i++ ) {
  580. alphaIndices[i] = (byte)arm[ indices & 7 ];
  581. indices >>= 3;
  582. }
  583. indices = (int)ReadByte() | ( (int)ReadByte() << 8 ) | ( (int)ReadByte() << 16 );
  584. for ( i = 8; i < 16; i++ ) {
  585. alphaIndices[i] = (byte)arm[ indices & 7 ];
  586. indices >>= 3;
  587. }
  588. }
  589. /*
  590. ========================
  591. idDxtDecoder::DecomposeImageDXT1
  592. ========================
  593. */
  594. void idDxtDecoder::DecomposeImageDXT1( const byte *inBuf, byte *colorIndices, byte *pic1, byte *pic2, int width, int height ) {
  595. byte colors[2][4];
  596. byte indices[16];
  597. this->width = width;
  598. this->height = height;
  599. this->inData = inBuf;
  600. // extract the colors from the DXT
  601. for ( int j = 0; j < height; j += 4 ) {
  602. for ( int i = 0; i < width; i += 4 ) {
  603. DecomposeColorBlock( colors, indices, false );
  604. memcpy( colorIndices + (j+0) * width + i, indices+ 0, 4 );
  605. memcpy( colorIndices + (j+1) * width + i, indices+ 4, 4 );
  606. memcpy( colorIndices + (j+2) * width + i, indices+ 8, 4 );
  607. memcpy( colorIndices + (j+3) * width + i, indices+12, 4 );
  608. memcpy( pic1 + j * width / 4 + i, colors[0], 4 );
  609. memcpy( pic2 + j * width / 4 + i, colors[1], 4 );
  610. }
  611. }
  612. }
  613. /*
  614. ========================
  615. idDxtDecoder::DecomposeImageDXT5
  616. ========================
  617. */
  618. void idDxtDecoder::DecomposeImageDXT5( const byte *inBuf, byte *colorIndices, byte *alphaIndices, byte *pic1, byte *pic2, int width, int height ) {
  619. byte colors[2][4];
  620. byte colorInd[16];
  621. byte alphaInd[16];
  622. this->width = width;
  623. this->height = height;
  624. this->inData = inBuf;
  625. // extract the colors from the DXT
  626. for ( int j = 0; j < height; j += 4 ) {
  627. for ( int i = 0; i < width; i += 4 ) {
  628. DecomposeAlphaBlock( colors, alphaInd );
  629. DecomposeColorBlock( colors, colorInd, true );
  630. memcpy( colorIndices + (j+0) * width + i, colorInd+ 0, 4 );
  631. memcpy( colorIndices + (j+1) * width + i, colorInd+ 4, 4 );
  632. memcpy( colorIndices + (j+2) * width + i, colorInd+ 8, 4 );
  633. memcpy( colorIndices + (j+3) * width + i, colorInd+12, 4 );
  634. memcpy( colorIndices + (j+0) * width + i, alphaInd+ 0, 4 );
  635. memcpy( colorIndices + (j+1) * width + i, alphaInd+ 4, 4 );
  636. memcpy( colorIndices + (j+2) * width + i, alphaInd+ 8, 4 );
  637. memcpy( colorIndices + (j+3) * width + i, alphaInd+12, 4 );
  638. memcpy( pic1 + j * width / 4 + i, colors[0], 4 );
  639. memcpy( pic2 + j * width / 4 + i, colors[1], 4 );
  640. }
  641. }
  642. }