ddsfile.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program 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. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "ddsfile.h"
  19. #include "ffactory.h"
  20. #include "bufffile.h"
  21. #include "formconv.h"
  22. #include "dx8wrapper.h"
  23. #include "bitmaphandler.h"
  24. // ----------------------------------------------------------------------------
  25. DDSFileClass::DDSFileClass(const char* name,unsigned reduction_factor)
  26. :
  27. Name(name),
  28. DDSMemory(NULL),
  29. Width(0),
  30. Height(0),
  31. LevelSizes(NULL),
  32. LevelOffsets(NULL),
  33. MipLevels(0),
  34. ReductionFactor(reduction_factor),
  35. Format(WW3D_FORMAT_UNKNOWN)
  36. {
  37. // The name could be given in .tga or .dds format, so ensure we're opening .dds...
  38. int len=strlen(Name);
  39. Name[len-3]='d';
  40. Name[len-2]='d';
  41. Name[len-1]='s';
  42. file_auto_ptr file(_TheFileFactory,Name);
  43. if (!file->Is_Available()) {
  44. return;
  45. }
  46. file->Open();
  47. char header[4];
  48. file->Read(header,4);
  49. // Now, we read DDSURFACEDESC2 defining the compressed data
  50. unsigned read_bytes=file->Read(&SurfaceDesc,sizeof(LegacyDDSURFACEDESC2));
  51. // Verify the structure size matches the read size
  52. WWASSERT(read_bytes==SurfaceDesc.Size);
  53. Format=D3DFormat_To_WW3DFormat((D3DFORMAT)SurfaceDesc.PixelFormat.FourCC);
  54. WWASSERT(
  55. Format==WW3D_FORMAT_DXT1 ||
  56. Format==WW3D_FORMAT_DXT2 ||
  57. Format==WW3D_FORMAT_DXT3 ||
  58. Format==WW3D_FORMAT_DXT4 ||
  59. Format==WW3D_FORMAT_DXT5);
  60. MipLevels=SurfaceDesc.MipMapCount;
  61. if (MipLevels==0) MipLevels=1;
  62. //Adjust the reduction factor to keep textures above some minimum dimensions
  63. if (MipLevels <= WW3D::Get_Texture_Min_Mip_Levels())
  64. ReductionFactor=0;
  65. else
  66. { int mipToDrop=MipLevels-WW3D::Get_Texture_Min_Mip_Levels();
  67. if (ReductionFactor >= mipToDrop)
  68. ReductionFactor=mipToDrop;
  69. }
  70. if (MipLevels>ReductionFactor) MipLevels-=ReductionFactor;
  71. else {
  72. MipLevels=1;
  73. ReductionFactor=ReductionFactor-MipLevels;
  74. }
  75. // Drop the two lowest miplevels!
  76. if (MipLevels>2) MipLevels-=2;
  77. else MipLevels=1;
  78. Width=SurfaceDesc.Width>>ReductionFactor;
  79. Height=SurfaceDesc.Height>>ReductionFactor;
  80. // MipLevels=SurfaceDesc.dwMipMapCount;
  81. // if (MipLevels==0) MipLevels=1;
  82. unsigned level_size=Calculate_DXTC_Surface_Size(SurfaceDesc.Width,SurfaceDesc.Height,Format);
  83. unsigned level_offset=0;
  84. LevelSizes=W3DNEWARRAY unsigned[MipLevels];
  85. LevelOffsets=W3DNEWARRAY unsigned[MipLevels];
  86. for (unsigned level=0;level<ReductionFactor;++level) {
  87. // level_offset+=level_size;
  88. if (level_size>16) { // If surface is bigger than one block (8 or 16 bytes)...
  89. level_size/=4;
  90. }
  91. }
  92. for (level=0;level<MipLevels;++level) {
  93. LevelSizes[level]=level_size;
  94. LevelOffsets[level]=level_offset;
  95. level_offset+=level_size;
  96. if (level_size>16) { // If surface is bigger than one block (8 or 16 bytes)...
  97. level_size/=4;
  98. }
  99. }
  100. // Verify we read through the whole file (not more, not less).
  101. #if 0
  102. // This code is commented out because it complains for all our files, but it doesn't seem to actually
  103. // be a problem. The new version of ddsfile from Vegas actually has removed this check entirely.
  104. int expected_size=level_offset+SurfaceDesc.Size+4;
  105. if (expected_size!=file->Size()) {
  106. WWDEBUG_SAY(("Warning: file % size is not consistent with the data (file size should be %d but was %d)\n",Name,expected_size,file->Size()));
  107. }
  108. #endif
  109. file->Close();
  110. }
  111. // ----------------------------------------------------------------------------
  112. DDSFileClass::~DDSFileClass()
  113. {
  114. delete[] DDSMemory;
  115. delete[] LevelSizes;
  116. delete[] LevelOffsets;
  117. }
  118. unsigned DDSFileClass::Get_Width(unsigned level) const
  119. {
  120. WWASSERT(level<MipLevels);
  121. unsigned width=Width>>level;
  122. if (width<4) width=4;
  123. return width;
  124. }
  125. unsigned DDSFileClass::Get_Height(unsigned level) const
  126. {
  127. WWASSERT(level<MipLevels);
  128. unsigned height=Height>>level;
  129. if (height<4) height=4;
  130. return height;
  131. }
  132. const unsigned char* DDSFileClass::Get_Memory_Pointer(unsigned level) const
  133. {
  134. WWASSERT(level<MipLevels);
  135. return DDSMemory+LevelOffsets[level];
  136. }
  137. unsigned DDSFileClass::Get_Level_Size(unsigned level) const
  138. {
  139. WWASSERT(level<MipLevels);
  140. return LevelSizes[level];
  141. }
  142. // For some reason DX-Tex tool doesn't fill the surface size field, so we need to calculate it...
  143. unsigned DDSFileClass::Calculate_DXTC_Surface_Size(unsigned width, unsigned height, WW3DFormat format)
  144. {
  145. unsigned level_size=(width/4)*(height/4);
  146. switch (format) {
  147. case WW3D_FORMAT_DXT1:
  148. level_size*=8;
  149. break;
  150. case WW3D_FORMAT_DXT2:
  151. case WW3D_FORMAT_DXT3:
  152. case WW3D_FORMAT_DXT4:
  153. case WW3D_FORMAT_DXT5:
  154. level_size*=16;
  155. break;
  156. }
  157. return level_size;
  158. }
  159. // ----------------------------------------------------------------------------
  160. bool DDSFileClass::Load()
  161. {
  162. if (DDSMemory) return false;
  163. if (!LevelSizes || !LevelOffsets) return false;
  164. file_auto_ptr file(_TheFileFactory,Name);
  165. if (!file->Is_Available()) {
  166. return false;
  167. }
  168. file->Open();
  169. // Data size is file size minus the header and info block
  170. unsigned size=file->Size()-SurfaceDesc.Size-4;
  171. // Skip mip levels if reduction factor is not zero
  172. unsigned level_size=Calculate_DXTC_Surface_Size(SurfaceDesc.Width,SurfaceDesc.Height,Format);
  173. unsigned skipped_offset=0;
  174. for (unsigned i=0;i<ReductionFactor;++i) {
  175. skipped_offset+=level_size;
  176. size-=level_size;
  177. if (level_size>16) { // If surface is bigger than one block (8 or 16 bytes)...
  178. level_size/=4;
  179. }
  180. }
  181. // Skip the header and info block and possible unused mip levels
  182. unsigned seek_size=file->Seek(SurfaceDesc.Size+4+skipped_offset);
  183. WWASSERT(seek_size==(SurfaceDesc.Size+4+skipped_offset));
  184. if (size) {
  185. // Allocate memory for the data excluding the headers
  186. DDSMemory=MSGW3DNEWARRAY("DDSMemory") unsigned char[size];
  187. // Read data
  188. unsigned read_size=file->Read(DDSMemory,size);
  189. // Verify we got all the data
  190. WWASSERT(read_size==size);
  191. }
  192. file->Close();
  193. return true;
  194. }
  195. // ----------------------------------------------------------------------------
  196. //
  197. // Copy mipmap level to D3D surface. The copying is performed using another
  198. // Copy_Level_To_Surface function (see below).
  199. //
  200. // ----------------------------------------------------------------------------
  201. void DDSFileClass::Copy_Level_To_Surface(unsigned level,IDirect3DSurface8* d3d_surface)
  202. {
  203. WWASSERT(d3d_surface);
  204. // Verify that the destination surface size matches the source surface size
  205. D3DSURFACE_DESC surface_desc;
  206. DX8_ErrorCode(d3d_surface->GetDesc(&surface_desc));
  207. // First lock the surface
  208. D3DLOCKED_RECT locked_rect;
  209. DX8_ErrorCode(d3d_surface->LockRect(&locked_rect,NULL,0));
  210. Copy_Level_To_Surface(
  211. level,
  212. D3DFormat_To_WW3DFormat(surface_desc.Format),
  213. surface_desc.Width,
  214. surface_desc.Height,
  215. reinterpret_cast<unsigned char*>(locked_rect.pBits),
  216. locked_rect.Pitch);
  217. // Finally, unlock the surface
  218. DX8_ErrorCode(d3d_surface->UnlockRect());
  219. }
  220. // ----------------------------------------------------------------------------
  221. //
  222. // Copy one mipmap level of texture to a memory surface. Surface type conversion
  223. // is performed if the destination is of different format. Scaling will be done
  224. // one of these days as well. Conversions between different types of compressed
  225. // surfaces are not performed and scaling of compressed surfaces is also not
  226. // possible.
  227. //
  228. // ----------------------------------------------------------------------------
  229. void DDSFileClass::Copy_Level_To_Surface(
  230. unsigned level,
  231. WW3DFormat dest_format,
  232. unsigned dest_width,
  233. unsigned dest_height,
  234. unsigned char* dest_surface,
  235. unsigned dest_pitch)
  236. {
  237. WWASSERT(DDSMemory);
  238. WWASSERT(dest_surface);
  239. // If the format and size is a match just copy the contents
  240. if (dest_format==Format && dest_width==Get_Width(level) && dest_height==Get_Height(level)) {
  241. unsigned compressed_size=Get_Level_Size(level);
  242. memcpy(dest_surface,Get_Memory_Pointer(level),compressed_size);
  243. }
  244. else {
  245. // If size matches, copy each pixel linearly with color space conversion
  246. if (dest_width==Get_Width(level) && dest_height==Get_Height(level)) {
  247. // An exception here - if the source format is DXT1 and the destination
  248. // is DXT2, just copy the contents and create an empty alpha channel.
  249. // This is needed on NVidia cards that have problems with DXT1 compression.
  250. if (Format==WW3D_FORMAT_DXT1 && dest_format==WW3D_FORMAT_DXT2) {
  251. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Memory_Pointer(level));
  252. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  253. for (unsigned y=0;y<dest_height;y+=4) {
  254. for (unsigned x=0;x<dest_width;x+=4) {
  255. *dest_ptr++=0xffffffff; // Bytes 1-4 of alpha block
  256. *dest_ptr++=0xffffffff; // Bytes 5-8 of alpha block
  257. *dest_ptr++=*src_ptr++; // Bytes 1-4 of color block
  258. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  259. }
  260. }
  261. }
  262. else {
  263. unsigned dest_bpp=Get_Bytes_Per_Pixel(dest_format);
  264. // Copy 4x4 block at a time
  265. bool contains_alpha=false;
  266. for (unsigned y=0;y<dest_height;y+=4) {
  267. unsigned char* dest_ptr=dest_surface;
  268. dest_ptr+=y*dest_pitch;
  269. for (unsigned x=0;x<dest_width;x+=4,dest_ptr+=dest_bpp*4) {
  270. contains_alpha|=Get_4x4_Block(dest_ptr,dest_pitch,dest_format,level,x,y);
  271. }
  272. }
  273. if (Format==WW3D_FORMAT_DXT1 && contains_alpha) {
  274. WWDEBUG_SAY(("Warning: DXT1 format should not contain alpha information - file %s\n",Name));
  275. }
  276. }
  277. }
  278. // TODO: Scaling not handled...
  279. else {
  280. //int a=0;
  281. }
  282. }
  283. }
  284. // ----------------------------------------------------------------------------
  285. WWINLINE static unsigned RGB565_To_ARGB8888(unsigned short rgb)
  286. {
  287. unsigned rgba=0;
  288. rgba|=unsigned(rgb&0x001f)<<3;
  289. rgba|=unsigned(rgb&0x07e0)<<5;
  290. rgba|=unsigned(rgb&0xf800)<<8;
  291. return rgba;
  292. }
  293. // ----------------------------------------------------------------------------
  294. WWINLINE static unsigned Combine_Colors(unsigned col1, unsigned col2, unsigned rel)
  295. {
  296. const unsigned R_B_MASK=0x00ff00ff;
  297. const unsigned G_MASK=0x0000ff00;
  298. unsigned rel2=255-rel;
  299. unsigned r_b_col1=col1&R_B_MASK;
  300. r_b_col1*=rel;
  301. unsigned r_b_col2=col2&R_B_MASK;
  302. r_b_col2*=rel2;
  303. r_b_col1+=r_b_col2;
  304. r_b_col1>>=8;
  305. r_b_col1&=R_B_MASK;
  306. unsigned g_col1=col1&G_MASK;
  307. g_col1*=rel;
  308. unsigned g_col2=col2&G_MASK;
  309. g_col2*=rel2;
  310. g_col1+=g_col2;
  311. g_col1>>=8;
  312. g_col1&=G_MASK;
  313. return r_b_col1|g_col1;
  314. /* float f=float(rel)/256.0f;
  315. unsigned new_col=0;
  316. new_col|=int(float(int(col1&0x00ff0000))*f+float(int(col2&0x00ff0000))*(1.0f-f))&0x00ff0000;
  317. new_col|=int(float(int(col1&0x0000ff00))*f+float(int(col2&0x0000ff00))*(1.0f-f))&0x0000ff00;
  318. new_col|=int(float(int(col1&0x000000ff))*f+float(int(col2&0x000000ff))*(1.0f-f))&0x000000ff;
  319. return new_col;
  320. */
  321. }
  322. // ----------------------------------------------------------------------------
  323. //
  324. // Note that this is NOT an efficient way of extracting pixels from compressed image - we should implement
  325. // faster block-copy method for non-scaled copying.
  326. //
  327. // ----------------------------------------------------------------------------
  328. unsigned DDSFileClass::Get_Pixel(unsigned level,unsigned x,unsigned y) const
  329. {
  330. WWASSERT(level<MipLevels);
  331. WWASSERT(x<Get_Width(level));
  332. WWASSERT(y<Get_Height(level));
  333. switch (Format) {
  334. // Note that we don't currently really support alpha on DXT1 - all alpha textures should use DXT5.
  335. // The reason for this is that when converting from DXT1 to 16 bit uncompressed texture we want
  336. // to be able to use RGB565 format instead of ARGB4444. As the alpha is encoded in DXT1 per-block
  337. // basis there isn't really a way to tell if the surface has an alpha or not so either we use alpha
  338. // or we don't.
  339. case WW3D_FORMAT_DXT1:
  340. {
  341. const unsigned char* block_memory=Get_Memory_Pointer(level)+(x/4)*8+((y/4)*(Get_Width(level)/4))*8;
  342. unsigned col0=RGB565_To_ARGB8888(*(unsigned short*)&block_memory[0]);
  343. unsigned col1=RGB565_To_ARGB8888(*(unsigned short*)&block_memory[2]);
  344. unsigned char line=block_memory[4+(y%4)];
  345. line>>=(x%4)*2;
  346. line=(line&3);
  347. if (col0>col1) {
  348. switch (line) {
  349. case 0: return col0|0xff000000;
  350. case 1: return col1|0xff000000;
  351. case 2: return Combine_Colors(col1,col0,85)|0xff000000;
  352. case 3: return Combine_Colors(col0,col1,85)|0xff000000;
  353. }
  354. }
  355. else {
  356. switch (line) {
  357. case 0: return col0|0xff000000;
  358. case 1: return col1|0xff000000;
  359. case 2: return Combine_Colors(col1,col0,128)|0xff000000;
  360. case 3: return 0x00000000;
  361. }
  362. }
  363. }
  364. break;
  365. case WW3D_FORMAT_DXT2:
  366. return 0xffffffff;
  367. case WW3D_FORMAT_DXT3:
  368. return 0xffffffff;
  369. case WW3D_FORMAT_DXT4:
  370. return 0xffffffff;
  371. case WW3D_FORMAT_DXT5:
  372. {
  373. const unsigned char* alpha_block=Get_Memory_Pointer(level)+(x/4)*16+((y/4)*(Get_Width(level)/4))*16;
  374. unsigned alpha0=alpha_block[0];
  375. unsigned alpha1=alpha_block[1];
  376. unsigned bit_idx=((x%4)+4*(y%4))*3;
  377. unsigned byte_idx=bit_idx/8;
  378. bit_idx%=8;
  379. unsigned alpha_index=0;
  380. for (int i=0;i<3;++i) {
  381. WWASSERT(byte_idx<6);
  382. unsigned alpha_bit=(alpha_block[2+byte_idx]>>(bit_idx))&1;
  383. alpha_index|=alpha_bit<<(i);
  384. bit_idx++;
  385. if (bit_idx>=8) {
  386. bit_idx=0;
  387. byte_idx++;
  388. }
  389. }
  390. WWASSERT(alpha_index<8);
  391. // 8-alpha or 6-alpha block?
  392. unsigned alpha_value=0;
  393. if (alpha0>alpha1) {
  394. // 8-alpha block: derive the other six alphas.
  395. // Bit code 000 = alpha_0, 001 = alpha_1, others are interpolated.
  396. switch (alpha_index) {
  397. case 0: alpha_value=alpha0; break;
  398. case 1: alpha_value=alpha1; break;
  399. case 2: alpha_value=(6*alpha0+1*alpha1+3) / 7; break; // bit code 010
  400. case 3: alpha_value=(5*alpha0+2*alpha1+3) / 7; break; // bit code 011
  401. case 4: alpha_value=(4*alpha0+3*alpha1+3) / 7; break; // bit code 100
  402. case 5: alpha_value=(3*alpha0+4*alpha1+3) / 7; break; // bit code 101
  403. case 6: alpha_value=(2*alpha0+5*alpha1+3) / 7; break; // bit code 110
  404. case 7: alpha_value=(1*alpha0+6*alpha1+3) / 7; break; // bit code 111
  405. }
  406. }
  407. else {
  408. // 6-alpha block.
  409. // Bit code 000 = alpha_0, 001 = alpha_1, others are interpolated.
  410. switch (alpha_index) {
  411. case 0: alpha_value=alpha0; break;
  412. case 1: alpha_value=alpha1; break;
  413. case 2: alpha_value=(4*alpha0+1*alpha1+2) / 5; break; // Bit code 010
  414. case 3: alpha_value=(3*alpha0+2*alpha1+2) / 5; break; // Bit code 011
  415. case 4: alpha_value=(2*alpha0+3*alpha1+2) / 5; break; // Bit code 100
  416. case 5: alpha_value=(1*alpha0+4*alpha1+2) / 5; break; // Bit code 101
  417. case 6: alpha_value=0; break; // Bit code 110
  418. case 7: alpha_value=255; break; // Bit code 111
  419. }
  420. }
  421. alpha_value<<=24;
  422. // Extract color
  423. const unsigned char* color_block=alpha_block+8;
  424. unsigned col0=RGB565_To_ARGB8888(*(unsigned short*)&color_block[0]);
  425. unsigned col1=RGB565_To_ARGB8888(*(unsigned short*)&color_block[2]);
  426. unsigned char line=color_block[4+(y%4)];
  427. line>>=(x%4)*2;
  428. line=(line&3);
  429. switch (line) {
  430. case 0: return col0|alpha_value;
  431. case 1: return col1|alpha_value;
  432. case 2: return Combine_Colors(col1,col0,85)|alpha_value;
  433. case 3: return Combine_Colors(col0,col1,85)|alpha_value;
  434. }
  435. }
  436. break;
  437. }
  438. return 0xffffffff;
  439. }
  440. // ----------------------------------------------------------------------------
  441. //
  442. // Uncompress one 4x4 block from the compressed image.
  443. //
  444. // Returns: true if block contained alpha, false is not
  445. //
  446. // Note: Destination can't be DXT or paletted surface!
  447. //
  448. // ----------------------------------------------------------------------------
  449. bool DDSFileClass::Get_4x4_Block(
  450. unsigned char* dest_ptr, // Destination surface pointer
  451. unsigned dest_pitch, // Destination surface pitch, in bytes
  452. WW3DFormat dest_format, // Destination surface format, A8R8G8B8 is fastest
  453. unsigned level, // DDS mipmap level to copy from
  454. unsigned source_x, // DDS x offset to copy from, must be aligned by 4!
  455. unsigned source_y) const // DDS y offset to copy from, must be aligned by 4!
  456. {
  457. // Verify the block alignment
  458. WWASSERT((source_x&3)==0);
  459. WWASSERT((source_y&3)==0);
  460. // Verify level
  461. WWASSERT(level<MipLevels);
  462. // Verify coordinate bounds
  463. WWASSERT(source_x<Get_Width(level));
  464. WWASSERT(source_y<Get_Height(level));
  465. unsigned dest_bpp=Get_Bytes_Per_Pixel(dest_format);
  466. switch (Format) {
  467. // Note that we don't currently really support alpha on DXT1 - all alpha textures should use DXT5.
  468. // The reason for this is that when converting from DXT1 to 16 bit uncompressed texture we want
  469. // to be able to use RGB565 format instead of ARGB4444. As the alpha is encoded in DXT1 per-block
  470. // basis there isn't really a way to tell if the surface has an alpha or not so either we use alpha
  471. // or we don't.
  472. case WW3D_FORMAT_DXT1:
  473. {
  474. const unsigned char* block_memory=Get_Memory_Pointer(level)+(source_x/4)*8+((source_y/4)*(Get_Width(level)/4))*8;
  475. unsigned col0=RGB565_To_ARGB8888(*(unsigned short*)&block_memory[0]);
  476. unsigned col1=RGB565_To_ARGB8888(*(unsigned short*)&block_memory[2]);
  477. // Even if we don't support alpha, decompression is different if source has alpha
  478. unsigned dest_pixel=0;
  479. if (col0>col1) {
  480. for (int y=0;y<4;++y) {
  481. unsigned char* tmp_dest_ptr=dest_ptr;
  482. dest_ptr+=dest_pitch;
  483. unsigned char line=block_memory[4+y];
  484. for (int x=0;x<4;++x) {
  485. switch (line&3) {
  486. case 0: dest_pixel=col0|0xff000000; break;
  487. case 1: dest_pixel=col1|0xff000000; break;
  488. case 2: dest_pixel=Combine_Colors(col1,col0,85)|0xff000000; break;
  489. case 3: dest_pixel=Combine_Colors(col0,col1,85)|0xff000000; break;
  490. }
  491. line>>=2;
  492. BitmapHandlerClass::Write_B8G8R8A8(tmp_dest_ptr,dest_format,dest_pixel);
  493. tmp_dest_ptr+=dest_bpp;
  494. }
  495. }
  496. return false; // No alpha found in the block
  497. }
  498. else {
  499. for (int y=0;y<4;++y) {
  500. unsigned char* tmp_dest_ptr=dest_ptr;
  501. dest_ptr+=dest_pitch;
  502. unsigned char line=block_memory[4+y];
  503. for (int x=0;x<4;++x) {
  504. switch (line&3) {
  505. case 0: dest_pixel=col0|0xff000000; break;
  506. case 1: dest_pixel=col1|0xff000000; break;
  507. case 2: dest_pixel=Combine_Colors(col1,col0,128)|0xff000000; break;
  508. case 3: dest_pixel=0x00000000; break;
  509. }
  510. line>>=2;
  511. BitmapHandlerClass::Write_B8G8R8A8(tmp_dest_ptr,dest_format,dest_pixel);
  512. tmp_dest_ptr+=dest_bpp;
  513. }
  514. }
  515. return true; // Alpha block...
  516. }
  517. }
  518. break;
  519. case WW3D_FORMAT_DXT2:
  520. return false;
  521. case WW3D_FORMAT_DXT3:
  522. return false;
  523. case WW3D_FORMAT_DXT4:
  524. return false;
  525. case WW3D_FORMAT_DXT5:
  526. {
  527. // Init alphas
  528. const unsigned char* alpha_block=Get_Memory_Pointer(level)+(source_x/4)*16+((source_y/4)*(Get_Width(level)/4))*16;
  529. unsigned alphas[8];
  530. alphas[0]=alpha_block[0];
  531. alphas[1]=alpha_block[1];
  532. // 8-alpha or 6-alpha block?
  533. if (alphas[0]>alphas[1]) {
  534. alphas[2]=(6*alphas[0]+1*alphas[1]+3) / 7; // bit code 010
  535. alphas[3]=(5*alphas[0]+2*alphas[1]+3) / 7; // bit code 011
  536. alphas[4]=(4*alphas[0]+3*alphas[1]+3) / 7; // bit code 100
  537. alphas[5]=(3*alphas[0]+4*alphas[1]+3) / 7; // bit code 101
  538. alphas[6]=(2*alphas[0]+5*alphas[1]+3) / 7; // bit code 110
  539. alphas[7]=(1*alphas[0]+6*alphas[1]+3) / 7; // bit code 111
  540. }
  541. else {
  542. alphas[2]=(4*alphas[0]+1*alphas[1]+2) / 5; // Bit code 010
  543. alphas[3]=(3*alphas[0]+2*alphas[1]+2) / 5; // Bit code 011
  544. alphas[4]=(2*alphas[0]+3*alphas[1]+2) / 5; // Bit code 100
  545. alphas[5]=(1*alphas[0]+4*alphas[1]+2) / 5; // Bit code 101
  546. alphas[6]=0; // Bit code 110
  547. alphas[7]=255; // Bit code 111
  548. }
  549. // Init colors
  550. const unsigned char* color_block=alpha_block+8;
  551. unsigned col0=RGB565_To_ARGB8888(*(unsigned short*)&color_block[0]);
  552. unsigned col1=RGB565_To_ARGB8888(*(unsigned short*)&color_block[2]);
  553. unsigned dest_pixel=0;
  554. unsigned bit_idx=0;
  555. unsigned contains_alpha=0xff;
  556. unsigned alpha_indices[16];
  557. unsigned* ai_ptr=alpha_indices;
  558. for (int a=0;a<2;++a) {
  559. ai_ptr[0]=alpha_block[2]&0x7;
  560. ai_ptr[1]=(alpha_block[2]>>3)&0x7;
  561. ai_ptr[2]=(alpha_block[2]>>6)|((alpha_block[3]&1)<<2);
  562. ai_ptr[3]=(alpha_block[3]>>1)&0x7;
  563. ai_ptr[4]=(alpha_block[3]>>4)&0x7;
  564. ai_ptr[5]=(alpha_block[3]>>7)|((alpha_block[4]&3)<<1);
  565. ai_ptr[6]=(alpha_block[4]>>2)&0x7;
  566. ai_ptr[7]=(alpha_block[4]>>5);
  567. ai_ptr+=8;
  568. alpha_block+=3;
  569. }
  570. unsigned aii=0;
  571. for (int y=0;y<4;++y) {
  572. unsigned char* tmp_dest_ptr=dest_ptr;
  573. dest_ptr+=dest_pitch;
  574. unsigned char line=color_block[4+y];
  575. for (int x=0;x<4;++x,bit_idx+=3) {
  576. unsigned alpha_value=alphas[alpha_indices[aii++]];
  577. contains_alpha&=alpha_value;
  578. alpha_value<<=24;
  579. // Extract color
  580. switch (line&3) {
  581. case 0: dest_pixel=col0|alpha_value; break;
  582. case 1: dest_pixel=col1|alpha_value; break;
  583. case 2: dest_pixel=Combine_Colors(col1,col0,85)|alpha_value; break;
  584. case 3: dest_pixel=Combine_Colors(col0,col1,85)|alpha_value; break;
  585. }
  586. line>>=2;
  587. BitmapHandlerClass::Write_B8G8R8A8(tmp_dest_ptr,dest_format,dest_pixel);
  588. tmp_dest_ptr+=dest_bpp;
  589. }
  590. }
  591. /*
  592. for (int y=0;y<4;++y) {
  593. unsigned char* tmp_dest_ptr=dest_ptr;
  594. dest_ptr+=dest_pitch;
  595. unsigned char line=color_block[4+y];
  596. for (int x=0;x<4;++x,bit_idx+=3) {
  597. unsigned byte_idx=bit_idx/8;
  598. unsigned tmp_bit_idx=bit_idx&7;
  599. unsigned alpha_index=0;
  600. for (int i=0;i<3;++i) {
  601. WWASSERT(byte_idx<6);
  602. unsigned alpha_bit=(alpha_block[2+byte_idx]>>(tmp_bit_idx))&1;
  603. alpha_index|=alpha_bit<<(i);
  604. tmp_bit_idx++;
  605. if (tmp_bit_idx>=8) {
  606. tmp_bit_idx=0;
  607. byte_idx++;
  608. }
  609. }
  610. WWASSERT(alpha_index<8);
  611. unsigned alpha_value=alphas[alpha_index];
  612. contains_alpha&=alpha_value;
  613. alpha_value<<=24;
  614. // Extract color
  615. switch (line&3) {
  616. case 0: dest_pixel=col0|alpha_value; break;
  617. case 1: dest_pixel=col1|alpha_value; break;
  618. case 2: dest_pixel=Combine_Colors(col1,col0,85)|alpha_value; break;
  619. case 3: dest_pixel=Combine_Colors(col0,col1,85)|alpha_value; break;
  620. }
  621. line>>=2;
  622. BitmapHandlerClass::Write_B8G8R8A8(tmp_dest_ptr,dest_format,dest_pixel);
  623. tmp_dest_ptr+=dest_bpp;
  624. }
  625. }
  626. */
  627. return contains_alpha!=0xff; // Alpha block... DXT5 should only be used when the image needs alpha
  628. // but for now check anyway...
  629. }
  630. }
  631. return false;
  632. }