texturec.cpp 22 KB

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