texturec.cpp 29 KB

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