texturec.cpp 20 KB

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