texturec.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 <bx/allocator.h>
  7. #include <bx/readerwriter.h>
  8. #include <bx/endian.h>
  9. #include <bimg/decode.h>
  10. #include <bimg/encode.h>
  11. #if 0
  12. # define DBG(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  13. #else
  14. # define DBG(...) BX_NOOP()
  15. #endif // DEBUG
  16. #include <bx/bx.h>
  17. #include <bx/commandline.h>
  18. #include <bx/crtimpl.h>
  19. #include <bx/uint32_t.h>
  20. struct Options
  21. {
  22. Options()
  23. : maxSize(UINT32_MAX)
  24. , edge(0.0f)
  25. , format(bimg::TextureFormat::Count)
  26. , mips(false)
  27. , normalMap(false)
  28. , iqa(false)
  29. , sdf(false)
  30. {
  31. }
  32. void dump()
  33. {
  34. DBG("Options:\n"
  35. "\t maxSize: %d\n"
  36. "\t edge: %f\n"
  37. "\t format: %s\n"
  38. "\t mips: %s\n"
  39. "\tnormalMap: %s\n"
  40. "\t iqa: %s\n"
  41. "\t sdf: %s\n"
  42. , maxSize
  43. , edge
  44. , bimg::getName(format)
  45. , mips ? "true" : "false"
  46. , normalMap ? "true" : "false"
  47. , iqa ? "true" : "false"
  48. , sdf ? "true" : "false"
  49. );
  50. }
  51. uint32_t maxSize;
  52. float edge;
  53. bimg::TextureFormat::Enum format;
  54. bool mips;
  55. bool normalMap;
  56. bool iqa;
  57. bool sdf;
  58. };
  59. bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData, uint32_t _inputSize, const Options& _options)
  60. {
  61. const uint8_t* inputData = (uint8_t*)_inputData;
  62. bimg::ImageContainer* output = NULL;
  63. bimg::ImageContainer* input = bimg::imageParse(_allocator, inputData, _inputSize);
  64. if (NULL != input)
  65. {
  66. bimg::TextureFormat::Enum format = input->m_format;
  67. if (bimg::TextureFormat::Count != _options.format)
  68. {
  69. format = _options.format;
  70. }
  71. if (input->m_width > _options.maxSize
  72. || input->m_height > _options.maxSize)
  73. {
  74. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input);
  75. uint32_t width;
  76. uint32_t height;
  77. if (input->m_width > input->m_height)
  78. {
  79. width = _options.maxSize;
  80. height = input->m_height * width / input->m_width;
  81. }
  82. else
  83. {
  84. height = _options.maxSize;
  85. width = input->m_width * height / input->m_height;
  86. }
  87. bimg::ImageContainer* dst = bimg::imageAlloc(_allocator
  88. , bimg::TextureFormat::RGBA32F
  89. , uint16_t(width)
  90. , uint16_t(height)
  91. , 1, 1, false, false
  92. );
  93. bimg::imageResizeRgba32fLinear(dst, src);
  94. bimg::imageFree(src);
  95. bimg::imageFree(input);
  96. input = bimg::imageConvert(_allocator, format, *dst);
  97. bimg::imageFree(dst);
  98. }
  99. bimg::ImageMip mip;
  100. if (bimg::imageGetRawData(*input, 0, 0, input->m_data, input->m_size, mip) )
  101. {
  102. uint8_t numMips = _options.mips
  103. ? bimg::imageGetNumMips(format, uint16_t(mip.m_width), uint16_t(mip.m_height) )
  104. : 1
  105. ;
  106. void* temp = NULL;
  107. if (_options.normalMap)
  108. {
  109. output = bimg::imageAlloc(
  110. _allocator
  111. , format
  112. , uint16_t(mip.m_width)
  113. , uint16_t(mip.m_height)
  114. , 0
  115. , 1
  116. , false
  117. , _options.mips
  118. );
  119. bimg::ImageMip dstMip;
  120. bimg::imageGetRawData(*output, 0, 0, NULL, 0, dstMip);
  121. if (mip.m_width != dstMip.m_width
  122. && mip.m_height != dstMip.m_height)
  123. {
  124. printf("Invalid input image size %dx%d, it must be at least %dx%d to be converted to %s format.\n"
  125. , mip.m_width
  126. , mip.m_height
  127. , dstMip.m_width
  128. , dstMip.m_height
  129. , getName(format)
  130. );
  131. return NULL;
  132. }
  133. uint32_t size = bimg::imageGetSize(
  134. NULL
  135. , uint16_t(dstMip.m_width)
  136. , uint16_t(dstMip.m_height)
  137. , 0
  138. , false
  139. , false
  140. , 1
  141. , bimg::TextureFormat::RGBA32F
  142. );
  143. temp = BX_ALLOC(_allocator, size);
  144. float* rgba = (float*)temp;
  145. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  146. bimg::imageDecodeToRgba32f(_allocator
  147. , rgba
  148. , mip.m_data
  149. , mip.m_width
  150. , mip.m_height
  151. , mip.m_width*mip.m_bpp/8
  152. , mip.m_format
  153. );
  154. if (bimg::TextureFormat::BC5 != mip.m_format)
  155. {
  156. for (uint32_t yy = 0; yy < mip.m_height; ++yy)
  157. {
  158. for (uint32_t xx = 0; xx < mip.m_width; ++xx)
  159. {
  160. const uint32_t offset = (yy*mip.m_width + xx) * 4;
  161. float* inout = &rgba[offset];
  162. inout[0] = inout[0] * 2.0f - 1.0f;
  163. inout[1] = inout[1] * 2.0f - 1.0f;
  164. inout[2] = inout[2] * 2.0f - 1.0f;
  165. inout[3] = inout[3] * 2.0f - 1.0f;
  166. }
  167. }
  168. }
  169. bimg::imageRgba32f11to01(rgbaDst, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
  170. bimg::imageEncodeFromRgba32f(_allocator, output->m_data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  171. for (uint8_t lod = 1; lod < numMips; ++lod)
  172. {
  173. bimg::imageRgba32fDownsample2x2NormalMap(rgba, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
  174. bimg::imageRgba32f11to01(rgbaDst, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
  175. bimg::imageGetRawData(*output, 0, lod, output->m_data, output->m_size, dstMip);
  176. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  177. bimg::imageEncodeFromRgba32f(_allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  178. }
  179. BX_FREE(_allocator, rgbaDst);
  180. }
  181. else if (!bimg::isCompressed(input->m_format)
  182. && 8 != bimg::getBlockInfo(input->m_format).rBits)
  183. {
  184. output = bimg::imageAlloc(
  185. _allocator
  186. , format
  187. , uint16_t(mip.m_width)
  188. , uint16_t(mip.m_height)
  189. , 0
  190. , 1
  191. , false
  192. , _options.mips
  193. );
  194. bimg::ImageMip dstMip;
  195. bimg::imageGetRawData(*output, 0, 0, NULL, 0, dstMip);
  196. if (mip.m_width != dstMip.m_width
  197. && mip.m_height != dstMip.m_height)
  198. {
  199. printf("Invalid input image size %dx%d, it must be at least %dx%d to be converted to %s format.\n"
  200. , mip.m_width
  201. , mip.m_height
  202. , dstMip.m_width
  203. , dstMip.m_height
  204. , getName(format)
  205. );
  206. return NULL;
  207. }
  208. uint32_t size = bimg::imageGetSize(
  209. NULL
  210. , uint16_t(dstMip.m_width)
  211. , uint16_t(dstMip.m_height)
  212. , 0
  213. , false
  214. , false
  215. , 1
  216. , bimg::TextureFormat::RGBA32F
  217. );
  218. temp = BX_ALLOC(_allocator, size);
  219. float* rgba = (float*)temp;
  220. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  221. bimg::imageDecodeToRgba32f(_allocator
  222. , rgba
  223. , mip.m_data
  224. , mip.m_width
  225. , mip.m_height
  226. , mip.m_width*16
  227. , mip.m_format
  228. );
  229. bimg::imageEncodeFromRgba32f(_allocator, output->m_data, rgba, dstMip.m_width, dstMip.m_height, format);
  230. bimg::imageRgba32fToLinear(rgba
  231. , mip.m_width
  232. , mip.m_height
  233. , mip.m_width*mip.m_bpp/8
  234. , rgba
  235. );
  236. for (uint8_t lod = 1; lod < numMips; ++lod)
  237. {
  238. bimg::imageRgba32fLinearDownsample2x2(rgba, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
  239. bimg::imageGetRawData(*output, 0, lod, output->m_data, output->m_size, dstMip);
  240. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  241. bimg::imageRgba32fToGamma(rgbaDst
  242. , mip.m_width
  243. , mip.m_height
  244. , mip.m_width*mip.m_bpp/8
  245. , rgba
  246. );
  247. bimg::imageEncodeFromRgba32f(_allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  248. }
  249. BX_FREE(_allocator, rgbaDst);
  250. }
  251. else
  252. {
  253. output = bimg::imageAlloc(
  254. _allocator
  255. , format
  256. , uint16_t(mip.m_width)
  257. , uint16_t(mip.m_height)
  258. , 0
  259. , 1
  260. , false
  261. , _options.mips
  262. );
  263. bimg::ImageMip dstMip;
  264. bimg::imageGetRawData(*output, 0, 0, NULL, 0, dstMip);
  265. if (mip.m_width != dstMip.m_width
  266. && mip.m_height != dstMip.m_height)
  267. {
  268. printf("Invalid input image size %dx%d, it must be at least %dx%d to be converted to %s format.\n"
  269. , mip.m_width
  270. , mip.m_height
  271. , dstMip.m_width
  272. , dstMip.m_height
  273. , getName(format)
  274. );
  275. return NULL;
  276. }
  277. uint32_t size = bimg::imageGetSize(
  278. NULL
  279. , uint16_t(dstMip.m_width)
  280. , uint16_t(dstMip.m_height)
  281. , 0
  282. , false
  283. , false
  284. , 1
  285. , bimg::TextureFormat::RGBA8
  286. );
  287. temp = BX_ALLOC(_allocator, size);
  288. bx::memSet(temp, 0, size);
  289. uint8_t* rgba = (uint8_t*)temp;
  290. bimg::imageDecodeToRgba8(rgba
  291. , mip.m_data
  292. , mip.m_width
  293. , mip.m_height
  294. , mip.m_width*4
  295. , mip.m_format
  296. );
  297. void* ref = NULL;
  298. if (_options.iqa)
  299. {
  300. ref = BX_ALLOC(_allocator, size);
  301. bx::memCopy(ref, rgba, size);
  302. }
  303. bimg::imageEncodeFromRgba8(output->m_data, rgba, dstMip.m_width, dstMip.m_height, format);
  304. for (uint8_t lod = 1; lod < numMips; ++lod)
  305. {
  306. bimg::imageRgba8Downsample2x2(rgba, dstMip.m_width, dstMip.m_height, dstMip.m_width*4, rgba);
  307. bimg::imageGetRawData(*output, 0, lod, output->m_data, output->m_size, dstMip);
  308. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  309. bimg::imageEncodeFromRgba8(data, rgba, dstMip.m_width, dstMip.m_height, format);
  310. }
  311. if (NULL != ref)
  312. {
  313. bimg::imageDecodeToRgba8(rgba
  314. , output->m_data
  315. , mip.m_width
  316. , mip.m_height
  317. , mip.m_width*mip.m_bpp/8
  318. , format
  319. );
  320. float result = bimg::imageQualityRgba8(
  321. ref
  322. , rgba
  323. , uint16_t(mip.m_width)
  324. , uint16_t(mip.m_height)
  325. );
  326. printf("%f\n", result);
  327. BX_FREE(_allocator, ref);
  328. }
  329. }
  330. BX_FREE(_allocator, temp);
  331. }
  332. }
  333. return output;
  334. }
  335. void help(const char* _error = NULL)
  336. {
  337. if (NULL != _error)
  338. {
  339. fprintf(stderr, "Error:\n%s\n\n", _error);
  340. }
  341. fprintf(stderr
  342. , "texturec, bgfx texture compiler tool\n"
  343. "Copyright 2011-2017 Branimir Karadzic. All rights reserved.\n"
  344. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  345. );
  346. fprintf(stderr
  347. , "Usage: texturec -f <in> -o <out> [-t <format>]\n"
  348. "\n"
  349. "Supported input file types:\n"
  350. " *.png Portable Network Graphics\n"
  351. " *.tga Targa\n"
  352. " *.dds Direct Draw Surface\n"
  353. " *.ktx Khronos Texture\n"
  354. " *.pvr PowerVR\n"
  355. "\n"
  356. "Options:\n"
  357. " -f <file path> Input file path.\n"
  358. " -o <file path> Output file path (file will be written in KTX format).\n"
  359. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  360. " -m, --mips Generate mip-maps.\n"
  361. " -n, --normalmap Input texture is normal map.\n"
  362. " --sdf <edge> Compute SDF texture.\n"
  363. " --iqa Image Quality Assesment\n"
  364. " --max <max size> Maximum width/height (image will be scaled down and\n"
  365. " aspect ratio will be preserved.\n"
  366. " --as <extension> Save as.\n"
  367. "\n"
  368. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  369. );
  370. }
  371. int main(int _argc, const char* _argv[])
  372. {
  373. bx::CommandLine cmdLine(_argc, _argv);
  374. if (cmdLine.hasArg('h', "help") )
  375. {
  376. help();
  377. return EXIT_FAILURE;
  378. }
  379. const char* inputFileName = cmdLine.findOption('f');
  380. if (NULL == inputFileName)
  381. {
  382. help("Input file must be specified.");
  383. return EXIT_FAILURE;
  384. }
  385. const char* outputFileName = cmdLine.findOption('o');
  386. if (NULL == outputFileName)
  387. {
  388. help("Output file must be specified.");
  389. return EXIT_FAILURE;
  390. }
  391. const char* saveAs = cmdLine.findOption("as");
  392. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".ktx") : saveAs;
  393. if (NULL == saveAs)
  394. {
  395. help("Output file format must be specified.");
  396. return EXIT_FAILURE;
  397. }
  398. Options options;
  399. const char* edgeOpt = cmdLine.findOption("sdf");
  400. if (NULL != edgeOpt)
  401. {
  402. options.sdf = true;
  403. options.edge = (float)atof(edgeOpt);
  404. }
  405. options.mips = cmdLine.hasArg('m', "mips");
  406. options.normalMap = cmdLine.hasArg('n', "normalmap");
  407. options.iqa = cmdLine.hasArg('\0', "iqa");
  408. const char* maxSize = cmdLine.findOption("max");
  409. if (NULL != maxSize)
  410. {
  411. options.maxSize = atoi(maxSize);
  412. }
  413. options.format = bimg::TextureFormat::Count;
  414. const char* type = cmdLine.findOption('t');
  415. if (NULL != type)
  416. {
  417. options.format = bimg::getFormat(type);
  418. if (!bimg::isValid(options.format) )
  419. {
  420. help("Invalid format specified.");
  421. return EXIT_FAILURE;
  422. }
  423. }
  424. bx::CrtFileReader reader;
  425. if (!bx::open(&reader, inputFileName) )
  426. {
  427. help("Failed to open input file.");
  428. return EXIT_FAILURE;
  429. }
  430. bx::CrtAllocator allocator;
  431. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  432. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  433. bx::Error err;
  434. bx::read(&reader, inputData, inputSize, &err);
  435. bx::close(&reader);
  436. if (!err.isOk() )
  437. {
  438. help("Failed to read input file.");
  439. return EXIT_FAILURE;
  440. }
  441. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options);
  442. BX_FREE(&allocator, inputData);
  443. if (NULL != output)
  444. {
  445. bx::CrtFileWriter writer;
  446. if (bx::open(&writer, outputFileName) )
  447. {
  448. if (NULL != bx::strFindI(saveAs, "ktx") )
  449. {
  450. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size);
  451. }
  452. bx::close(&writer);
  453. }
  454. else
  455. {
  456. help("Failed to open output file.");
  457. return EXIT_FAILURE;
  458. }
  459. bimg::imageFree(output);
  460. }
  461. else
  462. {
  463. help("No output generated.");
  464. return EXIT_FAILURE;
  465. }
  466. return EXIT_SUCCESS;
  467. }