texturec.cpp 30 KB

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