texturec.cpp 21 KB

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