texturec.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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 12
  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. , rgba
  290. );
  291. bimg::imageRgba32f11to01(rgbaDst
  292. , dstMip.m_width
  293. , dstMip.m_height
  294. , dstMip.m_depth
  295. , dstMip.m_width*16
  296. , rgba
  297. );
  298. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  299. dstData = const_cast<uint8_t*>(dstMip.m_data);
  300. bimg::imageEncodeFromRgba32f(_allocator
  301. , dstData
  302. , rgbaDst
  303. , dstMip.m_width
  304. , dstMip.m_height
  305. , dstMip.m_depth
  306. , outputFormat
  307. , _options.quality
  308. , _err
  309. );
  310. }
  311. BX_FREE(_allocator, rgbaDst);
  312. }
  313. else if ( (!bimg::isCompressed(input->m_format) && 8 != inputBlockInfo.rBits)
  314. || outputFormat == bimg::TextureFormat::BC6H
  315. || outputFormat == bimg::TextureFormat::BC7
  316. )
  317. {
  318. uint32_t size = bimg::imageGetSize(
  319. NULL
  320. , uint16_t(dstMip.m_width)
  321. , uint16_t(dstMip.m_height)
  322. , uint16_t(dstMip.m_depth)
  323. , false
  324. , false
  325. , 1
  326. , bimg::TextureFormat::RGBA32F
  327. );
  328. temp = BX_ALLOC(_allocator, size);
  329. float* rgba32f = (float*)temp;
  330. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  331. bimg::imageDecodeToRgba32f(_allocator
  332. , rgba32f
  333. , mip.m_data
  334. , mip.m_width
  335. , mip.m_height
  336. , mip.m_depth
  337. , mip.m_width*16
  338. , mip.m_format
  339. );
  340. bimg::imageEncodeFromRgba32f(_allocator
  341. , dstData
  342. , rgba32f
  343. , dstMip.m_width
  344. , dstMip.m_height
  345. , dstMip.m_depth
  346. , outputFormat
  347. , _options.quality
  348. , _err
  349. );
  350. if (1 < numMips
  351. && _err->isOk() )
  352. {
  353. bimg::imageRgba32fToLinear(rgba32f
  354. , mip.m_width
  355. , mip.m_height
  356. , mip.m_depth
  357. , mip.m_width*16
  358. , rgba32f
  359. );
  360. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  361. {
  362. bimg::imageRgba32fLinearDownsample2x2(rgba32f
  363. , dstMip.m_width
  364. , dstMip.m_height
  365. , dstMip.m_depth
  366. , dstMip.m_width*16
  367. , rgba32f
  368. );
  369. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  370. dstData = const_cast<uint8_t*>(dstMip.m_data);
  371. bimg::imageRgba32fToGamma(rgbaDst
  372. , mip.m_width
  373. , mip.m_height
  374. , mip.m_depth
  375. , mip.m_width*16
  376. , rgba32f
  377. );
  378. bimg::imageEncodeFromRgba32f(_allocator
  379. , dstData
  380. , rgbaDst
  381. , dstMip.m_width
  382. , dstMip.m_height
  383. , dstMip.m_depth
  384. , outputFormat
  385. , _options.quality
  386. , _err
  387. );
  388. }
  389. }
  390. BX_FREE(_allocator, rgbaDst);
  391. }
  392. else if (_options.sdf)
  393. {
  394. uint32_t size = bimg::imageGetSize(
  395. NULL
  396. , uint16_t(dstMip.m_width)
  397. , uint16_t(dstMip.m_height)
  398. , uint16_t(dstMip.m_depth)
  399. , false
  400. , false
  401. , 1
  402. , bimg::TextureFormat::R8
  403. );
  404. temp = BX_ALLOC(_allocator, size);
  405. uint8_t* rgba = (uint8_t*)temp;
  406. bimg::imageDecodeToR8(_allocator
  407. , rgba
  408. , mip.m_data
  409. , mip.m_width
  410. , mip.m_height
  411. , mip.m_depth
  412. , mip.m_width
  413. , mip.m_format
  414. );
  415. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  416. dstData = const_cast<uint8_t*>(dstMip.m_data);
  417. bimg::imageMakeDist(_allocator
  418. , dstData
  419. , mip.m_width
  420. , mip.m_height
  421. , mip.m_width
  422. , _options.edge
  423. , rgba
  424. );
  425. }
  426. else
  427. {
  428. uint32_t size = bimg::imageGetSize(
  429. NULL
  430. , uint16_t(dstMip.m_width)
  431. , uint16_t(dstMip.m_height)
  432. , uint16_t(dstMip.m_depth)
  433. , false
  434. , false
  435. , 1
  436. , bimg::TextureFormat::RGBA8
  437. );
  438. temp = BX_ALLOC(_allocator, size);
  439. uint8_t* rgba = (uint8_t*)temp;
  440. bimg::imageDecodeToRgba8(rgba
  441. , mip.m_data
  442. , mip.m_width
  443. , mip.m_height
  444. , mip.m_width*4
  445. , mip.m_format
  446. );
  447. float coverage = 0.0f;
  448. if (_options.alphaTest)
  449. {
  450. coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::RGBA8
  451. , mip.m_width
  452. , mip.m_height
  453. , mip.m_width*4
  454. , rgba
  455. , _options.edge
  456. );
  457. }
  458. void* ref = NULL;
  459. if (_options.iqa)
  460. {
  461. ref = BX_ALLOC(_allocator, size);
  462. bx::memCopy(ref, rgba, size);
  463. }
  464. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  465. dstData = const_cast<uint8_t*>(dstMip.m_data);
  466. bimg::imageEncodeFromRgba8(dstData
  467. , rgba
  468. , dstMip.m_width
  469. , dstMip.m_height
  470. , dstMip.m_depth
  471. , outputFormat
  472. , _options.quality
  473. , _err
  474. );
  475. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  476. {
  477. bimg::imageRgba8Downsample2x2(rgba
  478. , dstMip.m_width
  479. , dstMip.m_height
  480. , dstMip.m_depth
  481. , dstMip.m_width*4
  482. , rgba
  483. );
  484. if (_options.alphaTest)
  485. {
  486. bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8
  487. , dstMip.m_width
  488. , dstMip.m_height
  489. , dstMip.m_width*4
  490. , rgba
  491. , coverage
  492. , _options.edge
  493. );
  494. }
  495. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  496. dstData = const_cast<uint8_t*>(dstMip.m_data);
  497. bimg::imageEncodeFromRgba8(dstData
  498. , rgba
  499. , dstMip.m_width
  500. , dstMip.m_height
  501. , dstMip.m_depth
  502. , outputFormat
  503. , _options.quality
  504. , _err
  505. );
  506. }
  507. if (NULL != ref)
  508. {
  509. bimg::imageDecodeToRgba8(rgba
  510. , output->m_data
  511. , mip.m_width
  512. , mip.m_height
  513. , mip.m_width*mip.m_bpp/8
  514. , outputFormat
  515. );
  516. float result = bimg::imageQualityRgba8(
  517. ref
  518. , rgba
  519. , uint16_t(mip.m_width)
  520. , uint16_t(mip.m_height)
  521. );
  522. printf("%f\n", result);
  523. BX_FREE(_allocator, ref);
  524. }
  525. }
  526. BX_FREE(_allocator, temp);
  527. }
  528. }
  529. bimg::imageFree(input);
  530. }
  531. if (!_err->isOk()
  532. && NULL != output)
  533. {
  534. bimg::imageFree(output);
  535. output = NULL;
  536. }
  537. return output;
  538. }
  539. void help(const char* _error = NULL, bool _showHelp = true)
  540. {
  541. if (NULL != _error)
  542. {
  543. fprintf(stderr, "Error:\n%s\n\n", _error);
  544. if (!_showHelp)
  545. {
  546. return;
  547. }
  548. }
  549. fprintf(stderr
  550. , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  551. "Copyright 2011-2018 Branimir Karadzic. All rights reserved.\n"
  552. "License: https://github.com/bkaradzic/bimg#license-bsd-2-clause\n\n"
  553. , BIMG_TEXTUREC_VERSION_MAJOR
  554. , BIMG_TEXTUREC_VERSION_MINOR
  555. , BIMG_API_VERSION
  556. );
  557. fprintf(stderr
  558. , "Usage: texturec -f <in> -o <out> [-t <texture format>]\n"
  559. "\n"
  560. "Supported file formats:\n"
  561. " *.bmp (input) Windows Bitmap.\n"
  562. " *.dds (input, output) Direct Draw Surface.\n"
  563. " *.exr (input) OpenEXR.\n"
  564. " *.gif (input) Graphics Interchange Format.\n"
  565. " *.jpg (input) JPEG Interchange Format.\n"
  566. " *.hdr (input) Radiance RGBE.\n"
  567. " *.ktx (input, output) Khronos Texture.\n"
  568. " *.png (input, output) Portable Network Graphics.\n"
  569. " *.psd (input) Photoshop Document.\n"
  570. " *.pvr (input) PowerVR.\n"
  571. " *.tga (input) Targa.\n"
  572. "\n"
  573. "Options:\n"
  574. " -h, --help Help.\n"
  575. " -v, --version Version information only.\n"
  576. " -f <file path> Input file path.\n"
  577. " -o <file path> Output file path.\n"
  578. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  579. " -q <quality> Encoding quality (default, fastest, highest).\n"
  580. " -m, --mips Generate mip-maps.\n"
  581. " -n, --normalmap Input texture is normal map.\n"
  582. " --equirect Input texture equirectangular projection of cubemap.\n"
  583. " --sdf <edge> Compute SDF texture.\n"
  584. " --ref <alpha> Alpha reference value.\n"
  585. " --iqa Image Quality Assessment\n"
  586. " --max <max size> Maximum width/height (image will be scaled down and\n"
  587. " aspect ratio will be preserved.\n"
  588. " --as <extension> Save as.\n"
  589. " --validate *DEBUG* Validate that output image produced matches after loading.\n"
  590. "\n"
  591. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  592. );
  593. }
  594. void help(const char* _str, const bx::Error& _err)
  595. {
  596. std::string str;
  597. if (_str != NULL)
  598. {
  599. str.append(_str);
  600. str.append(" ");
  601. }
  602. const bx::StringView& sv = _err.getMessage();
  603. str.append(sv.getPtr(), sv.getTerm() - sv.getPtr() );
  604. help(str.c_str(), false);
  605. }
  606. int main(int _argc, const char* _argv[])
  607. {
  608. bx::CommandLine cmdLine(_argc, _argv);
  609. if (cmdLine.hasArg('v', "version") )
  610. {
  611. fprintf(stderr
  612. , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  613. , BIMG_TEXTUREC_VERSION_MAJOR
  614. , BIMG_TEXTUREC_VERSION_MINOR
  615. , BIMG_API_VERSION
  616. );
  617. return bx::kExitSuccess;
  618. }
  619. if (cmdLine.hasArg('h', "help") )
  620. {
  621. help();
  622. return bx::kExitFailure;
  623. }
  624. const char* inputFileName = cmdLine.findOption('f');
  625. if (NULL == inputFileName)
  626. {
  627. help("Input file must be specified.");
  628. return bx::kExitFailure;
  629. }
  630. const char* outputFileName = cmdLine.findOption('o');
  631. if (NULL == outputFileName)
  632. {
  633. help("Output file must be specified.");
  634. return bx::kExitFailure;
  635. }
  636. const char* saveAs = cmdLine.findOption("as");
  637. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".ktx") : saveAs;
  638. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".dds") : saveAs;
  639. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".png") : saveAs;
  640. if (NULL == saveAs)
  641. {
  642. help("Output file format must be specified.");
  643. return bx::kExitFailure;
  644. }
  645. Options options;
  646. const char* edgeOpt = cmdLine.findOption("sdf");
  647. if (NULL != edgeOpt)
  648. {
  649. options.sdf = true;
  650. if (!bx::fromString(&options.edge, edgeOpt) )
  651. {
  652. options.edge = 255.0f;
  653. }
  654. }
  655. else
  656. {
  657. const char* alphaRef = cmdLine.findOption("ref");
  658. if (NULL != alphaRef)
  659. {
  660. options.alphaTest = true;
  661. if (!bx::fromString(&options.edge, alphaRef))
  662. {
  663. options.edge = 0.5f;
  664. }
  665. }
  666. }
  667. options.mips = cmdLine.hasArg('m', "mips");
  668. options.normalMap = cmdLine.hasArg('n', "normalmap");
  669. options.equirect = cmdLine.hasArg("equirect");
  670. options.iqa = cmdLine.hasArg("iqa");
  671. const char* maxSize = cmdLine.findOption("max");
  672. if (NULL != maxSize)
  673. {
  674. options.maxSize = atoi(maxSize);
  675. }
  676. options.format = bimg::TextureFormat::Count;
  677. const char* type = cmdLine.findOption('t');
  678. if (NULL != type)
  679. {
  680. options.format = bimg::getFormat(type);
  681. if (!bimg::isValid(options.format) )
  682. {
  683. help("Invalid format specified.");
  684. return bx::kExitFailure;
  685. }
  686. }
  687. if (NULL != bx::strFindI(saveAs, "png") )
  688. {
  689. if (options.format == bimg::TextureFormat::Count)
  690. {
  691. options.format = bimg::TextureFormat::RGBA8;
  692. }
  693. else if (options.format != bimg::TextureFormat::RGBA8)
  694. {
  695. help("Ouput PNG format must be RGBA8.");
  696. return bx::kExitFailure;
  697. }
  698. }
  699. const char* quality = cmdLine.findOption('q');
  700. if (NULL != quality)
  701. {
  702. switch (bx::toLower(quality[0]) )
  703. {
  704. case 'h': options.quality = bimg::Quality::Highest; break;
  705. case 'f': options.quality = bimg::Quality::Fastest; break;
  706. case 'd': options.quality = bimg::Quality::Default; break;
  707. default:
  708. help("Invalid quality specified.");
  709. return bx::kExitFailure;
  710. }
  711. }
  712. const bool validate = cmdLine.hasArg("validate");
  713. bx::Error err;
  714. bx::FileReader reader;
  715. if (!bx::open(&reader, inputFileName, &err) )
  716. {
  717. help("Failed to open input file.", err);
  718. return bx::kExitFailure;
  719. }
  720. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  721. if (0 == inputSize)
  722. {
  723. help("Failed to read input file.", err);
  724. return bx::kExitFailure;
  725. }
  726. bx::DefaultAllocator allocator;
  727. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  728. bx::read(&reader, inputData, inputSize, &err);
  729. bx::close(&reader);
  730. if (!err.isOk() )
  731. {
  732. help("Failed to read input file.", err);
  733. return bx::kExitFailure;
  734. }
  735. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
  736. BX_FREE(&allocator, inputData);
  737. if (NULL != output)
  738. {
  739. bx::FileWriter writer;
  740. if (bx::open(&writer, outputFileName, false, &err) )
  741. {
  742. if (NULL != bx::strFindI(saveAs, "ktx") )
  743. {
  744. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size, &err);
  745. }
  746. else if (NULL != bx::strFindI(saveAs, "dds") )
  747. {
  748. bimg::imageWriteDds(&writer, *output, output->m_data, output->m_size, &err);
  749. }
  750. else if (NULL != bx::strFindI(saveAs, "png") )
  751. {
  752. bimg::ImageMip mip;
  753. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  754. bimg::imageWritePng(&writer
  755. , mip.m_width
  756. , mip.m_height
  757. , mip.m_width*4
  758. , mip.m_data
  759. , bimg::TextureFormat::RGBA8
  760. , false
  761. , &err);
  762. }
  763. bx::close(&writer);
  764. if (!err.isOk() )
  765. {
  766. help(NULL, err);
  767. return bx::kExitFailure;
  768. }
  769. }
  770. else
  771. {
  772. help("Failed to open output file.", err);
  773. return bx::kExitFailure;
  774. }
  775. if (validate)
  776. {
  777. if (!bx::open(&reader, outputFileName, &err) )
  778. {
  779. help("Failed to validate file.", err);
  780. return bx::kExitFailure;
  781. }
  782. inputSize = (uint32_t)bx::getSize(&reader);
  783. if (0 == inputSize)
  784. {
  785. help("Failed to validate file.", err);
  786. return bx::kExitFailure;
  787. }
  788. inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  789. bx::read(&reader, inputData, inputSize, &err);
  790. bx::close(&reader);
  791. bimg::ImageContainer* input = bimg::imageParse(&allocator, inputData, inputSize, bimg::TextureFormat::Count, &err);
  792. if (!err.isOk() )
  793. {
  794. help("Failed to validate file.", err);
  795. return bx::kExitFailure;
  796. }
  797. if (false
  798. || input->m_format != output->m_format
  799. || input->m_size != output->m_size
  800. || input->m_width != output->m_width
  801. || input->m_height != output->m_height
  802. || input->m_depth != output->m_depth
  803. || input->m_numLayers != output->m_numLayers
  804. || input->m_numMips != output->m_numMips
  805. || input->m_hasAlpha != output->m_hasAlpha
  806. || input->m_cubeMap != output->m_cubeMap
  807. )
  808. {
  809. help("Validation failed, image headers are different.");
  810. return bx::kExitFailure;
  811. }
  812. {
  813. const uint8_t numMips = output->m_numMips;
  814. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  815. for (uint8_t lod = 0; lod < numMips; ++lod)
  816. {
  817. for (uint16_t side = 0; side < numSides; ++side)
  818. {
  819. bimg::ImageMip srcMip;
  820. bool hasSrc = bimg::imageGetRawData(*input, side, lod, input->m_data, input->m_size, srcMip);
  821. bimg::ImageMip dstMip;
  822. bool hasDst = bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  823. if (false
  824. || hasSrc != hasDst
  825. || srcMip.m_size != dstMip.m_size
  826. )
  827. {
  828. help("Validation failed, image mip/layer/side are different.");
  829. return bx::kExitFailure;
  830. }
  831. if (0 != bx::memCmp(srcMip.m_data, dstMip.m_data, srcMip.m_size) )
  832. {
  833. help("Validation failed, image content are different.");
  834. return bx::kExitFailure;
  835. }
  836. }
  837. }
  838. }
  839. BX_FREE(&allocator, inputData);
  840. }
  841. bimg::imageFree(output);
  842. }
  843. else
  844. {
  845. help(NULL, err);
  846. return bx::kExitFailure;
  847. }
  848. return bx::kExitSuccess;
  849. }