texturec.cpp 27 KB

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