texturec.cpp 22 KB

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