texturec.cpp 14 KB

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