texturec.cpp 35 KB

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