texturec.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /*
  2. * Copyright 2011-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg#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 14
  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. , equirect(false)
  34. , iqa(false)
  35. , pma(false)
  36. , sdf(false)
  37. , alphaTest(false)
  38. {
  39. }
  40. void dump()
  41. {
  42. DBG("Options:\n"
  43. "\t maxSize: %d\n"
  44. "\t edge: %f\n"
  45. "\t format: %s\n"
  46. "\t mips: %s\n"
  47. "\tnormalMap: %s\n"
  48. "\t iqa: %s\n"
  49. "\t pma: %s\n"
  50. "\t sdf: %s\n"
  51. , maxSize
  52. , edge
  53. , bimg::getName(format)
  54. , mips ? "true" : "false"
  55. , normalMap ? "true" : "false"
  56. , iqa ? "true" : "false"
  57. , pma ? "true" : "false"
  58. , sdf ? "true" : "false"
  59. );
  60. }
  61. uint32_t maxSize;
  62. float edge;
  63. bimg::TextureFormat::Enum format;
  64. bimg::Quality::Enum quality;
  65. bool mips;
  66. bool normalMap;
  67. bool equirect;
  68. bool iqa;
  69. bool pma;
  70. bool sdf;
  71. bool alphaTest;
  72. };
  73. void imageRgba32fNormalize(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  74. {
  75. const uint8_t* src = (const uint8_t*)_src;
  76. uint8_t* dst = (uint8_t*)_dst;
  77. for (uint32_t yy = 0, ystep = _srcPitch; yy < _height; ++yy, src += ystep)
  78. {
  79. const float* rgba = (const float*)&src[0];
  80. for (uint32_t xx = 0; xx < _width; ++xx, rgba += 4, dst += 16)
  81. {
  82. float xyz[3];
  83. xyz[0] = rgba[0];
  84. xyz[1] = rgba[1];
  85. xyz[2] = rgba[2];
  86. bx::vec3Norm( (float*)dst, xyz);
  87. }
  88. }
  89. }
  90. void imagePremultiplyAlpha(void* _inOut, uint32_t _width, uint32_t _height, uint32_t _depth, bimg::TextureFormat::Enum _format)
  91. {
  92. uint8_t* inOut = (uint8_t*)_inOut;
  93. uint32_t bpp = bimg::getBitsPerPixel(_format);
  94. uint32_t pitch = _width*bpp/8;
  95. bimg::PackFn pack = bimg::getPack(_format);
  96. bimg::UnpackFn unpack = bimg::getUnpack(_format);
  97. for (uint32_t zz = 0; zz < _depth; ++zz)
  98. {
  99. for (uint32_t yy = 0; yy < _height; ++yy)
  100. {
  101. for (uint32_t xx = 0; xx < _width; ++xx)
  102. {
  103. const uint32_t offset = yy*pitch + xx*bpp/8;
  104. float rgba[4];
  105. unpack(rgba, &inOut[offset]);
  106. const float alpha = rgba[3];
  107. rgba[0] = bx::toGamma(bx::toLinear(rgba[0]) * alpha);
  108. rgba[1] = bx::toGamma(bx::toLinear(rgba[1]) * alpha);
  109. rgba[2] = bx::toGamma(bx::toLinear(rgba[2]) * alpha);
  110. pack(&inOut[offset], rgba);
  111. }
  112. }
  113. }
  114. }
  115. bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData, uint32_t _inputSize, const Options& _options, bx::Error* _err)
  116. {
  117. BX_ERROR_SCOPE(_err);
  118. const uint8_t* inputData = (uint8_t*)_inputData;
  119. bimg::ImageContainer* output = NULL;
  120. bimg::ImageContainer* input = bimg::imageParse(_allocator, inputData, _inputSize, bimg::TextureFormat::Count, _err);
  121. if (!_err->isOk() )
  122. {
  123. return NULL;
  124. }
  125. if (NULL != input)
  126. {
  127. const bimg::TextureFormat::Enum inputFormat = input->m_format;
  128. bimg::TextureFormat::Enum outputFormat = input->m_format;
  129. if (bimg::TextureFormat::Count != _options.format)
  130. {
  131. outputFormat = _options.format;
  132. }
  133. if (_options.sdf)
  134. {
  135. outputFormat = bimg::TextureFormat::R8;
  136. }
  137. const bimg::ImageBlockInfo& inputBlockInfo = bimg::getBlockInfo(inputFormat);
  138. const bimg::ImageBlockInfo& outputBlockInfo = bimg::getBlockInfo(outputFormat);
  139. const uint32_t blockWidth = outputBlockInfo.blockWidth;
  140. const uint32_t blockHeight = outputBlockInfo.blockHeight;
  141. const uint32_t minBlockX = outputBlockInfo.minBlockX;
  142. const uint32_t minBlockY = outputBlockInfo.minBlockY;
  143. uint32_t outputWidth = bx::uint32_max(blockWidth * minBlockX, ( (input->m_width + blockWidth - 1) / blockWidth )*blockWidth);
  144. uint32_t outputHeight = bx::uint32_max(blockHeight * minBlockY, ( (input->m_height + blockHeight - 1) / blockHeight)*blockHeight);
  145. uint32_t outputDepth = input->m_depth;
  146. if (outputWidth > _options.maxSize
  147. || outputHeight > _options.maxSize
  148. || outputDepth > _options.maxSize)
  149. {
  150. if (outputDepth > outputWidth
  151. && outputDepth > outputHeight)
  152. {
  153. outputWidth = outputWidth * _options.maxSize / outputDepth;
  154. outputHeight = outputHeight * _options.maxSize / outputDepth;
  155. outputDepth = _options.maxSize;
  156. }
  157. else if (outputWidth > outputHeight)
  158. {
  159. outputDepth = outputDepth * _options.maxSize / outputWidth;
  160. outputHeight = outputHeight * _options.maxSize / outputWidth;
  161. outputWidth = _options.maxSize;
  162. }
  163. else
  164. {
  165. outputDepth = outputDepth * _options.maxSize / outputHeight;
  166. outputWidth = outputWidth * _options.maxSize / outputHeight;
  167. outputHeight = _options.maxSize;
  168. }
  169. }
  170. const bool needResize = false
  171. || input->m_width != outputWidth
  172. || input->m_height != outputHeight
  173. ;
  174. const bool passThru = true
  175. && !needResize
  176. && (1 < input->m_numMips) == _options.mips
  177. && !_options.sdf
  178. && !_options.alphaTest
  179. && !_options.normalMap
  180. && !_options.equirect
  181. && !_options.iqa
  182. && !_options.pma
  183. ;
  184. if (needResize)
  185. {
  186. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input);
  187. bimg::ImageContainer* dst = bimg::imageAlloc(
  188. _allocator
  189. , bimg::TextureFormat::RGBA32F
  190. , uint16_t(outputWidth)
  191. , uint16_t(outputHeight)
  192. , uint16_t(outputDepth)
  193. , input->m_numLayers
  194. , input->m_cubeMap
  195. , false
  196. );
  197. bimg::imageResizeRgba32fLinear(dst, src);
  198. bimg::imageFree(src);
  199. bimg::imageFree(input);
  200. input = bimg::imageConvert(_allocator, inputFormat, *dst);
  201. bimg::imageFree(dst);
  202. }
  203. if (passThru)
  204. {
  205. if (inputFormat != outputFormat
  206. && bimg::isCompressed(outputFormat) )
  207. {
  208. output = bimg::imageEncode(_allocator, outputFormat, _options.quality, *input);
  209. }
  210. else
  211. {
  212. output = bimg::imageConvert(_allocator, outputFormat, *input);
  213. }
  214. bimg::imageFree(input);
  215. return output;
  216. }
  217. if (_options.equirect)
  218. {
  219. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input);
  220. bimg::imageFree(input);
  221. bimg::ImageContainer* dst = bimg::imageCubemapFromLatLongRgba32F(_allocator, *src, true, _err);
  222. bimg::imageFree(src);
  223. if (!_err->isOk() )
  224. {
  225. return NULL;
  226. }
  227. input = bimg::imageConvert(_allocator, inputFormat, *dst);
  228. bimg::imageFree(dst);
  229. }
  230. output = bimg::imageAlloc(
  231. _allocator
  232. , outputFormat
  233. , uint16_t(input->m_width)
  234. , uint16_t(input->m_height)
  235. , uint16_t(input->m_depth)
  236. , input->m_numLayers
  237. , input->m_cubeMap
  238. , _options.mips
  239. );
  240. const uint8_t numMips = output->m_numMips;
  241. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  242. for (uint16_t side = 0; side < numSides && _err->isOk(); ++side)
  243. {
  244. bimg::ImageMip mip;
  245. if (bimg::imageGetRawData(*input, side, 0, input->m_data, input->m_size, mip) )
  246. {
  247. bimg::ImageMip dstMip;
  248. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  249. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  250. void* temp = NULL;
  251. // Normal map.
  252. if (_options.normalMap)
  253. {
  254. uint32_t size = bimg::imageGetSize(
  255. NULL
  256. , uint16_t(dstMip.m_width)
  257. , uint16_t(dstMip.m_height)
  258. , 0
  259. , false
  260. , false
  261. , 1
  262. , bimg::TextureFormat::RGBA32F
  263. );
  264. temp = BX_ALLOC(_allocator, size);
  265. float* rgba = (float*)temp;
  266. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  267. bimg::imageDecodeToRgba32f(_allocator
  268. , rgba
  269. , mip.m_data
  270. , dstMip.m_width
  271. , dstMip.m_height
  272. , dstMip.m_depth
  273. , dstMip.m_width*16
  274. , mip.m_format
  275. );
  276. if (bimg::TextureFormat::BC5 != mip.m_format)
  277. {
  278. for (uint32_t yy = 0; yy < mip.m_height; ++yy)
  279. {
  280. for (uint32_t xx = 0; xx < mip.m_width; ++xx)
  281. {
  282. const uint32_t offset = (yy*mip.m_width + xx) * 4;
  283. float* inout = &rgba[offset];
  284. inout[0] = inout[0] * 2.0f - 1.0f;
  285. inout[1] = inout[1] * 2.0f - 1.0f;
  286. inout[2] = inout[2] * 2.0f - 1.0f;
  287. inout[3] = inout[3] * 2.0f - 1.0f;
  288. }
  289. }
  290. }
  291. imageRgba32fNormalize(rgba
  292. , dstMip.m_width
  293. , dstMip.m_height
  294. , dstMip.m_width*16
  295. , rgba
  296. );
  297. bimg::imageRgba32f11to01(rgbaDst
  298. , dstMip.m_width
  299. , dstMip.m_height
  300. , dstMip.m_depth
  301. , dstMip.m_width*16
  302. , rgba
  303. );
  304. bimg::imageEncodeFromRgba32f(_allocator
  305. , dstData
  306. , rgbaDst
  307. , dstMip.m_width
  308. , dstMip.m_height
  309. , dstMip.m_depth
  310. , outputFormat
  311. , _options.quality
  312. , _err
  313. );
  314. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  315. {
  316. bimg::imageRgba32fDownsample2x2NormalMap(rgba
  317. , dstMip.m_width
  318. , dstMip.m_height
  319. , dstMip.m_width*16
  320. , bx::strideAlign(dstMip.m_width/2, blockWidth)*16
  321. , rgba
  322. );
  323. bimg::imageRgba32f11to01(rgbaDst
  324. , dstMip.m_width
  325. , dstMip.m_height
  326. , dstMip.m_depth
  327. , dstMip.m_width*16
  328. , rgba
  329. );
  330. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  331. dstData = const_cast<uint8_t*>(dstMip.m_data);
  332. bimg::imageEncodeFromRgba32f(_allocator
  333. , dstData
  334. , rgbaDst
  335. , dstMip.m_width
  336. , dstMip.m_height
  337. , dstMip.m_depth
  338. , outputFormat
  339. , _options.quality
  340. , _err
  341. );
  342. }
  343. BX_FREE(_allocator, rgbaDst);
  344. }
  345. // HDR
  346. else if ( (!bimg::isCompressed(input->m_format) && 8 != inputBlockInfo.rBits)
  347. || outputFormat == bimg::TextureFormat::BC6H
  348. || outputFormat == bimg::TextureFormat::BC7
  349. )
  350. {
  351. uint32_t size = bimg::imageGetSize(
  352. NULL
  353. , uint16_t(dstMip.m_width)
  354. , uint16_t(dstMip.m_height)
  355. , uint16_t(dstMip.m_depth)
  356. , false
  357. , false
  358. , 1
  359. , bimg::TextureFormat::RGBA32F
  360. );
  361. temp = BX_ALLOC(_allocator, size);
  362. float* rgba32f = (float*)temp;
  363. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  364. bimg::imageDecodeToRgba32f(_allocator
  365. , rgba32f
  366. , mip.m_data
  367. , mip.m_width
  368. , mip.m_height
  369. , mip.m_depth
  370. , mip.m_width*16
  371. , mip.m_format
  372. );
  373. if (_options.pma)
  374. {
  375. imagePremultiplyAlpha(
  376. rgba32f
  377. , dstMip.m_width
  378. , dstMip.m_height
  379. , dstMip.m_depth
  380. , bimg::TextureFormat::RGBA32F
  381. );
  382. }
  383. bimg::imageEncodeFromRgba32f(_allocator
  384. , dstData
  385. , rgba32f
  386. , dstMip.m_width
  387. , dstMip.m_height
  388. , dstMip.m_depth
  389. , outputFormat
  390. , _options.quality
  391. , _err
  392. );
  393. if (1 < numMips
  394. && _err->isOk() )
  395. {
  396. bimg::imageRgba32fToLinear(rgba32f
  397. , mip.m_width
  398. , mip.m_height
  399. , mip.m_depth
  400. , mip.m_width*16
  401. , rgba32f
  402. );
  403. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  404. {
  405. bimg::imageRgba32fLinearDownsample2x2(rgba32f
  406. , dstMip.m_width
  407. , dstMip.m_height
  408. , dstMip.m_depth
  409. , dstMip.m_width*16
  410. , rgba32f
  411. );
  412. if (_options.pma)
  413. {
  414. imagePremultiplyAlpha(
  415. rgba32f
  416. , dstMip.m_width
  417. , dstMip.m_height
  418. , dstMip.m_depth
  419. , bimg::TextureFormat::RGBA32F
  420. );
  421. }
  422. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  423. dstData = const_cast<uint8_t*>(dstMip.m_data);
  424. bimg::imageRgba32fToGamma(rgbaDst
  425. , mip.m_width
  426. , mip.m_height
  427. , mip.m_depth
  428. , mip.m_width*16
  429. , rgba32f
  430. );
  431. bimg::imageEncodeFromRgba32f(_allocator
  432. , dstData
  433. , rgbaDst
  434. , dstMip.m_width
  435. , dstMip.m_height
  436. , dstMip.m_depth
  437. , outputFormat
  438. , _options.quality
  439. , _err
  440. );
  441. }
  442. }
  443. BX_FREE(_allocator, rgbaDst);
  444. }
  445. // SDF
  446. else if (_options.sdf)
  447. {
  448. uint32_t size = bimg::imageGetSize(
  449. NULL
  450. , uint16_t(dstMip.m_width)
  451. , uint16_t(dstMip.m_height)
  452. , uint16_t(dstMip.m_depth)
  453. , false
  454. , false
  455. , 1
  456. , bimg::TextureFormat::R8
  457. );
  458. temp = BX_ALLOC(_allocator, size);
  459. uint8_t* rgba = (uint8_t*)temp;
  460. bimg::imageDecodeToR8(_allocator
  461. , rgba
  462. , mip.m_data
  463. , mip.m_width
  464. , mip.m_height
  465. , mip.m_depth
  466. , mip.m_width
  467. , mip.m_format
  468. );
  469. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  470. dstData = const_cast<uint8_t*>(dstMip.m_data);
  471. bimg::imageMakeDist(_allocator
  472. , dstData
  473. , mip.m_width
  474. , mip.m_height
  475. , mip.m_width
  476. , _options.edge
  477. , rgba
  478. );
  479. }
  480. // RGBA8
  481. else
  482. {
  483. uint32_t size = bimg::imageGetSize(
  484. NULL
  485. , uint16_t(dstMip.m_width)
  486. , uint16_t(dstMip.m_height)
  487. , uint16_t(dstMip.m_depth)
  488. , false
  489. , false
  490. , 1
  491. , bimg::TextureFormat::RGBA8
  492. );
  493. temp = BX_ALLOC(_allocator, size);
  494. uint8_t* rgba = (uint8_t*)temp;
  495. bimg::imageDecodeToRgba8(rgba
  496. , mip.m_data
  497. , mip.m_width
  498. , mip.m_height
  499. , mip.m_width*4
  500. , mip.m_format
  501. );
  502. float coverage = 0.0f;
  503. if (_options.alphaTest)
  504. {
  505. coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::RGBA8
  506. , mip.m_width
  507. , mip.m_height
  508. , mip.m_width*4
  509. , rgba
  510. , _options.edge
  511. );
  512. }
  513. void* ref = NULL;
  514. if (_options.iqa)
  515. {
  516. ref = BX_ALLOC(_allocator, size);
  517. bx::memCopy(ref, rgba, size);
  518. }
  519. if (_options.pma)
  520. {
  521. imagePremultiplyAlpha(
  522. rgba
  523. , dstMip.m_width
  524. , dstMip.m_height
  525. , dstMip.m_depth
  526. , bimg::TextureFormat::RGBA8
  527. );
  528. }
  529. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  530. dstData = const_cast<uint8_t*>(dstMip.m_data);
  531. bimg::imageEncodeFromRgba8(dstData
  532. , rgba
  533. , dstMip.m_width
  534. , dstMip.m_height
  535. , dstMip.m_depth
  536. , outputFormat
  537. , _options.quality
  538. , _err
  539. );
  540. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  541. {
  542. bimg::imageRgba8Downsample2x2(rgba
  543. , dstMip.m_width
  544. , dstMip.m_height
  545. , dstMip.m_depth
  546. , dstMip.m_width*4
  547. , bx::strideAlign(dstMip.m_width/2, blockWidth)*4
  548. , rgba
  549. );
  550. if (_options.alphaTest)
  551. {
  552. bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8
  553. , dstMip.m_width
  554. , dstMip.m_height
  555. , dstMip.m_width*4
  556. , rgba
  557. , coverage
  558. , _options.edge
  559. );
  560. }
  561. if (_options.pma)
  562. {
  563. imagePremultiplyAlpha(
  564. rgba
  565. , dstMip.m_width
  566. , dstMip.m_height
  567. , dstMip.m_depth
  568. , bimg::TextureFormat::RGBA8
  569. );
  570. }
  571. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  572. dstData = const_cast<uint8_t*>(dstMip.m_data);
  573. bimg::imageEncodeFromRgba8(dstData
  574. , rgba
  575. , dstMip.m_width
  576. , dstMip.m_height
  577. , dstMip.m_depth
  578. , outputFormat
  579. , _options.quality
  580. , _err
  581. );
  582. }
  583. if (NULL != ref)
  584. {
  585. bimg::imageDecodeToRgba8(rgba
  586. , output->m_data
  587. , mip.m_width
  588. , mip.m_height
  589. , mip.m_width*mip.m_bpp/8
  590. , outputFormat
  591. );
  592. float result = bimg::imageQualityRgba8(
  593. ref
  594. , rgba
  595. , uint16_t(mip.m_width)
  596. , uint16_t(mip.m_height)
  597. );
  598. printf("%f\n", result);
  599. BX_FREE(_allocator, ref);
  600. }
  601. }
  602. BX_FREE(_allocator, temp);
  603. }
  604. }
  605. bimg::imageFree(input);
  606. }
  607. if (!_err->isOk()
  608. && NULL != output)
  609. {
  610. bimg::imageFree(output);
  611. output = NULL;
  612. }
  613. return output;
  614. }
  615. void help(const char* _error = NULL, bool _showHelp = true)
  616. {
  617. if (NULL != _error)
  618. {
  619. fprintf(stderr, "Error:\n%s\n\n", _error);
  620. if (!_showHelp)
  621. {
  622. return;
  623. }
  624. }
  625. fprintf(stderr
  626. , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  627. "Copyright 2011-2018 Branimir Karadzic. All rights reserved.\n"
  628. "License: https://github.com/bkaradzic/bimg#license-bsd-2-clause\n\n"
  629. , BIMG_TEXTUREC_VERSION_MAJOR
  630. , BIMG_TEXTUREC_VERSION_MINOR
  631. , BIMG_API_VERSION
  632. );
  633. fprintf(stderr
  634. , "Usage: texturec -f <in> -o <out> [-t <texture format>]\n"
  635. "\n"
  636. "Supported file formats:\n"
  637. " *.bmp (input) Windows Bitmap.\n"
  638. " *.dds (input, output) Direct Draw Surface.\n"
  639. " *.exr (input, output) OpenEXR.\n"
  640. " *.gif (input) Graphics Interchange Format.\n"
  641. " *.jpg (input) JPEG Interchange Format.\n"
  642. " *.hdr (input) Radiance RGBE.\n"
  643. " *.ktx (input, output) Khronos Texture.\n"
  644. " *.png (input, output) Portable Network Graphics.\n"
  645. " *.psd (input) Photoshop Document.\n"
  646. " *.pvr (input) PowerVR.\n"
  647. " *.tga (input) Targa.\n"
  648. "\n"
  649. "Options:\n"
  650. " -h, --help Help.\n"
  651. " -v, --version Version information only.\n"
  652. " -f <file path> Input file path.\n"
  653. " -o <file path> Output file path.\n"
  654. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  655. " -q <quality> Encoding quality (default, fastest, highest).\n"
  656. " -m, --mips Generate mip-maps.\n"
  657. " -n, --normalmap Input texture is normal map.\n"
  658. " --equirect Input texture equirectangular projection of cubemap.\n"
  659. " --sdf <edge> Compute SDF texture.\n"
  660. " --ref <alpha> Alpha reference value.\n"
  661. " --iqa Image Quality Assessment\n"
  662. " --pma Premultiply alpha into RGB channel.\n"
  663. " --max <max size> Maximum width/height (image will be scaled down and\n"
  664. " aspect ratio will be preserved.\n"
  665. " --as <extension> Save as.\n"
  666. " --validate *DEBUG* Validate that output image produced matches after loading.\n"
  667. "\n"
  668. "For additional information, see https://github.com/bkaradzic/bgfx\n"
  669. );
  670. }
  671. void help(const char* _str, const bx::Error& _err)
  672. {
  673. std::string str;
  674. if (_str != NULL)
  675. {
  676. str.append(_str);
  677. str.append(" ");
  678. }
  679. const bx::StringView& sv = _err.getMessage();
  680. str.append(sv.getPtr(), sv.getTerm() - sv.getPtr() );
  681. help(str.c_str(), false);
  682. }
  683. int main(int _argc, const char* _argv[])
  684. {
  685. bx::CommandLine cmdLine(_argc, _argv);
  686. if (cmdLine.hasArg('v', "version") )
  687. {
  688. fprintf(stderr
  689. , "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  690. , BIMG_TEXTUREC_VERSION_MAJOR
  691. , BIMG_TEXTUREC_VERSION_MINOR
  692. , BIMG_API_VERSION
  693. );
  694. return bx::kExitSuccess;
  695. }
  696. if (cmdLine.hasArg('h', "help") )
  697. {
  698. help();
  699. return bx::kExitFailure;
  700. }
  701. const char* inputFileName = cmdLine.findOption('f');
  702. if (NULL == inputFileName)
  703. {
  704. help("Input file must be specified.");
  705. return bx::kExitFailure;
  706. }
  707. const char* outputFileName = cmdLine.findOption('o');
  708. if (NULL == outputFileName)
  709. {
  710. help("Output file must be specified.");
  711. return bx::kExitFailure;
  712. }
  713. const char* saveAs = cmdLine.findOption("as");
  714. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".ktx") : saveAs;
  715. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".dds") : saveAs;
  716. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".png") : saveAs;
  717. saveAs = NULL == saveAs ? bx::strFindI(outputFileName, ".exr") : saveAs;
  718. if (NULL == saveAs)
  719. {
  720. help("Output file format must be specified.");
  721. return bx::kExitFailure;
  722. }
  723. Options options;
  724. const char* edgeOpt = cmdLine.findOption("sdf");
  725. if (NULL != edgeOpt)
  726. {
  727. options.sdf = true;
  728. if (!bx::fromString(&options.edge, edgeOpt) )
  729. {
  730. options.edge = 255.0f;
  731. }
  732. }
  733. else
  734. {
  735. const char* alphaRef = cmdLine.findOption("ref");
  736. if (NULL != alphaRef)
  737. {
  738. options.alphaTest = true;
  739. if (!bx::fromString(&options.edge, alphaRef))
  740. {
  741. options.edge = 0.5f;
  742. }
  743. }
  744. }
  745. options.mips = cmdLine.hasArg('m', "mips");
  746. options.normalMap = cmdLine.hasArg('n', "normalmap");
  747. options.equirect = cmdLine.hasArg("equirect");
  748. options.iqa = cmdLine.hasArg("iqa");
  749. options.pma = cmdLine.hasArg("pma");
  750. const char* maxSize = cmdLine.findOption("max");
  751. if (NULL != maxSize)
  752. {
  753. options.maxSize = atoi(maxSize);
  754. }
  755. options.format = bimg::TextureFormat::Count;
  756. const char* type = cmdLine.findOption('t');
  757. if (NULL != type)
  758. {
  759. options.format = bimg::getFormat(type);
  760. if (!bimg::isValid(options.format) )
  761. {
  762. help("Invalid format specified.");
  763. return bx::kExitFailure;
  764. }
  765. }
  766. if (NULL != bx::strFindI(saveAs, "png") )
  767. {
  768. if (options.format == bimg::TextureFormat::Count)
  769. {
  770. options.format = bimg::TextureFormat::RGBA8;
  771. }
  772. else if (options.format != bimg::TextureFormat::RGBA8)
  773. {
  774. help("Ouput PNG format must be RGBA8.");
  775. return bx::kExitFailure;
  776. }
  777. }
  778. else if (NULL != bx::strFindI(saveAs, "exr") )
  779. {
  780. if (options.format == bimg::TextureFormat::Count)
  781. {
  782. options.format = bimg::TextureFormat::RGBA16F;
  783. }
  784. else if (options.format != bimg::TextureFormat::RGBA16F)
  785. {
  786. help("Ouput EXR format must be RGBA16F.");
  787. return bx::kExitFailure;
  788. }
  789. }
  790. const char* quality = cmdLine.findOption('q');
  791. if (NULL != quality)
  792. {
  793. switch (bx::toLower(quality[0]) )
  794. {
  795. case 'h': options.quality = bimg::Quality::Highest; break;
  796. case 'f': options.quality = bimg::Quality::Fastest; break;
  797. case 'd': options.quality = bimg::Quality::Default; break;
  798. default:
  799. help("Invalid quality specified.");
  800. return bx::kExitFailure;
  801. }
  802. }
  803. const bool validate = cmdLine.hasArg("validate");
  804. bx::Error err;
  805. bx::FileReader reader;
  806. if (!bx::open(&reader, inputFileName, &err) )
  807. {
  808. help("Failed to open input file.", err);
  809. return bx::kExitFailure;
  810. }
  811. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  812. if (0 == inputSize)
  813. {
  814. help("Failed to read input file.", err);
  815. return bx::kExitFailure;
  816. }
  817. bx::DefaultAllocator allocator;
  818. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  819. bx::read(&reader, inputData, inputSize, &err);
  820. bx::close(&reader);
  821. if (!err.isOk() )
  822. {
  823. help("Failed to read input file.", err);
  824. return bx::kExitFailure;
  825. }
  826. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
  827. BX_FREE(&allocator, inputData);
  828. if (NULL != output)
  829. {
  830. bx::FileWriter writer;
  831. if (bx::open(&writer, outputFileName, false, &err) )
  832. {
  833. if (NULL != bx::strFindI(saveAs, "ktx") )
  834. {
  835. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size, &err);
  836. }
  837. else if (NULL != bx::strFindI(saveAs, "dds") )
  838. {
  839. bimg::imageWriteDds(&writer, *output, output->m_data, output->m_size, &err);
  840. }
  841. else if (NULL != bx::strFindI(saveAs, "png") )
  842. {
  843. bimg::ImageMip mip;
  844. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  845. bimg::imageWritePng(&writer
  846. , mip.m_width
  847. , mip.m_height
  848. , mip.m_width*4
  849. , mip.m_data
  850. , output->m_format
  851. , false
  852. , &err);
  853. }
  854. else if (NULL != bx::strFindI(saveAs, "exr") )
  855. {
  856. bimg::ImageMip mip;
  857. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  858. bimg::imageWriteExr(&writer
  859. , mip.m_width
  860. , mip.m_height
  861. , mip.m_width*8
  862. , mip.m_data
  863. , output->m_format
  864. , false
  865. , &err);
  866. }
  867. bx::close(&writer);
  868. if (!err.isOk() )
  869. {
  870. help(NULL, err);
  871. return bx::kExitFailure;
  872. }
  873. }
  874. else
  875. {
  876. help("Failed to open output file.", err);
  877. return bx::kExitFailure;
  878. }
  879. if (validate)
  880. {
  881. if (!bx::open(&reader, outputFileName, &err) )
  882. {
  883. help("Failed to validate file.", err);
  884. return bx::kExitFailure;
  885. }
  886. inputSize = (uint32_t)bx::getSize(&reader);
  887. if (0 == inputSize)
  888. {
  889. help("Failed to validate file.", err);
  890. return bx::kExitFailure;
  891. }
  892. inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  893. bx::read(&reader, inputData, inputSize, &err);
  894. bx::close(&reader);
  895. bimg::ImageContainer* input = bimg::imageParse(&allocator, inputData, inputSize, bimg::TextureFormat::Count, &err);
  896. if (!err.isOk() )
  897. {
  898. help("Failed to validate file.", err);
  899. return bx::kExitFailure;
  900. }
  901. if (false
  902. || input->m_format != output->m_format
  903. || input->m_size != output->m_size
  904. || input->m_width != output->m_width
  905. || input->m_height != output->m_height
  906. || input->m_depth != output->m_depth
  907. || input->m_numLayers != output->m_numLayers
  908. || input->m_numMips != output->m_numMips
  909. || input->m_hasAlpha != output->m_hasAlpha
  910. || input->m_cubeMap != output->m_cubeMap
  911. )
  912. {
  913. help("Validation failed, image headers are different.");
  914. return bx::kExitFailure;
  915. }
  916. {
  917. const uint8_t numMips = output->m_numMips;
  918. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  919. for (uint8_t lod = 0; lod < numMips; ++lod)
  920. {
  921. for (uint16_t side = 0; side < numSides; ++side)
  922. {
  923. bimg::ImageMip srcMip;
  924. bool hasSrc = bimg::imageGetRawData(*input, side, lod, input->m_data, input->m_size, srcMip);
  925. bimg::ImageMip dstMip;
  926. bool hasDst = bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  927. if (false
  928. || hasSrc != hasDst
  929. || srcMip.m_size != dstMip.m_size
  930. )
  931. {
  932. help("Validation failed, image mip/layer/side are different.");
  933. return bx::kExitFailure;
  934. }
  935. if (0 != bx::memCmp(srcMip.m_data, dstMip.m_data, srcMip.m_size) )
  936. {
  937. help("Validation failed, image content are different.");
  938. return bx::kExitFailure;
  939. }
  940. }
  941. }
  942. }
  943. BX_FREE(&allocator, inputData);
  944. }
  945. bimg::imageFree(output);
  946. }
  947. else
  948. {
  949. help(NULL, err);
  950. return bx::kExitFailure;
  951. }
  952. return bx::kExitSuccess;
  953. }