texturec.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg#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 <bx/math.h>
  10. #include <bimg/decode.h>
  11. #include <bimg/encode.h>
  12. #if 0
  13. # define DBG(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  14. #else
  15. # define DBG(...) BX_NOOP()
  16. #endif // DEBUG
  17. #include <bx/bx.h>
  18. #include <bx/commandline.h>
  19. #include <bx/file.h>
  20. #include <bx/uint32_t.h>
  21. #include <string>
  22. #define BIMG_TEXTUREC_VERSION_MAJOR 1
  23. #define BIMG_TEXTUREC_VERSION_MINOR 13
  24. struct Options
  25. {
  26. Options()
  27. : maxSize(UINT32_MAX)
  28. , edge(0.0f)
  29. , format(bimg::TextureFormat::Count)
  30. , quality(bimg::Quality::Default)
  31. , mips(false)
  32. , normalMap(false)
  33. , equirect(false)
  34. , iqa(false)
  35. , sdf(false)
  36. , alphaTest(false)
  37. {
  38. }
  39. void dump()
  40. {
  41. DBG("Options:\n"
  42. "\t maxSize: %d\n"
  43. "\t edge: %f\n"
  44. "\t format: %s\n"
  45. "\t mips: %s\n"
  46. "\tnormalMap: %s\n"
  47. "\t iqa: %s\n"
  48. "\t sdf: %s\n"
  49. , maxSize
  50. , edge
  51. , bimg::getName(format)
  52. , mips ? "true" : "false"
  53. , normalMap ? "true" : "false"
  54. , iqa ? "true" : "false"
  55. , sdf ? "true" : "false"
  56. );
  57. }
  58. uint32_t maxSize;
  59. float edge;
  60. bimg::TextureFormat::Enum format;
  61. bimg::Quality::Enum quality;
  62. bool mips;
  63. bool normalMap;
  64. bool equirect;
  65. bool iqa;
  66. bool sdf;
  67. bool alphaTest;
  68. };
  69. void imageRgba32fNormalize(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  70. {
  71. const uint8_t* src = (const uint8_t*)_src;
  72. uint8_t* dst = (uint8_t*)_dst;
  73. for (uint32_t yy = 0, ystep = _srcPitch; yy < _height; ++yy, src += ystep)
  74. {
  75. const float* rgba = (const float*)&src[0];
  76. for (uint32_t xx = 0; xx < _width; ++xx, rgba += 4, dst += 16)
  77. {
  78. float xyz[3];
  79. xyz[0] = rgba[0];
  80. xyz[1] = rgba[1];
  81. xyz[2] = rgba[2];
  82. bx::vec3Norm( (float*)dst, xyz);
  83. }
  84. }
  85. }
  86. bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData, uint32_t _inputSize, const Options& _options, bx::Error* _err)
  87. {
  88. BX_ERROR_SCOPE(_err);
  89. const uint8_t* inputData = (uint8_t*)_inputData;
  90. bimg::ImageContainer* output = NULL;
  91. bimg::ImageContainer* input = bimg::imageParse(_allocator, inputData, _inputSize, bimg::TextureFormat::Count, _err);
  92. if (!_err->isOk() )
  93. {
  94. return NULL;
  95. }
  96. if (NULL != input)
  97. {
  98. const bimg::TextureFormat::Enum inputFormat = input->m_format;
  99. bimg::TextureFormat::Enum outputFormat = input->m_format;
  100. if (bimg::TextureFormat::Count != _options.format)
  101. {
  102. outputFormat = _options.format;
  103. }
  104. if (_options.sdf)
  105. {
  106. outputFormat = bimg::TextureFormat::R8;
  107. }
  108. const bimg::ImageBlockInfo& inputBlockInfo = bimg::getBlockInfo(inputFormat);
  109. const bimg::ImageBlockInfo& outputBlockInfo = bimg::getBlockInfo(outputFormat);
  110. const uint32_t blockWidth = outputBlockInfo.blockWidth;
  111. const uint32_t blockHeight = outputBlockInfo.blockHeight;
  112. const uint32_t minBlockX = outputBlockInfo.minBlockX;
  113. const uint32_t minBlockY = outputBlockInfo.minBlockY;
  114. uint32_t outputWidth = bx::uint32_max(blockWidth * minBlockX, ( (input->m_width + blockWidth - 1) / blockWidth )*blockWidth);
  115. uint32_t outputHeight = bx::uint32_max(blockHeight * minBlockY, ( (input->m_height + blockHeight - 1) / blockHeight)*blockHeight);
  116. uint32_t outputDepth = input->m_depth;
  117. if (outputWidth > _options.maxSize
  118. || outputHeight > _options.maxSize
  119. || outputDepth > _options.maxSize)
  120. {
  121. if (outputDepth > outputWidth
  122. && outputDepth > outputHeight)
  123. {
  124. outputWidth = outputWidth * _options.maxSize / outputDepth;
  125. outputHeight = outputHeight * _options.maxSize / outputDepth;
  126. outputDepth = _options.maxSize;
  127. }
  128. else if (outputWidth > outputHeight)
  129. {
  130. outputDepth = outputDepth * _options.maxSize / outputWidth;
  131. outputHeight = outputHeight * _options.maxSize / outputWidth;
  132. outputWidth = _options.maxSize;
  133. }
  134. else
  135. {
  136. outputDepth = outputDepth * _options.maxSize / outputHeight;
  137. outputWidth = outputWidth * _options.maxSize / outputHeight;
  138. outputHeight = _options.maxSize;
  139. }
  140. }
  141. const bool needResize = false
  142. || input->m_width != outputWidth
  143. || input->m_height != outputHeight
  144. ;
  145. const bool passThru = true
  146. && !needResize
  147. && (1 < input->m_numMips) == _options.mips
  148. && !_options.sdf
  149. && !_options.alphaTest
  150. && !_options.normalMap
  151. && !_options.equirect
  152. && !_options.iqa
  153. ;
  154. if (needResize)
  155. {
  156. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input);
  157. bimg::ImageContainer* dst = bimg::imageAlloc(
  158. _allocator
  159. , bimg::TextureFormat::RGBA32F
  160. , uint16_t(outputWidth)
  161. , uint16_t(outputHeight)
  162. , uint16_t(outputDepth)
  163. , input->m_numLayers
  164. , input->m_cubeMap
  165. , false
  166. );
  167. bimg::imageResizeRgba32fLinear(dst, src);
  168. bimg::imageFree(src);
  169. bimg::imageFree(input);
  170. input = bimg::imageConvert(_allocator, inputFormat, *dst);
  171. bimg::imageFree(dst);
  172. }
  173. if (passThru)
  174. {
  175. if (inputFormat != outputFormat
  176. && bimg::isCompressed(outputFormat) )
  177. {
  178. output = bimg::imageEncode(_allocator, outputFormat, _options.quality, *input);
  179. }
  180. else
  181. {
  182. output = bimg::imageConvert(_allocator, outputFormat, *input);
  183. }
  184. bimg::imageFree(input);
  185. return output;
  186. }
  187. if (_options.equirect)
  188. {
  189. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input);
  190. bimg::imageFree(input);
  191. bimg::ImageContainer* dst = bimg::imageCubemapFromLatLongRgba32F(_allocator, *src, true, _err);
  192. bimg::imageFree(src);
  193. if (!_err->isOk() )
  194. {
  195. return NULL;
  196. }
  197. input = bimg::imageConvert(_allocator, inputFormat, *dst);
  198. bimg::imageFree(dst);
  199. }
  200. output = bimg::imageAlloc(
  201. _allocator
  202. , outputFormat
  203. , uint16_t(input->m_width)
  204. , uint16_t(input->m_height)
  205. , uint16_t(input->m_depth)
  206. , input->m_numLayers
  207. , input->m_cubeMap
  208. , _options.mips
  209. );
  210. const uint8_t numMips = output->m_numMips;
  211. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  212. for (uint16_t side = 0; side < numSides && _err->isOk(); ++side)
  213. {
  214. bimg::ImageMip mip;
  215. if (bimg::imageGetRawData(*input, side, 0, input->m_data, input->m_size, mip) )
  216. {
  217. bimg::ImageMip dstMip;
  218. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  219. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  220. void* temp = NULL;
  221. if (_options.normalMap)
  222. {
  223. uint32_t size = bimg::imageGetSize(
  224. NULL
  225. , uint16_t(dstMip.m_width)
  226. , uint16_t(dstMip.m_height)
  227. , 0
  228. , false
  229. , false
  230. , 1
  231. , bimg::TextureFormat::RGBA32F
  232. );
  233. temp = BX_ALLOC(_allocator, size);
  234. float* rgba = (float*)temp;
  235. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  236. bimg::imageDecodeToRgba32f(_allocator
  237. , rgba
  238. , mip.m_data
  239. , dstMip.m_width
  240. , dstMip.m_height
  241. , dstMip.m_depth
  242. , dstMip.m_width*16
  243. , mip.m_format
  244. );
  245. if (bimg::TextureFormat::BC5 != mip.m_format)
  246. {
  247. for (uint32_t yy = 0; yy < mip.m_height; ++yy)
  248. {
  249. for (uint32_t xx = 0; xx < mip.m_width; ++xx)
  250. {
  251. const uint32_t offset = (yy*mip.m_width + xx) * 4;
  252. float* inout = &rgba[offset];
  253. inout[0] = inout[0] * 2.0f - 1.0f;
  254. inout[1] = inout[1] * 2.0f - 1.0f;
  255. inout[2] = inout[2] * 2.0f - 1.0f;
  256. inout[3] = inout[3] * 2.0f - 1.0f;
  257. }
  258. }
  259. }
  260. imageRgba32fNormalize(rgba
  261. , dstMip.m_width
  262. , dstMip.m_height
  263. , dstMip.m_width*16
  264. , rgba
  265. );
  266. bimg::imageRgba32f11to01(rgbaDst
  267. , dstMip.m_width
  268. , dstMip.m_height
  269. , dstMip.m_depth
  270. , dstMip.m_width*16
  271. , rgba
  272. );
  273. bimg::imageEncodeFromRgba32f(_allocator
  274. , dstData
  275. , rgbaDst
  276. , dstMip.m_width
  277. , dstMip.m_height
  278. , dstMip.m_depth
  279. , outputFormat
  280. , _options.quality
  281. , _err
  282. );
  283. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  284. {
  285. bimg::imageRgba32fDownsample2x2NormalMap(rgba
  286. , dstMip.m_width
  287. , dstMip.m_height
  288. , dstMip.m_width*16
  289. , bx::strideAlign(dstMip.m_width/2, blockWidth)*16
  290. , rgba
  291. );
  292. bimg::imageRgba32f11to01(rgbaDst
  293. , dstMip.m_width
  294. , dstMip.m_height
  295. , dstMip.m_depth
  296. , dstMip.m_width*16
  297. , rgba
  298. );
  299. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  300. dstData = const_cast<uint8_t*>(dstMip.m_data);
  301. bimg::imageEncodeFromRgba32f(_allocator
  302. , dstData
  303. , rgbaDst
  304. , dstMip.m_width
  305. , dstMip.m_height
  306. , dstMip.m_depth
  307. , outputFormat
  308. , _options.quality
  309. , _err
  310. );
  311. }
  312. BX_FREE(_allocator, rgbaDst);
  313. }
  314. else if ( (!bimg::isCompressed(input->m_format) && 8 != inputBlockInfo.rBits)
  315. || outputFormat == bimg::TextureFormat::BC6H
  316. || outputFormat == bimg::TextureFormat::BC7
  317. )
  318. {
  319. uint32_t size = bimg::imageGetSize(
  320. NULL
  321. , uint16_t(dstMip.m_width)
  322. , uint16_t(dstMip.m_height)
  323. , uint16_t(dstMip.m_depth)
  324. , false
  325. , false
  326. , 1
  327. , bimg::TextureFormat::RGBA32F
  328. );
  329. temp = BX_ALLOC(_allocator, size);
  330. float* rgba32f = (float*)temp;
  331. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  332. bimg::imageDecodeToRgba32f(_allocator
  333. , rgba32f
  334. , mip.m_data
  335. , mip.m_width
  336. , mip.m_height
  337. , mip.m_depth
  338. , mip.m_width*16
  339. , mip.m_format
  340. );
  341. bimg::imageEncodeFromRgba32f(_allocator
  342. , dstData
  343. , rgba32f
  344. , dstMip.m_width
  345. , dstMip.m_height
  346. , dstMip.m_depth
  347. , outputFormat
  348. , _options.quality
  349. , _err
  350. );
  351. if (1 < numMips
  352. && _err->isOk() )
  353. {
  354. bimg::imageRgba32fToLinear(rgba32f
  355. , mip.m_width
  356. , mip.m_height
  357. , mip.m_depth
  358. , mip.m_width*16
  359. , rgba32f
  360. );
  361. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  362. {
  363. bimg::imageRgba32fLinearDownsample2x2(rgba32f
  364. , dstMip.m_width
  365. , dstMip.m_height
  366. , dstMip.m_depth
  367. , dstMip.m_width*16
  368. , rgba32f
  369. );
  370. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  371. dstData = const_cast<uint8_t*>(dstMip.m_data);
  372. bimg::imageRgba32fToGamma(rgbaDst
  373. , mip.m_width
  374. , mip.m_height
  375. , mip.m_depth
  376. , mip.m_width*16
  377. , rgba32f
  378. );
  379. bimg::imageEncodeFromRgba32f(_allocator
  380. , dstData
  381. , rgbaDst
  382. , dstMip.m_width
  383. , dstMip.m_height
  384. , dstMip.m_depth
  385. , outputFormat
  386. , _options.quality
  387. , _err
  388. );
  389. }
  390. }
  391. BX_FREE(_allocator, rgbaDst);
  392. }
  393. else if (_options.sdf)
  394. {
  395. uint32_t size = bimg::imageGetSize(
  396. NULL
  397. , uint16_t(dstMip.m_width)
  398. , uint16_t(dstMip.m_height)
  399. , uint16_t(dstMip.m_depth)
  400. , false
  401. , false
  402. , 1
  403. , bimg::TextureFormat::R8
  404. );
  405. temp = BX_ALLOC(_allocator, size);
  406. uint8_t* rgba = (uint8_t*)temp;
  407. bimg::imageDecodeToR8(_allocator
  408. , rgba
  409. , mip.m_data
  410. , mip.m_width
  411. , mip.m_height
  412. , mip.m_depth
  413. , mip.m_width
  414. , mip.m_format
  415. );
  416. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  417. dstData = const_cast<uint8_t*>(dstMip.m_data);
  418. bimg::imageMakeDist(_allocator
  419. , dstData
  420. , mip.m_width
  421. , mip.m_height
  422. , mip.m_width
  423. , _options.edge
  424. , rgba
  425. );
  426. }
  427. else
  428. {
  429. uint32_t size = bimg::imageGetSize(
  430. NULL
  431. , uint16_t(dstMip.m_width)
  432. , uint16_t(dstMip.m_height)
  433. , uint16_t(dstMip.m_depth)
  434. , false
  435. , false
  436. , 1
  437. , bimg::TextureFormat::RGBA8
  438. );
  439. temp = BX_ALLOC(_allocator, size);
  440. uint8_t* rgba = (uint8_t*)temp;
  441. bimg::imageDecodeToRgba8(rgba
  442. , mip.m_data
  443. , mip.m_width
  444. , mip.m_height
  445. , mip.m_width*4
  446. , mip.m_format
  447. );
  448. float coverage = 0.0f;
  449. if (_options.alphaTest)
  450. {
  451. coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::RGBA8
  452. , mip.m_width
  453. , mip.m_height
  454. , mip.m_width*4
  455. , rgba
  456. , _options.edge
  457. );
  458. }
  459. void* ref = NULL;
  460. if (_options.iqa)
  461. {
  462. ref = BX_ALLOC(_allocator, size);
  463. bx::memCopy(ref, rgba, size);
  464. }
  465. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  466. dstData = const_cast<uint8_t*>(dstMip.m_data);
  467. bimg::imageEncodeFromRgba8(dstData
  468. , rgba
  469. , dstMip.m_width
  470. , dstMip.m_height
  471. , dstMip.m_depth
  472. , outputFormat
  473. , _options.quality
  474. , _err
  475. );
  476. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  477. {
  478. bimg::imageRgba8Downsample2x2(rgba
  479. , dstMip.m_width
  480. , dstMip.m_height
  481. , dstMip.m_depth
  482. , dstMip.m_width*4
  483. , bx::strideAlign(dstMip.m_width/2, blockWidth)*4
  484. , rgba
  485. );
  486. if (_options.alphaTest)
  487. {
  488. bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8
  489. , dstMip.m_width
  490. , dstMip.m_height
  491. , dstMip.m_width*4
  492. , rgba
  493. , coverage
  494. , _options.edge
  495. );
  496. }
  497. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  498. dstData = const_cast<uint8_t*>(dstMip.m_data);
  499. bimg::imageEncodeFromRgba8(dstData
  500. , rgba
  501. , dstMip.m_width
  502. , dstMip.m_height
  503. , dstMip.m_depth
  504. , outputFormat
  505. , _options.quality
  506. , _err
  507. );
  508. }
  509. if (NULL != ref)
  510. {
  511. bimg::imageDecodeToRgba8(rgba
  512. , output->m_data
  513. , mip.m_width
  514. , mip.m_height
  515. , mip.m_width*mip.m_bpp/8
  516. , outputFormat
  517. );
  518. float result = bimg::imageQualityRgba8(
  519. ref
  520. , rgba
  521. , uint16_t(mip.m_width)
  522. , uint16_t(mip.m_height)
  523. );
  524. printf("%f\n", result);
  525. BX_FREE(_allocator, ref);
  526. }
  527. }
  528. BX_FREE(_allocator, temp);
  529. }
  530. }
  531. bimg::imageFree(input);
  532. }
  533. if (!_err->isOk()
  534. && NULL != output)
  535. {
  536. bimg::imageFree(output);
  537. output = NULL;
  538. }
  539. return output;
  540. }
  541. void help(const char* _error = NULL, bool _showHelp = true)
  542. {
  543. if (NULL != _error)
  544. {
  545. fprintf(stderr, "Error:\n%s\n\n", _error);
  546. if (!_showHelp)
  547. {
  548. return;
  549. }
  550. }
  551. fprintf(stderr
  552. , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  553. "Copyright 2011-2018 Branimir Karadzic. All rights reserved.\n"
  554. "License: https://github.com/bkaradzic/bimg#license-bsd-2-clause\n\n"
  555. , BIMG_TEXTUREC_VERSION_MAJOR
  556. , BIMG_TEXTUREC_VERSION_MINOR
  557. , BIMG_API_VERSION
  558. );
  559. fprintf(stderr
  560. , "Usage: texturec -f <in> -o <out> [-t <texture format>]\n"
  561. "\n"
  562. "Supported file formats:\n"
  563. " *.bmp (input) Windows Bitmap.\n"
  564. " *.dds (input, output) Direct Draw Surface.\n"
  565. " *.exr (input) OpenEXR.\n"
  566. " *.gif (input) Graphics Interchange Format.\n"
  567. " *.jpg (input) JPEG Interchange Format.\n"
  568. " *.hdr (input) Radiance RGBE.\n"
  569. " *.ktx (input, output) Khronos Texture.\n"
  570. " *.png (input, output) Portable Network Graphics.\n"
  571. " *.psd (input) Photoshop Document.\n"
  572. " *.pvr (input) PowerVR.\n"
  573. " *.tga (input) Targa.\n"
  574. "\n"
  575. "Options:\n"
  576. " -h, --help Help.\n"
  577. " -v, --version Version information only.\n"
  578. " -f <file path> Input file path.\n"
  579. " -o <file path> Output file path.\n"
  580. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  581. " -q <quality> Encoding quality (default, fastest, highest).\n"
  582. " -m, --mips Generate mip-maps.\n"
  583. " -n, --normalmap Input texture is normal map.\n"
  584. " --equirect Input texture equirectangular projection of cubemap.\n"
  585. " --sdf <edge> Compute SDF texture.\n"
  586. " --ref <alpha> Alpha reference value.\n"
  587. " --iqa Image Quality Assessment\n"
  588. " --max <max size> Maximum width/height (image will be scaled down and\n"
  589. " aspect ratio will be preserved.\n"
  590. " --as <extension> Save as.\n"
  591. " --validate *DEBUG* Validate that output image produced matches after loading.\n"
  592. "\n"
  593. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  594. );
  595. }
  596. void help(const char* _str, const bx::Error& _err)
  597. {
  598. std::string str;
  599. if (_str != NULL)
  600. {
  601. str.append(_str);
  602. str.append(" ");
  603. }
  604. const bx::StringView& sv = _err.getMessage();
  605. str.append(sv.getPtr(), sv.getTerm() - sv.getPtr() );
  606. help(str.c_str(), false);
  607. }
  608. int main(int _argc, const char* _argv[])
  609. {
  610. bx::CommandLine cmdLine(_argc, _argv);
  611. if (cmdLine.hasArg('v', "version") )
  612. {
  613. fprintf(stderr
  614. , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  615. , BIMG_TEXTUREC_VERSION_MAJOR
  616. , BIMG_TEXTUREC_VERSION_MINOR
  617. , BIMG_API_VERSION
  618. );
  619. return bx::kExitSuccess;
  620. }
  621. if (cmdLine.hasArg('h', "help") )
  622. {
  623. help();
  624. return bx::kExitFailure;
  625. }
  626. const char* inputFileName = cmdLine.findOption('f');
  627. if (NULL == inputFileName)
  628. {
  629. help("Input file must be specified.");
  630. return bx::kExitFailure;
  631. }
  632. const char* outputFileName = cmdLine.findOption('o');
  633. if (NULL == outputFileName)
  634. {
  635. help("Output file must be specified.");
  636. return bx::kExitFailure;
  637. }
  638. const char* saveAs = cmdLine.findOption("as");
  639. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".ktx") : saveAs;
  640. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".dds") : saveAs;
  641. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".png") : saveAs;
  642. if (NULL == saveAs)
  643. {
  644. help("Output file format must be specified.");
  645. return bx::kExitFailure;
  646. }
  647. Options options;
  648. const char* edgeOpt = cmdLine.findOption("sdf");
  649. if (NULL != edgeOpt)
  650. {
  651. options.sdf = true;
  652. if (!bx::fromString(&options.edge, edgeOpt) )
  653. {
  654. options.edge = 255.0f;
  655. }
  656. }
  657. else
  658. {
  659. const char* alphaRef = cmdLine.findOption("ref");
  660. if (NULL != alphaRef)
  661. {
  662. options.alphaTest = true;
  663. if (!bx::fromString(&options.edge, alphaRef))
  664. {
  665. options.edge = 0.5f;
  666. }
  667. }
  668. }
  669. options.mips = cmdLine.hasArg('m', "mips");
  670. options.normalMap = cmdLine.hasArg('n', "normalmap");
  671. options.equirect = cmdLine.hasArg("equirect");
  672. options.iqa = cmdLine.hasArg("iqa");
  673. const char* maxSize = cmdLine.findOption("max");
  674. if (NULL != maxSize)
  675. {
  676. options.maxSize = atoi(maxSize);
  677. }
  678. options.format = bimg::TextureFormat::Count;
  679. const char* type = cmdLine.findOption('t');
  680. if (NULL != type)
  681. {
  682. options.format = bimg::getFormat(type);
  683. if (!bimg::isValid(options.format) )
  684. {
  685. help("Invalid format specified.");
  686. return bx::kExitFailure;
  687. }
  688. }
  689. if (NULL != bx::strFindI(saveAs, "png") )
  690. {
  691. if (options.format == bimg::TextureFormat::Count)
  692. {
  693. options.format = bimg::TextureFormat::RGBA8;
  694. }
  695. else if (options.format != bimg::TextureFormat::RGBA8)
  696. {
  697. help("Ouput PNG format must be RGBA8.");
  698. return bx::kExitFailure;
  699. }
  700. }
  701. const char* quality = cmdLine.findOption('q');
  702. if (NULL != quality)
  703. {
  704. switch (bx::toLower(quality[0]) )
  705. {
  706. case 'h': options.quality = bimg::Quality::Highest; break;
  707. case 'f': options.quality = bimg::Quality::Fastest; break;
  708. case 'd': options.quality = bimg::Quality::Default; break;
  709. default:
  710. help("Invalid quality specified.");
  711. return bx::kExitFailure;
  712. }
  713. }
  714. const bool validate = cmdLine.hasArg("validate");
  715. bx::Error err;
  716. bx::FileReader reader;
  717. if (!bx::open(&reader, inputFileName, &err) )
  718. {
  719. help("Failed to open input file.", err);
  720. return bx::kExitFailure;
  721. }
  722. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  723. if (0 == inputSize)
  724. {
  725. help("Failed to read input file.", err);
  726. return bx::kExitFailure;
  727. }
  728. bx::DefaultAllocator allocator;
  729. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  730. bx::read(&reader, inputData, inputSize, &err);
  731. bx::close(&reader);
  732. if (!err.isOk() )
  733. {
  734. help("Failed to read input file.", err);
  735. return bx::kExitFailure;
  736. }
  737. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
  738. BX_FREE(&allocator, inputData);
  739. if (NULL != output)
  740. {
  741. bx::FileWriter writer;
  742. if (bx::open(&writer, outputFileName, false, &err) )
  743. {
  744. if (NULL != bx::strFindI(saveAs, "ktx") )
  745. {
  746. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size, &err);
  747. }
  748. else if (NULL != bx::strFindI(saveAs, "dds") )
  749. {
  750. bimg::imageWriteDds(&writer, *output, output->m_data, output->m_size, &err);
  751. }
  752. else if (NULL != bx::strFindI(saveAs, "png") )
  753. {
  754. bimg::ImageMip mip;
  755. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  756. bimg::imageWritePng(&writer
  757. , mip.m_width
  758. , mip.m_height
  759. , mip.m_width*4
  760. , mip.m_data
  761. , bimg::TextureFormat::RGBA8
  762. , false
  763. , &err);
  764. }
  765. bx::close(&writer);
  766. if (!err.isOk() )
  767. {
  768. help(NULL, err);
  769. return bx::kExitFailure;
  770. }
  771. }
  772. else
  773. {
  774. help("Failed to open output file.", err);
  775. return bx::kExitFailure;
  776. }
  777. if (validate)
  778. {
  779. if (!bx::open(&reader, outputFileName, &err) )
  780. {
  781. help("Failed to validate file.", err);
  782. return bx::kExitFailure;
  783. }
  784. inputSize = (uint32_t)bx::getSize(&reader);
  785. if (0 == inputSize)
  786. {
  787. help("Failed to validate file.", err);
  788. return bx::kExitFailure;
  789. }
  790. inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  791. bx::read(&reader, inputData, inputSize, &err);
  792. bx::close(&reader);
  793. bimg::ImageContainer* input = bimg::imageParse(&allocator, inputData, inputSize, bimg::TextureFormat::Count, &err);
  794. if (!err.isOk() )
  795. {
  796. help("Failed to validate file.", err);
  797. return bx::kExitFailure;
  798. }
  799. if (false
  800. || input->m_format != output->m_format
  801. || input->m_size != output->m_size
  802. || input->m_width != output->m_width
  803. || input->m_height != output->m_height
  804. || input->m_depth != output->m_depth
  805. || input->m_numLayers != output->m_numLayers
  806. || input->m_numMips != output->m_numMips
  807. || input->m_hasAlpha != output->m_hasAlpha
  808. || input->m_cubeMap != output->m_cubeMap
  809. )
  810. {
  811. help("Validation failed, image headers are different.");
  812. return bx::kExitFailure;
  813. }
  814. {
  815. const uint8_t numMips = output->m_numMips;
  816. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  817. for (uint8_t lod = 0; lod < numMips; ++lod)
  818. {
  819. for (uint16_t side = 0; side < numSides; ++side)
  820. {
  821. bimg::ImageMip srcMip;
  822. bool hasSrc = bimg::imageGetRawData(*input, side, lod, input->m_data, input->m_size, srcMip);
  823. bimg::ImageMip dstMip;
  824. bool hasDst = bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  825. if (false
  826. || hasSrc != hasDst
  827. || srcMip.m_size != dstMip.m_size
  828. )
  829. {
  830. help("Validation failed, image mip/layer/side are different.");
  831. return bx::kExitFailure;
  832. }
  833. if (0 != bx::memCmp(srcMip.m_data, dstMip.m_data, srcMip.m_size) )
  834. {
  835. help("Validation failed, image content are different.");
  836. return bx::kExitFailure;
  837. }
  838. }
  839. }
  840. }
  841. BX_FREE(&allocator, inputData);
  842. }
  843. bimg::imageFree(output);
  844. }
  845. else
  846. {
  847. help(NULL, err);
  848. return bx::kExitFailure;
  849. }
  850. return bx::kExitSuccess;
  851. }