il_dds.c
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 55k
Development Platform:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Sources
  4. // Copyright (C) 2000-2009 by Denton Woods
  5. // Last modified: 01/30/2009
  6. //
  7. // Filename: src-IL/src/il_dds.c
  8. //
  9. // Description: Reads from a DirectDraw Surface (.dds) file.
  10. //
  11. //-----------------------------------------------------------------------------
  12. //
  13. //
  14. // Note:  Almost all this code is from nVidia's DDS-loading example at
  15. // http://www.nvidia.com/view.asp?IO=dxtc_decompression_code
  16. // and from the specs at
  17. // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dx8_c/hh/dx8_c/graphics_using_0j03.asp
  18. // and
  19. // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dx8_c/directx_cpp/Graphics/ProgrammersGuide/Appendix/DDSFileFormat/ovwDDSFileFormat.asp
  20. // However, some not really valid .dds files are also read, for example
  21. // Volume Textures without the COMPLEX bit set, so the specs aren't taken
  22. // too strictly while reading.
  23. #include "il_internal.h"
  24. #ifndef IL_NO_DDS
  25. #include "il_dds.h"
  26. // Global variables
  27. static DDSHEAD Head; // Image header
  28. static ILubyte *CompData = NULL; // Compressed data
  29. static ILuint CompSize; // Compressed size
  30. //static ILuint CompFormat; // Compressed format
  31. static ILimage *Image;
  32. static ILint Width, Height, Depth;
  33. ILuint CubemapDirections[CUBEMAP_SIDES] = {
  34. DDS_CUBEMAP_POSITIVEX,
  35. DDS_CUBEMAP_NEGATIVEX,
  36. DDS_CUBEMAP_POSITIVEY,
  37. DDS_CUBEMAP_NEGATIVEY,
  38. DDS_CUBEMAP_POSITIVEZ,
  39. DDS_CUBEMAP_NEGATIVEZ
  40. };
  41. //! Checks if the file specified in FileName is a valid .dds file.
  42. ILboolean ilIsValid_DDS(ILconst_string FileName)
  43. {
  44. ILHANDLE DdsFile;
  45. ILboolean bDds = IL_FALSE;
  46. if (!iCheckExtension(FileName, IL_TEXT("dds"))) {
  47. ilSetError(IL_INVALID_EXTENSION);
  48. return bDds;
  49. }
  50. DdsFile = iopenr(FileName);
  51. if (DdsFile == NULL) {
  52. ilSetError(IL_COULD_NOT_OPEN_FILE);
  53. return bDds;
  54. }
  55. bDds = ilIsValidF_DDS(DdsFile);
  56. icloser(DdsFile);
  57. return bDds;
  58. }
  59. //! Checks if the ILHANDLE contains a valid .dds file at the current position.
  60. ILboolean ilIsValidF_DDS(ILHANDLE File)
  61. {
  62. ILuint FirstPos;
  63. ILboolean bRet;
  64. iSetInputFile(File);
  65. FirstPos = itell();
  66. bRet = iIsValidDds();
  67. iseek(FirstPos, IL_SEEK_SET);
  68. return bRet;
  69. }
  70. //! Checks if Lump is a valid .dds lump.
  71. ILboolean ilIsValidL_DDS(const void *Lump, ILuint Size)
  72. {
  73. iSetInputLump(Lump, Size);
  74. return iIsValidDds();
  75. }
  76. // Internal function used to get the .dds header from the current file.
  77. ILboolean iGetDdsHead(DDSHEAD *Header)
  78. {
  79. ILint i;
  80. iread(&Header->Signature, 1, 4);
  81. Header->Size1 = GetLittleUInt();
  82. Header->Flags1 = GetLittleUInt();
  83. Header->Height = GetLittleUInt();
  84. Header->Width = GetLittleUInt();
  85. Header->LinearSize = GetLittleUInt();
  86. Header->Depth = GetLittleUInt();
  87. Header->MipMapCount = GetLittleUInt();
  88. Header->AlphaBitDepth = GetLittleUInt();
  89. for (i = 0; i < 10; ++i)
  90. Header->NotUsed[i] = GetLittleUInt();
  91. Header->Size2 = GetLittleUInt();
  92. Header->Flags2 = GetLittleUInt();
  93. Header->FourCC = GetLittleUInt();
  94. Header->RGBBitCount = GetLittleUInt();
  95. Header->RBitMask = GetLittleUInt();
  96. Header->GBitMask = GetLittleUInt();
  97. Header->BBitMask = GetLittleUInt();
  98. Header->RGBAlphaBitMask = GetLittleUInt();
  99. Header->ddsCaps1 = GetLittleUInt();
  100. Header->ddsCaps2 = GetLittleUInt();
  101. Header->ddsCaps3 = GetLittleUInt();
  102. Header->ddsCaps4 = GetLittleUInt();
  103. Header->TextureStage = GetLittleUInt();
  104. if (Head.Depth == 0)
  105. Head.Depth = 1;
  106. return IL_TRUE;
  107. }
  108. // Internal function to get the header and check it.
  109. ILboolean iIsValidDds()
  110. {
  111. ILboolean IsValid;
  112. DDSHEAD Head;
  113. iGetDdsHead(&Head);
  114. iseek(-(ILint)sizeof(DDSHEAD), IL_SEEK_CUR);  // Go ahead and restore to previous state
  115. IsValid = iCheckDds(&Head);
  116. return IsValid;
  117. }
  118. // Internal function used to check if the HEADER is a valid .dds header.
  119. ILboolean iCheckDds(DDSHEAD *Head)
  120. {
  121. if (strncmp((const char*)Head->Signature, "DDS ", 4))
  122. return IL_FALSE;
  123. //note that if Size1 is "DDS " this is not a valid dds file according
  124. //to the file spec. Some broken tool out there seems to produce files
  125. //with this value in the size field, so we support reading them...
  126. if (Head->Size1 != 124 && Head->Size1 != IL_MAKEFOURCC('D', 'D', 'S', ' '))
  127. return IL_FALSE;
  128. if (Head->Size2 != 32)
  129. return IL_FALSE;
  130. if (Head->Width == 0 || Head->Height == 0)
  131. return IL_FALSE;
  132. return IL_TRUE;
  133. }
  134. //! Reads a .dds file
  135. ILboolean ilLoad_DDS(ILconst_string FileName)
  136. {
  137. ILHANDLE DdsFile;
  138. ILboolean bDds = IL_FALSE;
  139. DdsFile = iopenr(FileName);
  140. if (DdsFile == NULL) {
  141. ilSetError(IL_COULD_NOT_OPEN_FILE);
  142. return bDds;
  143. }
  144. bDds = ilLoadF_DDS(DdsFile);
  145. icloser(DdsFile);
  146. return bDds;
  147. }
  148. //! Reads an already-opened .dds file
  149. ILboolean ilLoadF_DDS(ILHANDLE File)
  150. {
  151. ILuint FirstPos;
  152. ILboolean bRet;
  153. iSetInputFile(File);
  154. FirstPos = itell();
  155. bRet = iLoadDdsInternal();
  156. iseek(FirstPos, IL_SEEK_SET);
  157. return bRet;
  158. }
  159. //! Reads from a memory "lump" that contains a .dds
  160. ILboolean ilLoadL_DDS(const void *Lump, ILuint Size)
  161. {
  162. iSetInputLump(Lump, Size);
  163. return iLoadDdsInternal();
  164. }
  165. ILubyte iCompFormatToBpp(ILenum Format)
  166. {
  167. //non-compressed (= non-FOURCC) codes
  168. if (Format == PF_LUMINANCE || Format == PF_LUMINANCE_ALPHA || Format == PF_ARGB)
  169. return Head.RGBBitCount/8;
  170. //fourcc formats
  171. else if (Format == PF_RGB || Format == PF_3DC || Format == PF_RXGB)
  172. return 3;
  173. else if (Format == PF_ATI1N)
  174. return 1;
  175. else if (Format == PF_R16F)
  176. return 2;
  177. else if (Format == PF_A16B16G16R16 || Format == PF_A16B16G16R16F
  178. || Format == PF_G32R32F)
  179. return 8;
  180. else if (Format == PF_A32B32G32R32F)
  181. return 16;
  182. else //if (Format == PF_G16R16F || Format == PF_R32F || dxt)
  183. return 4;
  184. }
  185. ILubyte iCompFormatToBpc(ILenum Format)
  186. {
  187. if (Format == PF_R16F || Format == PF_G16R16F || Format == PF_A16B16G16R16F)
  188. //DevIL has no internal half type, so these formats are converted to 32 bits
  189. return 4;
  190. else if (Format == PF_R32F || Format == PF_G32R32F || Format == PF_A32B32G32R32F)
  191. return 4;
  192. else if(Format == PF_A16B16G16R16)
  193. return 2;
  194. else
  195. return 1;
  196. }
  197. ILubyte iCompFormatToChannelCount(ILenum Format)
  198. {
  199. if (Format == PF_RGB || Format == PF_3DC || Format == PF_RXGB)
  200. return 3;
  201. else if (Format == PF_LUMINANCE || Format == PF_R16F || Format == PF_R32F || Format == PF_ATI1N)
  202. return 1;
  203. else if (Format == PF_LUMINANCE_ALPHA || Format == PF_G16R16F || Format == PF_G32R32F)
  204. return 2;
  205. else //if(Format == PF_ARGB || dxt)
  206. return 4;
  207. }
  208. ILboolean iLoadDdsCubemapInternal(ILuint CompFormat)
  209. {
  210. ILuint i;
  211. ILubyte Bpp, Channels, Bpc;
  212. ILimage *startImage;
  213. CompData = NULL;
  214. Bpp = iCompFormatToBpp(CompFormat);
  215. Channels = iCompFormatToChannelCount(CompFormat);
  216. Bpc = iCompFormatToBpc(CompFormat);
  217. if (CompFormat == PF_LUMINANCE && Head.RGBBitCount == 16 && Head.RBitMask == 0xFFFF) { //@TODO: This is a HACK.
  218. Bpc = 2; Bpp = 2;
  219. }
  220. startImage = Image;
  221. // Run through cube map possibilities
  222. for (i = 0; i < CUBEMAP_SIDES; i++) {
  223. // Reset each time
  224. Width = Head.Width;
  225. Height = Head.Height;
  226. Depth = Head.Depth;
  227. if (Head.ddsCaps2 & CubemapDirections[i]) {
  228. if (i != 0) {
  229. Image->Faces = ilNewImage(Width, Height, Depth, Channels, Bpc);
  230. if (Image->Faces == NULL)
  231. return IL_FALSE;
  232. Image = Image->Faces;
  233. if (CompFormat == PF_R16F
  234. || CompFormat == PF_G16R16F
  235. || CompFormat == PF_A16B16G16R16F
  236. || CompFormat == PF_R32F
  237. || CompFormat == PF_G32R32F
  238. || CompFormat == PF_A32B32G32R32F) {
  239. // DevIL's format autodetection doesn't work for
  240. //  float images...correct this.
  241. Image->Type = IL_FLOAT;
  242. Image->Bpp = Channels;
  243. }
  244. ilBindImage(ilGetCurName()); // Set to parent image first.
  245. //ilActiveImage(i); //@TODO: now Image == iCurImage...globals SUCK, fix this!!!
  246. ilActiveFace(i);
  247. }
  248. if (!ReadData())
  249. return IL_FALSE;
  250. if (!AllocImage(CompFormat)) {
  251. if (CompData) {
  252. ifree(CompData);
  253. CompData = NULL;
  254. }
  255. return IL_FALSE;
  256. }
  257. Image->CubeFlags = CubemapDirections[i];
  258. if (!DdsDecompress(CompFormat)) {
  259. if (CompData) {
  260. ifree(CompData);
  261. CompData = NULL;
  262. }
  263. return IL_FALSE;
  264. }
  265. if (!ReadMipmaps(CompFormat)) {
  266. if (CompData) {
  267. ifree(CompData);
  268. CompData = NULL;
  269. }
  270. return IL_FALSE;
  271. }
  272. }
  273. }
  274. if (CompData) {
  275. ifree(CompData);
  276. CompData = NULL;
  277. }
  278. ilBindImage(ilGetCurName());  // Set to parent image first.
  279. return ilFixImage();
  280. }
  281. ILboolean iLoadDdsInternal()
  282. {
  283. ILuint BlockSize = 0;
  284. ILuint CompFormat;
  285. CompData = NULL;
  286. Image = NULL;
  287. if (iCurImage == NULL) {
  288. ilSetError(IL_ILLEGAL_OPERATION);
  289. return IL_FALSE;
  290. }
  291. if (!iGetDdsHead(&Head)) {
  292. ilSetError(IL_INVALID_FILE_HEADER);
  293. return IL_FALSE;
  294. }
  295. if (!iCheckDds(&Head)) {
  296. ilSetError(IL_INVALID_FILE_HEADER);
  297. return IL_FALSE;
  298. }
  299. BlockSize = DecodePixelFormat(&CompFormat);
  300. if (CompFormat == PF_UNKNOWN) {
  301. ilSetError(IL_INVALID_FILE_HEADER);
  302. return IL_FALSE;
  303. }
  304. // Microsoft bug, they're not following their own documentation.
  305. if (!(Head.Flags1 & (DDS_LINEARSIZE | DDS_PITCH))
  306. || Head.LinearSize == 0) {
  307. Head.Flags1 |= DDS_LINEARSIZE;
  308. Head.LinearSize = BlockSize;
  309. }
  310. Image = iCurImage;
  311. if (Head.ddsCaps1 & DDS_COMPLEX) {
  312. if (Head.ddsCaps2 & DDS_CUBEMAP) {
  313. if (!iLoadDdsCubemapInternal(CompFormat))
  314. return IL_FALSE;
  315. return IL_TRUE;
  316. }
  317. }
  318. Width = Head.Width;
  319. Height = Head.Height;
  320. Depth = Head.Depth;
  321. AdjustVolumeTexture(&Head, CompFormat);
  322. if (!ReadData())
  323. return IL_FALSE;
  324. if (!AllocImage(CompFormat)) {
  325. if (CompData) {
  326. ifree(CompData);
  327. CompData = NULL;
  328. }
  329. return IL_FALSE;
  330. }
  331. if (!DdsDecompress(CompFormat)) {
  332. if (CompData) {
  333. ifree(CompData);
  334. CompData = NULL;
  335. }
  336. return IL_FALSE;
  337. }
  338. if (!ReadMipmaps(CompFormat)) {
  339. if (CompData) {
  340. ifree(CompData);
  341. CompData = NULL;
  342. }
  343. return IL_FALSE;
  344. }
  345. if (CompData) {
  346. ifree(CompData);
  347. CompData = NULL;
  348. }
  349. ilBindImage(ilGetCurName());  // Set to parent image first.
  350. return ilFixImage();
  351. }
  352. ILuint DecodePixelFormat(ILuint *CompFormat)
  353. {
  354. ILuint BlockSize;
  355. if (Head.Flags2 & DDS_FOURCC) {
  356. BlockSize = ((Head.Width + 3)/4) * ((Head.Height + 3)/4) * Head.Depth;
  357. switch (Head.FourCC)
  358. {
  359. case IL_MAKEFOURCC('D','X','T','1'):
  360. *CompFormat = PF_DXT1;
  361. BlockSize *= 8;
  362. break;
  363. case IL_MAKEFOURCC('D','X','T','2'):
  364. *CompFormat = PF_DXT2;
  365. BlockSize *= 16;
  366. break;
  367. case IL_MAKEFOURCC('D','X','T','3'):
  368. *CompFormat = PF_DXT3;
  369. BlockSize *= 16;
  370. break;
  371. case IL_MAKEFOURCC('D','X','T','4'):
  372. *CompFormat = PF_DXT4;
  373. BlockSize *= 16;
  374. break;
  375. case IL_MAKEFOURCC('D','X','T','5'):
  376. *CompFormat = PF_DXT5;
  377. BlockSize *= 16;
  378. break;
  379. case IL_MAKEFOURCC('A', 'T', 'I', '1'):
  380. *CompFormat = PF_ATI1N;
  381. BlockSize *= 8;
  382. break;
  383. case IL_MAKEFOURCC('A', 'T', 'I', '2'):
  384. *CompFormat = PF_3DC;
  385. BlockSize *= 16;
  386. break;
  387. case IL_MAKEFOURCC('R', 'X', 'G', 'B'):
  388. *CompFormat = PF_RXGB;
  389. BlockSize *= 16;
  390. break;
  391. case IL_MAKEFOURCC('$', '', '', ''):
  392. *CompFormat = PF_A16B16G16R16;
  393. BlockSize = Head.Width * Head.Height * Head.Depth * 8;
  394. break;
  395. case IL_MAKEFOURCC('o', '', '', ''):
  396. *CompFormat = PF_R16F;
  397. BlockSize = Head.Width * Head.Height * Head.Depth * 2;
  398. break;
  399. case IL_MAKEFOURCC('p', '', '', ''):
  400. *CompFormat = PF_G16R16F;
  401. BlockSize = Head.Width * Head.Height * Head.Depth * 4;
  402. break;
  403. case IL_MAKEFOURCC('q', '', '', ''):
  404. *CompFormat = PF_A16B16G16R16F;
  405. BlockSize = Head.Width * Head.Height * Head.Depth * 8;
  406. break;
  407. case IL_MAKEFOURCC('r', '', '', ''):
  408. *CompFormat = PF_R32F;
  409. BlockSize = Head.Width * Head.Height * Head.Depth * 4;
  410. break;
  411. case IL_MAKEFOURCC('s', '', '', ''):
  412. *CompFormat = PF_G32R32F;
  413. BlockSize = Head.Width * Head.Height * Head.Depth * 8;
  414. break;
  415. case IL_MAKEFOURCC('t', '', '', ''):
  416. *CompFormat = PF_A32B32G32R32F;
  417. BlockSize = Head.Width * Head.Height * Head.Depth * 16;
  418. break;
  419. default:
  420. *CompFormat = PF_UNKNOWN;
  421. BlockSize *= 16;
  422. break;
  423. }
  424. } else {
  425. // This dds texture isn't compressed so write out ARGB or luminance format
  426. if (Head.Flags2 & DDS_LUMINANCE) {
  427. if (Head.Flags2 & DDS_ALPHAPIXELS) {
  428. *CompFormat = PF_LUMINANCE_ALPHA;
  429. } else {
  430. *CompFormat = PF_LUMINANCE;
  431. }
  432. }
  433. else {
  434. if (Head.Flags2 & DDS_ALPHAPIXELS) {
  435. *CompFormat = PF_ARGB;
  436. } else {
  437. *CompFormat = PF_RGB;
  438. }
  439. }
  440. BlockSize = (Head.Width * Head.Height * Head.Depth * (Head.RGBBitCount >> 3));
  441. }
  442. return BlockSize;
  443. }
  444. // The few volume textures that I have don't have consistent LinearSize
  445. // entries, even though the DDS_LINEARSIZE flag is set.
  446. void AdjustVolumeTexture(DDSHEAD *Head, ILuint CompFormat)
  447. {
  448. if (Head->Depth <= 1)
  449. return;
  450. // All volume textures I've seem so far didn't have the DDS_COMPLEX flag set,
  451. // even though this is normally required. But because noone does set it,
  452. // also read images without it (TODO: check file size for 3d texture?)
  453. if (/*!(Head->ddsCaps1 & DDS_COMPLEX) ||*/ !(Head->ddsCaps2 & DDS_VOLUME)) {
  454. Head->Depth = 1;
  455. Depth = 1;
  456. }
  457. switch (CompFormat)
  458. {
  459. case PF_ARGB:
  460. case PF_RGB:
  461. case PF_LUMINANCE:
  462. case PF_LUMINANCE_ALPHA:
  463. //don't use the iCompFormatToBpp() function because this way
  464. //argb images with only 8 bits (eg. a1r2g3b2) work as well
  465. Head->LinearSize = IL_MAX(1,Head->Width) * IL_MAX(1,Head->Height) *
  466. (Head->RGBBitCount / 8);
  467. break;
  468. case PF_DXT1:
  469. case PF_ATI1N:
  470. Head->LinearSize = ((Head->Width+3)/4) * ((Head->Height+3)/4) * 8;
  471. break;
  472. case PF_DXT2:
  473. case PF_DXT3:
  474. case PF_DXT4:
  475. case PF_DXT5:
  476. case PF_3DC:
  477. case PF_RXGB:
  478. Head->LinearSize = ((Head->Width+3)/4) * ((Head->Height+3)/4) * 16;
  479. break;
  480. case PF_A16B16G16R16:
  481. case PF_R16F:
  482. case PF_G16R16F:
  483. case PF_A16B16G16R16F:
  484. case PF_R32F:
  485. case PF_G32R32F:
  486. case PF_A32B32G32R32F:
  487. Head->LinearSize = IL_MAX(1,Head->Width) * IL_MAX(1,Head->Height) *
  488. iCompFormatToBpp(CompFormat);
  489. break;
  490. }
  491. Head->Flags1 |= DDS_LINEARSIZE;
  492. Head->LinearSize *= Head->Depth;
  493. return;
  494. }
  495. // Reads the compressed data
  496. ILboolean ReadData()
  497. {
  498. ILuint Bps;
  499. ILint y, z;
  500. ILubyte *Temp;
  501. if (CompData) {
  502. ifree(CompData);
  503. CompData = NULL;
  504. }
  505. if (Head.Flags1 & DDS_LINEARSIZE) {
  506. //Head.LinearSize = Head.LinearSize * Depth;
  507. CompData = (ILubyte*)ialloc(Head.LinearSize);
  508. if (CompData == NULL) {
  509. return IL_FALSE;
  510. }
  511. if (iread(CompData, 1, Head.LinearSize) != (ILuint)Head.LinearSize) {
  512. ifree(CompData);
  513. CompData = NULL;
  514. return IL_FALSE;
  515. }
  516. }
  517. else {
  518. Bps = Width * Head.RGBBitCount / 8;
  519. CompSize = Bps * Height * Depth;
  520. CompData = (ILubyte*)ialloc(CompSize);
  521. if (CompData == NULL) {
  522. return IL_FALSE;
  523. }
  524. Temp = CompData;
  525. for (z = 0; z < Depth; z++) {
  526. for (y = 0; y < Height; y++) {
  527. if (iread(Temp, 1, Bps) != Bps) {
  528. ifree(CompData);
  529. CompData = NULL;
  530. return IL_FALSE;
  531. }
  532. Temp += Bps;
  533. }
  534. }
  535. }
  536. return IL_TRUE;
  537. }
  538. ILboolean AllocImage(ILuint CompFormat)
  539. {
  540. ILubyte channels = 4;
  541. ILenum format = IL_RGBA;
  542. switch (CompFormat)
  543. {
  544. case PF_RGB:
  545. if (!ilTexImage(Width, Height, Depth, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL))
  546. return IL_FALSE;
  547. break;
  548. case PF_ARGB:
  549. if (!ilTexImage(Width, Height, Depth, 4, IL_RGBA, IL_UNSIGNED_BYTE, NULL))
  550. return IL_FALSE;
  551. break;
  552. case PF_LUMINANCE:
  553. if(Head.RGBBitCount == 16 && Head.RBitMask == 0xFFFF) { //HACK
  554. if (!ilTexImage(Width, Height, Depth, 1, IL_LUMINANCE, IL_UNSIGNED_SHORT, NULL))
  555. return IL_FALSE;
  556. }
  557. else
  558. if (!ilTexImage(Width, Height, Depth, 1, IL_LUMINANCE, IL_UNSIGNED_BYTE, NULL))
  559. return IL_FALSE;
  560. break;
  561. case PF_LUMINANCE_ALPHA:
  562. if (!ilTexImage(Width, Height, Depth, 2, IL_LUMINANCE_ALPHA, IL_UNSIGNED_BYTE, NULL))
  563. return IL_FALSE;
  564. break;
  565. case PF_ATI1N:
  566. //right now there's no OpenGL api to use the compressed 3dc data, so
  567. //throw it away (I don't know how DirectX works, though)?
  568. if (!ilTexImage(Width, Height, Depth, 1, IL_LUMINANCE, IL_UNSIGNED_BYTE, NULL))
  569. return IL_FALSE;
  570. break;
  571. case PF_3DC:
  572. //right now there's no OpenGL api to use the compressed 3dc data, so
  573. //throw it away (I don't know how DirectX works, though)?
  574. if (!ilTexImage(Width, Height, Depth, 3, IL_RGB, IL_UNSIGNED_BYTE, NULL))
  575. return IL_FALSE;
  576. break;
  577. case PF_A16B16G16R16:
  578. if (!ilTexImage(Width, Height, Depth, iCompFormatToChannelCount(CompFormat),
  579. ilGetFormatBpp(iCompFormatToChannelCount(CompFormat)), IL_UNSIGNED_SHORT, NULL))
  580. return IL_FALSE;
  581. break;
  582. case PF_R16F:
  583. case PF_G16R16F:
  584. case PF_A16B16G16R16F:
  585. case PF_R32F:
  586. case PF_G32R32F:
  587. case PF_A32B32G32R32F:
  588. if (!ilTexImage(Width, Height, Depth, iCompFormatToChannelCount(CompFormat),
  589. ilGetFormatBpp(iCompFormatToChannelCount(CompFormat)), IL_FLOAT, NULL))
  590. return IL_FALSE;
  591. break;
  592. default:
  593. if (CompFormat == PF_RXGB) {
  594. channels = 3; //normal map
  595. format = IL_RGB;
  596. }
  597. if (!ilTexImage(Width, Height, Depth, channels, format, IL_UNSIGNED_BYTE, NULL))
  598. return IL_FALSE;
  599. if (ilGetInteger(IL_KEEP_DXTC_DATA) == IL_TRUE && CompData) {
  600. iCurImage->DxtcData = (ILubyte*)ialloc(Head.LinearSize);
  601. if (iCurImage->DxtcData == NULL)
  602. return IL_FALSE;
  603. iCurImage->DxtcFormat = CompFormat - PF_DXT1 + IL_DXT1;
  604. iCurImage->DxtcSize = Head.LinearSize;
  605. memcpy(iCurImage->DxtcData, CompData, iCurImage->DxtcSize);
  606. }
  607. break;
  608. }
  609. Image->Origin = IL_ORIGIN_UPPER_LEFT;
  610. return IL_TRUE;
  611. }
  612. /*
  613.  * Assumes that the global variable CompFormat stores the format of the
  614.  * global pointer CompData (that's the pointer to the compressed data).
  615.  * Decompresses this data into Image->Data, returns if it was successful.
  616.  * It also uses the globals Width and Height.
  617.  *
  618.  * Assumes that iCurImage has valid Width, Height, Depth, Data, SizeOfData,
  619.  * Bpp, Bpc, Bps, SizeOfPlane, Format and Type fields. It is more or
  620.  * less assumed that the image has u8 rgba data (perhaps not for float
  621.  * images...)
  622.  *
  623.  *
  624.  * @TODO: don't use globals, clean this function (and this file) up
  625.  */
  626. ILboolean DdsDecompress(ILuint CompFormat)
  627. {
  628. switch (CompFormat)
  629. {
  630. case PF_ARGB:
  631. case PF_RGB:
  632. case PF_LUMINANCE:
  633. case PF_LUMINANCE_ALPHA:
  634. return DecompressARGB();
  635. case PF_DXT1:
  636. return DecompressDXT1(Image, CompData);
  637. case PF_DXT2:
  638. return DecompressDXT2(Image, CompData);
  639. case PF_DXT3:
  640. return DecompressDXT3(Image, CompData);
  641. case PF_DXT4:
  642. return DecompressDXT4(Image, CompData);
  643. case PF_DXT5:
  644. return DecompressDXT5(Image, CompData);
  645. case PF_ATI1N:
  646. return DecompressAti1n();
  647. case PF_3DC:
  648. return Decompress3Dc();
  649. case PF_RXGB:
  650. return DecompressRXGB();
  651. case PF_A16B16G16R16:
  652. memcpy(Image->Data, CompData, Image->SizeOfData);
  653. return IL_TRUE;
  654. case PF_R16F:
  655. case PF_G16R16F:
  656. case PF_A16B16G16R16F:
  657. case PF_R32F:
  658. case PF_G32R32F:
  659. case PF_A32B32G32R32F:
  660. return DecompressFloat(CompFormat);
  661. case PF_UNKNOWN:
  662. return IL_FALSE;
  663. }
  664. return IL_FALSE;
  665. }
  666. ILboolean ReadMipmaps(ILuint CompFormat)
  667. {
  668. ILuint i, CompFactor=0;
  669. ILubyte Bpp, Channels, Bpc;
  670. ILimage *StartImage, *TempImage;
  671. ILuint LastLinear;
  672. ILuint minW, minH;
  673. ILboolean isCompressed = IL_FALSE;
  674. Bpp = iCompFormatToBpp(CompFormat);
  675. Channels = iCompFormatToChannelCount(CompFormat);
  676. Bpc = iCompFormatToBpc(CompFormat);
  677. if (CompFormat == PF_LUMINANCE && Head.RGBBitCount == 16 && Head.RBitMask == 0xFFFF) { //HACK
  678. Bpc = 2; Bpp = 2;
  679. }
  680. //This doesn't work for images which first mipmap (= the image
  681. //itself) has width or height < 4
  682. //if (Head.Flags1 & DDS_LINEARSIZE) {
  683. // CompFactor = (Width * Height * Depth * Bpp) / Head.LinearSize;
  684. //}
  685. switch (CompFormat)
  686. {
  687. case PF_DXT1:
  688. //This is officially 6, we have 8 here because DXT1 may contain alpha
  689. CompFactor = 8;
  690. break;
  691. case PF_DXT2:
  692. case PF_DXT3:
  693. case PF_DXT4:
  694. case PF_DXT5:
  695. CompFactor = 4;
  696. break;
  697. case PF_RXGB:
  698. case PF_3DC:
  699. //This is officially 4 for 3dc, but that's bullshit :) There's no
  700. //alpha data in 3dc images
  701. CompFactor = 3;
  702. break;
  703. case PF_ATI1N:
  704. CompFactor = 2;
  705. break;
  706. default:
  707. CompFactor = 1;
  708. }
  709. StartImage = Image;
  710. if (!(Head.Flags1 & DDS_MIPMAPCOUNT) || Head.MipMapCount == 0) {
  711. //some .dds-files have their mipmap flag set,
  712. //but a mipmapcount of 0. Because mipMapCount is an uint, 0 - 1 gives
  713. //overflow - don't let this happen:
  714. Head.MipMapCount = 1;
  715. }
  716. LastLinear = Head.LinearSize;
  717. for (i = 0; i < Head.MipMapCount - 1; i++) {
  718. Depth = Depth / 2;
  719. Width = Width / 2;
  720. Height = Height / 2;
  721. if (Depth == 0) 
  722. Depth = 1;
  723. if (Width == 0) 
  724. Width = 1;
  725. if (Height == 0) 
  726. Height = 1;
  727. Image->Mipmaps = ilNewImage(Width, Height, Depth, Channels, Bpc);
  728. if (Image->Mipmaps == NULL)
  729. goto mip_fail;
  730. Image = Image->Mipmaps;
  731. Image->Origin = IL_ORIGIN_UPPER_LEFT;
  732. if (Head.Flags1 & DDS_LINEARSIZE) {
  733. if (CompFormat == PF_R16F
  734. || CompFormat == PF_G16R16F
  735. || CompFormat == PF_A16B16G16R16F
  736. || CompFormat == PF_R32F
  737. || CompFormat == PF_G32R32F
  738. || CompFormat == PF_A32B32G32R32F) {
  739. Head.LinearSize = Width * Height * Depth * Bpp;
  740. //DevIL's format autodetection doesn't work for
  741. //float images...correct this
  742. Image->Type = IL_FLOAT;
  743. Image->Bpp = Channels;
  744. }
  745. else if (CompFormat == PF_A16B16G16R16)
  746. Head.LinearSize = Width * Height * Depth * Bpp;
  747. else if (CompFormat != PF_RGB && CompFormat != PF_ARGB
  748. && CompFormat != PF_LUMINANCE
  749. && CompFormat != PF_LUMINANCE_ALPHA) {
  750. //compressed format
  751. minW = (((Width+3)/4))*4;
  752. minH = (((Height+3)/4))*4;
  753. Head.LinearSize = (minW * minH * Depth * Bpp) / CompFactor;
  754. isCompressed = IL_TRUE;
  755. }
  756. else {
  757. //don't use Bpp to support argb images with less than 32 bits
  758. Head.LinearSize = Width * Height * Depth * (Head.RGBBitCount >> 3);
  759. }
  760. }
  761. else {
  762. Head.LinearSize >>= 1;
  763. }
  764. if (!ReadData())
  765. goto mip_fail;
  766. if (ilGetInteger(IL_KEEP_DXTC_DATA) == IL_TRUE && isCompressed == IL_TRUE && CompData) {
  767. Image->DxtcData = (ILubyte*)ialloc(Head.LinearSize);
  768. if (Image->DxtcData == NULL)
  769. return IL_FALSE;
  770. Image->DxtcFormat = CompFormat - PF_DXT1 + IL_DXT1;
  771. Image->DxtcSize = Head.LinearSize;
  772. memcpy(Image->DxtcData, CompData, Image->DxtcSize);
  773. }
  774. if (!DdsDecompress(CompFormat))
  775. goto mip_fail;
  776. }
  777. Head.LinearSize = LastLinear;
  778. Image = StartImage;
  779. return IL_TRUE;
  780. mip_fail:
  781. Image = StartImage;
  782. StartImage = StartImage->Mipmaps;
  783. while (StartImage) {
  784. TempImage = StartImage;
  785. StartImage = StartImage->Mipmaps;
  786. ifree(TempImage);
  787. }
  788. Image->Mipmaps = NULL;
  789. return IL_FALSE;
  790. }
  791. void DxtcReadColors(const ILubyte* Data, Color8888* Out)
  792. {
  793. ILubyte r0, g0, b0, r1, g1, b1;
  794. b0 = Data[0] & 0x1F;
  795. g0 = ((Data[0] & 0xE0) >> 5) | ((Data[1] & 0x7) << 3);
  796. r0 = (Data[1] & 0xF8) >> 3;
  797. b1 = Data[2] & 0x1F;
  798. g1 = ((Data[2] & 0xE0) >> 5) | ((Data[3] & 0x7) << 3);
  799. r1 = (Data[3] & 0xF8) >> 3;
  800. Out[0].r = r0 << 3 | r0 >> 2;
  801. Out[0].g = g0 << 2 | g0 >> 3;
  802. Out[0].b = b0 << 3 | b0 >> 2;
  803. Out[1].r = r1 << 3 | r1 >> 2;
  804. Out[1].g = g1 << 2 | g1 >> 3;
  805. Out[1].b = b1 << 3 | b1 >> 2;
  806. }
  807. //@TODO: Probably not safe on Big Endian.
  808. void DxtcReadColor(ILushort Data, Color8888* Out)
  809. {
  810. ILubyte r, g, b;
  811. b = Data & 0x1f;
  812. g = (Data & 0x7E0) >> 5;
  813. r = (Data & 0xF800) >> 11;
  814. Out->r = r << 3 | r >> 2;
  815. Out->g = g << 2 | g >> 3;
  816. Out->b = b << 3 | r >> 2;
  817. }
  818. ILboolean DecompressDXT1(ILimage *lImage, ILubyte *lCompData)
  819. {
  820. ILuint x, y, z, i, j, k, Select;
  821. ILubyte *Temp;
  822. Color8888 colours[4], *col;
  823. ILushort color_0, color_1;
  824. ILuint bitmask, Offset;
  825. if (!lCompData)
  826. return IL_FALSE;
  827. Temp = lCompData;
  828. colours[0].a = 0xFF;
  829. colours[1].a = 0xFF;
  830. colours[2].a = 0xFF;
  831. //colours[3].a = 0xFF;
  832. for (z = 0; z < lImage->Depth; z++) {
  833. for (y = 0; y < lImage->Height; y += 4) {
  834. for (x = 0; x < lImage->Width; x += 4) {
  835. color_0 = *((ILushort*)Temp);
  836. UShort(&color_0);
  837. color_1 = *((ILushort*)(Temp + 2));
  838. UShort(&color_1);
  839. DxtcReadColor(color_0, colours);
  840. DxtcReadColor(color_1, colours + 1);
  841. bitmask = ((ILuint*)Temp)[1];
  842. UInt(&bitmask);
  843. Temp += 8;
  844. if (color_0 > color_1) {
  845. // Four-color block: derive the other two colors.
  846. // 00 = color_0, 01 = color_1, 10 = color_2, 11 = color_3
  847. // These 2-bit codes correspond to the 2-bit fields 
  848. // stored in the 64-bit block.
  849. colours[2].b = (2 * colours[0].b + colours[1].b + 1) / 3;
  850. colours[2].g = (2 * colours[0].g + colours[1].g + 1) / 3;
  851. colours[2].r = (2 * colours[0].r + colours[1].r + 1) / 3;
  852. //colours[2].a = 0xFF;
  853. colours[3].b = (colours[0].b + 2 * colours[1].b + 1) / 3;
  854. colours[3].g = (colours[0].g + 2 * colours[1].g + 1) / 3;
  855. colours[3].r = (colours[0].r + 2 * colours[1].r + 1) / 3;
  856. colours[3].a = 0xFF;
  857. }
  858. else { 
  859. // Three-color block: derive the other color.
  860. // 00 = color_0,  01 = color_1,  10 = color_2,
  861. // 11 = transparent.
  862. // These 2-bit codes correspond to the 2-bit fields 
  863. // stored in the 64-bit block. 
  864. colours[2].b = (colours[0].b + colours[1].b) / 2;
  865. colours[2].g = (colours[0].g + colours[1].g) / 2;
  866. colours[2].r = (colours[0].r + colours[1].r) / 2;
  867. //colours[2].a = 0xFF;
  868. colours[3].b = (colours[0].b + 2 * colours[1].b + 1) / 3;
  869. colours[3].g = (colours[0].g + 2 * colours[1].g + 1) / 3;
  870. colours[3].r = (colours[0].r + 2 * colours[1].r + 1) / 3;
  871. colours[3].a = 0x00;
  872. }
  873. for (j = 0, k = 0; j < 4; j++) {
  874. for (i = 0; i < 4; i++, k++) {
  875. Select = (bitmask & (0x03 << k*2)) >> k*2;
  876. col = &colours[Select];
  877. if (((x + i) < lImage->Width) && ((y + j) < lImage->Height)) {
  878. Offset = z * lImage->SizeOfPlane + (y + j) * lImage->Bps + (x + i) * lImage->Bpp;
  879. lImage->Data[Offset + 0] = col->r;
  880. lImage->Data[Offset + 1] = col->g;
  881. lImage->Data[Offset + 2] = col->b;
  882. lImage->Data[Offset + 3] = col->a;
  883. }
  884. }
  885. }
  886. }
  887. }
  888. }
  889. return IL_TRUE;
  890. }
  891. ILboolean DecompressDXT2(ILimage *lImage, ILubyte *lCompData)
  892. {
  893. // Can do color & alpha same as dxt3, but color is pre-multiplied 
  894. //   so the result will be wrong unless corrected. 
  895. if (!DecompressDXT3(Image, CompData))
  896. return IL_FALSE;
  897. CorrectPreMult();
  898. return IL_TRUE;
  899. }
  900. ILboolean DecompressDXT3(ILimage *lImage, ILubyte *lCompData)
  901. {
  902. ILuint x, y, z, i, j, k, Select;
  903. ILubyte *Temp;
  904. //Color565 *color_0, *color_1;
  905. Color8888 colours[4], *col;
  906. ILuint bitmask, Offset;
  907. ILushort word;
  908. ILubyte *alpha;
  909. if (!lCompData)
  910. return IL_FALSE;
  911. Temp = lCompData;
  912. for (z = 0; z < lImage->Depth; z++) {
  913. for (y = 0; y < lImage->Height; y += 4) {
  914. for (x = 0; x < lImage->Width; x += 4) {
  915. alpha = Temp;
  916. Temp += 8;
  917. DxtcReadColors(Temp, colours);
  918. bitmask = ((ILuint*)Temp)[1];
  919. UInt(&bitmask);
  920. Temp += 8;
  921. // Four-color block: derive the other two colors.    
  922. // 00 = color_0, 01 = color_1, 10 = color_2, 11 = color_3
  923. // These 2-bit codes correspond to the 2-bit fields 
  924. // stored in the 64-bit block.
  925. colours[2].b = (2 * colours[0].b + colours[1].b + 1) / 3;
  926. colours[2].g = (2 * colours[0].g + colours[1].g + 1) / 3;
  927. colours[2].r = (2 * colours[0].r + colours[1].r + 1) / 3;
  928. //colours[2].a = 0xFF;
  929. colours[3].b = (colours[0].b + 2 * colours[1].b + 1) / 3;
  930. colours[3].g = (colours[0].g + 2 * colours[1].g + 1) / 3;
  931. colours[3].r = (colours[0].r + 2 * colours[1].r + 1) / 3;
  932. //colours[3].a = 0xFF;
  933. k = 0;
  934. for (j = 0; j < 4; j++) {
  935. for (i = 0; i < 4; i++, k++) {
  936. Select = (bitmask & (0x03 << k*2)) >> k*2;
  937. col = &colours[Select];
  938. if (((x + i) < lImage->Width) && ((y + j) < lImage->Height)) {
  939. Offset = z * lImage->SizeOfPlane + (y + j) * lImage->Bps + (x + i) * lImage->Bpp;
  940. lImage->Data[Offset + 0] = col->r;
  941. lImage->Data[Offset + 1] = col->g;
  942. lImage->Data[Offset + 2] = col->b;
  943. }
  944. }
  945. }
  946. for (j = 0; j < 4; j++) {
  947. word = alpha[2*j] + 256*alpha[2*j+1];
  948. for (i = 0; i < 4; i++) {
  949. if (((x + i) < lImage->Width) && ((y + j) < lImage->Height)) {
  950. Offset = z * lImage->SizeOfPlane + (y + j) * lImage->Bps + (x + i) * lImage->Bpp + 3;
  951. lImage->Data[Offset] = word & 0x0F;
  952. lImage->Data[Offset] = lImage->Data[Offset] | (lImage->Data[Offset] << 4);
  953. }
  954. word >>= 4;
  955. }
  956. }
  957. }
  958. }
  959. }
  960. return IL_TRUE;
  961. }
  962. ILboolean DecompressDXT4(ILimage *lImage, ILubyte *lCompData)
  963. {
  964. // Can do color & alpha same as dxt5, but color is pre-multiplied 
  965. //   so the result will be wrong unless corrected. 
  966. if (!DecompressDXT5(Image, CompData))
  967. return IL_FALSE;
  968. CorrectPreMult();
  969. return IL_FALSE;
  970. }
  971. ILboolean DecompressDXT5(ILimage *lImage, ILubyte *lCompData)
  972. {
  973. ILuint x, y, z, i, j, k, Select;
  974. ILubyte *Temp; //, r0, g0, b0, r1, g1, b1;
  975. Color8888 colours[4], *col;
  976. ILuint bitmask, Offset;
  977. ILubyte alphas[8], *alphamask;
  978. ILuint bits;
  979. if (!lCompData)
  980. return IL_FALSE;
  981. Temp = lCompData;
  982. for (z = 0; z < lImage->Depth; z++) {
  983. for (y = 0; y < lImage->Height; y += 4) {
  984. for (x = 0; x < lImage->Width; x += 4) {
  985. if (y >= lImage->Height || x >= lImage->Width)
  986. break;
  987. alphas[0] = Temp[0];
  988. alphas[1] = Temp[1];
  989. alphamask = Temp + 2;
  990. Temp += 8;
  991. DxtcReadColors(Temp, colours);
  992. bitmask = ((ILuint*)Temp)[1];
  993. UInt(&bitmask);
  994. Temp += 8;
  995. // Four-color block: derive the other two colors.    
  996. // 00 = color_0, 01 = color_1, 10 = color_2, 11 = color_3
  997. // These 2-bit codes correspond to the 2-bit fields 
  998. // stored in the 64-bit block.
  999. colours[2].b = (2 * colours[0].b + colours[1].b + 1) / 3;
  1000. colours[2].g = (2 * colours[0].g + colours[1].g + 1) / 3;
  1001. colours[2].r = (2 * colours[0].r + colours[1].r + 1) / 3;
  1002. //colours[2].a = 0xFF;
  1003. colours[3].b = (colours[0].b + 2 * colours[1].b + 1) / 3;
  1004. colours[3].g = (colours[0].g + 2 * colours[1].g + 1) / 3;
  1005. colours[3].r = (colours[0].r + 2 * colours[1].r + 1) / 3;
  1006. //colours[3].a = 0xFF;
  1007. k = 0;
  1008. for (j = 0; j < 4; j++) {
  1009. for (i = 0; i < 4; i++, k++) {
  1010. Select = (bitmask & (0x03 << k*2)) >> k*2;
  1011. col = &colours[Select];
  1012. // only put pixels out < width or height
  1013. if (((x + i) < lImage->Width) && ((y + j) < lImage->Height)) {
  1014. Offset = z * lImage->SizeOfPlane + (y + j) * lImage->Bps + (x + i) * lImage->Bpp;
  1015. lImage->Data[Offset + 0] = col->r;
  1016. lImage->Data[Offset + 1] = col->g;
  1017. lImage->Data[Offset + 2] = col->b;
  1018. }
  1019. }
  1020. }
  1021. // 8-alpha or 6-alpha block?    
  1022. if (alphas[0] > alphas[1]) {    
  1023. // 8-alpha block:  derive the other six alphas.    
  1024. // Bit code 000 = alpha_0, 001 = alpha_1, others are interpolated.
  1025. alphas[2] = (6 * alphas[0] + 1 * alphas[1] + 3) / 7; // bit code 010
  1026. alphas[3] = (5 * alphas[0] + 2 * alphas[1] + 3) / 7; // bit code 011
  1027. alphas[4] = (4 * alphas[0] + 3 * alphas[1] + 3) / 7; // bit code 100
  1028. alphas[5] = (3 * alphas[0] + 4 * alphas[1] + 3) / 7; // bit code 101
  1029. alphas[6] = (2 * alphas[0] + 5 * alphas[1] + 3) / 7; // bit code 110
  1030. alphas[7] = (1 * alphas[0] + 6 * alphas[1] + 3) / 7; // bit code 111
  1031. }
  1032. else {
  1033. // 6-alpha block.
  1034. // Bit code 000 = alpha_0, 001 = alpha_1, others are interpolated.
  1035. alphas[2] = (4 * alphas[0] + 1 * alphas[1] + 2) / 5; // Bit code 010
  1036. alphas[3] = (3 * alphas[0] + 2 * alphas[1] + 2) / 5; // Bit code 011
  1037. alphas[4] = (2 * alphas[0] + 3 * alphas[1] + 2) / 5; // Bit code 100
  1038. alphas[5] = (1 * alphas[0] + 4 * alphas[1] + 2) / 5; // Bit code 101
  1039. alphas[6] = 0x00; // Bit code 110
  1040. alphas[7] = 0xFF; // Bit code 111
  1041. }
  1042. // Note: Have to separate the next two loops,
  1043. // it operates on a 6-byte system.
  1044. // First three bytes
  1045. //bits = *((ILint*)alphamask);
  1046. bits = (alphamask[0]) | (alphamask[1] << 8) | (alphamask[2] << 16);
  1047. for (j = 0; j < 2; j++) {
  1048. for (i = 0; i < 4; i++) {
  1049. // only put pixels out < width or height
  1050. if (((x + i) < lImage->Width) && ((y + j) < lImage->Height)) {
  1051. Offset = z * lImage->SizeOfPlane + (y + j) * lImage->Bps + (x + i) * lImage->Bpp + 3;
  1052. lImage->Data[Offset] = alphas[bits & 0x07];
  1053. }
  1054. bits >>= 3;
  1055. }
  1056. }
  1057. // Last three bytes
  1058. //bits = *((ILint*)&alphamask[3]);
  1059. bits = (alphamask[3]) | (alphamask[4] << 8) | (alphamask[5] << 16);
  1060. for (j = 2; j < 4; j++) {
  1061. for (i = 0; i < 4; i++) {
  1062. // only put pixels out < width or height
  1063. if (((x + i) < lImage->Width) && ((y + j) < lImage->Height)) {
  1064. Offset = z * lImage->SizeOfPlane + (y + j) * lImage->Bps + (x + i) * lImage->Bpp + 3;
  1065. lImage->Data[Offset] = alphas[bits & 0x07];
  1066. }
  1067. bits >>= 3;
  1068. }
  1069. }
  1070. }
  1071. }
  1072. }
  1073. return IL_TRUE;
  1074. }
  1075. ILboolean Decompress3Dc()
  1076. {
  1077. int x, y, z, i, j, k, t1, t2;
  1078. ILubyte *Temp, *Temp2;
  1079. ILubyte XColours[8], YColours[8];
  1080. ILuint bitmask, bitmask2, Offset, CurrOffset;
  1081. if (!CompData)
  1082. return IL_FALSE;
  1083. Temp = CompData;
  1084. Offset = 0;
  1085. for (z = 0; z < Depth; z++) {
  1086. for (y = 0; y < Height; y += 4) {
  1087. for (x = 0; x < Width; x += 4) {
  1088. Temp2 = Temp + 8;
  1089. //Read Y palette
  1090. t1 = YColours[0] = Temp[0];
  1091. t2 = YColours[1] = Temp[1];
  1092. Temp += 2;
  1093. if (t1 > t2)
  1094. for (i = 2; i < 8; ++i)
  1095. YColours[i] = t1 + ((t2 - t1)*(i - 1))/7;
  1096. else {
  1097. for (i = 2; i < 6; ++i)
  1098. YColours[i] = t1 + ((t2 - t1)*(i - 1))/5;
  1099. YColours[6] = 0;
  1100. YColours[7] = 255;
  1101. }
  1102. // Read X palette
  1103. t1 = XColours[0] = Temp2[0];
  1104. t2 = XColours[1] = Temp2[1];
  1105. Temp2 += 2;
  1106. if (t1 > t2)
  1107. for (i = 2; i < 8; ++i)
  1108. XColours[i] = t1 + ((t2 - t1)*(i - 1))/7;
  1109. else {
  1110. for (i = 2; i < 6; ++i)
  1111. XColours[i] = t1 + ((t2 - t1)*(i - 1))/5;
  1112. XColours[6] = 0;
  1113. XColours[7] = 255;
  1114. }
  1115. //decompress pixel data
  1116. CurrOffset = Offset;
  1117. for (k = 0; k < 4; k += 2) {
  1118. // First three bytes
  1119. bitmask = ((ILuint)(Temp[0]) << 0) | ((ILuint)(Temp[1]) << 8) | ((ILuint)(Temp[2]) << 16);
  1120. bitmask2 = ((ILuint)(Temp2[0]) << 0) | ((ILuint)(Temp2[1]) << 8) | ((ILuint)(Temp2[2]) << 16);
  1121. for (j = 0; j < 2; j++) {
  1122. // only put pixels out < height
  1123. if ((y + k + j) < Height) {
  1124. for (i = 0; i < 4; i++) {
  1125. // only put pixels out < width
  1126. if (((x + i) < Width)) {
  1127. ILint t, tx, ty;
  1128. t1 = CurrOffset + (x + i)*3;
  1129. Image->Data[t1 + 1] = ty = YColours[bitmask & 0x07];
  1130. Image->Data[t1 + 0] = tx = XColours[bitmask2 & 0x07];
  1131. //calculate b (z) component ((r/255)^2 + (g/255)^2 + (b/255)^2 = 1
  1132. t = 127*128 - (tx - 127)*(tx - 128) - (ty - 127)*(ty - 128);
  1133. if (t > 0)
  1134. Image->Data[t1 + 2] = (ILubyte)(iSqrt(t) + 128);
  1135. else
  1136. Image->Data[t1 + 2] = 0x7F;
  1137. }
  1138. bitmask >>= 3;
  1139. bitmask2 >>= 3;
  1140. }
  1141. CurrOffset += Image->Bps;
  1142. }
  1143. }
  1144. Temp += 3;
  1145. Temp2 += 3;
  1146. }
  1147. //skip bytes that were read via Temp2
  1148. Temp += 8;
  1149. }
  1150. Offset += Image->Bps*4;
  1151. }
  1152. }
  1153. return IL_TRUE;
  1154. }
  1155. ILboolean DecompressAti1n()
  1156. {
  1157. int x, y, z, i, j, k, t1, t2;
  1158. ILubyte *Temp;
  1159. ILubyte Colours[8];
  1160. ILuint bitmask, Offset, CurrOffset;
  1161. if (!CompData)
  1162. return IL_FALSE;
  1163. Temp = CompData;
  1164. Offset = 0;
  1165. for (z = 0; z < Depth; z++) {
  1166. for (y = 0; y < Height; y += 4) {
  1167. for (x = 0; x < Width; x += 4) {
  1168. //Read palette
  1169. t1 = Colours[0] = Temp[0];
  1170. t2 = Colours[1] = Temp[1];
  1171. Temp += 2;
  1172. if (t1 > t2)
  1173. for (i = 2; i < 8; ++i)
  1174. Colours[i] = t1 + ((t2 - t1)*(i - 1))/7;
  1175. else {
  1176. for (i = 2; i < 6; ++i)
  1177. Colours[i] = t1 + ((t2 - t1)*(i - 1))/5;
  1178. Colours[6] = 0;
  1179. Colours[7] = 255;
  1180. }
  1181. //decompress pixel data
  1182. CurrOffset = Offset;
  1183. for (k = 0; k < 4; k += 2) {
  1184. // First three bytes
  1185. bitmask = ((ILuint)(Temp[0]) << 0) | ((ILuint)(Temp[1]) << 8) | ((ILuint)(Temp[2]) << 16);
  1186. for (j = 0; j < 2; j++) {
  1187. // only put pixels out < height
  1188. if ((y + k + j) < Height) {
  1189. for (i = 0; i < 4; i++) {
  1190. // only put pixels out < width
  1191. if (((x + i) < Width)) {
  1192. t1 = CurrOffset + (x + i);
  1193. Image->Data[t1] = Colours[bitmask & 0x07];
  1194. }
  1195. bitmask >>= 3;
  1196. }
  1197. CurrOffset += Image->Bps;
  1198. }
  1199. }
  1200. Temp += 3;
  1201. }
  1202. }
  1203. Offset += Image->Bps*4;
  1204. }
  1205. }
  1206. return IL_TRUE;
  1207. }
  1208. //This is nearly exactly the same as DecompressDXT5...
  1209. //I have to clean up this file (put common code in
  1210. //helper functions etc)
  1211. ILboolean DecompressRXGB()
  1212. {
  1213. int x, y, z, i, j, k, Select;
  1214. ILubyte *Temp;
  1215. Color565 *color_0, *color_1;
  1216. Color8888 colours[4], *col;
  1217. ILuint bitmask, Offset;
  1218. ILubyte alphas[8], *alphamask;
  1219. ILuint bits;
  1220. if (!CompData)
  1221. return IL_FALSE;
  1222. Temp = CompData;
  1223. for (z = 0; z < Depth; z++) {
  1224. for (y = 0; y < Height; y += 4) {
  1225. for (x = 0; x < Width; x += 4) {
  1226. if (y >= Height || x >= Width)
  1227. break;
  1228. alphas[0] = Temp[0];
  1229. alphas[1] = Temp[1];
  1230. alphamask = Temp + 2;
  1231. Temp += 8;
  1232. color_0 = ((Color565*)Temp);
  1233. color_1 = ((Color565*)(Temp+2));
  1234. bitmask = ((ILuint*)Temp)[1];
  1235. Temp += 8;
  1236. colours[0].r = color_0->nRed << 3;
  1237. colours[0].g = color_0->nGreen << 2;
  1238. colours[0].b = color_0->nBlue << 3;
  1239. colours[0].a = 0xFF;
  1240. colours[1].r = color_1->nRed << 3;
  1241. colours[1].g = color_1->nGreen << 2;
  1242. colours[1].b = color_1->nBlue << 3;
  1243. colours[1].a = 0xFF;
  1244. // Four-color block: derive the other two colors.    
  1245. // 00 = color_0, 01 = color_1, 10 = color_2, 11 = color_3
  1246. // These 2-bit codes correspond to the 2-bit fields 
  1247. // stored in the 64-bit block.
  1248. colours[2].b = (2 * colours[0].b + colours[1].b + 1) / 3;
  1249. colours[2].g = (2 * colours[0].g + colours[1].g + 1) / 3;
  1250. colours[2].r = (2 * colours[0].r + colours[1].r + 1) / 3;
  1251. colours[2].a = 0xFF;
  1252. colours[3].b = (colours[0].b + 2 * colours[1].b + 1) / 3;
  1253. colours[3].g = (colours[0].g + 2 * colours[1].g + 1) / 3;
  1254. colours[3].r = (colours[0].r + 2 * colours[1].r + 1) / 3;
  1255. colours[3].a = 0xFF;
  1256. k = 0;
  1257. for (j = 0; j < 4; j++) {
  1258. for (i = 0; i < 4; i++, k++) {
  1259. Select = (bitmask & (0x03 << k*2)) >> k*2;
  1260. col = &colours[Select];
  1261. // only put pixels out < width or height
  1262. if (((x + i) < Width) && ((y + j) < Height)) {
  1263. Offset = z * Image->SizeOfPlane + (y + j) * Image->Bps + (x + i) * Image->Bpp;
  1264. Image->Data[Offset + 0] = col->r;
  1265. Image->Data[Offset + 1] = col->g;
  1266. Image->Data[Offset + 2] = col->b;
  1267. }
  1268. }
  1269. }
  1270. // 8-alpha or 6-alpha block?    
  1271. if (alphas[0] > alphas[1]) {    
  1272. // 8-alpha block:  derive the other six alphas.    
  1273. // Bit code 000 = alpha_0, 001 = alpha_1, others are interpolated.
  1274. alphas[2] = (6 * alphas[0] + 1 * alphas[1] + 3) / 7; // bit code 010
  1275. alphas[3] = (5 * alphas[0] + 2 * alphas[1] + 3) / 7; // bit code 011
  1276. alphas[4] = (4 * alphas[0] + 3 * alphas[1] + 3) / 7; // bit code 100
  1277. alphas[5] = (3 * alphas[0] + 4 * alphas[1] + 3) / 7; // bit code 101
  1278. alphas[6] = (2 * alphas[0] + 5 * alphas[1] + 3) / 7; // bit code 110
  1279. alphas[7] = (1 * alphas[0] + 6 * alphas[1] + 3) / 7; // bit code 111
  1280. }
  1281. else {
  1282. // 6-alpha block.
  1283. // Bit code 000 = alpha_0, 001 = alpha_1, others are interpolated.
  1284. alphas[2] = (4 * alphas[0] + 1 * alphas[1] + 2) / 5; // Bit code 010
  1285. alphas[3] = (3 * alphas[0] + 2 * alphas[1] + 2) / 5; // Bit code 011
  1286. alphas[4] = (2 * alphas[0] + 3 * alphas[1] + 2) / 5; // Bit code 100
  1287. alphas[5] = (1 * alphas[0] + 4 * alphas[1] + 2) / 5; // Bit code 101
  1288. alphas[6] = 0x00; // Bit code 110
  1289. alphas[7] = 0xFF; // Bit code 111
  1290. }
  1291. // Note: Have to separate the next two loops,
  1292. // it operates on a 6-byte system.
  1293. // First three bytes
  1294. bits = *((ILint*)alphamask);
  1295. for (j = 0; j < 2; j++) {
  1296. for (i = 0; i < 4; i++) {
  1297. // only put pixels out < width or height
  1298. if (((x + i) < Width) && ((y + j) < Height)) {
  1299. Offset = z * Image->SizeOfPlane + (y + j) * Image->Bps + (x + i) * Image->Bpp + 0;
  1300. Image->Data[Offset] = alphas[bits & 0x07];
  1301. }
  1302. bits >>= 3;
  1303. }
  1304. }
  1305. // Last three bytes
  1306. bits = *((ILint*)&alphamask[3]);
  1307. for (j = 2; j < 4; j++) {
  1308. for (i = 0; i < 4; i++) {
  1309. // only put pixels out < width or height
  1310. if (((x + i) < Width) && ((y + j) < Height)) {
  1311. Offset = z * Image->SizeOfPlane + (y + j) * Image->Bps + (x + i) * Image->Bpp + 0;
  1312. Image->Data[Offset] = alphas[bits & 0x07];
  1313. }
  1314. bits >>= 3;
  1315. }
  1316. }
  1317. }
  1318. }
  1319. }
  1320. return IL_TRUE;
  1321. }
  1322. //Taken from OpenEXR
  1323. unsigned int
  1324. halfToFloat (unsigned short y)
  1325. {
  1326. int s = (y >> 15) & 0x00000001;
  1327. int e = (y >> 10) & 0x0000001f;
  1328. int m =  y   & 0x000003ff;
  1329. if (e == 0)
  1330. {
  1331. if (m == 0)
  1332. {
  1333. //
  1334. // Plus or minus zero
  1335. //
  1336. return s << 31;
  1337. }
  1338. else
  1339. {
  1340. //
  1341. // Denormalized number -- renormalize it
  1342. //
  1343. while (!(m & 0x00000400))
  1344. {
  1345. m <<= 1;
  1346. e -=  1;
  1347. }
  1348. e += 1;
  1349. m &= ~0x00000400;
  1350. }
  1351. }
  1352. else if (e == 31)
  1353. {
  1354. if (m == 0)
  1355. {
  1356. //
  1357. // Positive or negative infinity
  1358. //
  1359. return (s << 31) | 0x7f800000;
  1360. }
  1361. else
  1362. {
  1363. //
  1364. // Nan -- preserve sign and significand bits
  1365. //
  1366. return (s << 31) | 0x7f800000 | (m << 13);
  1367. }
  1368. }
  1369. //
  1370. // Normalized number
  1371. //
  1372. e = e + (127 - 15);
  1373. m = m << 13;
  1374. //
  1375. // Assemble s, e and m.
  1376. //
  1377. return (s << 31) | (e << 23) | m;
  1378. }
  1379. ILboolean iConvFloat16ToFloat32(ILuint* dest, ILushort* src, ILuint size)
  1380. {
  1381. ILuint i;
  1382. for (i = 0; i < size; ++i, ++dest, ++src) {
  1383. //float: 1 sign bit, 8 exponent bits, 23 mantissa bits
  1384. //half: 1 sign bit, 5 exponent bits, 10 mantissa bits
  1385. *dest = halfToFloat(*src);
  1386. }
  1387. return IL_TRUE;
  1388. }
  1389. ILboolean DecompressFloat(ILuint lCompFormat)
  1390. {
  1391. switch (lCompFormat)
  1392. {
  1393. case PF_R32F:
  1394. case PF_G32R32F:
  1395. case PF_A32B32G32R32F:
  1396. memcpy(Image->Data, CompData, Image->SizeOfData);
  1397. return IL_TRUE;
  1398. case PF_R16F:
  1399. case PF_G16R16F:
  1400. case PF_A16B16G16R16F:
  1401. return iConvFloat16ToFloat32((ILuint*)Image->Data, (ILushort*)CompData,
  1402. Image->Width * Image->Height * Image->Depth * Image->Bpp);
  1403. default:
  1404. return IL_FALSE;
  1405. }
  1406. }
  1407. void CorrectPreMult()
  1408. {
  1409. ILuint i;
  1410. for (i = 0; i < Image->SizeOfData; i += 4) {
  1411. if (Image->Data[i+3] != 0) {  // Cannot divide by 0.
  1412. Image->Data[i]   = (ILubyte)(((ILuint)Image->Data[i]   << 8) / Image->Data[i+3]);
  1413. Image->Data[i+1] = (ILubyte)(((ILuint)Image->Data[i+1] << 8) / Image->Data[i+3]);
  1414. Image->Data[i+2] = (ILubyte)(((ILuint)Image->Data[i+2] << 8) / Image->Data[i+3]);
  1415. }
  1416. }
  1417. return;
  1418. }
  1419. ILboolean DecompressARGB(ILuint CompFormat) {
  1420. ILuint ReadI = 0, TempBpp, i;
  1421. ILuint RedL, RedR;
  1422. ILuint GreenL, GreenR;
  1423. ILuint BlueL, BlueR;
  1424. ILuint AlphaL, AlphaR;
  1425. ILubyte *Temp;
  1426. if (!CompData)
  1427. return IL_FALSE;
  1428. if (CompFormat == PF_LUMINANCE && Head.RGBBitCount == 16 && Head.RBitMask == 0xFFFF) { //HACK
  1429. memcpy(Image->Data, CompData, Image->SizeOfData);
  1430. return IL_TRUE;
  1431. }
  1432. GetBitsFromMask(Head.RBitMask, &RedL, &RedR);
  1433. GetBitsFromMask(Head.GBitMask, &GreenL, &GreenR);
  1434. GetBitsFromMask(Head.BBitMask, &BlueL, &BlueR);
  1435. GetBitsFromMask(Head.RGBAlphaBitMask, &AlphaL, &AlphaR);
  1436. Temp = CompData;
  1437. TempBpp = Head.RGBBitCount / 8;
  1438. for (i = 0; i < Image->SizeOfData; i += Image->Bpp) {
  1439. //@TODO: This is SLOOOW...
  1440. //but the old version crashed in release build under
  1441. //winxp (and xp is right to stop this code - I always
  1442. //wondered that it worked the old way at all)
  1443. if (Image->SizeOfData - i < 4) { //less than 4 byte to write?
  1444. if (TempBpp == 3) { //this branch is extra-SLOOOW
  1445. ReadI =
  1446. *Temp
  1447. | ((*(Temp + 1)) << 8)
  1448. | ((*(Temp + 2)) << 16);
  1449. }
  1450. else if (TempBpp == 1)
  1451. ReadI = *((ILubyte*)Temp);
  1452. else if (TempBpp == 2)
  1453. ReadI = Temp[0] | (Temp[1] << 8);
  1454. }
  1455. else
  1456. ReadI = Temp[0] | (Temp[1] << 8) | (Temp[2] << 16) | (Temp[3] << 24);
  1457. Temp += TempBpp;
  1458. Image->Data[i] = ((ReadI & Head.RBitMask) >> RedR) << RedL;
  1459. if(Image->Bpp >= 3) {
  1460. Image->Data[i+1] = ((ReadI & Head.GBitMask) >> GreenR) << GreenL;
  1461. Image->Data[i+2] = ((ReadI & Head.BBitMask) >> BlueR) << BlueL;
  1462. if (Image->Bpp == 4) {
  1463. Image->Data[i+3] = ((ReadI & Head.RGBAlphaBitMask) >> AlphaR) << AlphaL;
  1464. if (AlphaL >= 7) {
  1465. Image->Data[i+3] = Image->Data[i+3] ? 0xFF : 0x00;
  1466. }
  1467. else if (AlphaL >= 4) {
  1468. Image->Data[i+3] = Image->Data[i+3] | (Image->Data[i+3] >> 4);
  1469. }
  1470. }
  1471. }
  1472. else if (Image->Bpp == 2) {
  1473. Image->Data[i+1] = ((ReadI & Head.RGBAlphaBitMask) >> AlphaR) << AlphaL;
  1474. if (AlphaL >= 7) {
  1475. Image->Data[i+1] = Image->Data[i+1] ? 0xFF : 0x00;
  1476. }
  1477. else if (AlphaL >= 4) {
  1478. Image->Data[i+1] = Image->Data[i+1] | (Image->Data[i+3] >> 4);
  1479. }
  1480. }
  1481. }
  1482. return IL_TRUE;
  1483. }
  1484. // @TODO:  Look at using the BSF/BSR operands for inline ASM here.
  1485. void GetBitsFromMask(ILuint Mask, ILuint *ShiftLeft, ILuint *ShiftRight)
  1486. {
  1487. ILuint Temp, i;
  1488. if (Mask == 0) {
  1489. *ShiftLeft = *ShiftRight = 0;
  1490. return;
  1491. }
  1492. Temp = Mask;
  1493. for (i = 0; i < 32; i++, Temp >>= 1) {
  1494. if (Temp & 1)
  1495. break;
  1496. }
  1497. *ShiftRight = i;
  1498. // Temp is preserved, so use it again:
  1499. for (i = 0; i < 8; i++, Temp >>= 1) {
  1500. if (!(Temp & 1))
  1501. break;
  1502. }
  1503. *ShiftLeft = 8 - i;
  1504. return;
  1505. }
  1506. //
  1507. //
  1508. // DXT extension code
  1509. //
  1510. //
  1511. ILubyte* ILAPIENTRY ilGetDxtcData()
  1512. {
  1513. if (iCurImage == NULL) {
  1514. ilSetError(IL_INTERNAL_ERROR);
  1515. return NULL;
  1516. }
  1517. return iCurImage->DxtcData;
  1518. }
  1519. void ilFreeSurfaceDxtcData()
  1520. {
  1521. if (iCurImage != NULL && iCurImage->DxtcData != NULL) {
  1522. ifree(iCurImage->DxtcData);
  1523. iCurImage->DxtcData = NULL;
  1524. iCurImage->DxtcSize = 0;
  1525. iCurImage->DxtcFormat = IL_DXT_NO_COMP;
  1526. }
  1527. }
  1528. void ilFreeImageDxtcData()
  1529. {
  1530. ILint i, j;
  1531. ILuint ImgID = ilGetInteger(IL_CUR_IMAGE);
  1532. ILint ImgCount = ilGetInteger(IL_NUM_IMAGES);
  1533. ILint MipCount;
  1534. for(i = 0; i <= ImgCount; ++i) {
  1535. ilBindImage(ImgID);
  1536. ilActiveImage(i);
  1537. MipCount = ilGetInteger(IL_NUM_MIPMAPS);
  1538. for(j = 0; j <= MipCount; ++j) {
  1539. ilBindImage(ImgID);
  1540. ilActiveImage(i);
  1541. ilActiveMipmap(j);
  1542. ilFreeSurfaceDxtcData();
  1543. }
  1544. }
  1545. }
  1546. /*
  1547.  * This assumes DxtcData, DxtcFormat, width, height, and depth are valid
  1548.  */
  1549. ILAPI ILboolean ILAPIENTRY ilDxtcDataToSurface()
  1550. {
  1551. ILuint CompFormat;
  1552. if (iCurImage == NULL || iCurImage->DxtcData == NULL) {
  1553. ilSetError(IL_INVALID_PARAM);
  1554. return IL_FALSE;
  1555. }
  1556. if (!(iCurImage->DxtcFormat == IL_DXT1 || iCurImage->DxtcFormat == IL_DXT3
  1557. || iCurImage->DxtcFormat == IL_DXT5)) {
  1558. ilSetError(IL_INVALID_PARAM); //TODO
  1559. return IL_FALSE;
  1560. }
  1561. //@TODO: is this right for all dxt formats? works for
  1562. //  DXT1, 3, 5
  1563. iCurImage->Bpp = 4;
  1564. iCurImage->Bpc = 1;
  1565. iCurImage->Bps = iCurImage->Width*iCurImage->Bpp*iCurImage->Bpc;
  1566. iCurImage->SizeOfPlane = iCurImage->Height*iCurImage->Bps;
  1567. iCurImage->Format = IL_RGBA;
  1568. iCurImage->Type = IL_UNSIGNED_BYTE;
  1569. if (iCurImage->SizeOfData != iCurImage->SizeOfPlane*iCurImage->Depth) {
  1570. iCurImage->SizeOfData = iCurImage->Depth*iCurImage->SizeOfPlane;
  1571. if (iCurImage->Data != NULL)
  1572. ifree(iCurImage->Data);
  1573. iCurImage->Data = NULL;
  1574. }
  1575. if (iCurImage->Data == NULL) {
  1576. iCurImage->Data = ialloc(iCurImage->SizeOfData);
  1577. }
  1578. Image = iCurImage;
  1579. Width = iCurImage->Width;
  1580. Height = iCurImage->Height;
  1581. Depth = iCurImage->Depth;
  1582. switch(iCurImage->DxtcFormat)
  1583. {
  1584. case IL_DXT1: CompFormat = PF_DXT1; break;
  1585. case IL_DXT3: CompFormat = PF_DXT3; break;
  1586. case IL_DXT5: CompFormat = PF_DXT5; break;
  1587. }
  1588. CompData = iCurImage->DxtcData;
  1589. DdsDecompress(CompFormat); //globals suck...fix this
  1590. //@TODO: origin should be set in Decompress()...
  1591. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  1592. return ilFixCur();
  1593. }
  1594. ILAPI ILboolean ILAPIENTRY ilDxtcDataToImage()
  1595. {
  1596. ILint i, j;
  1597. ILuint ImgID = ilGetInteger(IL_CUR_IMAGE);
  1598. ILint ImgCount = ilGetInteger(IL_NUM_IMAGES);
  1599. ILint MipCount;
  1600. ILboolean ret = IL_TRUE;
  1601. for(i = 0; i <= ImgCount; ++i) {
  1602. ilBindImage(ImgID);
  1603. ilActiveImage(i);
  1604. MipCount = ilGetInteger(IL_NUM_MIPMAPS);
  1605. for(j = 0; j <= MipCount; ++j) {
  1606. ilBindImage(ImgID);
  1607. ilActiveImage(i);
  1608. ilActiveMipmap(j);
  1609. if (!ilDxtcDataToSurface())
  1610. ret = IL_FALSE;
  1611. }
  1612. }
  1613.     ilBindImage(ImgID);
  1614. return ret;
  1615. }
  1616. ILAPI ILboolean ILAPIENTRY ilSurfaceToDxtcData(ILenum Format)
  1617. {
  1618. ILuint Size;
  1619. void* Data;
  1620. ilFreeSurfaceDxtcData();
  1621. Size = ilGetDXTCData(NULL, 0, Format);
  1622. if (Size == 0) {
  1623. return IL_FALSE;
  1624. }
  1625. Data = ialloc(Size);
  1626.     
  1627. if (Data == NULL)
  1628. return IL_FALSE;
  1629.             
  1630. ilGetDXTCData(Data, Size, Format);
  1631. //These have to be after the call to ilGetDXTCData()
  1632. iCurImage->DxtcData = Data;
  1633. iCurImage->DxtcFormat = Format;
  1634. iCurImage->DxtcSize = Size;
  1635. return IL_TRUE;
  1636. }
  1637. ILAPI ILboolean ILAPIENTRY ilImageToDxtcData(ILenum Format)
  1638. {
  1639. ILint i, j;
  1640. ILuint ImgID = ilGetInteger(IL_CUR_IMAGE);
  1641. ILint ImgCount = ilGetInteger(IL_NUM_IMAGES);
  1642. ILint MipCount;
  1643. ILboolean ret = IL_TRUE;
  1644. for (i = 0; i <= ImgCount; ++i) {
  1645. ilBindImage(ImgID);
  1646. ilActiveImage(i);
  1647. MipCount = ilGetInteger(IL_NUM_MIPMAPS);
  1648. for(j = 0; j <= MipCount; ++j) {
  1649. ilBindImage(ImgID);
  1650. ilActiveImage(i);
  1651. ilActiveMipmap(j);
  1652. if (!ilSurfaceToDxtcData(Format))
  1653. ret = IL_FALSE;
  1654. }
  1655. }
  1656. return ret;
  1657. }
  1658. //works like ilTexImage(), ie. destroys mipmaps etc (which sucks, but
  1659. //is consistent. There should be a ilTexSurface() and ilTexSurfaceDxtc()
  1660. //functions as well, but for now this is sufficient)
  1661. ILAPI ILboolean ILAPIENTRY ilTexImageDxtc(ILint w, ILint h, ILint d, ILenum DxtFormat, const ILubyte* data)
  1662. {
  1663. ILimage* Image = iCurImage;
  1664. ILint xBlocks, yBlocks, BlockSize, LineSize, DataSize;
  1665. //The next few lines are copied from ilTexImage() and ilInitImage() -
  1666. //should be factored in more reusable functions...
  1667. if (Image == NULL) {
  1668. ilSetError(IL_ILLEGAL_OPERATION);
  1669. return IL_FALSE;
  1670. }
  1671. ////
  1672. // Not sure if we should be getting rid of the palette...
  1673. if (Image->Pal.Palette && Image->Pal.PalSize && Image->Pal.PalType != IL_PAL_NONE) {
  1674. ifree(Image->Pal.Palette);
  1675. }
  1676. // These are set NULL later by the memset call.
  1677. ilCloseImage(Image->Mipmaps);
  1678. ilCloseImage(Image->Next);
  1679. ilCloseImage(Image->Faces);
  1680. ilCloseImage(Image->Layers);
  1681. if (Image->AnimList) ifree(Image->AnimList);
  1682. if (Image->Profile)  ifree(Image->Profile);
  1683. if (Image->DxtcData) ifree(Image->DxtcData);
  1684. if (Image->Data)  ifree(Image->Data);
  1685. ////
  1686. memset(Image, 0, sizeof(ILimage));
  1687. Image->Width    = w;
  1688. Image->Height    = h;
  1689. Image->Depth    = d;
  1690. //TODO: What about origin with dxtc data?
  1691. Image->Origin    = IL_ORIGIN_LOWER_LEFT;
  1692. Image->Pal.PalType = IL_PAL_NONE;
  1693.     // Allocate DXT data buffer
  1694. xBlocks = (w + 3)/4;
  1695. yBlocks = (h + 3)/4;
  1696. if (DxtFormat == IL_DXT1)
  1697. BlockSize = 8;
  1698. else
  1699. BlockSize = 16;
  1700. LineSize = xBlocks * BlockSize;
  1701. DataSize = yBlocks * LineSize * d;
  1702. Image->DxtcFormat  = DxtFormat;
  1703.         Image->DxtcSize = DataSize;
  1704. Image->DxtcData    = ialloc(DataSize);
  1705. if (Image->DxtcData == NULL) {
  1706. return IL_FALSE;
  1707. }
  1708. if (data != NULL)
  1709. memcpy(Image->DxtcData, data, DataSize);
  1710. return IL_TRUE;
  1711. }
  1712. /* ------------------------------------------------------------------- */
  1713. void iFlipColorBlock(ILubyte *data)
  1714. {
  1715.     ILubyte tmp;
  1716.     tmp = data[4];
  1717.     data[4] = data[7];
  1718.     data[7] = tmp;
  1719.     tmp = data[5];
  1720.     data[5] = data[6];
  1721.     data[6] = tmp;
  1722. }
  1723. void iFlipSimpleAlphaBlock(ILushort *data)
  1724. {
  1725. ILushort tmp;
  1726. tmp = data[0];
  1727. data[0] = data[3];
  1728. data[3] = tmp;
  1729. tmp = data[1];
  1730. data[1] = data[2];
  1731. data[2] = tmp;
  1732. }
  1733. void iComplexAlphaHelper(ILubyte* Data)
  1734. {
  1735. ILushort tmp[2];
  1736. //one 4 pixel line is 12 bit, copy each line into
  1737. //a ushort, swap them and copy back
  1738. tmp[0] = (Data[0] | (Data[1] << 8)) & 0xfff;
  1739. tmp[1] = ((Data[1] >> 4) | (Data[2] << 4)) & 0xfff;
  1740. Data[0] = (ILubyte)tmp[1];
  1741. Data[1] = (tmp[1] >> 8) | (tmp[0] << 4);
  1742. Data[2] = tmp[0] >> 4;
  1743. }
  1744. void iFlipComplexAlphaBlock(ILubyte *Data)
  1745. {
  1746. ILubyte tmp[3];
  1747. Data += 2; //Skip 'palette'
  1748. //swap upper two rows with lower two rows
  1749. memcpy(tmp, Data, 3);
  1750. memcpy(Data, Data + 3, 3);
  1751. memcpy(Data + 3, tmp, 3);
  1752. //swap 1st with 2nd row, 3rd with 4th
  1753. iComplexAlphaHelper(Data);
  1754. iComplexAlphaHelper(Data + 3);
  1755. }
  1756. void iFlipDxt1(ILubyte* data, ILuint count)
  1757. {
  1758. ILuint i;
  1759. for (i = 0; i < count; ++i) {
  1760. iFlipColorBlock(data);
  1761. data += 8; //advance to next block
  1762. }
  1763. }
  1764. void iFlipDxt3(ILubyte* data, ILuint count)
  1765. {
  1766. ILuint i;
  1767. for (i = 0; i < count; ++i) {
  1768. iFlipSimpleAlphaBlock((ILushort*)data);
  1769. iFlipColorBlock(data + 8);
  1770. data += 16; //advance to next block
  1771. }
  1772. }
  1773. void iFlipDxt5(ILubyte* data, ILuint count)
  1774. {
  1775. ILuint i;
  1776. for (i = 0; i < count; ++i) {
  1777. iFlipComplexAlphaBlock(data);
  1778. iFlipColorBlock(data + 8);
  1779. data += 16; //advance to next block
  1780. }
  1781. }
  1782. void iFlip3dc(ILubyte* data, ILuint count)
  1783. {
  1784. ILuint i;
  1785. for (i = 0; i < count; ++i) {
  1786. iFlipComplexAlphaBlock(data);
  1787. iFlipComplexAlphaBlock(data + 8);
  1788. data += 16; //advance to next block
  1789. }
  1790. }
  1791. ILAPI void ILAPIENTRY ilFlipSurfaceDxtcData()
  1792. {
  1793. ILuint y, z;
  1794. ILuint BlockSize, LineSize;
  1795. ILubyte *Temp, *Runner, *Top, *Bottom;
  1796. ILuint numXBlocks, numYBlocks;
  1797. void (*FlipBlocks)(ILubyte* data, ILuint count);
  1798. if (iCurImage == NULL || iCurImage->DxtcData == NULL) {
  1799. ilSetError(IL_INVALID_PARAM);
  1800. return;
  1801. }
  1802. numXBlocks = (iCurImage->Width + 3)/4;
  1803. numYBlocks = (iCurImage->Height + 3)/4;
  1804. switch (iCurImage->DxtcFormat)
  1805. {
  1806. case IL_DXT1:
  1807. BlockSize = 8;
  1808. FlipBlocks = iFlipDxt1;
  1809. break;
  1810. case IL_DXT2:
  1811. case IL_DXT3:
  1812. BlockSize = 16;
  1813. FlipBlocks = iFlipDxt3;
  1814. break;
  1815. case IL_DXT4:
  1816. case IL_DXT5:
  1817. case IL_RXGB:
  1818. BlockSize = 16;
  1819. FlipBlocks = iFlipDxt5;
  1820. break;
  1821. case IL_3DC:
  1822. BlockSize = 16;
  1823. FlipBlocks = iFlip3dc;
  1824. break;
  1825. default:
  1826. ilSetError(IL_INVALID_PARAM);
  1827. return;
  1828. }
  1829. LineSize = numXBlocks * BlockSize;
  1830. Temp = ialloc(LineSize);
  1831. if (Temp == NULL)
  1832.     return;
  1833. Runner = iCurImage->DxtcData;
  1834. for (z = 0; z < iCurImage->Depth; ++z) {
  1835. Top = Runner;
  1836. Bottom = Runner + (numYBlocks - 1)*LineSize;
  1837.     
  1838. for (y = 0; y < numYBlocks/2; ++y) {
  1839. //swap block row
  1840. memcpy(Temp, Top, LineSize);
  1841. memcpy(Top, Bottom, LineSize);
  1842. memcpy(Bottom, Temp, LineSize);
  1843. //swap blocks
  1844. FlipBlocks(Top, numXBlocks);
  1845. FlipBlocks(Bottom, numXBlocks);
  1846. Top += LineSize;
  1847. Bottom -= LineSize;
  1848. }
  1849. //middle line
  1850. if (numYBlocks % 2 != 0)
  1851. FlipBlocks(Top, numXBlocks);
  1852. Runner += LineSize * numYBlocks;
  1853. }
  1854. ifree(Temp);
  1855. }
  1856. /**********************************************************************/
  1857. void iInvertDxt3Alpha(ILubyte *data)
  1858. {
  1859. ILint i;
  1860. for (i = 0; i < 8; ++i) {
  1861. /*
  1862. ILubyte b, t1, t2;
  1863. b = data[i];
  1864. t1 = b & 0xf;
  1865. t1 = 15 - t1;
  1866. t2 = b >> 4;
  1867. t2 = 15 - t2;
  1868. data[i] = (t2 << 4) | t1;
  1869. */
  1870. //simpler:
  1871. data[i] = ~data[i];
  1872. }
  1873. }
  1874. void iInvertDxt5Alpha(ILubyte *data)
  1875. {
  1876. ILubyte a0, a1;
  1877. ILint i, j;
  1878. const ILubyte map1[] = { 1, 0, 7, 6, 5, 4, 3, 2 };
  1879. const ILubyte map2[] = { 1, 0, 5, 4, 3, 2, 7, 6 };
  1880. a0 = data[0];
  1881. a1 = data[1];
  1882. //a0 > a1 <=> 255 - a0 < 255 - a1. Because of this,
  1883. //a1 and a2 have to be swapped, and the indices
  1884. //have to be changed as well.
  1885. //invert and swap alpha
  1886. data[0] = 255 - a1;
  1887. data[1] = 255 - a0;
  1888. data += 2;
  1889. //fix indices
  1890. for (i = 0; i < 6; i += 3) {
  1891. ILuint in = data[i] | (data[i+1] << 8) | (data[i+2] << 16);
  1892. ILuint out = 0;
  1893. for (j = 0; j < 24; j += 3) {
  1894. ILubyte b = (in >> j) & 0x7;
  1895. if (a0 > a1)
  1896. b = map1[b];
  1897. else
  1898. b = map2[b];
  1899. out |= b << j;
  1900. }
  1901. data[i] = out;
  1902. data[i+1] = out >> 8;
  1903. data[i+2] = out >> 16;
  1904. }
  1905. }
  1906. ILAPI ILboolean ILAPIENTRY ilInvertSurfaceDxtcDataAlpha()
  1907. {
  1908. ILint i;
  1909. ILuint BlockSize;
  1910. ILubyte *Runner;
  1911. ILint numXBlocks, numYBlocks, numBlocks;
  1912. void (*InvertAlpha)(ILubyte* data);
  1913. if (iCurImage == NULL || iCurImage->DxtcData == NULL) {
  1914. ilSetError(IL_INVALID_PARAM);
  1915. return IL_FALSE;
  1916. }
  1917. numXBlocks = (iCurImage->Width + 3)/4;
  1918. numYBlocks = (iCurImage->Height + 3)/4;
  1919. numBlocks = numXBlocks*numYBlocks*iCurImage->Depth;
  1920. BlockSize = 16;
  1921. switch (iCurImage->DxtcFormat)
  1922. {
  1923. case IL_DXT3:
  1924. InvertAlpha = iInvertDxt3Alpha;
  1925. break;
  1926. case IL_DXT5:
  1927. InvertAlpha = iInvertDxt5Alpha;
  1928. break;
  1929. default:
  1930. //DXT2/4 are not supported yet because nobody
  1931. //uses them anyway and I would have to change
  1932. //the color blocks as well...
  1933. //DXT1 is not supported because DXT1 alpha is
  1934. //seldom used and it's not easily invertable.
  1935. ilSetError(IL_INVALID_PARAM);
  1936. return IL_FALSE;
  1937. }
  1938. Runner = iCurImage->DxtcData;
  1939. for (i = 0; i < numBlocks; ++i, Runner += BlockSize) {
  1940. InvertAlpha(Runner);
  1941. }
  1942. return IL_TRUE;
  1943. }
  1944. #endif//IL_NO_DDS