texturec.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause
  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, ...) fprintf(stderr, "" _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/6 == outputHeight)
  174. {
  175. if (outputWidth/6 > _options.maxSize)
  176. {
  177. outputWidth = _options.maxSize*6;
  178. outputHeight = _options.maxSize;
  179. }
  180. }
  181. else
  182. {
  183. bimg::imageFree(input);
  184. BX_ERROR_SET(_err, TEXTRUREC_ERROR, "Input image format is not horizontal strip.");
  185. return NULL;
  186. }
  187. }
  188. else if (outputWidth > _options.maxSize
  189. || outputHeight > _options.maxSize
  190. || outputDepth > _options.maxSize)
  191. {
  192. if (outputDepth > outputWidth
  193. && outputDepth > outputHeight)
  194. {
  195. outputWidth = outputWidth * _options.maxSize / outputDepth;
  196. outputHeight = outputHeight * _options.maxSize / outputDepth;
  197. outputDepth = _options.maxSize;
  198. }
  199. else if (outputWidth > outputHeight)
  200. {
  201. outputDepth = outputDepth * _options.maxSize / outputWidth;
  202. outputHeight = outputHeight * _options.maxSize / outputWidth;
  203. outputWidth = _options.maxSize;
  204. }
  205. else
  206. {
  207. outputDepth = outputDepth * _options.maxSize / outputHeight;
  208. outputWidth = outputWidth * _options.maxSize / outputHeight;
  209. outputHeight = _options.maxSize;
  210. }
  211. }
  212. const bool needResize = false
  213. || input->m_width != outputWidth
  214. || input->m_height != outputHeight
  215. ;
  216. const bool passThru = true
  217. && !needResize
  218. && (1 < input->m_numMips) == _options.mips
  219. && !_options.sdf
  220. && !_options.alphaTest
  221. && !_options.normalMap
  222. && !(_options.equirect || _options.strip)
  223. && !_options.iqa
  224. && !_options.pma
  225. && (bimg::LightingModel::Count == _options.radiance)
  226. ;
  227. if (!_options.sdf
  228. && needResize)
  229. {
  230. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input, false);
  231. bimg::ImageContainer* dst = bimg::imageAlloc(
  232. _allocator
  233. , bimg::TextureFormat::RGBA32F
  234. , uint16_t(outputWidth)
  235. , uint16_t(outputHeight)
  236. , uint16_t(outputDepth)
  237. , input->m_numLayers
  238. , input->m_cubeMap
  239. , false
  240. );
  241. if (!_options.linear)
  242. {
  243. bimg::imageRgba32fToLinear(src);
  244. }
  245. bimg::imageResizeRgba32fLinear(dst, src);
  246. if (!_options.linear)
  247. {
  248. bimg::imageRgba32fToGamma(dst);
  249. }
  250. bimg::imageFree(src);
  251. bimg::imageFree(input);
  252. if (bimg::isCompressed(inputFormat) )
  253. {
  254. if (inputFormat == bimg::TextureFormat::BC6H)
  255. {
  256. inputFormat = bimg::TextureFormat::RGBA32F;
  257. }
  258. else
  259. {
  260. inputFormat = bimg::TextureFormat::RGBA8;
  261. }
  262. }
  263. input = bimg::imageConvert(_allocator, inputFormat, *dst);
  264. bimg::imageFree(dst);
  265. }
  266. if (passThru)
  267. {
  268. if (inputFormat != outputFormat
  269. && bimg::isCompressed(outputFormat) )
  270. {
  271. output = bimg::imageEncode(_allocator, outputFormat, _options.quality, *input);
  272. }
  273. else
  274. {
  275. output = bimg::imageConvert(_allocator, outputFormat, *input);
  276. }
  277. bimg::imageFree(input);
  278. return output;
  279. }
  280. if (_options.equirect
  281. || _options.strip)
  282. {
  283. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input);
  284. bimg::imageFree(input);
  285. bimg::ImageContainer* dst;
  286. if (outputWidth/2 == outputHeight)
  287. {
  288. dst = bimg::imageCubemapFromLatLongRgba32F(_allocator, *src, true, _err);
  289. bimg::imageFree(src);
  290. }
  291. else
  292. {
  293. dst = bimg::imageCubemapFromStripRgba32F(_allocator, *src, _err);
  294. bimg::imageFree(src);
  295. }
  296. if (!_err->isOk() )
  297. {
  298. return NULL;
  299. }
  300. input = bimg::imageConvert(_allocator, inputFormat, *dst);
  301. bimg::imageFree(dst);
  302. }
  303. if (bimg::LightingModel::Count != _options.radiance)
  304. {
  305. output = bimg::imageCubemapRadianceFilter(_allocator, *input, _options.radiance, _err);
  306. if (!_err->isOk() )
  307. {
  308. return NULL;
  309. }
  310. if (bimg::TextureFormat::RGBA32F != outputFormat)
  311. {
  312. bimg::ImageContainer* temp = bimg::imageEncode(_allocator, outputFormat, _options.quality, *output);
  313. bimg::imageFree(output);
  314. output = temp;
  315. }
  316. bimg::imageFree(input);
  317. return output;
  318. }
  319. output = bimg::imageAlloc(
  320. _allocator
  321. , outputFormat
  322. , uint16_t(input->m_width)
  323. , uint16_t(input->m_height)
  324. , uint16_t(input->m_depth)
  325. , input->m_numLayers
  326. , input->m_cubeMap
  327. , _options.mips
  328. );
  329. const uint8_t numMips = output->m_numMips;
  330. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  331. for (uint16_t side = 0; side < numSides && _err->isOk(); ++side)
  332. {
  333. bimg::ImageMip mip;
  334. if (bimg::imageGetRawData(*input, side, 0, input->m_data, input->m_size, mip) )
  335. {
  336. bimg::ImageMip dstMip;
  337. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  338. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  339. void* temp = NULL;
  340. // Normal map.
  341. if (_options.normalMap)
  342. {
  343. uint32_t size = bimg::imageGetSize(
  344. NULL
  345. , uint16_t(dstMip.m_width)
  346. , uint16_t(dstMip.m_height)
  347. , 0
  348. , false
  349. , false
  350. , 1
  351. , bimg::TextureFormat::RGBA32F
  352. );
  353. temp = BX_ALLOC(_allocator, size);
  354. float* rgba = (float*)temp;
  355. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  356. bimg::imageDecodeToRgba32f(_allocator
  357. , rgba
  358. , mip.m_data
  359. , dstMip.m_width
  360. , dstMip.m_height
  361. , dstMip.m_depth
  362. , dstMip.m_width*16
  363. , mip.m_format
  364. );
  365. if (bimg::TextureFormat::BC5 != mip.m_format)
  366. {
  367. for (uint32_t yy = 0; yy < mip.m_height; ++yy)
  368. {
  369. for (uint32_t xx = 0; xx < mip.m_width; ++xx)
  370. {
  371. const uint32_t offset = (yy*mip.m_width + xx) * 4;
  372. float* inout = &rgba[offset];
  373. inout[0] = inout[0] * 2.0f - 1.0f;
  374. inout[1] = inout[1] * 2.0f - 1.0f;
  375. inout[2] = inout[2] * 2.0f - 1.0f;
  376. inout[3] = inout[3] * 2.0f - 1.0f;
  377. }
  378. }
  379. }
  380. imageRgba32fNormalize(rgba
  381. , dstMip.m_width
  382. , dstMip.m_height
  383. , dstMip.m_width*16
  384. , rgba
  385. );
  386. bimg::imageRgba32f11to01(rgbaDst
  387. , dstMip.m_width
  388. , dstMip.m_height
  389. , dstMip.m_depth
  390. , dstMip.m_width*16
  391. , rgba
  392. );
  393. bimg::Quality::Enum nmapQuality = bimg::Quality::Enum(_options.quality + bimg::Quality::NormalMapDefault);
  394. bimg::imageEncodeFromRgba32f(_allocator
  395. , dstData
  396. , rgbaDst
  397. , dstMip.m_width
  398. , dstMip.m_height
  399. , dstMip.m_depth
  400. , outputFormat
  401. , nmapQuality
  402. , _err
  403. );
  404. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  405. {
  406. bimg::imageRgba32fDownsample2x2NormalMap(rgba
  407. , dstMip.m_width
  408. , dstMip.m_height
  409. , dstMip.m_width*16
  410. , bx::strideAlign(dstMip.m_width/2, blockWidth)*16
  411. , rgba
  412. );
  413. bimg::imageRgba32f11to01(rgbaDst
  414. , dstMip.m_width
  415. , dstMip.m_height
  416. , dstMip.m_depth
  417. , dstMip.m_width*16
  418. , rgba
  419. );
  420. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  421. dstData = const_cast<uint8_t*>(dstMip.m_data);
  422. bimg::imageEncodeFromRgba32f(_allocator
  423. , dstData
  424. , rgbaDst
  425. , dstMip.m_width
  426. , dstMip.m_height
  427. , dstMip.m_depth
  428. , outputFormat
  429. , nmapQuality
  430. , _err
  431. );
  432. }
  433. BX_FREE(_allocator, rgbaDst);
  434. }
  435. // HDR
  436. else if ( (!bimg::isCompressed(inputFormat) && 8 != inputBlockInfo.rBits)
  437. || outputFormat == bimg::TextureFormat::BC6H
  438. || outputFormat == bimg::TextureFormat::BC7
  439. )
  440. {
  441. uint32_t size = bimg::imageGetSize(
  442. NULL
  443. , uint16_t(dstMip.m_width)
  444. , uint16_t(dstMip.m_height)
  445. , uint16_t(dstMip.m_depth)
  446. , false
  447. , false
  448. , 1
  449. , bimg::TextureFormat::RGBA32F
  450. );
  451. temp = BX_ALLOC(_allocator, size);
  452. float* rgba32f = (float*)temp;
  453. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  454. bimg::imageDecodeToRgba32f(_allocator
  455. , rgba32f
  456. , mip.m_data
  457. , mip.m_width
  458. , mip.m_height
  459. , mip.m_depth
  460. , mip.m_width*16
  461. , mip.m_format
  462. );
  463. if (_options.pma)
  464. {
  465. imagePremultiplyAlpha(
  466. rgba32f
  467. , dstMip.m_width
  468. , dstMip.m_height
  469. , dstMip.m_depth
  470. , bimg::TextureFormat::RGBA32F
  471. );
  472. }
  473. bimg::imageEncodeFromRgba32f(_allocator
  474. , dstData
  475. , rgba32f
  476. , dstMip.m_width
  477. , dstMip.m_height
  478. , dstMip.m_depth
  479. , outputFormat
  480. , _options.quality
  481. , _err
  482. );
  483. if (1 < numMips
  484. && _err->isOk() )
  485. {
  486. bimg::imageRgba32fToLinear(rgba32f
  487. , mip.m_width
  488. , mip.m_height
  489. , mip.m_depth
  490. , mip.m_width*16
  491. , rgba32f
  492. );
  493. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  494. {
  495. bimg::imageRgba32fLinearDownsample2x2(rgba32f
  496. , dstMip.m_width
  497. , dstMip.m_height
  498. , dstMip.m_depth
  499. , dstMip.m_width*16
  500. , rgba32f
  501. );
  502. if (_options.pma)
  503. {
  504. imagePremultiplyAlpha(
  505. rgba32f
  506. , dstMip.m_width
  507. , dstMip.m_height
  508. , dstMip.m_depth
  509. , bimg::TextureFormat::RGBA32F
  510. );
  511. }
  512. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  513. dstData = const_cast<uint8_t*>(dstMip.m_data);
  514. bimg::imageRgba32fToGamma(rgbaDst
  515. , mip.m_width
  516. , mip.m_height
  517. , mip.m_depth
  518. , mip.m_width*16
  519. , rgba32f
  520. );
  521. bimg::imageEncodeFromRgba32f(_allocator
  522. , dstData
  523. , rgbaDst
  524. , dstMip.m_width
  525. , dstMip.m_height
  526. , dstMip.m_depth
  527. , outputFormat
  528. , _options.quality
  529. , _err
  530. );
  531. }
  532. }
  533. BX_FREE(_allocator, rgbaDst);
  534. }
  535. // SDF
  536. else if (_options.sdf)
  537. {
  538. uint32_t size = bimg::imageGetSize(
  539. NULL
  540. , uint16_t(dstMip.m_width)
  541. , uint16_t(dstMip.m_height)
  542. , uint16_t(dstMip.m_depth)
  543. , false
  544. , false
  545. , 1
  546. , bimg::TextureFormat::R8
  547. );
  548. temp = BX_ALLOC(_allocator, size);
  549. uint8_t* r8 = (uint8_t*)temp;
  550. bimg::imageDecodeToR8(_allocator
  551. , r8
  552. , mip.m_data
  553. , mip.m_width
  554. , mip.m_height
  555. , mip.m_depth
  556. , mip.m_width
  557. , mip.m_format
  558. );
  559. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  560. dstData = const_cast<uint8_t*>(dstMip.m_data);
  561. bimg::imageMakeDist(_allocator
  562. , dstData
  563. , mip.m_width
  564. , mip.m_height
  565. , mip.m_width
  566. , r8
  567. );
  568. if (_options.mips) {
  569. const float alphaRef = 0.5f;
  570. float coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::A8
  571. , mip.m_width
  572. , mip.m_height
  573. , mip.m_width
  574. , r8
  575. , alphaRef
  576. );
  577. size = bimg::imageGetSize(
  578. NULL
  579. , uint16_t(dstMip.m_width)
  580. , uint16_t(dstMip.m_height)
  581. , uint16_t(dstMip.m_depth)
  582. , false
  583. , false
  584. , 1
  585. , bimg::TextureFormat::RGBA8
  586. );
  587. void* rgbaTemp = BX_ALLOC(_allocator, size);
  588. uint8_t* rgba = (uint8_t*)rgbaTemp;
  589. bimg::imageDecodeToRgba8(
  590. _allocator
  591. , rgba
  592. , dstMip.m_data
  593. , dstMip.m_width
  594. , dstMip.m_height
  595. , dstMip.m_width * 4
  596. , bimg::TextureFormat::A8
  597. );
  598. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod) {
  599. bimg::imageRgba8Downsample2x2(rgba
  600. , dstMip.m_width
  601. , dstMip.m_height
  602. , dstMip.m_depth
  603. , dstMip.m_width * 4
  604. , bx::strideAlign(dstMip.m_width / 2, blockWidth) * 4
  605. , rgba
  606. );
  607. // For each mip, upscale to original size,
  608. // scale image alpha to get same coverage as mip0
  609. uint32_t upsample = 1 << lod;
  610. uint32_t destWidth = dstMip.m_width / 2;
  611. uint32_t destHeight = dstMip.m_height / 2;
  612. bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8
  613. , destWidth
  614. , destHeight
  615. , destWidth * 4
  616. , rgba
  617. , coverage
  618. , alphaRef
  619. , upsample
  620. );
  621. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  622. dstData = const_cast<uint8_t*>(dstMip.m_data);
  623. bimg::imageEncodeFromRgba8(
  624. _allocator
  625. , dstData
  626. , rgba
  627. , dstMip.m_width
  628. , dstMip.m_height
  629. , dstMip.m_depth
  630. , bimg::TextureFormat::A8
  631. , _options.quality
  632. , _err
  633. );
  634. }
  635. BX_FREE(_allocator, rgbaTemp);
  636. }
  637. }
  638. // RGBA8
  639. else
  640. {
  641. uint32_t size = bimg::imageGetSize(
  642. NULL
  643. , uint16_t(dstMip.m_width)
  644. , uint16_t(dstMip.m_height)
  645. , uint16_t(dstMip.m_depth)
  646. , false
  647. , false
  648. , 1
  649. , bimg::TextureFormat::RGBA8
  650. );
  651. temp = BX_ALLOC(_allocator, size);
  652. uint8_t* rgba = (uint8_t*)temp;
  653. bimg::imageDecodeToRgba8(
  654. _allocator
  655. , rgba
  656. , mip.m_data
  657. , mip.m_width
  658. , mip.m_height
  659. , mip.m_width*4
  660. , mip.m_format
  661. );
  662. float coverage = 0.0f;
  663. if (_options.alphaTest)
  664. {
  665. coverage = bimg::imageAlphaTestCoverage(bimg::TextureFormat::RGBA8
  666. , mip.m_width
  667. , mip.m_height
  668. , mip.m_width*4
  669. , rgba
  670. , _options.edge
  671. );
  672. }
  673. void* ref = NULL;
  674. if (_options.iqa)
  675. {
  676. ref = BX_ALLOC(_allocator, size);
  677. bx::memCopy(ref, rgba, size);
  678. }
  679. if (_options.pma)
  680. {
  681. imagePremultiplyAlpha(
  682. rgba
  683. , dstMip.m_width
  684. , dstMip.m_height
  685. , dstMip.m_depth
  686. , bimg::TextureFormat::RGBA8
  687. );
  688. }
  689. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  690. dstData = const_cast<uint8_t*>(dstMip.m_data);
  691. bimg::imageEncodeFromRgba8(
  692. _allocator
  693. , dstData
  694. , rgba
  695. , dstMip.m_width
  696. , dstMip.m_height
  697. , dstMip.m_depth
  698. , outputFormat
  699. , _options.quality
  700. , _err
  701. );
  702. for (uint8_t lod = 1; lod < numMips && _err->isOk(); ++lod)
  703. {
  704. bimg::imageRgba8Downsample2x2(rgba
  705. , dstMip.m_width
  706. , dstMip.m_height
  707. , dstMip.m_depth
  708. , dstMip.m_width*4
  709. , bx::strideAlign(dstMip.m_width/2, blockWidth)*4
  710. , rgba
  711. );
  712. if (_options.alphaTest)
  713. {
  714. bimg::imageScaleAlphaToCoverage(bimg::TextureFormat::RGBA8
  715. , dstMip.m_width
  716. , dstMip.m_height
  717. , dstMip.m_width*4
  718. , rgba
  719. , coverage
  720. , _options.edge
  721. );
  722. }
  723. if (_options.pma)
  724. {
  725. imagePremultiplyAlpha(
  726. rgba
  727. , dstMip.m_width
  728. , dstMip.m_height
  729. , dstMip.m_depth
  730. , bimg::TextureFormat::RGBA8
  731. );
  732. }
  733. bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  734. dstData = const_cast<uint8_t*>(dstMip.m_data);
  735. bimg::imageEncodeFromRgba8(
  736. _allocator
  737. , dstData
  738. , rgba
  739. , dstMip.m_width
  740. , dstMip.m_height
  741. , dstMip.m_depth
  742. , outputFormat
  743. , _options.quality
  744. , _err
  745. );
  746. }
  747. if (NULL != ref)
  748. {
  749. bimg::imageDecodeToRgba8(
  750. _allocator
  751. , rgba
  752. , output->m_data
  753. , mip.m_width
  754. , mip.m_height
  755. , mip.m_width*mip.m_bpp/8
  756. , outputFormat
  757. );
  758. float result = bimg::imageQualityRgba8(
  759. ref
  760. , rgba
  761. , uint16_t(mip.m_width)
  762. , uint16_t(mip.m_height)
  763. );
  764. bx::printf("%f\n", result);
  765. BX_FREE(_allocator, ref);
  766. }
  767. }
  768. BX_FREE(_allocator, temp);
  769. }
  770. }
  771. bimg::imageFree(input);
  772. }
  773. if (!_err->isOk()
  774. && NULL != output)
  775. {
  776. bimg::imageFree(output);
  777. output = NULL;
  778. }
  779. return output;
  780. }
  781. void help(const char* _error = NULL, bool _showHelp = true)
  782. {
  783. if (NULL != _error)
  784. {
  785. bx::printf("Error:\n%s\n\n", _error);
  786. if (!_showHelp)
  787. {
  788. return;
  789. }
  790. }
  791. bx::printf(
  792. "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  793. "Copyright 2011-2019 Branimir Karadzic. All rights reserved.\n"
  794. "License: https://github.com/bkaradzic/bimg#license-bsd-2-clause\n\n"
  795. , BIMG_TEXTUREC_VERSION_MAJOR
  796. , BIMG_TEXTUREC_VERSION_MINOR
  797. , BIMG_API_VERSION
  798. );
  799. bx::printf(
  800. "Usage: texturec -f <in> -o <out> [-t <texture format>]\n"
  801. "\n"
  802. "Supported file formats:\n"
  803. " *.bmp (input) Windows Bitmap.\n"
  804. " *.dds (input, output) Direct Draw Surface.\n"
  805. " *.exr (input, output) OpenEXR.\n"
  806. " *.gif (input) Graphics Interchange Format.\n"
  807. " *.jpg (input) JPEG Interchange Format.\n"
  808. " *.hdr (input, output) Radiance RGBE.\n"
  809. " *.ktx (input, output) Khronos Texture.\n"
  810. " *.png (input, output) Portable Network Graphics.\n"
  811. " *.psd (input) Photoshop Document.\n"
  812. " *.pvr (input) PowerVR.\n"
  813. " *.tga (input) Truevision TGA.\n"
  814. "\n"
  815. "Options:\n"
  816. " -h, --help Help.\n"
  817. " -v, --version Version information only.\n"
  818. " -f <file path> Input file path.\n"
  819. " -o <file path> Output file path.\n"
  820. " -t <format> Output format type (BC1/2/3/4/5, ETC1, PVR14, etc.).\n"
  821. " -q <quality> Encoding quality (default, fastest, highest).\n"
  822. " -m, --mips Generate mip-maps.\n"
  823. " --mipskip <N> Skip <N> number of mips.\n"
  824. " -n, --normalmap Input texture is normal map.\n"
  825. " --equirect Input texture is equirectangular projection of cubemap.\n"
  826. " --strip Input texture is horizontal strip of cubemap.\n"
  827. " --sdf Compute SDF texture.\n"
  828. " --ref <alpha> Alpha reference value.\n"
  829. " --iqa Image Quality Assessment\n"
  830. " --pma Premultiply alpha into RGB channel.\n"
  831. " --linear Input and output texture is linear color space (gamma correction won't be applied).\n"
  832. " --max <max size> Maximum width/height (image will be scaled down and\n"
  833. " aspect ratio will be preserved.\n"
  834. " --radiance <model> Radiance cubemap filter. (Lighting model: Phong, PhongBrdf, Blinn, BlinnBrdf, GGX)\n"
  835. " --as <extension> Save as.\n"
  836. " --formats List all supported formats.\n"
  837. " --validate *DEBUG* Validate that output image produced matches after loading.\n"
  838. "\n"
  839. "For additional information, see https://github.com/bkaradzic/bimg\n"
  840. );
  841. }
  842. void help(const char* _str, const bx::Error& _err)
  843. {
  844. std::string str;
  845. if (_str != NULL)
  846. {
  847. str.append(_str);
  848. str.append(" ");
  849. }
  850. const bx::StringView& sv = _err.getMessage();
  851. str.append(sv.getPtr(), sv.getTerm() - sv.getPtr() );
  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. fprintf(stderr
  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. const char* maxSize = cmdLine.findOption("max");
  959. if (NULL != maxSize)
  960. {
  961. if (!bx::fromString(&options.maxSize, maxSize) )
  962. {
  963. help("Parsing `--max` failed.");
  964. return bx::kExitFailure;
  965. }
  966. }
  967. const char* mipSkip = cmdLine.findOption("mipskip");
  968. if (NULL != mipSkip)
  969. {
  970. if (!bx::fromString(&options.mipSkip, mipSkip) )
  971. {
  972. help("Parsing `--mipskip` failed.");
  973. return bx::kExitFailure;
  974. }
  975. }
  976. options.format = bimg::TextureFormat::Count;
  977. const char* type = cmdLine.findOption('t');
  978. if (NULL != type)
  979. {
  980. options.format = bimg::getFormat(type);
  981. if (!bimg::isValid(options.format) )
  982. {
  983. help("Invalid format specified.");
  984. return bx::kExitFailure;
  985. }
  986. }
  987. if (!bx::strFindI(saveAs, "png").isEmpty() )
  988. {
  989. if (options.format == bimg::TextureFormat::Count)
  990. {
  991. options.format = bimg::TextureFormat::RGBA8;
  992. }
  993. else if (options.format != bimg::TextureFormat::RGBA8)
  994. {
  995. help("Output PNG format must be RGBA8.");
  996. return bx::kExitFailure;
  997. }
  998. }
  999. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1000. {
  1001. if (options.format == bimg::TextureFormat::Count)
  1002. {
  1003. options.format = bimg::TextureFormat::RGBA16F;
  1004. }
  1005. else if (options.format != bimg::TextureFormat::RGBA16F)
  1006. {
  1007. help("Output EXR format must be RGBA16F.");
  1008. return bx::kExitFailure;
  1009. }
  1010. }
  1011. const char* quality = cmdLine.findOption('q');
  1012. if (NULL != quality)
  1013. {
  1014. switch (bx::toLower(quality[0]) )
  1015. {
  1016. case 'h': options.quality = bimg::Quality::Highest; break;
  1017. case 'f': options.quality = bimg::Quality::Fastest; break;
  1018. case 'd': options.quality = bimg::Quality::Default; break;
  1019. default:
  1020. help("Invalid quality specified.");
  1021. return bx::kExitFailure;
  1022. }
  1023. }
  1024. const char* radiance = cmdLine.findOption("radiance");
  1025. if (NULL != radiance)
  1026. {
  1027. if (0 == bx::strCmpI(radiance, "phong" ) ) { options.radiance = bimg::LightingModel::Phong; }
  1028. else if (0 == bx::strCmpI(radiance, "phongbrdf") ) { options.radiance = bimg::LightingModel::PhongBrdf; }
  1029. else if (0 == bx::strCmpI(radiance, "blinn" ) ) { options.radiance = bimg::LightingModel::Blinn; }
  1030. else if (0 == bx::strCmpI(radiance, "blinnbrdf") ) { options.radiance = bimg::LightingModel::BlinnBrdf; }
  1031. else if (0 == bx::strCmpI(radiance, "ggx" ) ) { options.radiance = bimg::LightingModel::Ggx; }
  1032. else
  1033. {
  1034. help("Invalid radiance lighting model specified.");
  1035. return bx::kExitFailure;
  1036. }
  1037. }
  1038. const bool validate = cmdLine.hasArg("validate");
  1039. bx::Error err;
  1040. bx::FileReader reader;
  1041. if (!bx::open(&reader, inputFileName, &err) )
  1042. {
  1043. help("Failed to open input file.", err);
  1044. return bx::kExitFailure;
  1045. }
  1046. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  1047. if (0 == inputSize)
  1048. {
  1049. help("Failed to read input file.", err);
  1050. return bx::kExitFailure;
  1051. }
  1052. bx::DefaultAllocator defaultAllocator;
  1053. AlignedAllocator allocator(&defaultAllocator, 16);
  1054. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1055. bx::read(&reader, inputData, inputSize, &err);
  1056. bx::close(&reader);
  1057. if (!err.isOk() )
  1058. {
  1059. help("Failed to read input file.", err);
  1060. return bx::kExitFailure;
  1061. }
  1062. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
  1063. BX_FREE(&allocator, inputData);
  1064. if (NULL != output)
  1065. {
  1066. bx::FileWriter writer;
  1067. if (bx::open(&writer, outputFileName, false, &err) )
  1068. {
  1069. if (!bx::strFindI(saveAs, "ktx").isEmpty() )
  1070. {
  1071. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size, &err);
  1072. }
  1073. else if (!bx::strFindI(saveAs, "dds").isEmpty() )
  1074. {
  1075. bimg::imageWriteDds(&writer, *output, output->m_data, output->m_size, &err);
  1076. }
  1077. else if (!bx::strFindI(saveAs, "png").isEmpty() )
  1078. {
  1079. if (output->m_format != bimg::TextureFormat::RGBA8)
  1080. {
  1081. help("Incompatible output texture format. Output PNG format must be RGBA8.", err);
  1082. return bx::kExitFailure;
  1083. }
  1084. bimg::ImageMip mip;
  1085. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1086. bimg::imageWritePng(&writer
  1087. , mip.m_width
  1088. , mip.m_height
  1089. , mip.m_width*4
  1090. , mip.m_data
  1091. , output->m_format
  1092. , false
  1093. , &err
  1094. );
  1095. }
  1096. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1097. {
  1098. bimg::ImageMip mip;
  1099. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1100. bimg::imageWriteExr(&writer
  1101. , mip.m_width
  1102. , mip.m_height
  1103. , mip.m_width*8
  1104. , mip.m_data
  1105. , output->m_format
  1106. , false
  1107. , &err
  1108. );
  1109. }
  1110. else if (!bx::strFindI(saveAs, "hdr").isEmpty() )
  1111. {
  1112. bimg::ImageMip mip;
  1113. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1114. bimg::imageWriteHdr(&writer
  1115. , mip.m_width
  1116. , mip.m_height
  1117. , mip.m_width*getBitsPerPixel(mip.m_format)/8
  1118. , mip.m_data
  1119. , output->m_format
  1120. , false
  1121. , &err
  1122. );
  1123. }
  1124. bx::close(&writer);
  1125. if (!err.isOk() )
  1126. {
  1127. help(NULL, err);
  1128. return bx::kExitFailure;
  1129. }
  1130. }
  1131. else
  1132. {
  1133. help("Failed to open output file.", err);
  1134. return bx::kExitFailure;
  1135. }
  1136. if (validate)
  1137. {
  1138. if (!bx::open(&reader, outputFileName, &err) )
  1139. {
  1140. help("Failed to validate file.", err);
  1141. return bx::kExitFailure;
  1142. }
  1143. inputSize = (uint32_t)bx::getSize(&reader);
  1144. if (0 == inputSize)
  1145. {
  1146. help("Failed to validate file.", err);
  1147. return bx::kExitFailure;
  1148. }
  1149. inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1150. bx::read(&reader, inputData, inputSize, &err);
  1151. bx::close(&reader);
  1152. bimg::ImageContainer* input = bimg::imageParse(&allocator, inputData, inputSize, bimg::TextureFormat::Count, &err);
  1153. if (!err.isOk() )
  1154. {
  1155. help("Failed to validate file.", err);
  1156. return bx::kExitFailure;
  1157. }
  1158. if (false
  1159. || input->m_format != output->m_format
  1160. || input->m_size != output->m_size
  1161. || input->m_width != output->m_width
  1162. || input->m_height != output->m_height
  1163. || input->m_depth != output->m_depth
  1164. || input->m_numLayers != output->m_numLayers
  1165. || input->m_numMips != output->m_numMips
  1166. || input->m_hasAlpha != output->m_hasAlpha
  1167. || input->m_cubeMap != output->m_cubeMap
  1168. )
  1169. {
  1170. help("Validation failed, image headers are different.");
  1171. return bx::kExitFailure;
  1172. }
  1173. {
  1174. const uint8_t numMips = output->m_numMips;
  1175. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  1176. for (uint8_t lod = 0; lod < numMips; ++lod)
  1177. {
  1178. for (uint16_t side = 0; side < numSides; ++side)
  1179. {
  1180. bimg::ImageMip srcMip;
  1181. bool hasSrc = bimg::imageGetRawData(*input, side, lod, input->m_data, input->m_size, srcMip);
  1182. bimg::ImageMip dstMip;
  1183. bool hasDst = bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  1184. if (false
  1185. || hasSrc != hasDst
  1186. || srcMip.m_size != dstMip.m_size
  1187. )
  1188. {
  1189. help("Validation failed, image mip/layer/side are different.");
  1190. return bx::kExitFailure;
  1191. }
  1192. if (0 != bx::memCmp(srcMip.m_data, dstMip.m_data, srcMip.m_size) )
  1193. {
  1194. help("Validation failed, image content are different.");
  1195. return bx::kExitFailure;
  1196. }
  1197. }
  1198. }
  1199. }
  1200. BX_FREE(&allocator, inputData);
  1201. }
  1202. bimg::imageFree(output);
  1203. }
  1204. else
  1205. {
  1206. help("Failed to create output", err);
  1207. return bx::kExitFailure;
  1208. }
  1209. return bx::kExitSuccess;
  1210. }