texturec.cpp 23 KB

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