texturec.cpp 12 KB

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