texturec.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385
  1. /*
  2. * Copyright 2011-2022 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.");
  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* rgba = (float*)temp;
  353. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  354. bimg::imageDecodeToRgba32f(_allocator
  355. , rgba
  356. , mip.m_data
  357. , dstMip.m_width
  358. , dstMip.m_height
  359. , dstMip.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 = &rgba[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(rgba
  379. , dstMip.m_width
  380. , dstMip.m_height
  381. , dstMip.m_width*16
  382. , rgba
  383. );
  384. bimg::imageRgba32f11to01(rgbaDst
  385. , dstMip.m_width
  386. , dstMip.m_height
  387. , dstMip.m_depth
  388. , dstMip.m_width*16
  389. , rgba
  390. );
  391. bimg::Quality::Enum nmapQuality = bimg::Quality::Enum(_options.quality + bimg::Quality::NormalMapDefault);
  392. bimg::imageEncodeFromRgba32f(_allocator
  393. , dstData
  394. , rgbaDst
  395. , dstMip.m_width
  396. , dstMip.m_height
  397. , dstMip.m_depth
  398. , outputFormat
  399. , nmapQuality
  400. , _err
  401. );
  402. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  403. {
  404. bimg::imageRgba32fDownsample2x2NormalMap(rgba
  405. , dstMip.m_width
  406. , dstMip.m_height
  407. , dstMip.m_width*16
  408. , bx::strideAlign(dstMip.m_width/2, blockWidth)*16
  409. , rgba
  410. );
  411. bimg::imageRgba32f11to01(rgbaDst
  412. , dstMip.m_width
  413. , dstMip.m_height
  414. , dstMip.m_depth
  415. , dstMip.m_width*16
  416. , rgba
  417. );
  418. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  419. dstData = const_cast<uint8_t*>(dstMip.m_data);
  420. bimg::imageEncodeFromRgba32f(_allocator
  421. , dstData
  422. , rgbaDst
  423. , dstMip.m_width
  424. , dstMip.m_height
  425. , dstMip.m_depth
  426. , outputFormat
  427. , nmapQuality
  428. , _err
  429. );
  430. }
  431. BX_FREE(_allocator, rgbaDst);
  432. }
  433. // HDR
  434. else if ( (!bimg::isCompressed(inputFormat) && 8 != inputBlockInfo.rBits)
  435. || outputFormat == bimg::TextureFormat::BC6H
  436. || outputFormat == bimg::TextureFormat::BC7
  437. )
  438. {
  439. uint32_t size = bimg::imageGetSize(
  440. NULL
  441. , uint16_t(dstMip.m_width)
  442. , uint16_t(dstMip.m_height)
  443. , uint16_t(dstMip.m_depth)
  444. , false
  445. , false
  446. , 1
  447. , bimg::TextureFormat::RGBA32F
  448. );
  449. temp = BX_ALLOC(_allocator, size);
  450. float* rgba32f = (float*)temp;
  451. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  452. bimg::imageDecodeToRgba32f(_allocator
  453. , rgba32f
  454. , mip.m_data
  455. , mip.m_width
  456. , mip.m_height
  457. , mip.m_depth
  458. , mip.m_width*16
  459. , mip.m_format
  460. );
  461. if (_options.pma)
  462. {
  463. imagePremultiplyAlpha(
  464. rgba32f
  465. , dstMip.m_width
  466. , dstMip.m_height
  467. , dstMip.m_depth
  468. , bimg::TextureFormat::RGBA32F
  469. );
  470. }
  471. bimg::imageEncodeFromRgba32f(_allocator
  472. , dstData
  473. , rgba32f
  474. , dstMip.m_width
  475. , dstMip.m_height
  476. , dstMip.m_depth
  477. , outputFormat
  478. , _options.quality
  479. , _err
  480. );
  481. if (1 < numMips
  482. && _err->isOk() )
  483. {
  484. bimg::imageRgba32fToLinear(rgba32f
  485. , mip.m_width
  486. , mip.m_height
  487. , mip.m_depth
  488. , mip.m_width*16
  489. , rgba32f
  490. );
  491. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  492. {
  493. bimg::imageRgba32fLinearDownsample2x2(rgba32f
  494. , dstMip.m_width
  495. , dstMip.m_height
  496. , dstMip.m_depth
  497. , dstMip.m_width*16
  498. , rgba32f
  499. );
  500. if (_options.pma)
  501. {
  502. imagePremultiplyAlpha(
  503. rgba32f
  504. , dstMip.m_width
  505. , dstMip.m_height
  506. , dstMip.m_depth
  507. , bimg::TextureFormat::RGBA32F
  508. );
  509. }
  510. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  511. dstData = const_cast<uint8_t*>(dstMip.m_data);
  512. bimg::imageRgba32fToGamma(rgbaDst
  513. , mip.m_width
  514. , mip.m_height
  515. , mip.m_depth
  516. , mip.m_width*16
  517. , rgba32f
  518. );
  519. bimg::imageEncodeFromRgba32f(_allocator
  520. , dstData
  521. , rgbaDst
  522. , dstMip.m_width
  523. , dstMip.m_height
  524. , dstMip.m_depth
  525. , outputFormat
  526. , _options.quality
  527. , _err
  528. );
  529. }
  530. }
  531. BX_FREE(_allocator, rgbaDst);
  532. }
  533. // SDF
  534. else if (_options.sdf)
  535. {
  536. uint32_t size = bimg::imageGetSize(
  537. NULL
  538. , uint16_t(dstMip.m_width)
  539. , uint16_t(dstMip.m_height)
  540. , uint16_t(dstMip.m_depth)
  541. , false
  542. , false
  543. , 1
  544. , bimg::TextureFormat::R8
  545. );
  546. temp = BX_ALLOC(_allocator, size);
  547. uint8_t* r8 = (uint8_t*)temp;
  548. bimg::imageDecodeToR8(_allocator
  549. , r8
  550. , mip.m_data
  551. , mip.m_width
  552. , mip.m_height
  553. , mip.m_depth
  554. , mip.m_width
  555. , mip.m_format
  556. );
  557. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  558. dstData = const_cast<uint8_t*>(dstMip.m_data);
  559. bimg::imageMakeDist(_allocator
  560. , dstData
  561. , mip.m_width
  562. , mip.m_height
  563. , mip.m_width
  564. , r8
  565. );
  566. if (_options.mips) {
  567. const float alphaRef = 0.5f;
  568. float coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::A8
  569. , mip.m_width
  570. , mip.m_height
  571. , mip.m_width
  572. , r8
  573. , alphaRef
  574. );
  575. size = bimg::imageGetSize(
  576. NULL
  577. , uint16_t(dstMip.m_width)
  578. , uint16_t(dstMip.m_height)
  579. , uint16_t(dstMip.m_depth)
  580. , false
  581. , false
  582. , 1
  583. , bimg::TextureFormat::RGBA8
  584. );
  585. void* rgbaTemp = BX_ALLOC(_allocator, size);
  586. uint8_t* rgba = (uint8_t*)rgbaTemp;
  587. bimg::imageDecodeToRgba8(
  588. _allocator
  589. , rgba
  590. , dstMip.m_data
  591. , dstMip.m_width
  592. , dstMip.m_height
  593. , dstMip.m_width * 4
  594. , bimg::TextureFormat::A8
  595. );
  596. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod) {
  597. bimg::imageRgba8Downsample2x2(rgba
  598. , dstMip.m_width
  599. , dstMip.m_height
  600. , dstMip.m_depth
  601. , dstMip.m_width * 4
  602. , bx::strideAlign(dstMip.m_width / 2, blockWidth) * 4
  603. , rgba
  604. );
  605. // For each mip, upscale to original size,
  606. // scale image alpha to get same coverage as mip0
  607. uint32_t upsample = 1 << lod;
  608. uint32_t destWidth = dstMip.m_width / 2;
  609. uint32_t destHeight = dstMip.m_height / 2;
  610. bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8
  611. , destWidth
  612. , destHeight
  613. , destWidth * 4
  614. , rgba
  615. , coverage
  616. , alphaRef
  617. , upsample
  618. );
  619. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  620. dstData = const_cast<uint8_t*>(dstMip.m_data);
  621. bimg::imageEncodeFromRgba8(
  622. _allocator
  623. , dstData
  624. , rgba
  625. , dstMip.m_width
  626. , dstMip.m_height
  627. , dstMip.m_depth
  628. , bimg::TextureFormat::A8
  629. , _options.quality
  630. , _err
  631. );
  632. }
  633. BX_FREE(_allocator, rgbaTemp);
  634. }
  635. }
  636. // RGBA8
  637. else
  638. {
  639. uint32_t size = bimg::imageGetSize(
  640. NULL
  641. , uint16_t(dstMip.m_width)
  642. , uint16_t(dstMip.m_height)
  643. , uint16_t(dstMip.m_depth)
  644. , false
  645. , false
  646. , 1
  647. , bimg::TextureFormat::RGBA8
  648. );
  649. temp = BX_ALLOC(_allocator, size);
  650. uint8_t* rgba = (uint8_t*)temp;
  651. bimg::imageDecodeToRgba8(
  652. _allocator
  653. , rgba
  654. , mip.m_data
  655. , mip.m_width
  656. , mip.m_height
  657. , mip.m_width*4
  658. , mip.m_format
  659. );
  660. float coverage = 0.0f;
  661. if (_options.alphaTest)
  662. {
  663. coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::RGBA8
  664. , mip.m_width
  665. , mip.m_height
  666. , mip.m_width*4
  667. , rgba
  668. , _options.edge
  669. );
  670. }
  671. void* ref = NULL;
  672. if (_options.iqa)
  673. {
  674. ref = BX_ALLOC(_allocator, size);
  675. bx::memCopy(ref, rgba, size);
  676. }
  677. if (_options.pma)
  678. {
  679. imagePremultiplyAlpha(
  680. rgba
  681. , dstMip.m_width
  682. , dstMip.m_height
  683. , dstMip.m_depth
  684. , bimg::TextureFormat::RGBA8
  685. );
  686. }
  687. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  688. dstData = const_cast<uint8_t*>(dstMip.m_data);
  689. bimg::imageEncodeFromRgba8(
  690. _allocator
  691. , dstData
  692. , rgba
  693. , dstMip.m_width
  694. , dstMip.m_height
  695. , dstMip.m_depth
  696. , outputFormat
  697. , _options.quality
  698. , _err
  699. );
  700. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  701. {
  702. bimg::imageRgba8Downsample2x2(rgba
  703. , dstMip.m_width
  704. , dstMip.m_height
  705. , dstMip.m_depth
  706. , dstMip.m_width*4
  707. , bx::strideAlign(dstMip.m_width/2, blockWidth)*4
  708. , rgba
  709. );
  710. if (_options.alphaTest)
  711. {
  712. bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8
  713. , dstMip.m_width
  714. , dstMip.m_height
  715. , dstMip.m_width*4
  716. , rgba
  717. , coverage
  718. , _options.edge
  719. );
  720. }
  721. if (_options.pma)
  722. {
  723. imagePremultiplyAlpha(
  724. rgba
  725. , dstMip.m_width
  726. , dstMip.m_height
  727. , dstMip.m_depth
  728. , bimg::TextureFormat::RGBA8
  729. );
  730. }
  731. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  732. dstData = const_cast<uint8_t*>(dstMip.m_data);
  733. bimg::imageEncodeFromRgba8(
  734. _allocator
  735. , dstData
  736. , rgba
  737. , dstMip.m_width
  738. , dstMip.m_height
  739. , dstMip.m_depth
  740. , outputFormat
  741. , _options.quality
  742. , _err
  743. );
  744. }
  745. if (NULL != ref)
  746. {
  747. bimg::imageDecodeToRgba8(
  748. _allocator
  749. , rgba
  750. , output->m_data
  751. , mip.m_width
  752. , mip.m_height
  753. , mip.m_width*mip.m_bpp/8
  754. , outputFormat
  755. );
  756. float result = bimg::imageQualityRgba8(
  757. ref
  758. , rgba
  759. , uint16_t(mip.m_width)
  760. , uint16_t(mip.m_height)
  761. );
  762. bx::printf("%f\n", result);
  763. BX_FREE(_allocator, ref);
  764. }
  765. }
  766. BX_FREE(_allocator, temp);
  767. }
  768. }
  769. bimg::imageFree(input);
  770. }
  771. if (!_err->isOk()
  772. && NULL != output)
  773. {
  774. bimg::imageFree(output);
  775. output = NULL;
  776. }
  777. return output;
  778. }
  779. void help(const char* _error = NULL, bool _showHelp = true)
  780. {
  781. if (NULL != _error)
  782. {
  783. bx::printf("Error:\n%s\n\n", _error);
  784. if (!_showHelp)
  785. {
  786. return;
  787. }
  788. }
  789. bx::printf(
  790. "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  791. "Copyright 2011-2022 Branimir Karadzic. All rights reserved.\n"
  792. "License: https://github.com/bkaradzic/bimg/blob/master/LICENSE\n\n"
  793. , BIMG_TEXTUREC_VERSION_MAJOR
  794. , BIMG_TEXTUREC_VERSION_MINOR
  795. , BIMG_API_VERSION
  796. );
  797. bx::printf(
  798. "Usage: texturec -f <in> -o <out> [-t <texture format>]\n"
  799. "\n"
  800. "Supported file formats:\n"
  801. " *.bmp (input) Windows Bitmap.\n"
  802. " *.dds (input, output) Direct Draw Surface.\n"
  803. " *.exr (input, output) OpenEXR.\n"
  804. " *.gif (input) Graphics Interchange Format.\n"
  805. " *.jpg (input) JPEG Interchange Format.\n"
  806. " *.hdr (input, output) Radiance RGBE.\n"
  807. " *.ktx (input, output) Khronos Texture.\n"
  808. " *.png (input, output) Portable Network Graphics.\n"
  809. " *.psd (input) Photoshop Document.\n"
  810. " *.pvr (input) PowerVR.\n"
  811. " *.tga (input) Truevision TGA.\n"
  812. "\n"
  813. "Options:\n"
  814. " -h, --help Help.\n"
  815. " -v, --version Version information only.\n"
  816. " -f <file path> Input file path.\n"
  817. " -o <file path> Output file path.\n"
  818. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  819. " -q <quality> Encoding quality (default, fastest, highest).\n"
  820. " -m, --mips Generate mip-maps.\n"
  821. " --mipskip <N> Skip <N> number of mips.\n"
  822. " -n, --normalmap Input texture is normal map. (Implies --linear)\n"
  823. " --equirect Input texture is equirectangular projection of cubemap.\n"
  824. " --strip Input texture is horizontal or vertical strip of cubemap.\n"
  825. " --sdf Compute SDF texture.\n"
  826. " --ref <alpha> Alpha reference value.\n"
  827. " --iqa Image Quality Assessment\n"
  828. " --pma Premultiply alpha into RGB channel.\n"
  829. " --linear Input and output texture is linear color space (gamma correction won't be applied).\n"
  830. " --max <max size> Maximum width/height (image will be scaled down and\n"
  831. " aspect ratio will be preserved)\n"
  832. " --radiance <model> Radiance cubemap filter. (Lighting model: Phong, PhongBrdf, Blinn, BlinnBrdf, GGX)\n"
  833. " --as <extension> Save as.\n"
  834. " --formats List all supported formats.\n"
  835. " --validate *DEBUG* Validate that output image produced matches after loading.\n"
  836. "\n"
  837. "For additional information, see https://github.com/bkaradzic/bimg\n"
  838. );
  839. }
  840. void help(const bx::StringView _str, const bx::Error& _err)
  841. {
  842. std::string str;
  843. if (!_str.isEmpty() )
  844. {
  845. str.append(_str.getPtr(), _str.getTerm() - _str.getPtr() );
  846. str.append(": ");
  847. }
  848. const bx::StringView& sv = _err.getMessage();
  849. str.append("'");
  850. str.append(sv.getPtr(), sv.getTerm() - sv.getPtr() );
  851. str.append("'");
  852. help(str.c_str(), false);
  853. }
  854. class AlignedAllocator : public bx::AllocatorI
  855. {
  856. public:
  857. AlignedAllocator(bx::AllocatorI* _allocator, size_t _minAlignment)
  858. : m_allocator(_allocator)
  859. , m_minAlignment(_minAlignment)
  860. {
  861. }
  862. virtual void* realloc(
  863. void* _ptr
  864. , size_t _size
  865. , size_t _align
  866. , const char* _file
  867. , uint32_t _line
  868. )
  869. {
  870. return m_allocator->realloc(_ptr, _size, bx::max(_align, m_minAlignment), _file, _line);
  871. }
  872. bx::AllocatorI* m_allocator;
  873. size_t m_minAlignment;
  874. };
  875. int main(int _argc, const char* _argv[])
  876. {
  877. bx::CommandLine cmdLine(_argc, _argv);
  878. if (cmdLine.hasArg('v', "version") )
  879. {
  880. bx::printf(
  881. "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  882. , BIMG_TEXTUREC_VERSION_MAJOR
  883. , BIMG_TEXTUREC_VERSION_MINOR
  884. , BIMG_API_VERSION
  885. );
  886. return bx::kExitSuccess;
  887. }
  888. if (cmdLine.hasArg('h', "help") )
  889. {
  890. help();
  891. return bx::kExitFailure;
  892. }
  893. if (cmdLine.hasArg("formats"))
  894. {
  895. bx::printf("Uncompressed formats:\n");
  896. for (int format = bimg::TextureFormat::Unknown + 1; format < bimg::TextureFormat::UnknownDepth; format++)
  897. {
  898. bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  899. }
  900. for (int format = bimg::TextureFormat::UnknownDepth + 1; format < bimg::TextureFormat::Count; format++)
  901. {
  902. bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  903. }
  904. bx::printf("Compressed formats:\n");
  905. for (int format = 0; format < bimg::TextureFormat::Unknown; format++)
  906. {
  907. bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  908. }
  909. return bx::kExitSuccess;
  910. }
  911. const char* inputFileName = cmdLine.findOption('f');
  912. if (NULL == inputFileName)
  913. {
  914. help("Input file must be specified.");
  915. return bx::kExitFailure;
  916. }
  917. const char* outputFileName = cmdLine.findOption('o');
  918. if (NULL == outputFileName)
  919. {
  920. help("Output file must be specified.");
  921. return bx::kExitFailure;
  922. }
  923. bx::StringView saveAs = cmdLine.findOption("as");
  924. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".ktx") : saveAs;
  925. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".dds") : saveAs;
  926. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".png") : saveAs;
  927. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".exr") : saveAs;
  928. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".hdr") : saveAs;
  929. if (saveAs.isEmpty() )
  930. {
  931. help("Output file format must be specified.");
  932. return bx::kExitFailure;
  933. }
  934. Options options;
  935. const char* alphaRef = cmdLine.findOption("ref");
  936. if (NULL != alphaRef)
  937. {
  938. options.alphaTest = true;
  939. if (!bx::fromString(&options.edge, alphaRef))
  940. {
  941. options.edge = 0.5f;
  942. }
  943. }
  944. options.sdf = cmdLine.hasArg("sdf");
  945. options.mips = cmdLine.hasArg('m', "mips");
  946. options.normalMap = cmdLine.hasArg('n', "normalmap");
  947. options.equirect = cmdLine.hasArg("equirect");
  948. options.strip = cmdLine.hasArg("strip");
  949. options.iqa = cmdLine.hasArg("iqa");
  950. options.pma = cmdLine.hasArg("pma");
  951. options.linear = cmdLine.hasArg("linear");
  952. if (options.equirect
  953. && options.strip)
  954. {
  955. help("Image can't be equirect and strip at the same time.");
  956. return bx::kExitFailure;
  957. }
  958. // Normal maps are always linear
  959. if (options.normalMap)
  960. {
  961. options.linear = true;
  962. }
  963. const char* maxSize = cmdLine.findOption("max");
  964. if (NULL != maxSize)
  965. {
  966. if (!bx::fromString(&options.maxSize, maxSize) )
  967. {
  968. help("Parsing `--max` failed.");
  969. return bx::kExitFailure;
  970. }
  971. }
  972. const char* mipSkip = cmdLine.findOption("mipskip");
  973. if (NULL != mipSkip)
  974. {
  975. if (!bx::fromString(&options.mipSkip, mipSkip) )
  976. {
  977. help("Parsing `--mipskip` failed.");
  978. return bx::kExitFailure;
  979. }
  980. }
  981. options.format = bimg::TextureFormat::Count;
  982. const char* type = cmdLine.findOption('t');
  983. if (NULL != type)
  984. {
  985. options.format = bimg::getFormat(type);
  986. if (!bimg::isValid(options.format) )
  987. {
  988. help("Invalid format specified.");
  989. return bx::kExitFailure;
  990. }
  991. }
  992. if (!bx::strFindI(saveAs, "png").isEmpty() )
  993. {
  994. if (options.format == bimg::TextureFormat::Count)
  995. {
  996. options.format = bimg::TextureFormat::RGBA8;
  997. }
  998. else if (options.format != bimg::TextureFormat::RGBA8)
  999. {
  1000. help("Output PNG format must be RGBA8.");
  1001. return bx::kExitFailure;
  1002. }
  1003. }
  1004. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1005. {
  1006. if (options.format == bimg::TextureFormat::Count)
  1007. {
  1008. options.format = bimg::TextureFormat::RGBA16F;
  1009. }
  1010. else if (options.format != bimg::TextureFormat::RGBA16F)
  1011. {
  1012. help("Output EXR format must be RGBA16F.");
  1013. return bx::kExitFailure;
  1014. }
  1015. }
  1016. const char* quality = cmdLine.findOption('q');
  1017. if (NULL != quality)
  1018. {
  1019. switch (bx::toLower(quality[0]) )
  1020. {
  1021. case 'h': options.quality = bimg::Quality::Highest; break;
  1022. case 'f': options.quality = bimg::Quality::Fastest; break;
  1023. case 'd': options.quality = bimg::Quality::Default; break;
  1024. default:
  1025. help("Invalid quality specified.");
  1026. return bx::kExitFailure;
  1027. }
  1028. }
  1029. const char* radiance = cmdLine.findOption("radiance");
  1030. if (NULL != radiance)
  1031. {
  1032. if (0 == bx::strCmpI(radiance, "phong" ) ) { options.radiance = bimg::LightingModel::Phong; }
  1033. else if (0 == bx::strCmpI(radiance, "phongbrdf") ) { options.radiance = bimg::LightingModel::PhongBrdf; }
  1034. else if (0 == bx::strCmpI(radiance, "blinn" ) ) { options.radiance = bimg::LightingModel::Blinn; }
  1035. else if (0 == bx::strCmpI(radiance, "blinnbrdf") ) { options.radiance = bimg::LightingModel::BlinnBrdf; }
  1036. else if (0 == bx::strCmpI(radiance, "ggx" ) ) { options.radiance = bimg::LightingModel::Ggx; }
  1037. else
  1038. {
  1039. help("Invalid radiance lighting model specified.");
  1040. return bx::kExitFailure;
  1041. }
  1042. }
  1043. const bool validate = cmdLine.hasArg("validate");
  1044. bx::Error err;
  1045. bx::FileReader reader;
  1046. if (!bx::open(&reader, inputFileName, &err) )
  1047. {
  1048. help("Failed to open input file.", err);
  1049. return bx::kExitFailure;
  1050. }
  1051. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  1052. if (0 == inputSize)
  1053. {
  1054. help("Failed to read input file.", err);
  1055. return bx::kExitFailure;
  1056. }
  1057. bx::DefaultAllocator defaultAllocator;
  1058. AlignedAllocator allocator(&defaultAllocator, 16);
  1059. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1060. bx::read(&reader, inputData, inputSize, &err);
  1061. bx::close(&reader);
  1062. if (!err.isOk() )
  1063. {
  1064. help("Failed to read input file.", err);
  1065. return bx::kExitFailure;
  1066. }
  1067. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
  1068. BX_FREE(&allocator, inputData);
  1069. if (NULL != output)
  1070. {
  1071. output->m_srgb = !options.linear;
  1072. bx::FileWriter writer;
  1073. if (bx::open(&writer, outputFileName, false, &err) )
  1074. {
  1075. if (!bx::strFindI(saveAs, "ktx").isEmpty() )
  1076. {
  1077. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size, &err);
  1078. }
  1079. else if (!bx::strFindI(saveAs, "dds").isEmpty() )
  1080. {
  1081. bimg::imageWriteDds(&writer, *output, output->m_data, output->m_size, &err);
  1082. }
  1083. else if (!bx::strFindI(saveAs, "png").isEmpty() )
  1084. {
  1085. if (output->m_format != bimg::TextureFormat::RGBA8)
  1086. {
  1087. help("Incompatible output texture format. Output PNG format must be RGBA8.", err);
  1088. return bx::kExitFailure;
  1089. }
  1090. bimg::ImageMip mip;
  1091. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1092. bimg::imageWritePng(&writer
  1093. , mip.m_width
  1094. , mip.m_height
  1095. , mip.m_width*4
  1096. , mip.m_data
  1097. , output->m_format
  1098. , false
  1099. , &err
  1100. );
  1101. }
  1102. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1103. {
  1104. bimg::ImageMip mip;
  1105. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1106. bimg::imageWriteExr(&writer
  1107. , mip.m_width
  1108. , mip.m_height
  1109. , mip.m_width*8
  1110. , mip.m_data
  1111. , output->m_format
  1112. , false
  1113. , &err
  1114. );
  1115. }
  1116. else if (!bx::strFindI(saveAs, "hdr").isEmpty() )
  1117. {
  1118. bimg::ImageMip mip;
  1119. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1120. bimg::imageWriteHdr(&writer
  1121. , mip.m_width
  1122. , mip.m_height
  1123. , mip.m_width*getBitsPerPixel(mip.m_format)/8
  1124. , mip.m_data
  1125. , output->m_format
  1126. , false
  1127. , &err
  1128. );
  1129. }
  1130. bx::close(&writer);
  1131. if (!err.isOk() )
  1132. {
  1133. help("", err);
  1134. return bx::kExitFailure;
  1135. }
  1136. }
  1137. else
  1138. {
  1139. help("Failed to open output file.", err);
  1140. return bx::kExitFailure;
  1141. }
  1142. if (validate)
  1143. {
  1144. if (!bx::open(&reader, outputFileName, &err) )
  1145. {
  1146. help("Failed to validate file.", err);
  1147. return bx::kExitFailure;
  1148. }
  1149. inputSize = (uint32_t)bx::getSize(&reader);
  1150. if (0 == inputSize)
  1151. {
  1152. help("Failed to validate file.", err);
  1153. return bx::kExitFailure;
  1154. }
  1155. inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1156. bx::read(&reader, inputData, inputSize, &err);
  1157. bx::close(&reader);
  1158. bimg::ImageContainer* input = bimg::imageParse(&allocator, inputData, inputSize, bimg::TextureFormat::Count, &err);
  1159. if (!err.isOk() )
  1160. {
  1161. help("Failed to validate file.", err);
  1162. return bx::kExitFailure;
  1163. }
  1164. if (false
  1165. || input->m_format != output->m_format
  1166. || input->m_size != output->m_size
  1167. || input->m_width != output->m_width
  1168. || input->m_height != output->m_height
  1169. || input->m_depth != output->m_depth
  1170. || input->m_numLayers != output->m_numLayers
  1171. || input->m_numMips != output->m_numMips
  1172. || input->m_hasAlpha != output->m_hasAlpha
  1173. || input->m_cubeMap != output->m_cubeMap
  1174. )
  1175. {
  1176. help("Validation failed, image headers are different.");
  1177. return bx::kExitFailure;
  1178. }
  1179. {
  1180. const uint8_t numMips = output->m_numMips;
  1181. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  1182. for (uint8_t lod = 0; lod < numMips; ++lod)
  1183. {
  1184. for (uint16_t side = 0; side < numSides; ++side)
  1185. {
  1186. bimg::ImageMip srcMip;
  1187. bool hasSrc = bimg::imageGetRawData(*input, side, lod, input->m_data, input->m_size, srcMip);
  1188. bimg::ImageMip dstMip;
  1189. bool hasDst = bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  1190. if (false
  1191. || hasSrc != hasDst
  1192. || srcMip.m_size != dstMip.m_size
  1193. )
  1194. {
  1195. help("Validation failed, image mip/layer/side are different.");
  1196. return bx::kExitFailure;
  1197. }
  1198. if (0 != bx::memCmp(srcMip.m_data, dstMip.m_data, srcMip.m_size) )
  1199. {
  1200. help("Validation failed, image content are different.");
  1201. return bx::kExitFailure;
  1202. }
  1203. }
  1204. }
  1205. }
  1206. BX_FREE(&allocator, inputData);
  1207. }
  1208. bimg::imageFree(output);
  1209. }
  1210. else
  1211. {
  1212. help("Failed to create output", err);
  1213. return bx::kExitFailure;
  1214. }
  1215. return bx::kExitSuccess;
  1216. }