texturec.cpp 23 KB

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