texturec.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  1. /*
  2. * Copyright 2011-2016 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. // Just hacking DDS loading code in here.
  9. #include "bgfx_p.h"
  10. #include "image.h"
  11. #include <libsquish/squish.h>
  12. #include <etc1/etc1.h>
  13. #include <etc2/ProcessRGB.hpp>
  14. #include <nvtt/nvtt.h>
  15. #include <pvrtc/PvrTcEncoder.h>
  16. #include <tinyexr/tinyexr.h>
  17. #include <edtaa3/edtaa3func.h>
  18. extern "C" {
  19. #include <iqa.h>
  20. }
  21. #define LODEPNG_NO_COMPILE_ENCODER
  22. #define LODEPNG_NO_COMPILE_DISK
  23. #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS
  24. #define LODEPNG_NO_COMPILE_ERROR_TEXT
  25. #define LODEPNG_NO_COMPILE_ALLOCATORS
  26. #define LODEPNG_NO_COMPILE_CPP
  27. #include <lodepng/lodepng.cpp>
  28. void* lodepng_malloc(size_t _size)
  29. {
  30. return ::malloc(_size);
  31. }
  32. void* lodepng_realloc(void* _ptr, size_t _size)
  33. {
  34. return ::realloc(_ptr, _size);
  35. }
  36. void lodepng_free(void* _ptr)
  37. {
  38. ::free(_ptr);
  39. }
  40. BX_PRAGMA_DIAGNOSTIC_PUSH();
  41. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wmissing-field-initializers");
  42. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wshadow");
  43. BX_PRAGMA_DIAGNOSTIC_IGNORED_CLANG_GCC("-Wint-to-pointer-cast")
  44. #define STBI_MALLOC(_size) lodepng_malloc(_size)
  45. #define STBI_REALLOC(_ptr, _size) lodepng_realloc(_ptr, _size)
  46. #define STBI_FREE(_ptr) lodepng_free(_ptr)
  47. #define STB_IMAGE_IMPLEMENTATION
  48. #include <stb/stb_image.c>
  49. BX_PRAGMA_DIAGNOSTIC_POP();
  50. #if 0
  51. # define BX_TRACE(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  52. #endif // DEBUG
  53. #include <bx/bx.h>
  54. #include <bx/commandline.h>
  55. #include <bx/crtimpl.h>
  56. #include <bx/uint32_t.h>
  57. namespace bgfx
  58. {
  59. const Memory* alloc(uint32_t _size)
  60. {
  61. Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) + _size);
  62. mem->size = _size;
  63. mem->data = (uint8_t*)mem + sizeof(Memory);
  64. return mem;
  65. }
  66. const Memory* makeRef(const void* _data, uint32_t _size, ReleaseFn _releaseFn, void* _userData)
  67. {
  68. BX_UNUSED(_releaseFn, _userData);
  69. Memory* mem = (Memory*)::realloc(NULL, sizeof(Memory) );
  70. mem->size = _size;
  71. mem->data = (uint8_t*)_data;
  72. return mem;
  73. }
  74. void release(const Memory* _mem)
  75. {
  76. Memory* mem = const_cast<Memory*>(_mem);
  77. ::free(mem);
  78. }
  79. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size, void** _out)
  80. {
  81. *_out = NULL;
  82. bool loaded = imageParse(_imageContainer, _data, _size);
  83. if (!loaded)
  84. {
  85. bgfx::TextureFormat::Enum format = bgfx::TextureFormat::RGBA8;
  86. uint32_t bpp = 32;
  87. uint32_t width = 0;
  88. uint32_t height = 0;
  89. uint8_t* out = NULL;
  90. static uint8_t pngMagic[] = { 0x89, 0x50, 0x4E, 0x47, 0x0d, 0x0a };
  91. if (0 == memcmp(_data, pngMagic, sizeof(pngMagic) ) )
  92. {
  93. unsigned error;
  94. LodePNGState state;
  95. lodepng_state_init(&state);
  96. state.decoder.color_convert = 0;
  97. error = lodepng_decode(&out, &width, &height, &state, (uint8_t*)_data, _size);
  98. if (0 == error)
  99. {
  100. *_out = out;
  101. switch (state.info_raw.bitdepth)
  102. {
  103. case 8:
  104. switch (state.info_raw.colortype)
  105. {
  106. case LCT_GREY:
  107. format = bgfx::TextureFormat::R8;
  108. bpp = 8;
  109. break;
  110. case LCT_GREY_ALPHA:
  111. format = bgfx::TextureFormat::RG8;
  112. bpp = 16;
  113. break;
  114. case LCT_RGB:
  115. format = bgfx::TextureFormat::RGB8;
  116. bpp = 24;
  117. break;
  118. case LCT_RGBA:
  119. format = bgfx::TextureFormat::RGBA8;
  120. bpp = 32;
  121. break;
  122. case LCT_PALETTE:
  123. break;
  124. }
  125. break;
  126. case 16:
  127. switch (state.info_raw.colortype)
  128. {
  129. case LCT_GREY:
  130. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  131. {
  132. uint16_t* rgba = (uint16_t*)out + ii;
  133. rgba[0] = bx::toHostEndian(rgba[0], false);
  134. }
  135. format = bgfx::TextureFormat::R16;
  136. bpp = 16;
  137. break;
  138. case LCT_GREY_ALPHA:
  139. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  140. {
  141. uint16_t* rgba = (uint16_t*)out + ii*2;
  142. rgba[0] = bx::toHostEndian(rgba[0], false);
  143. rgba[1] = bx::toHostEndian(rgba[1], false);
  144. }
  145. format = bgfx::TextureFormat::RG16;
  146. bpp = 32;
  147. break;
  148. case LCT_RGBA:
  149. for (uint32_t ii = 0, num = width*height; ii < num; ++ii)
  150. {
  151. uint16_t* rgba = (uint16_t*)out + ii*4;
  152. rgba[0] = bx::toHostEndian(rgba[0], false);
  153. rgba[1] = bx::toHostEndian(rgba[1], false);
  154. rgba[2] = bx::toHostEndian(rgba[2], false);
  155. rgba[3] = bx::toHostEndian(rgba[3], false);
  156. }
  157. format = bgfx::TextureFormat::RGBA16;
  158. bpp = 64;
  159. break;
  160. case LCT_RGB:
  161. case LCT_PALETTE:
  162. break;
  163. }
  164. break;
  165. default:
  166. break;
  167. }
  168. }
  169. lodepng_state_cleanup(&state);
  170. }
  171. else
  172. {
  173. int comp = 0;
  174. *_out = stbi_load_from_memory( (uint8_t*)_data, _size, (int*)&width, (int*)&height, &comp, 4);
  175. }
  176. loaded = NULL != *_out;
  177. if (loaded)
  178. {
  179. _imageContainer.m_data = *_out;
  180. _imageContainer.m_size = width*height*bpp/8;
  181. _imageContainer.m_offset = 0;
  182. _imageContainer.m_width = width;
  183. _imageContainer.m_height = height;
  184. _imageContainer.m_depth = 1;
  185. _imageContainer.m_numLayers = 1;
  186. _imageContainer.m_format = format;
  187. _imageContainer.m_numMips = 1;
  188. _imageContainer.m_hasAlpha = true;
  189. _imageContainer.m_cubeMap = false;
  190. _imageContainer.m_ktx = false;
  191. _imageContainer.m_ktxLE = false;
  192. _imageContainer.m_srgb = false;
  193. }
  194. }
  195. return loaded;
  196. }
  197. bool imageEncodeFromRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
  198. {
  199. TextureFormat::Enum format = TextureFormat::Enum(_format);
  200. switch (format)
  201. {
  202. case TextureFormat::BC1:
  203. case TextureFormat::BC2:
  204. case TextureFormat::BC3:
  205. case TextureFormat::BC4:
  206. case TextureFormat::BC5:
  207. squish::CompressImage( (const uint8_t*)_src, _width, _height, _dst
  208. , format == TextureFormat::BC2 ? squish::kDxt3
  209. : format == TextureFormat::BC3 ? squish::kDxt5
  210. : format == TextureFormat::BC4 ? squish::kBc4
  211. : format == TextureFormat::BC5 ? squish::kBc5
  212. : squish::kDxt1
  213. );
  214. return true;
  215. case TextureFormat::BC6H:
  216. nvtt::compressBC6H( (const uint8_t*)_src, _width, _height, 4, _dst);
  217. return true;
  218. case TextureFormat::BC7:
  219. nvtt::compressBC7( (const uint8_t*)_src, _width, _height, 4, _dst);
  220. return true;
  221. case TextureFormat::ETC1:
  222. etc1_encode_image( (const uint8_t*)_src, _width, _height, 4, _width*4, (uint8_t*)_dst);
  223. return true;
  224. case TextureFormat::ETC2:
  225. {
  226. const uint32_t blockWidth = (_width +3)/4;
  227. const uint32_t blockHeight = (_height+3)/4;
  228. const uint32_t pitch = _width*4;
  229. const uint8_t* src = (const uint8_t*)_src;
  230. uint64_t* dst = (uint64_t*)_dst;
  231. for (uint32_t yy = 0; yy < blockHeight; ++yy)
  232. {
  233. for (uint32_t xx = 0; xx < blockWidth; ++xx)
  234. {
  235. uint8_t block[4*4*4];
  236. const uint8_t* ptr = &src[(yy*pitch+xx*4)*4];
  237. for (uint32_t ii = 0; ii < 16; ++ii)
  238. { // BGRx
  239. memcpy(&block[ii*4], &ptr[(ii%4)*pitch + (ii&~3)], 4);
  240. bx::xchg(block[ii*4+0], block[ii*4+2]);
  241. }
  242. *dst++ = ProcessRGB_ETC2(block);
  243. }
  244. }
  245. }
  246. return true;
  247. case TextureFormat::PTC14:
  248. {
  249. using namespace Javelin;
  250. RgbaBitmap bmp;
  251. bmp.width = _width;
  252. bmp.height = _height;
  253. bmp.data = (uint8_t*)const_cast<void*>(_src);
  254. PvrTcEncoder::EncodeRgb4Bpp(_dst, bmp);
  255. bmp.data = NULL;
  256. }
  257. return true;
  258. case TextureFormat::PTC14A:
  259. {
  260. using namespace Javelin;
  261. RgbaBitmap bmp;
  262. bmp.width = _width;
  263. bmp.height = _height;
  264. bmp.data = (uint8_t*)const_cast<void*>(_src);
  265. PvrTcEncoder::EncodeRgba4Bpp(_dst, bmp);
  266. bmp.data = NULL;
  267. }
  268. return true;
  269. case TextureFormat::BGRA8:
  270. imageSwizzleBgra8(_width, _height, _width*4, _src, _dst);
  271. return true;
  272. case TextureFormat::RGBA8:
  273. memcpy(_dst, _src, _width*_height*4);
  274. return true;
  275. default:
  276. return imageConvert(_dst, format, _src, TextureFormat::RGBA8, _width, _height);
  277. }
  278. return false;
  279. }
  280. bool imageEncodeFromRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint8_t _format)
  281. {
  282. TextureFormat::Enum format = TextureFormat::Enum(_format);
  283. const uint8_t* src = (const uint8_t*)_src;
  284. switch (format)
  285. {
  286. case TextureFormat::RGBA8:
  287. {
  288. uint8_t* dst = (uint8_t*)_dst;
  289. for (uint32_t yy = 0; yy < _height; ++yy)
  290. {
  291. for (uint32_t xx = 0; xx < _width; ++xx)
  292. {
  293. const uint32_t offset = yy*_width + xx;
  294. const float* input = (const float*)&src[offset * 16];
  295. uint8_t* output = &dst[offset * 4];
  296. output[0] = uint8_t(input[0]*255.0f + 0.5f);
  297. output[1] = uint8_t(input[1]*255.0f + 0.5f);
  298. output[2] = uint8_t(input[2]*255.0f + 0.5f);
  299. output[3] = uint8_t(input[3]*255.0f + 0.5f);
  300. }
  301. }
  302. }
  303. return true;
  304. case TextureFormat::BC5:
  305. {
  306. uint8_t* temp = (uint8_t*)BX_ALLOC(_allocator, _width*_height*4);
  307. for (uint32_t yy = 0; yy < _height; ++yy)
  308. {
  309. for (uint32_t xx = 0; xx < _width; ++xx)
  310. {
  311. const uint32_t offset = yy*_width + xx;
  312. const float* input = (const float*)&src[offset * 16];
  313. uint8_t* output = &temp[offset * 4];
  314. output[0] = uint8_t(input[0]*255.0f + 0.5f);
  315. output[1] = uint8_t(input[1]*255.0f + 0.5f);
  316. output[2] = uint8_t(input[2]*255.0f + 0.5f);
  317. output[3] = uint8_t(input[3]*255.0f + 0.5f);
  318. }
  319. }
  320. imageEncodeFromRgba8(_dst, temp, _width, _height, _format);
  321. BX_FREE(_allocator, temp);
  322. }
  323. return true;
  324. default:
  325. return imageConvert(_dst, format, _src, TextureFormat::RGBA32F, _width, _height);
  326. }
  327. return false;
  328. }
  329. void imageRgba32f11to01(void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src)
  330. {
  331. const uint8_t* src = (const uint8_t*)_src;
  332. uint8_t* dst = (uint8_t*)_dst;
  333. for (uint32_t yy = 0; yy < _height; ++yy)
  334. {
  335. for (uint32_t xx = 0; xx < _width; ++xx)
  336. {
  337. const uint32_t offset = yy*_pitch + xx * 16;
  338. const float* input = (const float*)&src[offset];
  339. float* output = (float*)&dst[offset];
  340. output[0] = input[0]*0.5f + 0.5f;
  341. output[1] = input[1]*0.5f + 0.5f;
  342. output[2] = input[2]*0.5f + 0.5f;
  343. output[3] = input[3]*0.5f + 0.5f;
  344. }
  345. }
  346. }
  347. static void edtaa3(bx::AllocatorI* _allocator, double* _dst, uint32_t _width, uint32_t _height, double* _src)
  348. {
  349. const uint32_t numPixels = _width*_height;
  350. short* xdist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  351. short* ydist = (short *)BX_ALLOC(_allocator, numPixels*sizeof(short) );
  352. double* gx = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  353. double* gy = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  354. ::computegradient(_src, _width, _height, gx, gy);
  355. ::edtaa3(_src, gx, gy, _width, _height, xdist, ydist, _dst);
  356. for (uint32_t ii = 0; ii < numPixels; ++ii)
  357. {
  358. if (_dst[ii] < 0.0)
  359. {
  360. _dst[ii] = 0.0;
  361. }
  362. }
  363. BX_FREE(_allocator, xdist);
  364. BX_FREE(_allocator, ydist);
  365. BX_FREE(_allocator, gx);
  366. BX_FREE(_allocator, gy);
  367. }
  368. inline double min(double _a, double _b)
  369. {
  370. return _a > _b ? _b : _a;
  371. }
  372. inline double max(double _a, double _b)
  373. {
  374. return _a > _b ? _a : _b;
  375. }
  376. inline double clamp(double _val, double _min, double _max)
  377. {
  378. return max(min(_val, _max), _min);
  379. }
  380. void imageMakeDist(bx::AllocatorI* _allocator, void* _dst, uint32_t _width, uint32_t _height, uint32_t _pitch, float _edge, const void* _src)
  381. {
  382. const uint32_t numPixels = _width*_height;
  383. double* imgIn = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  384. double* outside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  385. double* inside = (double*)BX_ALLOC(_allocator, numPixels*sizeof(double) );
  386. for (uint32_t yy = 0; yy < _height; ++yy)
  387. {
  388. const uint8_t* src = (const uint8_t*)_src + yy*_pitch;
  389. double* dst = &imgIn[yy*_width];
  390. for (uint32_t xx = 0; xx < _width; ++xx)
  391. {
  392. dst[xx] = double(src[xx])/255.0;
  393. }
  394. }
  395. edtaa3(_allocator, outside, _width, _height, imgIn);
  396. for (uint32_t ii = 0; ii < numPixels; ++ii)
  397. {
  398. imgIn[ii] = 1.0 - imgIn[ii];
  399. }
  400. edtaa3(_allocator, inside, _width, _height, imgIn);
  401. BX_FREE(_allocator, imgIn);
  402. uint8_t* dst = (uint8_t*)_dst;
  403. double edgeOffset = _edge*0.5;
  404. double invEdge = 1.0/_edge;
  405. for (uint32_t ii = 0; ii < numPixels; ++ii)
  406. {
  407. double dist = clamp( ( (outside[ii] - inside[ii])+edgeOffset) * invEdge, 0.0, 1.0);
  408. dst[ii] = 255-uint8_t(dist * 255.0);
  409. }
  410. BX_FREE(_allocator, inside);
  411. BX_FREE(_allocator, outside);
  412. }
  413. } // namespace bgfx
  414. void help(const char* _error = NULL)
  415. {
  416. if (NULL != _error)
  417. {
  418. fprintf(stderr, "Error:\n%s\n\n", _error);
  419. }
  420. fprintf(stderr
  421. , "texturec, bgfx texture compiler tool\n"
  422. "Copyright 2011-2016 Branimir Karadzic. All rights reserved.\n"
  423. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  424. );
  425. fprintf(stderr
  426. , "Usage: texturec -f <in> -o <out> [-t <format>]\n"
  427. "\n"
  428. "Supported input file types:\n"
  429. " *.png Portable Network Graphics\n"
  430. " *.tga Targa\n"
  431. " *.dds Direct Draw Surface\n"
  432. " *.ktx Khronos Texture\n"
  433. " *.pvr PowerVR\n"
  434. "\n"
  435. "Options:\n"
  436. " -f <file path> Input file path.\n"
  437. " -o <file path> Output file path (file will be written in KTX format).\n"
  438. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  439. " -m, --mips Generate mip-maps.\n"
  440. " -n, --normalmap Input texture is normal map.\n"
  441. " --sdf <edge> Compute SDF texture.\n"
  442. " --iqa Image Quality Assesment\n"
  443. "\n"
  444. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  445. );
  446. }
  447. int main(int _argc, const char* _argv[])
  448. {
  449. bx::CommandLine cmdLine(_argc, _argv);
  450. if (cmdLine.hasArg('h', "help") )
  451. {
  452. help();
  453. return EXIT_FAILURE;
  454. }
  455. const char* inputFileName = cmdLine.findOption('f');
  456. if (NULL == inputFileName)
  457. {
  458. help("Input file must be specified.");
  459. return EXIT_FAILURE;
  460. }
  461. const char* outputFileName = cmdLine.findOption('o');
  462. if (NULL == outputFileName)
  463. {
  464. help("Output file must be specified.");
  465. return EXIT_FAILURE;
  466. }
  467. bool sdf = false;
  468. double edge = 16.0;
  469. const char* edgeOpt = cmdLine.findOption("sdf");
  470. if (NULL != edgeOpt)
  471. {
  472. sdf = true;
  473. edge = atof(edgeOpt);
  474. }
  475. BX_UNUSED(sdf, edge);
  476. bx::CrtFileReader reader;
  477. if (!bx::open(&reader, inputFileName) )
  478. {
  479. help("Failed to open input file.");
  480. return EXIT_FAILURE;
  481. }
  482. const bool mips = cmdLine.hasArg('m', "mips");
  483. const bool normalMap = cmdLine.hasArg('n', "normalmap");
  484. const bool iqa = cmdLine.hasArg('\0', "iqa");
  485. const bgfx::Memory* mem;
  486. {
  487. uint32_t size = (uint32_t)bx::getSize(&reader);
  488. mem = bgfx::alloc(size);
  489. bx::read(&reader, mem->data, mem->size);
  490. bx::close(&reader);
  491. }
  492. {
  493. using namespace bgfx;
  494. uint8_t* decodedImage = NULL;
  495. ImageContainer imageContainer;
  496. bool loaded = imageParse(imageContainer, mem->data, mem->size, (void**)&decodedImage);
  497. if (NULL != decodedImage)
  498. {
  499. release(mem);
  500. mem = makeRef(imageContainer.m_data, imageContainer.m_size);
  501. }
  502. if (loaded)
  503. {
  504. const char* type = cmdLine.findOption('t');
  505. bgfx::TextureFormat::Enum format = imageContainer.m_format;
  506. if (NULL != type)
  507. {
  508. format = bgfx::getFormat(type);
  509. if (!isValid(format) )
  510. {
  511. help("Invalid format specified.");
  512. return EXIT_FAILURE;
  513. }
  514. }
  515. bx::CrtAllocator allocator;
  516. const Memory* output = NULL;
  517. ImageMip mip;
  518. if (imageGetRawData(imageContainer, 0, 0, mem->data, mem->size, mip) )
  519. {
  520. uint8_t numMips = mips
  521. ? imageGetNumMips(format, mip.m_width, mip.m_height)
  522. : 1
  523. ;
  524. void* temp = NULL;
  525. if (normalMap)
  526. {
  527. output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, 1, false, mips);
  528. ImageMip dstMip;
  529. imageGetRawData(imageContainer, 0, 0, NULL, 0, dstMip);
  530. if (mip.m_width != dstMip.m_width
  531. && mip.m_height != dstMip.m_height)
  532. {
  533. printf("Invalid input image size %dx%d, it must be at least %dx%d to be converted to %s format.\n"
  534. , mip.m_width
  535. , mip.m_height
  536. , dstMip.m_width
  537. , dstMip.m_height
  538. , getName(format)
  539. );
  540. return EXIT_FAILURE;
  541. }
  542. uint32_t size = imageGetSize(TextureFormat::RGBA32F, dstMip.m_width, dstMip.m_height);
  543. temp = BX_ALLOC(&allocator, size);
  544. float* rgba = (float*)temp;
  545. float* rgbaDst = (float*)BX_ALLOC(&allocator, size);
  546. imageDecodeToRgba32f(&allocator
  547. , rgba
  548. , mip.m_data
  549. , mip.m_width
  550. , mip.m_height
  551. , mip.m_width*mip.m_bpp/8
  552. , mip.m_format
  553. );
  554. if (TextureFormat::BC5 != mip.m_format)
  555. {
  556. for (uint32_t yy = 0; yy < mip.m_height; ++yy)
  557. {
  558. for (uint32_t xx = 0; xx < mip.m_width; ++xx)
  559. {
  560. const uint32_t offset = (yy*mip.m_width + xx) * 4;
  561. float* inout = &rgba[offset];
  562. inout[0] = inout[0] * 2.0f - 1.0f;
  563. inout[1] = inout[1] * 2.0f - 1.0f;
  564. inout[2] = inout[2] * 2.0f - 1.0f;
  565. inout[3] = inout[3] * 2.0f - 1.0f;
  566. }
  567. }
  568. }
  569. imageRgba32f11to01(rgbaDst, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
  570. imageEncodeFromRgba32f(&allocator, output->data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  571. for (uint8_t lod = 1; lod < numMips; ++lod)
  572. {
  573. imageRgba32fDownsample2x2NormalMap(dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba, rgba);
  574. imageRgba32f11to01(rgbaDst, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
  575. imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
  576. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  577. imageEncodeFromRgba32f(&allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  578. }
  579. BX_FREE(&allocator, rgbaDst);
  580. }
  581. else if (8 != getBlockInfo(imageContainer.m_format).rBits)
  582. {
  583. output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, 1, false, mips);
  584. ImageMip dstMip;
  585. imageGetRawData(imageContainer, 0, 0, NULL, 0, dstMip);
  586. if (mip.m_width != dstMip.m_width
  587. && mip.m_height != dstMip.m_height)
  588. {
  589. printf("Invalid input image size %dx%d, it must be at least %dx%d to be converted to %s format.\n"
  590. , mip.m_width
  591. , mip.m_height
  592. , dstMip.m_width
  593. , dstMip.m_height
  594. , getName(format)
  595. );
  596. return EXIT_FAILURE;
  597. }
  598. uint32_t size = imageGetSize(TextureFormat::RGBA32F, dstMip.m_width, dstMip.m_height);
  599. temp = BX_ALLOC(&allocator, size);
  600. float* rgba = (float*)temp;
  601. float* rgbaDst = (float*)BX_ALLOC(&allocator, size);
  602. imageDecodeToRgba32f(&allocator
  603. , rgba
  604. , mip.m_data
  605. , mip.m_width
  606. , mip.m_height
  607. , mip.m_width*mip.m_bpp/8
  608. , mip.m_format
  609. );
  610. imageEncodeFromRgba32f(&allocator, output->data, rgba, dstMip.m_width, dstMip.m_height, format);
  611. imageRgba32fToLinear(rgba
  612. , mip.m_width
  613. , mip.m_height
  614. , mip.m_width*mip.m_bpp/8
  615. , rgba
  616. );
  617. for (uint8_t lod = 1; lod < numMips; ++lod)
  618. {
  619. imageRgba32fLinearDownsample2x2(dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba, rgba);
  620. imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
  621. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  622. imageRgba32fToGamma(rgbaDst
  623. , mip.m_width
  624. , mip.m_height
  625. , mip.m_width*mip.m_bpp/8
  626. , rgba
  627. );
  628. imageEncodeFromRgba32f(&allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  629. }
  630. BX_FREE(&allocator, rgbaDst);
  631. }
  632. else
  633. {
  634. output = imageAlloc(imageContainer, format, mip.m_width, mip.m_height, 0, 1, false, mips);
  635. ImageMip dstMip;
  636. imageGetRawData(imageContainer, 0, 0, NULL, 0, dstMip);
  637. if (mip.m_width != dstMip.m_width
  638. && mip.m_height != dstMip.m_height)
  639. {
  640. printf("Invalid input image size %dx%d, it must be at least %dx%d to be converted to %s format.\n"
  641. , mip.m_width
  642. , mip.m_height
  643. , dstMip.m_width
  644. , dstMip.m_height
  645. , getName(format)
  646. );
  647. return EXIT_FAILURE;
  648. }
  649. uint32_t size = imageGetSize(TextureFormat::RGBA8, dstMip.m_width, dstMip.m_height);
  650. temp = BX_ALLOC(&allocator, size);
  651. memset(temp, 0, size);
  652. uint8_t* rgba = (uint8_t*)temp;
  653. imageDecodeToRgba8(rgba
  654. , mip.m_data
  655. , mip.m_width
  656. , mip.m_height
  657. , mip.m_width*mip.m_bpp/8
  658. , mip.m_format
  659. );
  660. void* ref = NULL;
  661. if (iqa)
  662. {
  663. ref = BX_ALLOC(&allocator, size);
  664. memcpy(ref, rgba, size);
  665. }
  666. imageEncodeFromRgba8(output->data, rgba, dstMip.m_width, dstMip.m_height, format);
  667. for (uint8_t lod = 1; lod < numMips; ++lod)
  668. {
  669. imageRgba8Downsample2x2(dstMip.m_width, dstMip.m_height, dstMip.m_width*4, rgba, rgba);
  670. imageGetRawData(imageContainer, 0, lod, output->data, output->size, dstMip);
  671. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  672. imageEncodeFromRgba8(data, rgba, dstMip.m_width, dstMip.m_height, format);
  673. }
  674. if (NULL != ref)
  675. {
  676. imageDecodeToRgba8(rgba
  677. , output->data
  678. , mip.m_width
  679. , mip.m_height
  680. , mip.m_width*mip.m_bpp/8
  681. , format
  682. );
  683. static const iqa_ssim_args args =
  684. {
  685. 0.39f, // alpha
  686. 0.731f, // beta
  687. 1.12f, // gamma
  688. 187, // L
  689. 0.025987f, // K1
  690. 0.0173f, // K2
  691. 1 // factor
  692. };
  693. float result = iqa_ssim( (uint8_t*)ref
  694. , rgba
  695. , mip.m_width
  696. , mip.m_height
  697. , mip.m_width*mip.m_bpp/8
  698. , 0
  699. , &args
  700. );
  701. printf("%f\n", result);
  702. BX_FREE(&allocator, ref);
  703. }
  704. }
  705. BX_FREE(&allocator, temp);
  706. }
  707. if (NULL != output)
  708. {
  709. bx::CrtFileWriter writer;
  710. if (bx::open(&writer, outputFileName) )
  711. {
  712. if (NULL != bx::stristr(outputFileName, ".ktx") )
  713. {
  714. imageWriteKtx(&writer, imageContainer, output->data, output->size);
  715. }
  716. bx::close(&writer);
  717. }
  718. else
  719. {
  720. help("Failed to open output file.");
  721. return EXIT_FAILURE;
  722. }
  723. imageFree(output);
  724. }
  725. else
  726. {
  727. help("No output generated.");
  728. return EXIT_FAILURE;
  729. }
  730. }
  731. else
  732. {
  733. help("Failed to load input file.");
  734. return EXIT_FAILURE;
  735. }
  736. release(mem);
  737. }
  738. return EXIT_SUCCESS;
  739. }