ddsfile.cpp 22 KB

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