texturec.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 (8 != bimg::getBlockInfo(input->m_format).rBits)
  152. {
  153. output = bimg::imageAlloc(_allocator, format, uint16_t(mip.m_width), uint16_t(mip.m_height), 0, 1, false, _options.mips);
  154. bimg::ImageMip dstMip;
  155. bimg::imageGetRawData(*output, 0, 0, NULL, 0, dstMip);
  156. if (mip.m_width != dstMip.m_width
  157. && mip.m_height != dstMip.m_height)
  158. {
  159. printf("Invalid input image size %dx%d, it must be at least %dx%d to be converted to %s format.\n"
  160. , mip.m_width
  161. , mip.m_height
  162. , dstMip.m_width
  163. , dstMip.m_height
  164. , getName(format)
  165. );
  166. return NULL;
  167. }
  168. uint32_t size = bimg::imageGetSize(
  169. NULL
  170. , uint16_t(dstMip.m_width)
  171. , uint16_t(dstMip.m_height)
  172. , 0
  173. , false
  174. , false
  175. , 1
  176. , bimg::TextureFormat::RGBA32F
  177. );
  178. temp = BX_ALLOC(_allocator, size);
  179. float* rgba = (float*)temp;
  180. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  181. bimg::imageDecodeToRgba32f(_allocator
  182. , rgba
  183. , mip.m_data
  184. , mip.m_width
  185. , mip.m_height
  186. , mip.m_width*mip.m_bpp/8
  187. , mip.m_format
  188. );
  189. bimg::imageEncodeFromRgba32f(_allocator, output->m_data, rgba, dstMip.m_width, dstMip.m_height, format);
  190. bimg::imageRgba32fToLinear(rgba
  191. , mip.m_width
  192. , mip.m_height
  193. , mip.m_width*mip.m_bpp/8
  194. , rgba
  195. );
  196. for (uint8_t lod = 1; lod < numMips; ++lod)
  197. {
  198. bimg::imageRgba32fLinearDownsample2x2(rgba, dstMip.m_width, dstMip.m_height, dstMip.m_width*16, rgba);
  199. bimg::imageGetRawData(*output, 0, lod, output->m_data, output->m_size, dstMip);
  200. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  201. bimg::imageRgba32fToGamma(rgbaDst
  202. , mip.m_width
  203. , mip.m_height
  204. , mip.m_width*mip.m_bpp/8
  205. , rgba
  206. );
  207. bimg::imageEncodeFromRgba32f(_allocator, data, rgbaDst, dstMip.m_width, dstMip.m_height, format);
  208. }
  209. BX_FREE(_allocator, rgbaDst);
  210. }
  211. else
  212. {
  213. output = bimg::imageAlloc(_allocator, format, uint16_t(mip.m_width), uint16_t(mip.m_height), 0, 1, false, _options.mips);
  214. bimg::ImageMip dstMip;
  215. bimg::imageGetRawData(*output, 0, 0, NULL, 0, dstMip);
  216. if (mip.m_width != dstMip.m_width
  217. && mip.m_height != dstMip.m_height)
  218. {
  219. printf("Invalid input image size %dx%d, it must be at least %dx%d to be converted to %s format.\n"
  220. , mip.m_width
  221. , mip.m_height
  222. , dstMip.m_width
  223. , dstMip.m_height
  224. , getName(format)
  225. );
  226. return NULL;
  227. }
  228. uint32_t size = bimg::imageGetSize(
  229. NULL
  230. , uint16_t(dstMip.m_width)
  231. , uint16_t(dstMip.m_height)
  232. , 0
  233. , false
  234. , false
  235. , 1
  236. , bimg::TextureFormat::RGBA8
  237. );
  238. temp = BX_ALLOC(_allocator, size);
  239. bx::memSet(temp, 0, size);
  240. uint8_t* rgba = (uint8_t*)temp;
  241. bimg::imageDecodeToRgba8(rgba
  242. , mip.m_data
  243. , mip.m_width
  244. , mip.m_height
  245. , mip.m_width*mip.m_bpp/8
  246. , mip.m_format
  247. );
  248. void* ref = NULL;
  249. if (_options.iqa)
  250. {
  251. ref = BX_ALLOC(_allocator, size);
  252. bx::memCopy(ref, rgba, size);
  253. }
  254. bimg::imageEncodeFromRgba8(output->m_data, rgba, dstMip.m_width, dstMip.m_height, format);
  255. for (uint8_t lod = 1; lod < numMips; ++lod)
  256. {
  257. bimg::imageRgba8Downsample2x2(rgba, dstMip.m_width, dstMip.m_height, dstMip.m_width*4, rgba);
  258. bimg::imageGetRawData(*output, 0, lod, output->m_data, output->m_size, dstMip);
  259. uint8_t* data = const_cast<uint8_t*>(dstMip.m_data);
  260. bimg::imageEncodeFromRgba8(data, rgba, dstMip.m_width, dstMip.m_height, format);
  261. }
  262. if (NULL != ref)
  263. {
  264. bimg::imageDecodeToRgba8(rgba
  265. , output->m_data
  266. , mip.m_width
  267. , mip.m_height
  268. , mip.m_width*mip.m_bpp/8
  269. , format
  270. );
  271. float result = bimg::imageQualityRgba8(
  272. ref
  273. , rgba
  274. , uint16_t(mip.m_width)
  275. , uint16_t(mip.m_height)
  276. );
  277. printf("%f\n", result);
  278. BX_FREE(_allocator, ref);
  279. }
  280. }
  281. BX_FREE(_allocator, temp);
  282. }
  283. }
  284. return output;
  285. }
  286. void help(const char* _error = NULL)
  287. {
  288. if (NULL != _error)
  289. {
  290. fprintf(stderr, "Error:\n%s\n\n", _error);
  291. }
  292. fprintf(stderr
  293. , "texturec, bgfx texture compiler tool\n"
  294. "Copyright 2011-2017 Branimir Karadzic. All rights reserved.\n"
  295. "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
  296. );
  297. fprintf(stderr
  298. , "Usage: texturec -f <in> -o <out> [-t <format>]\n"
  299. "\n"
  300. "Supported input file types:\n"
  301. " *.png Portable Network Graphics\n"
  302. " *.tga Targa\n"
  303. " *.dds Direct Draw Surface\n"
  304. " *.ktx Khronos Texture\n"
  305. " *.pvr PowerVR\n"
  306. "\n"
  307. "Options:\n"
  308. " -f <file path> Input file path.\n"
  309. " -o <file path> Output file path (file will be written in KTX format).\n"
  310. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  311. " -m, --mips Generate mip-maps.\n"
  312. " -n, --normalmap Input texture is normal map.\n"
  313. " --sdf <edge> Compute SDF texture.\n"
  314. " --iqa Image Quality Assesment\n"
  315. " --max <max size> Maximum width/height (image will be scaled down and\n"
  316. " aspect ratio will be preserved.\n"
  317. "\n"
  318. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  319. );
  320. }
  321. int main(int _argc, const char* _argv[])
  322. {
  323. bx::CommandLine cmdLine(_argc, _argv);
  324. if (cmdLine.hasArg('h', "help") )
  325. {
  326. help();
  327. return EXIT_FAILURE;
  328. }
  329. const char* inputFileName = cmdLine.findOption('f');
  330. if (NULL == inputFileName)
  331. {
  332. help("Input file must be specified.");
  333. return EXIT_FAILURE;
  334. }
  335. const char* outputFileName = cmdLine.findOption('o');
  336. if (NULL == outputFileName)
  337. {
  338. help("Output file must be specified.");
  339. return EXIT_FAILURE;
  340. }
  341. Options options;
  342. const char* edgeOpt = cmdLine.findOption("sdf");
  343. if (NULL != edgeOpt)
  344. {
  345. options.sdf = true;
  346. options.edge = (float)atof(edgeOpt);
  347. }
  348. options.mips = cmdLine.hasArg('m', "mips");
  349. options.normalMap = cmdLine.hasArg('n', "normalmap");
  350. options.iqa = cmdLine.hasArg('\0', "iqa");
  351. const char* maxSize = cmdLine.findOption("max");
  352. if (NULL != maxSize)
  353. {
  354. options.maxSize = atoi(maxSize);
  355. }
  356. bx::CrtFileReader reader;
  357. if (!bx::open(&reader, inputFileName) )
  358. {
  359. help("Failed to open input file.");
  360. return EXIT_FAILURE;
  361. }
  362. options.format = bimg::TextureFormat::Count;
  363. const char* type = cmdLine.findOption('t');
  364. if (NULL != type)
  365. {
  366. options.format = bimg::getFormat(type);
  367. if (!bimg::isValid(options.format) )
  368. {
  369. help("Invalid format specified.");
  370. return EXIT_FAILURE;
  371. }
  372. }
  373. bx::CrtAllocator allocator;
  374. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  375. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  376. bx::read(&reader, inputData, inputSize);
  377. bx::close(&reader);
  378. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options);
  379. BX_FREE(&allocator, inputData);
  380. if (NULL != output)
  381. {
  382. bx::CrtFileWriter writer;
  383. if (bx::open(&writer, outputFileName) )
  384. {
  385. if (NULL != bx::stristr(outputFileName, ".ktx") )
  386. {
  387. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size);
  388. }
  389. bx::close(&writer);
  390. }
  391. else
  392. {
  393. help("Failed to open output file.");
  394. return EXIT_FAILURE;
  395. }
  396. bimg::imageFree(output);
  397. }
  398. else
  399. {
  400. help("No output generated.");
  401. return EXIT_FAILURE;
  402. }
  403. return EXIT_SUCCESS;
  404. }