texturec.cpp 32 KB

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