texturec.cpp 22 KB

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