texturec.cpp 15 KB

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