texturec.cpp 13 KB

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