texturec.cpp 20 KB

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