texturec.cpp 20 KB

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