texturec.cpp 13 KB

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