texturec.cpp 22 KB

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