texturec.cpp 10 KB

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