texturec.cpp 30 KB

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