texturec.cpp 13 KB

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