CImageLoaderTEX.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. #include "CImageLoaderTEX.h"
  2. #include <irrlicht/irrlicht.h>
  3. #include <lz4.h>
  4. #include <wallpaperengine/irrlicht.h>
  5. #include <string>
  6. namespace irr {
  7. namespace video {
  8. //! returns true if the file maybe is able to be loaded by this class
  9. //! based on the file extension (e.g. ".tga")
  10. bool CImageLoaderTex::isALoadableFileExtension (const io::path &filename) const
  11. {
  12. return core::hasFileExtension (filename, "tex");
  13. }
  14. //! returns true if the file maybe is able to be loaded by this class
  15. bool CImageLoaderTex::isALoadableFileFormat (io::IReadFile *file) const
  16. {
  17. return false;
  18. }
  19. // load in the image data
  20. IImage *CImageLoaderTex::loadImage (io::IReadFile *input) const
  21. {
  22. if (!input)
  23. return nullptr;
  24. video::IImage *image = nullptr;
  25. char buffer [1024];
  26. if (input->read (buffer, 9) != 9)
  27. {
  28. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: cannot read header\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  29. return nullptr;
  30. }
  31. if (memcmp (buffer, "TEXV0005", 9) != 0)
  32. {
  33. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: not really a tex\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  34. return nullptr;
  35. }
  36. if (input->read (buffer, 9) != 9)
  37. {
  38. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: cannot read second header\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  39. return nullptr;
  40. }
  41. if (memcmp (buffer, "TEXI0001", 9) != 0)
  42. {
  43. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: not really a tex\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  44. return nullptr;
  45. }
  46. u32 width;
  47. u32 height;
  48. u32 texture_width;
  49. u32 texture_height;
  50. u32 format;
  51. u32 imageFormat = FREE_IMAGE_FORMAT::FIF_UNKNOWN;
  52. u8 containerVersion = 0;
  53. input->read (&format, 4);
  54. input->seek (4, true); // ignore bytes
  55. input->read (&texture_width, 4);
  56. input->read (&texture_height, 4);
  57. input->read (&width, 4);
  58. input->read (&height, 4);
  59. input->seek (4, true); // ignore bytes
  60. input->read (buffer, 9);
  61. if (memcmp (buffer, "TEXB0003", 9) == 0)
  62. {
  63. containerVersion = 3;
  64. input->seek (4, true);
  65. input->read (&imageFormat, 4);
  66. }
  67. else if (memcmp (buffer, "TEXB0002", 9) == 0)
  68. {
  69. containerVersion = 2;
  70. input->seek (4, true);
  71. }
  72. else if (memcmp (buffer, "TEXB0001", 9) == 0)
  73. {
  74. containerVersion = 1;
  75. input->seek (4, true);
  76. }
  77. else
  78. {
  79. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: Unknown container type\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  80. return nullptr;
  81. }
  82. if (format == TextureFormat::A8)
  83. {
  84. wp::irrlicht::device->getLogger ()-> log ("LOAD TEX: A8 not supported\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  85. return nullptr;
  86. }
  87. if (format == TextureFormat::RA88)
  88. {
  89. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: RA88 not supported\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  90. return nullptr;
  91. }
  92. u32 mipmap_count = 0;
  93. input->read (&mipmap_count, 4);
  94. u32 mipmap_width = 0;
  95. u32 mipmap_height = 0;
  96. u32 mipmap_compression = 0;
  97. u32 mipmap_uncompressed_size = 0;
  98. u32 mipmap_compressed_size = 0;
  99. input->read (&mipmap_width, 4);
  100. input->read (&mipmap_height, 4);
  101. if (containerVersion > 1)
  102. {
  103. input->read (&mipmap_compression, 4);
  104. input->read (&mipmap_uncompressed_size, 4);
  105. }
  106. input->read (&mipmap_compressed_size, 4);
  107. char *decompressedBuffer = new char [mipmap_uncompressed_size];
  108. if (mipmap_compression == 1)
  109. {
  110. char *compressedBuffer = new char [mipmap_compressed_size];
  111. input->read (compressedBuffer, mipmap_compressed_size);
  112. int result = LZ4_decompress_safe (compressedBuffer, decompressedBuffer, mipmap_compressed_size, mipmap_uncompressed_size);
  113. if (!result)
  114. {
  115. delete [] decompressedBuffer;
  116. delete [] compressedBuffer;
  117. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: cannot decompress texture data\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  118. return nullptr;
  119. }
  120. delete [] compressedBuffer;
  121. }
  122. else
  123. {
  124. input->read (decompressedBuffer, mipmap_uncompressed_size);
  125. }
  126. if (containerVersion == 2 || containerVersion == 1)
  127. {
  128. image = wp::irrlicht::driver->createImage (ECF_A8R8G8B8, irr::core::dimension2d<u32> (width, height));
  129. if (!image)
  130. {
  131. delete [] decompressedBuffer;
  132. delete image;
  133. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: cannot create destination image\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  134. return nullptr;
  135. }
  136. switch (format)
  137. {
  138. case TextureFormat::ARGB8888:
  139. this->loadImageFromARGB8Data (image, decompressedBuffer, width, height, mipmap_width);
  140. break;
  141. case TextureFormat::DXT5:
  142. this->loadImageFromDXT5 (image, decompressedBuffer, width, height, mipmap_width, mipmap_height);
  143. break;
  144. case TextureFormat::DXT1:
  145. this->loadImageFromDXT1 (image, decompressedBuffer, width, height, mipmap_width, mipmap_height);
  146. break;
  147. case TextureFormat::DXT3:
  148. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: DXT3 textures not supported yet\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  149. delete [] decompressedBuffer;
  150. delete image;
  151. return nullptr;
  152. }
  153. }
  154. else
  155. {
  156. // copy the buffer to a new address
  157. char* filebuffer = new char [mipmap_uncompressed_size];
  158. char tmpname [TMP_MAX];
  159. // copy file data to the final file buffer to be used
  160. memcpy (filebuffer, decompressedBuffer, mipmap_uncompressed_size);
  161. // generate temporal name
  162. std::tmpnam (tmpname);
  163. // store it in a std::string
  164. std::string filename = tmpname;
  165. irr::io::IReadFile* file;
  166. // free image format
  167. switch (imageFormat)
  168. {
  169. case FREE_IMAGE_FORMAT::FIF_BMP:
  170. // add extension to the file
  171. filename += ".bmp";
  172. file = wp::irrlicht::device->getFileSystem ()->createMemoryReadFile (filebuffer, mipmap_uncompressed_size, filename.c_str (), true);
  173. break;
  174. case FREE_IMAGE_FORMAT::FIF_PNG:
  175. // add extension to the file
  176. filename += ".png";
  177. file = wp::irrlicht::device->getFileSystem ()->createMemoryReadFile (filebuffer, mipmap_uncompressed_size, filename.c_str (), true);
  178. break;
  179. case FREE_IMAGE_FORMAT::FIF_JPEG:
  180. // add extension to the file
  181. filename += ".jpg";
  182. file = wp::irrlicht::device->getFileSystem ()->createMemoryReadFile (filebuffer, mipmap_uncompressed_size, filename.c_str (), true);
  183. break;
  184. default:
  185. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: detected unsupported free-image format\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  186. delete [] decompressedBuffer;
  187. delete [] filebuffer;
  188. return nullptr;
  189. }
  190. image = wp::irrlicht::driver->createImageFromFile (file);
  191. if (!image)
  192. {
  193. file->drop ();
  194. delete [] decompressedBuffer;
  195. delete image;
  196. wp::irrlicht::device->getLogger ()->log ("LOAD TEX: cannot create destination image\n", input->getFileName ().c_str (), irr::ELL_ERROR);
  197. return nullptr;
  198. }
  199. }
  200. delete [] decompressedBuffer;
  201. return image;
  202. }
  203. void CImageLoaderTex::loadImageFromARGB8Data (IImage* output, const char* input, u32 width, u32 height, u32 mipmap_width) const
  204. {
  205. u32 bytesPerPixel = output->getBytesPerPixel ();
  206. char *imagedata = (char *) output->lock ();
  207. for (u32 y = 0; y < height; y ++)
  208. {
  209. u32 baseDestination = y * output->getPitch ();
  210. u32 baseOrigin = y * (mipmap_width * 4);
  211. for (u32 x = 0; x < width; x ++)
  212. {
  213. imagedata [baseDestination + (x * bytesPerPixel) + 2] = input [baseOrigin + ((width - x) * 4) + 0]; // r
  214. imagedata [baseDestination + (x * bytesPerPixel) + 1] = input [baseOrigin + ((width - x) * 4) + 1]; // g
  215. imagedata [baseDestination + (x * bytesPerPixel) + 0] = input [baseOrigin + ((width - x) * 4) + 2]; // b
  216. imagedata [baseDestination + (x * bytesPerPixel) + 3] = input [baseOrigin + ((width - x) * 4) + 3]; // alpha
  217. }
  218. }
  219. output->unlock ();
  220. }
  221. void CImageLoaderTex::loadImageFromDXT1 (IImage* output, const char* input, u32 destination_width, u32 destination_height, u32 origin_width, u32 origin_height) const
  222. {
  223. char* decompressedBuffer = new char [origin_width * origin_height * 4];
  224. this->BlockDecompressImageDXT1 (origin_width, origin_height, (const unsigned char*) input, (unsigned long*) decompressedBuffer);
  225. this->loadImageFromARGB8Data (output, decompressedBuffer, destination_width, destination_height, origin_width);
  226. delete [] decompressedBuffer;
  227. }
  228. void CImageLoaderTex::loadImageFromDXT5 (IImage* output, const char* input, u32 destination_width, u32 destination_height, u32 origin_width, u32 origin_height) const
  229. {
  230. char* decompressedBuffer = new char [origin_width * origin_height * 4];
  231. this->BlockDecompressImageDXT5 (origin_width, origin_height, (const unsigned char*) input, (unsigned long*) decompressedBuffer);
  232. this->loadImageFromARGB8Data (output, decompressedBuffer, destination_width, destination_height, origin_width);
  233. delete [] decompressedBuffer;
  234. }
  235. // ------------------------------------------------------------------------------------
  236. // The following code is a slightly modified version of this repository
  237. // https://github.com/Benjamin-Dobell/s3tc-dxt-decompression
  238. // ------------------------------------------------------------------------------------
  239. // unsigned long PackRGBA(): Helper method that packs RGBA channels into a single 4 byte pixel.
  240. //
  241. // unsigned char r: red channel.
  242. // unsigned char g: green channel.
  243. // unsigned char b: blue channel.
  244. // unsigned char a: alpha channel.
  245. unsigned long CImageLoaderTex::PackRGBA(unsigned char r, unsigned char g, unsigned char b, unsigned char a) const
  246. {
  247. return ((r << 24) | (g << 16) | (b << 8) | a);
  248. }
  249. // void DecompressBlockDXT1(): Decompresses one block of a DXT1 texture and stores the resulting pixels at the appropriate offset in 'image'.
  250. //
  251. // unsigned long x: x-coordinate of the first pixel in the block.
  252. // unsigned long y: y-coordinate of the first pixel in the block.
  253. // unsigned long width: width of the texture being decompressed.
  254. // unsigned long height: height of the texture being decompressed.
  255. // const unsigned char *blockStorage: pointer to the block to decompress.
  256. // unsigned long *image: pointer to image where the decompressed pixel data should be stored.
  257. void CImageLoaderTex::DecompressBlockDXT1(unsigned long x, unsigned long y, unsigned long width, const unsigned char *blockStorage, unsigned long *image) const
  258. {
  259. unsigned short color0 = *reinterpret_cast<const unsigned short *>(blockStorage);
  260. unsigned short color1 = *reinterpret_cast<const unsigned short *>(blockStorage + 2);
  261. unsigned long temp;
  262. temp = (color0 >> 11) * 255 + 16;
  263. unsigned char r0 = (unsigned char)((temp/32 + temp)/32);
  264. temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
  265. unsigned char g0 = (unsigned char)((temp/64 + temp)/64);
  266. temp = (color0 & 0x001F) * 255 + 16;
  267. unsigned char b0 = (unsigned char)((temp/32 + temp)/32);
  268. temp = (color1 >> 11) * 255 + 16;
  269. unsigned char r1 = (unsigned char)((temp/32 + temp)/32);
  270. temp = ((color1 & 0x07E0) >> 5) * 255 + 32;
  271. unsigned char g1 = (unsigned char)((temp/64 + temp)/64);
  272. temp = (color1 & 0x001F) * 255 + 16;
  273. unsigned char b1 = (unsigned char)((temp/32 + temp)/32);
  274. unsigned long code = *reinterpret_cast<const unsigned long *>(blockStorage + 4);
  275. for (int j=0; j < 4; j++)
  276. {
  277. for (int i=0; i < 4; i++)
  278. {
  279. unsigned long finalColor = 0;
  280. unsigned char positionCode = (code >> 2*(4*j+i)) & 0x03;
  281. if (color0 > color1)
  282. {
  283. switch (positionCode)
  284. {
  285. case 0:
  286. finalColor = PackRGBA(r0, g0, b0, 255);
  287. break;
  288. case 1:
  289. finalColor = PackRGBA(r1, g1, b1, 255);
  290. break;
  291. case 2:
  292. finalColor = PackRGBA((2*r0+r1)/3, (2*g0+g1)/3, (2*b0+b1)/3, 255);
  293. break;
  294. case 3:
  295. finalColor = PackRGBA((r0+2*r1)/3, (g0+2*g1)/3, (b0+2*b1)/3, 255);
  296. break;
  297. }
  298. }
  299. else
  300. {
  301. switch (positionCode)
  302. {
  303. case 0:
  304. finalColor = PackRGBA(r0, g0, b0, 255);
  305. break;
  306. case 1:
  307. finalColor = PackRGBA(r1, g1, b1, 255);
  308. break;
  309. case 2:
  310. finalColor = PackRGBA((r0+r1)/2, (g0+g1)/2, (b0+b1)/2, 255);
  311. break;
  312. case 3:
  313. finalColor = PackRGBA(0, 0, 0, 255);
  314. break;
  315. }
  316. }
  317. if (x + i < width)
  318. image[(y + j)*width + (x + i)] = finalColor;
  319. }
  320. }
  321. }
  322. // void BlockDecompressImageDXT1(): Decompresses all the blocks of a DXT1 compressed texture and stores the resulting pixels in 'image'.
  323. //
  324. // unsigned long width: Texture width.
  325. // unsigned long height: Texture height.
  326. // const unsigned char *blockStorage: pointer to compressed DXT1 blocks.
  327. // unsigned long *image: pointer to the image where the decompressed pixels will be stored.
  328. void CImageLoaderTex::BlockDecompressImageDXT1(unsigned long width, unsigned long height, const unsigned char *blockStorage, unsigned long *image) const
  329. {
  330. unsigned long blockCountX = (width + 3) / 4;
  331. unsigned long blockCountY = (height + 3) / 4;
  332. unsigned long blockWidth = (width < 4) ? width : 4;
  333. unsigned long blockHeight = (height < 4) ? height : 4;
  334. for (unsigned long j = 0; j < blockCountY; j++)
  335. {
  336. for (unsigned long i = 0; i < blockCountX; i++) DecompressBlockDXT1(i*4, j*4, width, blockStorage + i * 8, image);
  337. blockStorage += blockCountX * 8;
  338. }
  339. }
  340. // void DecompressBlockDXT5(): Decompresses one block of a DXT5 texture and stores the resulting pixels at the appropriate offset in 'image'.
  341. //
  342. // unsigned long x: x-coordinate of the first pixel in the block.
  343. // unsigned long y: y-coordinate of the first pixel in the block.
  344. // unsigned long width: width of the texture being decompressed.
  345. // unsigned long height: height of the texture being decompressed.
  346. // const unsigned char *blockStorage: pointer to the block to decompress.
  347. // unsigned long *image: pointer to image where the decompressed pixel data should be stored.
  348. void CImageLoaderTex::DecompressBlockDXT5(unsigned long x, unsigned long y, unsigned long width, const unsigned char *blockStorage, unsigned long *image) const
  349. {
  350. unsigned char alpha0 = *reinterpret_cast<const unsigned char *>(blockStorage);
  351. unsigned char alpha1 = *reinterpret_cast<const unsigned char *>(blockStorage + 1);
  352. const unsigned char *bits = blockStorage + 2;
  353. unsigned long alphaCode1 = bits[2] | (bits[3] << 8) | (bits[4] << 16) | (bits[5] << 24);
  354. unsigned short alphaCode2 = bits[0] | (bits[1] << 8);
  355. unsigned short color0 = *reinterpret_cast<const unsigned short *>(blockStorage + 8);
  356. unsigned short color1 = *reinterpret_cast<const unsigned short *>(blockStorage + 10);
  357. unsigned long temp;
  358. temp = (color0 >> 11) * 255 + 16;
  359. unsigned char r0 = (unsigned char)((temp/32 + temp)/32);
  360. temp = ((color0 & 0x07E0) >> 5) * 255 + 32;
  361. unsigned char g0 = (unsigned char)((temp/64 + temp)/64);
  362. temp = (color0 & 0x001F) * 255 + 16;
  363. unsigned char b0 = (unsigned char)((temp/32 + temp)/32);
  364. temp = (color1 >> 11) * 255 + 16;
  365. unsigned char r1 = (unsigned char)((temp/32 + temp)/32);
  366. temp = ((color1 & 0x07E0) >> 5) * 255 + 32;
  367. unsigned char g1 = (unsigned char)((temp/64 + temp)/64);
  368. temp = (color1 & 0x001F) * 255 + 16;
  369. unsigned char b1 = (unsigned char)((temp/32 + temp)/32);
  370. unsigned long code = *reinterpret_cast<const unsigned long *>(blockStorage + 12);
  371. for (int j=0; j < 4; j++)
  372. {
  373. for (int i=0; i < 4; i++)
  374. {
  375. int alphaCodeIndex = 3*(4*j+i);
  376. int alphaCode;
  377. if (alphaCodeIndex <= 12)
  378. {
  379. alphaCode = (alphaCode2 >> alphaCodeIndex) & 0x07;
  380. }
  381. else if (alphaCodeIndex == 15)
  382. {
  383. alphaCode = (alphaCode2 >> 15) | ((alphaCode1 << 1) & 0x06);
  384. }
  385. else // alphaCodeIndex >= 18 && alphaCodeIndex <= 45
  386. {
  387. alphaCode = (alphaCode1 >> (alphaCodeIndex - 16)) & 0x07;
  388. }
  389. unsigned char finalAlpha;
  390. if (alphaCode == 0)
  391. {
  392. finalAlpha = alpha0;
  393. }
  394. else if (alphaCode == 1)
  395. {
  396. finalAlpha = alpha1;
  397. }
  398. else
  399. {
  400. if (alpha0 > alpha1)
  401. {
  402. finalAlpha = ((8-alphaCode)*alpha0 + (alphaCode-1)*alpha1)/7;
  403. }
  404. else
  405. {
  406. if (alphaCode == 6)
  407. finalAlpha = 0;
  408. else if (alphaCode == 7)
  409. finalAlpha = 255;
  410. else
  411. finalAlpha = ((6-alphaCode)*alpha0 + (alphaCode-1)*alpha1)/5;
  412. }
  413. }
  414. unsigned char colorCode = (code >> 2*(4*j+i)) & 0x03;
  415. unsigned long finalColor;
  416. switch (colorCode)
  417. {
  418. case 0:
  419. finalColor = PackRGBA(r0, g0, b0, finalAlpha);
  420. break;
  421. case 1:
  422. finalColor = PackRGBA(r1, g1, b1, finalAlpha);
  423. break;
  424. case 2:
  425. finalColor = PackRGBA((2*r0+r1)/3, (2*g0+g1)/3, (2*b0+b1)/3, finalAlpha);
  426. break;
  427. case 3:
  428. finalColor = PackRGBA((r0+2*r1)/3, (g0+2*g1)/3, (b0+2*b1)/3, finalAlpha);
  429. break;
  430. }
  431. if (x + i < width)
  432. image[(y + j)*width + (x + i)] = finalColor;
  433. }
  434. }
  435. }
  436. // void BlockDecompressImageDXT5(): Decompresses all the blocks of a DXT5 compressed texture and stores the resulting pixels in 'image'.
  437. //
  438. // unsigned long width: Texture width.
  439. // unsigned long height: Texture height.
  440. // const unsigned char *blockStorage: pointer to compressed DXT5 blocks.
  441. // unsigned long *image: pointer to the image where the decompressed pixels will be stored.
  442. void CImageLoaderTex::BlockDecompressImageDXT5(unsigned long width, unsigned long height, const unsigned char *blockStorage, unsigned long *image) const
  443. {
  444. unsigned long blockCountX = (width + 3) / 4;
  445. unsigned long blockCountY = (height + 3) / 4;
  446. unsigned long blockWidth = (width < 4) ? width : 4;
  447. unsigned long blockHeight = (height < 4) ? height : 4;
  448. for (unsigned long j = 0; j < blockCountY; j++)
  449. {
  450. for (unsigned long i = 0; i < blockCountX; i++) DecompressBlockDXT5(i*4, j*4, width, blockStorage + i * 16, image);
  451. blockStorage += blockCountX * 16;
  452. }
  453. }
  454. }// end namespace irr
  455. }//end namespace video