texturec.cpp 26 KB

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