texturec.cpp 31 KB

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