texturec.cpp 27 KB

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