texturec.cpp 28 KB

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