ddsfile.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. // 08/06/02 KM Added cube map and volume texture support
  19. #include "ddsfile.h"
  20. #include "ffactory.h"
  21. #include "bufffile.h"
  22. #include "formconv.h"
  23. #include "dx8wrapper.h"
  24. #include "bitmaphandler.h"
  25. #include "colorspace.h"
  26. #include <string.h>
  27. #include <ddraw.h>
  28. // ----------------------------------------------------------------------------
  29. DDSFileClass::DDSFileClass(const char* name,unsigned reduction_factor)
  30. :
  31. DDSMemory(NULL),
  32. Width(0),
  33. Height(0),
  34. Depth(0),
  35. FullWidth(0),
  36. FullHeight(0),
  37. FullDepth(0),
  38. LevelSizes(NULL),
  39. LevelOffsets(NULL),
  40. MipLevels(0),
  41. ReductionFactor(reduction_factor),
  42. Format(WW3D_FORMAT_UNKNOWN),
  43. Type(DDS_TEXTURE),
  44. DateTime(0),
  45. CubeFaceSize(0)
  46. {
  47. strncpy(Name,name,sizeof(Name));
  48. // The name could be given in .tga or .dds format, so ensure we're opening .dds...
  49. int len=strlen(Name);
  50. Name[len-3]='d';
  51. Name[len-2]='d';
  52. Name[len-1]='s';
  53. file_auto_ptr file(_TheFileFactory,Name);
  54. if (!file->Is_Available())
  55. {
  56. return;
  57. }
  58. int result=file->Open();
  59. if (!result)
  60. {
  61. WWASSERT("File would not open\n");
  62. return;
  63. }
  64. DateTime=file->Get_Date_Time();
  65. char header[4];
  66. unsigned read_bytes=file->Read(header,4);
  67. if (!read_bytes)
  68. {
  69. WWASSERT("File loading failed trying to read header\n");
  70. return;
  71. }
  72. // Now, we read DDSURFACEDESC2 defining the compressed data
  73. read_bytes=file->Read(&SurfaceDesc,sizeof(LegacyDDSURFACEDESC2));
  74. // Verify the structure size matches the read size
  75. if (read_bytes==0 || read_bytes!=SurfaceDesc.Size)
  76. {
  77. StringClass tmp(0,true);
  78. tmp.Format("File %s loading failed.\nTried to read %d bytes, got %d. (SurfDesc.size=%d)\n",name,sizeof(LegacyDDSURFACEDESC2),read_bytes,SurfaceDesc.Size);
  79. WWASSERT_PRINT(0,tmp);
  80. return;
  81. }
  82. Format=D3DFormat_To_WW3DFormat((D3DFORMAT)SurfaceDesc.PixelFormat.FourCC);
  83. WWASSERT(
  84. Format==WW3D_FORMAT_DXT1 ||
  85. Format==WW3D_FORMAT_DXT2 ||
  86. Format==WW3D_FORMAT_DXT3 ||
  87. Format==WW3D_FORMAT_DXT4 ||
  88. Format==WW3D_FORMAT_DXT5);
  89. MipLevels=SurfaceDesc.MipMapCount;
  90. if (MipLevels==0) MipLevels=1;
  91. if (MipLevels>ReductionFactor) MipLevels-=ReductionFactor;
  92. else {
  93. MipLevels=1;
  94. ReductionFactor=ReductionFactor-MipLevels;
  95. }
  96. // Drop the two lowest miplevels!
  97. if (MipLevels>2) MipLevels-=2;
  98. else MipLevels=1;
  99. // check texture type, normal, cube or volume
  100. if (SurfaceDesc.Caps.Caps2&DDSCAPS2_CUBEMAP)
  101. {
  102. Type=DDS_CUBEMAP;
  103. }
  104. else if (SurfaceDesc.Caps.Caps2&DDSCAPS2_VOLUME)
  105. {
  106. Type=DDS_VOLUME;
  107. }
  108. FullWidth=SurfaceDesc.Width;
  109. FullHeight=SurfaceDesc.Height;
  110. FullDepth=SurfaceDesc.Depth;
  111. Width=SurfaceDesc.Width>>ReductionFactor;
  112. Height=SurfaceDesc.Height>>ReductionFactor;
  113. Depth=SurfaceDesc.Depth;
  114. unsigned level_size=Calculate_DXTC_Surface_Size
  115. (
  116. SurfaceDesc.Width,
  117. SurfaceDesc.Height,
  118. Format
  119. );
  120. unsigned level_offset=0;
  121. unsigned level_mip_dec=4;
  122. if (Type==DDS_VOLUME)
  123. {
  124. // add slices to level data size
  125. level_size*=SurfaceDesc.Depth;
  126. level_mip_dec=8;
  127. }
  128. LevelSizes=W3DNEWARRAY unsigned[MipLevels];
  129. LevelOffsets=W3DNEWARRAY unsigned[MipLevels];
  130. for (unsigned level=0;level<ReductionFactor;++level)
  131. {
  132. if (level_size>16)
  133. { // If surface is bigger than one block (8 or 16 bytes)...
  134. level_size/=level_mip_dec;
  135. }
  136. }
  137. for (level=0;level<MipLevels;++level)
  138. {
  139. LevelSizes[level]=level_size;
  140. LevelOffsets[level]=level_offset;
  141. level_offset+=level_size;
  142. if (level_size>16)
  143. { // If surface is bigger than one block (8 or 16 bytes)...
  144. level_size/=level_mip_dec;
  145. }
  146. }
  147. if (Type==DDS_CUBEMAP)
  148. {
  149. for (level=0; level<MipLevels;++level)
  150. {
  151. CubeFaceSize+=LevelSizes[level];
  152. }
  153. // this accounts for dropping 2 lowest mip levels
  154. if (MipLevels>2)
  155. CubeFaceSize+=16;
  156. }
  157. file->Close();
  158. }
  159. // ----------------------------------------------------------------------------
  160. DDSFileClass::~DDSFileClass()
  161. {
  162. delete[] DDSMemory;
  163. delete[] LevelSizes;
  164. delete[] LevelOffsets;
  165. }
  166. unsigned DDSFileClass::Get_Width(unsigned level) const
  167. {
  168. WWASSERT(level<MipLevels);
  169. unsigned width=Width>>level;
  170. if (width<4) width=4;
  171. return width;
  172. }
  173. unsigned DDSFileClass::Get_Height(unsigned level) const
  174. {
  175. WWASSERT(level<MipLevels);
  176. unsigned height=Height>>level;
  177. if (height<4) height=4;
  178. return height;
  179. }
  180. unsigned DDSFileClass::Get_Depth(unsigned level) const
  181. {
  182. WWASSERT(level<MipLevels);
  183. unsigned depth=Depth>>level;
  184. if (depth<4) depth=4;
  185. return depth;
  186. }
  187. const unsigned char* DDSFileClass::Get_Memory_Pointer(unsigned level) const
  188. {
  189. WWASSERT(level<MipLevels);
  190. return DDSMemory+LevelOffsets[level];
  191. }
  192. unsigned DDSFileClass::Get_Level_Size(unsigned level) const
  193. {
  194. WWASSERT(level<MipLevels);
  195. return LevelSizes[level];
  196. }
  197. // For some reason DX-Tex tool doesn't fill the surface size field, so we need to calculate it...
  198. unsigned DDSFileClass::Calculate_DXTC_Surface_Size
  199. (
  200. unsigned width,
  201. unsigned height,
  202. WW3DFormat format
  203. )
  204. {
  205. unsigned level_size=(width/4)*(height/4);
  206. switch (format)
  207. {
  208. case WW3D_FORMAT_DXT1:
  209. level_size*=8;
  210. break;
  211. case WW3D_FORMAT_DXT2:
  212. case WW3D_FORMAT_DXT3:
  213. case WW3D_FORMAT_DXT4:
  214. case WW3D_FORMAT_DXT5:
  215. level_size*=16;
  216. break;
  217. }
  218. return level_size;
  219. }
  220. // ----------------------------------------------------------------------------
  221. bool DDSFileClass::Load()
  222. {
  223. if (DDSMemory) return false;
  224. if (!LevelSizes || !LevelOffsets) return false;
  225. file_auto_ptr file(_TheFileFactory,Name);
  226. if (!file->Is_Available())
  227. {
  228. return false;
  229. }
  230. file->Open();
  231. // Data size is file size minus the header and info block
  232. unsigned size=file->Size()-SurfaceDesc.Size-4;
  233. if (!size)
  234. {
  235. return false;
  236. }
  237. // Skip mip levels if reduction factor is not zero
  238. unsigned level_size=Calculate_DXTC_Surface_Size
  239. (
  240. SurfaceDesc.Width,
  241. SurfaceDesc.Height,
  242. Format
  243. );
  244. unsigned skipped_offset=0;
  245. for (unsigned i=0;i<ReductionFactor;++i)
  246. {
  247. skipped_offset+=level_size;
  248. size-=level_size;
  249. if (level_size>16)
  250. { // If surface is bigger than one block (8 or 16 bytes)...
  251. level_size/=4;
  252. }
  253. }
  254. // Skip the header and info block and possible unused mip levels
  255. unsigned seek_size=file->Seek(SurfaceDesc.Size+4+skipped_offset);
  256. WWASSERT(seek_size==(SurfaceDesc.Size+4+skipped_offset));
  257. if (size && size<0x80000000)
  258. {
  259. // Allocate memory for the data excluding the headers
  260. DDSMemory=MSGW3DNEWARRAY("DDSMemory") unsigned char[size];
  261. // Read data
  262. unsigned read_size=file->Read(DDSMemory,size);
  263. // Verify we got all the data
  264. WWASSERT(read_size==size);
  265. }
  266. file->Close();
  267. return true;
  268. }
  269. // ----------------------------------------------------------------------------
  270. WWINLINE static unsigned RGB565_To_ARGB8888(unsigned short rgb)
  271. {
  272. unsigned rgba=0;
  273. rgba|=unsigned(rgb&0x001f)<<3;
  274. rgba|=unsigned(rgb&0x07e0)<<5;
  275. rgba|=unsigned(rgb&0xf800)<<8;
  276. return rgba;
  277. }
  278. WWINLINE static unsigned short ARGB8888_To_RGB565(unsigned argb_)
  279. {
  280. unsigned char* argb=(unsigned char*)&argb_;
  281. unsigned short rgb;
  282. rgb=((argb[2])&0xf8)<<8;
  283. rgb|=((argb[1])&0xfc)<<3;
  284. rgb|=((argb[0])&0xf8)>>3;
  285. return rgb;
  286. }
  287. // ----------------------------------------------------------------------------
  288. //
  289. // Copy mipmap level to D3D surface. The copying is performed using another
  290. // Copy_Level_To_Surface function (see below).
  291. //
  292. // ----------------------------------------------------------------------------
  293. void DDSFileClass::Copy_Level_To_Surface(unsigned level,IDirect3DSurface8* d3d_surface,const Vector3& hsv_shift)
  294. {
  295. WWASSERT(d3d_surface);
  296. // Verify that the destination surface size matches the source surface size
  297. D3DSURFACE_DESC surface_desc;
  298. DX8_ErrorCode(d3d_surface->GetDesc(&surface_desc));
  299. // First lock the surface
  300. D3DLOCKED_RECT locked_rect;
  301. DX8_ErrorCode(d3d_surface->LockRect(&locked_rect,NULL,0));
  302. Copy_Level_To_Surface(
  303. level,
  304. D3DFormat_To_WW3DFormat(surface_desc.Format),
  305. surface_desc.Width,
  306. surface_desc.Height,
  307. reinterpret_cast<unsigned char*>(locked_rect.pBits),
  308. locked_rect.Pitch,
  309. hsv_shift);
  310. // Finally, unlock the surface
  311. DX8_ErrorCode(d3d_surface->UnlockRect());
  312. }
  313. // ----------------------------------------------------------------------------
  314. //
  315. // Copy one mipmap level of texture to a memory surface. Surface type conversion
  316. // is performed if the destination is of different format. Scaling will be done
  317. // one of these days as well. Conversions between different types of compressed
  318. // surfaces are not performed and scaling of compressed surfaces is also not
  319. // possible.
  320. //
  321. // ----------------------------------------------------------------------------
  322. void DDSFileClass::Copy_Level_To_Surface
  323. (
  324. unsigned level,
  325. WW3DFormat dest_format,
  326. unsigned dest_width,
  327. unsigned dest_height,
  328. unsigned char* dest_surface,
  329. unsigned dest_pitch,
  330. const Vector3& hsv_shift
  331. )
  332. {
  333. WWASSERT(DDSMemory);
  334. WWASSERT(dest_surface);
  335. if (!DDSMemory || !Get_Memory_Pointer(level))
  336. {
  337. WWASSERT_PRINT(DDSMemory,"Surface mip level pointer is missing\n");
  338. return;
  339. }
  340. // If the format and size is a match just copy the contents
  341. bool has_hsv_shift = hsv_shift[0]!=0.0f || hsv_shift[1]!=0.0f || hsv_shift[2]!=0.0f;
  342. if (dest_format==Format && dest_width==Get_Width(level) && dest_height==Get_Height(level)) {
  343. // If hue shift, we can't just copy...
  344. if (has_hsv_shift) {
  345. if (Format==WW3D_FORMAT_DXT1) {
  346. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Memory_Pointer(level));
  347. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  348. for (unsigned y=0;y<dest_height;y+=4) {
  349. for (unsigned x=0;x<dest_width;x+=4) {
  350. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  351. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  352. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  353. Recolor(col0,hsv_shift);
  354. Recolor(col1,hsv_shift);
  355. col0=ARGB8888_To_RGB565(col0);
  356. col1=ARGB8888_To_RGB565(col1);
  357. cols=unsigned(col0)<<16|col1;
  358. *dest_ptr++=cols;
  359. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  360. }
  361. }
  362. }
  363. else if (Format==WW3D_FORMAT_DXT5) {
  364. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Memory_Pointer(level));
  365. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  366. for (unsigned y=0;y<dest_height;y+=4) {
  367. for (unsigned x=0;x<dest_width;x+=4) {
  368. *dest_ptr++=*src_ptr++; // Bytes 1-4 of alpha block
  369. *dest_ptr++=*src_ptr++; // Bytes 5-8 of alpha block
  370. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  371. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  372. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  373. Recolor(col0,hsv_shift);
  374. Recolor(col1,hsv_shift);
  375. col0=ARGB8888_To_RGB565(col0);
  376. col1=ARGB8888_To_RGB565(col1);
  377. cols=unsigned(col0)<<16|col1;
  378. *dest_ptr++=cols;
  379. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  380. }
  381. }
  382. }
  383. else {
  384. WWASSERT(0);
  385. }
  386. }
  387. else {
  388. unsigned compressed_size=Get_Level_Size(level);
  389. memcpy(dest_surface,Get_Memory_Pointer(level),compressed_size);
  390. }
  391. }
  392. else {
  393. // If size matches, copy each pixel linearly with color space conversion
  394. if (dest_width==Get_Width(level) && dest_height==Get_Height(level)) {
  395. // An exception here - if the source format is DXT1 and the destination
  396. // is DXT2, just copy the contents and create an empty alpha channel.
  397. // This is needed on NVidia cards that have problems with DXT1 compression.
  398. if (Format==WW3D_FORMAT_DXT1 && dest_format==WW3D_FORMAT_DXT2) {
  399. // If hue shift, we can't just copy...
  400. if (has_hsv_shift) {
  401. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Memory_Pointer(level));
  402. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  403. for (unsigned y=0;y<dest_height;y+=4) {
  404. for (unsigned x=0;x<dest_width;x+=4) {
  405. *dest_ptr++=0xffffffff; // Bytes 1-4 of alpha block
  406. *dest_ptr++=0xffffffff; // Bytes 5-8 of alpha block
  407. // *dest_ptr++=*src_ptr++; // Bytes 1-4 of color block
  408. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  409. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  410. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  411. Recolor(col0,hsv_shift);
  412. Recolor(col1,hsv_shift);
  413. col0=ARGB8888_To_RGB565(col0);
  414. col1=ARGB8888_To_RGB565(col1);
  415. cols=unsigned(col0)<<16|col1;
  416. *dest_ptr++=cols;
  417. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  418. }
  419. }
  420. }
  421. else {
  422. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Memory_Pointer(level));
  423. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  424. for (unsigned y=0;y<dest_height;y+=4) {
  425. for (unsigned x=0;x<dest_width;x+=4) {
  426. *dest_ptr++=0xffffffff; // Bytes 1-4 of alpha block
  427. *dest_ptr++=0xffffffff; // Bytes 5-8 of alpha block
  428. *dest_ptr++=*src_ptr++; // Bytes 1-4 of color block
  429. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  430. }
  431. }
  432. }
  433. }
  434. else {
  435. unsigned dest_bpp=Get_Bytes_Per_Pixel(dest_format);
  436. // Copy 4x4 block at a time
  437. bool contains_alpha=false;
  438. for (unsigned y=0;y<dest_height;y+=4) {
  439. unsigned char* dest_ptr=dest_surface;
  440. dest_ptr+=y*dest_pitch;
  441. for (unsigned x=0;x<dest_width;x+=4,dest_ptr+=dest_bpp*4) {
  442. contains_alpha|=Get_4x4_Block(dest_ptr,dest_pitch,dest_format,level,x,y,hsv_shift);
  443. }
  444. }
  445. if (Format==WW3D_FORMAT_DXT1 && contains_alpha) {
  446. WWDEBUG_SAY(("Warning: DXT1 format should not contain alpha information - file %s\n",Name));
  447. }
  448. }
  449. }
  450. }
  451. }
  452. // cube map
  453. const unsigned char* DDSFileClass::Get_CubeMap_Memory_Pointer
  454. (
  455. unsigned int face,
  456. unsigned int level
  457. ) const
  458. {
  459. return &DDSMemory[CubeFaceSize*face+LevelOffsets[level]];
  460. }
  461. void DDSFileClass::Copy_CubeMap_Level_To_Surface
  462. (
  463. unsigned face,
  464. unsigned level,
  465. WW3DFormat dest_format,
  466. unsigned dest_width,
  467. unsigned dest_height,
  468. unsigned char* dest_surface,
  469. unsigned dest_pitch,
  470. const Vector3& hsv_shift
  471. )
  472. {
  473. WWASSERT(DDSMemory);
  474. WWASSERT(dest_surface);
  475. if (!DDSMemory || !Get_CubeMap_Memory_Pointer(face,level))
  476. {
  477. WWASSERT_PRINT(DDSMemory,"Surface mip level pointer is missing\n");
  478. return;
  479. }
  480. // If the format and size is a match just copy the contents
  481. bool has_hsv_shift = hsv_shift[0]!=0.0f || hsv_shift[1]!=0.0f || hsv_shift[2]!=0.0f;
  482. if (dest_format==Format && dest_width==Get_Width(level) && dest_height==Get_Height(level))
  483. {
  484. // If hue shift, we can't just copy...
  485. if (has_hsv_shift)
  486. {
  487. if (Format==WW3D_FORMAT_DXT1)
  488. {
  489. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_CubeMap_Memory_Pointer(face,level));
  490. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  491. for (unsigned y=0;y<dest_height;y+=4)
  492. {
  493. for (unsigned x=0;x<dest_width;x+=4)
  494. {
  495. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  496. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  497. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  498. Recolor(col0,hsv_shift);
  499. Recolor(col1,hsv_shift);
  500. col0=ARGB8888_To_RGB565(col0);
  501. col1=ARGB8888_To_RGB565(col1);
  502. cols=unsigned(col0)<<16|col1;
  503. *dest_ptr++=cols;
  504. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  505. }
  506. }
  507. }
  508. else if (Format==WW3D_FORMAT_DXT5)
  509. {
  510. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_CubeMap_Memory_Pointer(face,level));
  511. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  512. for (unsigned y=0;y<dest_height;y+=4)
  513. {
  514. for (unsigned x=0;x<dest_width;x+=4)
  515. {
  516. *dest_ptr++=*src_ptr++; // Bytes 1-4 of alpha block
  517. *dest_ptr++=*src_ptr++; // Bytes 5-8 of alpha block
  518. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  519. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  520. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  521. Recolor(col0,hsv_shift);
  522. Recolor(col1,hsv_shift);
  523. col0=ARGB8888_To_RGB565(col0);
  524. col1=ARGB8888_To_RGB565(col1);
  525. cols=unsigned(col0)<<16|col1;
  526. *dest_ptr++=cols;
  527. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  528. }
  529. }
  530. }
  531. else
  532. {
  533. WWASSERT(0);
  534. }
  535. }
  536. else
  537. {
  538. unsigned compressed_size=Get_Level_Size(level);
  539. memcpy(dest_surface,Get_CubeMap_Memory_Pointer(face,level),compressed_size);
  540. }
  541. }
  542. else
  543. {
  544. // If size matches, copy each pixel linearly with color space conversion
  545. if (dest_width==Get_Width(level) && dest_height==Get_Height(level))
  546. {
  547. // An exception here - if the source format is DXT1 and the destination
  548. // is DXT2, just copy the contents and create an empty alpha channel.
  549. // This is needed on NVidia cards that have problems with DXT1 compression.
  550. if (Format==WW3D_FORMAT_DXT1 && dest_format==WW3D_FORMAT_DXT2)
  551. {
  552. // If hue shift, we can't just copy...
  553. if (has_hsv_shift)
  554. {
  555. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_CubeMap_Memory_Pointer(face,level));
  556. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  557. for (unsigned y=0;y<dest_height;y+=4)
  558. {
  559. for (unsigned x=0;x<dest_width;x+=4)
  560. {
  561. *dest_ptr++=0xffffffff; // Bytes 1-4 of alpha block
  562. *dest_ptr++=0xffffffff; // Bytes 5-8 of alpha block
  563. // *dest_ptr++=*src_ptr++; // Bytes 1-4 of color block
  564. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  565. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  566. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  567. Recolor(col0,hsv_shift);
  568. Recolor(col1,hsv_shift);
  569. col0=ARGB8888_To_RGB565(col0);
  570. col1=ARGB8888_To_RGB565(col1);
  571. cols=unsigned(col0)<<16|col1;
  572. *dest_ptr++=cols;
  573. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  574. }
  575. }
  576. }
  577. else
  578. {
  579. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_CubeMap_Memory_Pointer(face,level));
  580. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  581. for (unsigned y=0;y<dest_height;y+=4)
  582. {
  583. for (unsigned x=0;x<dest_width;x+=4)
  584. {
  585. *dest_ptr++=0xffffffff; // Bytes 1-4 of alpha block
  586. *dest_ptr++=0xffffffff; // Bytes 5-8 of alpha block
  587. *dest_ptr++=*src_ptr++; // Bytes 1-4 of color block
  588. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  589. }
  590. }
  591. }
  592. }
  593. else
  594. {
  595. unsigned dest_bpp=Get_Bytes_Per_Pixel(dest_format);
  596. // Copy 4x4 block at a time
  597. bool contains_alpha=false;
  598. for (unsigned y=0;y<dest_height;y+=4)
  599. {
  600. unsigned char* dest_ptr=dest_surface;
  601. dest_ptr+=y*dest_pitch;
  602. for (unsigned x=0;x<dest_width;x+=4,dest_ptr+=dest_bpp*4)
  603. {
  604. contains_alpha|=Get_4x4_Block(dest_ptr,dest_pitch,dest_format,level,x,y,hsv_shift);
  605. }
  606. }
  607. if (Format==WW3D_FORMAT_DXT1 && contains_alpha)
  608. {
  609. WWDEBUG_SAY(("Warning: DXT1 format should not contain alpha information - file %s\n",Name));
  610. }
  611. }
  612. }
  613. }
  614. }
  615. // volume texture copy
  616. const unsigned char* DDSFileClass::Get_Volume_Memory_Pointer(unsigned int level) const
  617. {
  618. return NULL;//DDSMemory[
  619. }
  620. void DDSFileClass::Copy_Volume_Level_To_Surface
  621. (
  622. unsigned level,
  623. unsigned depth,
  624. WW3DFormat dest_format,
  625. unsigned dest_width,
  626. unsigned dest_height,
  627. unsigned char* dest_surface,
  628. unsigned row_pitch,
  629. unsigned slice_pitch,
  630. const Vector3& hsv_shift
  631. )
  632. {
  633. WWASSERT(DDSMemory);
  634. WWASSERT(dest_surface);
  635. if (!DDSMemory || !Get_Volume_Memory_Pointer(level))
  636. {
  637. WWASSERT_PRINT(DDSMemory,"Surface mip level pointer is missing\n");
  638. return;
  639. }
  640. // get 'dest_surface'
  641. // If the format and size is a match just copy the contents
  642. bool has_hsv_shift = hsv_shift[0]!=0.0f || hsv_shift[1]!=0.0f || hsv_shift[2]!=0.0f;
  643. if (dest_format==Format && dest_width==Get_Width(level) && dest_height==Get_Height(level))
  644. {
  645. // If hue shift, we can't just copy...
  646. if (has_hsv_shift)
  647. {
  648. if (Format==WW3D_FORMAT_DXT1)
  649. {
  650. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Volume_Memory_Pointer(level));
  651. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  652. for (unsigned y=0;y<dest_height;y+=4)
  653. {
  654. for (unsigned x=0;x<dest_width;x+=4)
  655. {
  656. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  657. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  658. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  659. Recolor(col0,hsv_shift);
  660. Recolor(col1,hsv_shift);
  661. col0=ARGB8888_To_RGB565(col0);
  662. col1=ARGB8888_To_RGB565(col1);
  663. cols=unsigned(col0)<<16|col1;
  664. *dest_ptr++=cols;
  665. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  666. }
  667. }
  668. }
  669. else if (Format==WW3D_FORMAT_DXT5)
  670. {
  671. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Volume_Memory_Pointer(level));
  672. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  673. for (unsigned y=0;y<dest_height;y+=4)
  674. {
  675. for (unsigned x=0;x<dest_width;x+=4)
  676. {
  677. *dest_ptr++=*src_ptr++; // Bytes 1-4 of alpha block
  678. *dest_ptr++=*src_ptr++; // Bytes 5-8 of alpha block
  679. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  680. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  681. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  682. Recolor(col0,hsv_shift);
  683. Recolor(col1,hsv_shift);
  684. col0=ARGB8888_To_RGB565(col0);
  685. col1=ARGB8888_To_RGB565(col1);
  686. cols=unsigned(col0)<<16|col1;
  687. *dest_ptr++=cols;
  688. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  689. }
  690. }
  691. }
  692. else
  693. {
  694. WWASSERT(0);
  695. }
  696. }
  697. else
  698. {
  699. unsigned compressed_size=Get_Level_Size(level);
  700. memcpy(dest_surface,Get_Volume_Memory_Pointer(level),compressed_size);
  701. }
  702. }
  703. else
  704. {
  705. // If size matches, copy each pixel linearly with color space conversion
  706. if (dest_width==Get_Width(level) && dest_height==Get_Height(level))
  707. {
  708. // An exception here - if the source format is DXT1 and the destination
  709. // is DXT2, just copy the contents and create an empty alpha channel.
  710. // This is needed on NVidia cards that have problems with DXT1 compression.
  711. if (Format==WW3D_FORMAT_DXT1 && dest_format==WW3D_FORMAT_DXT2)
  712. {
  713. // If hue shift, we can't just copy...
  714. if (has_hsv_shift)
  715. {
  716. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Volume_Memory_Pointer(level));
  717. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  718. for (unsigned y=0;y<dest_height;y+=4)
  719. {
  720. for (unsigned x=0;x<dest_width;x+=4)
  721. {
  722. *dest_ptr++=0xffffffff; // Bytes 1-4 of alpha block
  723. *dest_ptr++=0xffffffff; // Bytes 5-8 of alpha block
  724. // *dest_ptr++=*src_ptr++; // Bytes 1-4 of color block
  725. unsigned cols=*src_ptr++; // Bytes 1-4 of color block
  726. unsigned col0=RGB565_To_ARGB8888(unsigned short(cols>>16));
  727. unsigned col1=RGB565_To_ARGB8888(unsigned short(cols&0xffff));
  728. Recolor(col0,hsv_shift);
  729. Recolor(col1,hsv_shift);
  730. col0=ARGB8888_To_RGB565(col0);
  731. col1=ARGB8888_To_RGB565(col1);
  732. cols=unsigned(col0)<<16|col1;
  733. *dest_ptr++=cols;
  734. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  735. }
  736. }
  737. }
  738. else
  739. {
  740. const unsigned* src_ptr=reinterpret_cast<const unsigned*>(Get_Volume_Memory_Pointer(level));
  741. unsigned* dest_ptr=reinterpret_cast<unsigned*>(dest_surface);
  742. for (unsigned y=0;y<dest_height;y+=4)
  743. {
  744. for (unsigned x=0;x<dest_width;x+=4)
  745. {
  746. *dest_ptr++=0xffffffff; // Bytes 1-4 of alpha block
  747. *dest_ptr++=0xffffffff; // Bytes 5-8 of alpha block
  748. *dest_ptr++=*src_ptr++; // Bytes 1-4 of color block
  749. *dest_ptr++=*src_ptr++; // Bytes 5-8 of color block
  750. }
  751. }
  752. }
  753. }
  754. else
  755. {
  756. WWASSERT(0);
  757. /* todo
  758. unsigned dest_bpp=Get_Bytes_Per_Pixel(dest_format);
  759. // Copy 4x4 block at a time
  760. bool contains_alpha=false;
  761. for (unsigned z=0;z<dest_depth;z++)
  762. {
  763. for (unsigned y=0;y<dest_height;y+=4)
  764. {
  765. unsigned char* dest_ptr=dest_surface;
  766. row_ptr+=y*row_pitch;
  767. for (unsigned x=0;x<dest_width;x+=4,dest_ptr+=dest_bpp*4)
  768. {
  769. contains_alpha|=Get_4x4_Block(dest_ptr,dest_pitch,dest_format,level,x,y,hsv_shift);
  770. }
  771. }
  772. if (Format==WW3D_FORMAT_DXT1 && contains_alpha)
  773. {
  774. WWDEBUG_SAY(("Warning: DXT1 format should not contain alpha information - file %s\n",Name));
  775. }
  776. }*/
  777. }
  778. }
  779. }
  780. }
  781. // ----------------------------------------------------------------------------
  782. WWINLINE static unsigned Combine_Colors(unsigned col1, unsigned col2, unsigned rel)
  783. {
  784. const unsigned R_B_MASK=0x00ff00ff;
  785. const unsigned G_MASK=0x0000ff00;
  786. unsigned rel2=255-rel;
  787. unsigned r_b_col1=col1&R_B_MASK;
  788. r_b_col1*=rel;
  789. unsigned r_b_col2=col2&R_B_MASK;
  790. r_b_col2*=rel2;
  791. r_b_col1+=r_b_col2;
  792. r_b_col1>>=8;
  793. r_b_col1&=R_B_MASK;
  794. unsigned g_col1=col1&G_MASK;
  795. g_col1*=rel;
  796. unsigned g_col2=col2&G_MASK;
  797. g_col2*=rel2;
  798. g_col1+=g_col2;
  799. g_col1>>=8;
  800. g_col1&=G_MASK;
  801. return r_b_col1|g_col1;
  802. /* float f=float(rel)/256.0f;
  803. unsigned new_col=0;
  804. new_col|=int(float(int(col1&0x00ff0000))*f+float(int(col2&0x00ff0000))*(1.0f-f))&0x00ff0000;
  805. new_col|=int(float(int(col1&0x0000ff00))*f+float(int(col2&0x0000ff00))*(1.0f-f))&0x0000ff00;
  806. new_col|=int(float(int(col1&0x000000ff))*f+float(int(col2&0x000000ff))*(1.0f-f))&0x000000ff;
  807. return new_col;
  808. */
  809. }
  810. // ----------------------------------------------------------------------------
  811. //
  812. // Note that this is NOT an efficient way of extracting pixels from compressed image - we should implement
  813. // faster block-copy method for non-scaled copying.
  814. //
  815. // ----------------------------------------------------------------------------
  816. unsigned DDSFileClass::Get_Pixel(unsigned level,unsigned x,unsigned y) const
  817. {
  818. WWASSERT(level<MipLevels);
  819. WWASSERT(x<Get_Width(level));
  820. WWASSERT(y<Get_Height(level));
  821. switch (Format) {
  822. // Note that we don't currently really support alpha on DXT1 - all alpha textures should use DXT5.
  823. // The reason for this is that when converting from DXT1 to 16 bit uncompressed texture we want
  824. // to be able to use RGB565 format instead of ARGB4444. As the alpha is encoded in DXT1 per-block
  825. // basis there isn't really a way to tell if the surface has an alpha or not so either we use alpha
  826. // or we don't.
  827. case WW3D_FORMAT_DXT1:
  828. {
  829. const unsigned char* block_memory=Get_Memory_Pointer(level)+(x/4)*8+((y/4)*(Get_Width(level)/4))*8;
  830. unsigned col0=RGB565_To_ARGB8888(*(unsigned short*)&block_memory[0]);
  831. unsigned col1=RGB565_To_ARGB8888(*(unsigned short*)&block_memory[2]);
  832. unsigned char line=block_memory[4+(y%4)];
  833. line>>=(x%4)*2;
  834. line=(line&3);
  835. if (col0>col1) {
  836. switch (line) {
  837. case 0: return col0|0xff000000;
  838. case 1: return col1|0xff000000;
  839. case 2: return Combine_Colors(col1,col0,85)|0xff000000;
  840. case 3: return Combine_Colors(col0,col1,85)|0xff000000;
  841. }
  842. }
  843. else {
  844. switch (line) {
  845. case 0: return col0|0xff000000;
  846. case 1: return col1|0xff000000;
  847. case 2: return Combine_Colors(col1,col0,128)|0xff000000;
  848. case 3: return 0x00000000;
  849. }
  850. }
  851. }
  852. break;
  853. case WW3D_FORMAT_DXT2:
  854. return 0xffffffff;
  855. case WW3D_FORMAT_DXT3:
  856. return 0xffffffff;
  857. case WW3D_FORMAT_DXT4:
  858. return 0xffffffff;
  859. case WW3D_FORMAT_DXT5:
  860. {
  861. const unsigned char* alpha_block=Get_Memory_Pointer(level)+(x/4)*16+((y/4)*(Get_Width(level)/4))*16;
  862. unsigned alpha0=alpha_block[0];
  863. unsigned alpha1=alpha_block[1];
  864. unsigned bit_idx=((x%4)+4*(y%4))*3;
  865. unsigned byte_idx=bit_idx/8;
  866. bit_idx%=8;
  867. unsigned alpha_index=0;
  868. for (int i=0;i<3;++i) {
  869. WWASSERT(byte_idx<6);
  870. unsigned alpha_bit=(alpha_block[2+byte_idx]>>(bit_idx))&1;
  871. alpha_index|=alpha_bit<<(i);
  872. bit_idx++;
  873. if (bit_idx>=8) {
  874. bit_idx=0;
  875. byte_idx++;
  876. }
  877. }
  878. WWASSERT(alpha_index<8);
  879. // 8-alpha or 6-alpha block?
  880. unsigned alpha_value=0;
  881. if (alpha0>alpha1) {
  882. // 8-alpha block: derive the other six alphas.
  883. // Bit code 000 = alpha_0, 001 = alpha_1, others are interpolated.
  884. switch (alpha_index) {
  885. case 0: alpha_value=alpha0; break;
  886. case 1: alpha_value=alpha1; break;
  887. case 2: alpha_value=(6*alpha0+1*alpha1+3) / 7; break; // bit code 010
  888. case 3: alpha_value=(5*alpha0+2*alpha1+3) / 7; break; // bit code 011
  889. case 4: alpha_value=(4*alpha0+3*alpha1+3) / 7; break; // bit code 100
  890. case 5: alpha_value=(3*alpha0+4*alpha1+3) / 7; break; // bit code 101
  891. case 6: alpha_value=(2*alpha0+5*alpha1+3) / 7; break; // bit code 110
  892. case 7: alpha_value=(1*alpha0+6*alpha1+3) / 7; break; // bit code 111
  893. }
  894. }
  895. else {
  896. // 6-alpha block.
  897. // Bit code 000 = alpha_0, 001 = alpha_1, others are interpolated.
  898. switch (alpha_index) {
  899. case 0: alpha_value=alpha0; break;
  900. case 1: alpha_value=alpha1; break;
  901. case 2: alpha_value=(4*alpha0+1*alpha1+2) / 5; break; // Bit code 010
  902. case 3: alpha_value=(3*alpha0+2*alpha1+2) / 5; break; // Bit code 011
  903. case 4: alpha_value=(2*alpha0+3*alpha1+2) / 5; break; // Bit code 100
  904. case 5: alpha_value=(1*alpha0+4*alpha1+2) / 5; break; // Bit code 101
  905. case 6: alpha_value=0; break; // Bit code 110
  906. case 7: alpha_value=255; break; // Bit code 111
  907. }
  908. }
  909. alpha_value<<=24;
  910. // Extract color
  911. const unsigned char* color_block=alpha_block+8;
  912. unsigned col0=RGB565_To_ARGB8888(*(unsigned short*)&color_block[0]);
  913. unsigned col1=RGB565_To_ARGB8888(*(unsigned short*)&color_block[2]);
  914. unsigned char line=color_block[4+(y%4)];
  915. line>>=(x%4)*2;
  916. line=(line&3);
  917. switch (line) {
  918. case 0: return col0|alpha_value;
  919. case 1: return col1|alpha_value;
  920. case 2: return Combine_Colors(col1,col0,85)|alpha_value;
  921. case 3: return Combine_Colors(col0,col1,85)|alpha_value;
  922. }
  923. }
  924. break;
  925. }
  926. return 0xffffffff;
  927. }
  928. // ----------------------------------------------------------------------------
  929. //
  930. // Uncompress one 4x4 block from the compressed image.
  931. //
  932. // Returns: true if block contained alpha, false is not
  933. //
  934. // Note: Destination can't be DXT or paletted surface!
  935. //
  936. // ----------------------------------------------------------------------------
  937. bool DDSFileClass::Get_4x4_Block(
  938. unsigned char* dest_ptr, // Destination surface pointer
  939. unsigned dest_pitch, // Destination surface pitch, in bytes
  940. WW3DFormat dest_format, // Destination surface format, A8R8G8B8 is fastest
  941. unsigned level, // DDS mipmap level to copy from
  942. unsigned source_x, // DDS x offset to copy from, must be aligned by 4!
  943. unsigned source_y, // DDS y offset to copy from, must be aligned by 4!
  944. const Vector3& hsv_shift) const
  945. {
  946. // Verify the block alignment
  947. WWASSERT((source_x&3)==0);
  948. WWASSERT((source_y&3)==0);
  949. // Verify level
  950. WWASSERT(level<MipLevels);
  951. // Verify coordinate bounds
  952. WWASSERT(source_x<Get_Width(level));
  953. WWASSERT(source_y<Get_Height(level));
  954. unsigned dest_bpp=Get_Bytes_Per_Pixel(dest_format);
  955. bool has_hsv_shift = hsv_shift[0]!=0.0f || hsv_shift[1]!=0.0f || hsv_shift[2]!=0.0f;
  956. switch (Format) {
  957. // Note that we don't currently really support alpha on DXT1 - all alpha textures should use DXT5.
  958. // The reason for this is that when converting from DXT1 to 16 bit uncompressed texture we want
  959. // to be able to use RGB565 format instead of ARGB4444. As the alpha is encoded in DXT1 per-block
  960. // basis there isn't really a way to tell if the surface has an alpha or not so either we use alpha
  961. // or we don't.
  962. case WW3D_FORMAT_DXT1:
  963. {
  964. const unsigned char* block_memory=Get_Memory_Pointer(level)+(source_x/4)*8+((source_y/4)*(Get_Width(level)/4))*8;
  965. unsigned col0=RGB565_To_ARGB8888(*(unsigned short*)&block_memory[0]);
  966. unsigned col1=RGB565_To_ARGB8888(*(unsigned short*)&block_memory[2]);
  967. // Even if we don't support alpha, decompression is different if source has alpha
  968. unsigned dest_pixel=0;
  969. if (col0>col1) {
  970. if (has_hsv_shift) {
  971. Recolor(col0,hsv_shift);
  972. Recolor(col1,hsv_shift);
  973. }
  974. for (int y=0;y<4;++y) {
  975. unsigned char* tmp_dest_ptr=dest_ptr;
  976. dest_ptr+=dest_pitch;
  977. unsigned char line=block_memory[4+y];
  978. for (int x=0;x<4;++x) {
  979. switch (line&3) {
  980. case 0: dest_pixel=col0|0xff000000; break;
  981. case 1: dest_pixel=col1|0xff000000; break;
  982. case 2: dest_pixel=Combine_Colors(col1,col0,85)|0xff000000; break;
  983. case 3: dest_pixel=Combine_Colors(col0,col1,85)|0xff000000; break;
  984. }
  985. line>>=2;
  986. BitmapHandlerClass::Write_B8G8R8A8(tmp_dest_ptr,dest_format,dest_pixel);
  987. tmp_dest_ptr+=dest_bpp;
  988. }
  989. }
  990. return false; // No alpha found in the block
  991. }
  992. else {
  993. if (has_hsv_shift) {
  994. Recolor(col0,hsv_shift);
  995. Recolor(col1,hsv_shift);
  996. }
  997. bool contains_alpha=false;
  998. for (int y=0;y<4;++y) {
  999. unsigned char* tmp_dest_ptr=dest_ptr;
  1000. dest_ptr+=dest_pitch;
  1001. unsigned char line=block_memory[4+y];
  1002. for (int x=0;x<4;++x) {
  1003. switch (line&3) {
  1004. case 0: dest_pixel=col0|0xff000000; break;
  1005. case 1: dest_pixel=col1|0xff000000; break;
  1006. case 2: dest_pixel=Combine_Colors(col1,col0,128)|0xff000000; break;
  1007. case 3: dest_pixel=0x00000000; contains_alpha=true; break;
  1008. }
  1009. line>>=2;
  1010. BitmapHandlerClass::Write_B8G8R8A8(tmp_dest_ptr,dest_format,dest_pixel);
  1011. tmp_dest_ptr+=dest_bpp;
  1012. }
  1013. }
  1014. return contains_alpha; // Alpha block...?
  1015. }
  1016. }
  1017. break;
  1018. case WW3D_FORMAT_DXT2:
  1019. return false;
  1020. case WW3D_FORMAT_DXT3:
  1021. return false;
  1022. case WW3D_FORMAT_DXT4:
  1023. return false;
  1024. case WW3D_FORMAT_DXT5:
  1025. {
  1026. // Init alphas
  1027. const unsigned char* alpha_block=Get_Memory_Pointer(level)+(source_x/4)*16+((source_y/4)*(Get_Width(level)/4))*16;
  1028. unsigned alphas[8];
  1029. alphas[0]=alpha_block[0];
  1030. alphas[1]=alpha_block[1];
  1031. // 8-alpha or 6-alpha block?
  1032. if (alphas[0]>alphas[1]) {
  1033. alphas[2]=(6*alphas[0]+1*alphas[1]+3) / 7; // bit code 010
  1034. alphas[3]=(5*alphas[0]+2*alphas[1]+3) / 7; // bit code 011
  1035. alphas[4]=(4*alphas[0]+3*alphas[1]+3) / 7; // bit code 100
  1036. alphas[5]=(3*alphas[0]+4*alphas[1]+3) / 7; // bit code 101
  1037. alphas[6]=(2*alphas[0]+5*alphas[1]+3) / 7; // bit code 110
  1038. alphas[7]=(1*alphas[0]+6*alphas[1]+3) / 7; // bit code 111
  1039. }
  1040. else {
  1041. alphas[2]=(4*alphas[0]+1*alphas[1]+2) / 5; // Bit code 010
  1042. alphas[3]=(3*alphas[0]+2*alphas[1]+2) / 5; // Bit code 011
  1043. alphas[4]=(2*alphas[0]+3*alphas[1]+2) / 5; // Bit code 100
  1044. alphas[5]=(1*alphas[0]+4*alphas[1]+2) / 5; // Bit code 101
  1045. alphas[6]=0; // Bit code 110
  1046. alphas[7]=255; // Bit code 111
  1047. }
  1048. // Init colors
  1049. const unsigned char* color_block=alpha_block+8;
  1050. unsigned col0=RGB565_To_ARGB8888(*(unsigned short*)&color_block[0]);
  1051. unsigned col1=RGB565_To_ARGB8888(*(unsigned short*)&color_block[2]);
  1052. if (has_hsv_shift) {
  1053. Recolor(col0,hsv_shift);
  1054. Recolor(col1,hsv_shift);
  1055. }
  1056. unsigned dest_pixel=0;
  1057. unsigned bit_idx=0;
  1058. unsigned contains_alpha=0xff;
  1059. unsigned alpha_indices[16];
  1060. unsigned* ai_ptr=alpha_indices;
  1061. for (int a=0;a<2;++a) {
  1062. ai_ptr[0]=alpha_block[2]&0x7;
  1063. ai_ptr[1]=(alpha_block[2]>>3)&0x7;
  1064. ai_ptr[2]=(alpha_block[2]>>6)|((alpha_block[3]&1)<<2);
  1065. ai_ptr[3]=(alpha_block[3]>>1)&0x7;
  1066. ai_ptr[4]=(alpha_block[3]>>4)&0x7;
  1067. ai_ptr[5]=(alpha_block[3]>>7)|((alpha_block[4]&3)<<1);
  1068. ai_ptr[6]=(alpha_block[4]>>2)&0x7;
  1069. ai_ptr[7]=(alpha_block[4]>>5);
  1070. ai_ptr+=8;
  1071. alpha_block+=3;
  1072. }
  1073. unsigned aii=0;
  1074. for (int y=0;y<4;++y) {
  1075. unsigned char* tmp_dest_ptr=dest_ptr;
  1076. dest_ptr+=dest_pitch;
  1077. unsigned char line=color_block[4+y];
  1078. for (int x=0;x<4;++x,bit_idx+=3) {
  1079. unsigned alpha_value=alphas[alpha_indices[aii++]];
  1080. contains_alpha&=alpha_value;
  1081. alpha_value<<=24;
  1082. // Extract color
  1083. switch (line&3) {
  1084. case 0: dest_pixel=col0|alpha_value; break;
  1085. case 1: dest_pixel=col1|alpha_value; break;
  1086. case 2: dest_pixel=Combine_Colors(col1,col0,85)|alpha_value; break;
  1087. case 3: dest_pixel=Combine_Colors(col0,col1,85)|alpha_value; break;
  1088. }
  1089. line>>=2;
  1090. BitmapHandlerClass::Write_B8G8R8A8(tmp_dest_ptr,dest_format,dest_pixel);
  1091. tmp_dest_ptr+=dest_bpp;
  1092. }
  1093. }
  1094. /*
  1095. for (int y=0;y<4;++y) {
  1096. unsigned char* tmp_dest_ptr=dest_ptr;
  1097. dest_ptr+=dest_pitch;
  1098. unsigned char line=color_block[4+y];
  1099. for (int x=0;x<4;++x,bit_idx+=3) {
  1100. unsigned byte_idx=bit_idx/8;
  1101. unsigned tmp_bit_idx=bit_idx&7;
  1102. unsigned alpha_index=0;
  1103. for (int i=0;i<3;++i) {
  1104. WWASSERT(byte_idx<6);
  1105. unsigned alpha_bit=(alpha_block[2+byte_idx]>>(tmp_bit_idx))&1;
  1106. alpha_index|=alpha_bit<<(i);
  1107. tmp_bit_idx++;
  1108. if (tmp_bit_idx>=8) {
  1109. tmp_bit_idx=0;
  1110. byte_idx++;
  1111. }
  1112. }
  1113. WWASSERT(alpha_index<8);
  1114. unsigned alpha_value=alphas[alpha_index];
  1115. contains_alpha&=alpha_value;
  1116. alpha_value<<=24;
  1117. // Extract color
  1118. switch (line&3) {
  1119. case 0: dest_pixel=col0|alpha_value; break;
  1120. case 1: dest_pixel=col1|alpha_value; break;
  1121. case 2: dest_pixel=Combine_Colors(col1,col0,85)|alpha_value; break;
  1122. case 3: dest_pixel=Combine_Colors(col0,col1,85)|alpha_value; break;
  1123. }
  1124. line>>=2;
  1125. BitmapHandlerClass::Write_B8G8R8A8(tmp_dest_ptr,dest_format,dest_pixel);
  1126. tmp_dest_ptr+=dest_bpp;
  1127. }
  1128. }
  1129. */
  1130. return contains_alpha!=0xff; // Alpha block... DXT5 should only be used when the image needs alpha
  1131. // but for now check anyway...
  1132. }
  1133. }
  1134. return false;
  1135. }