texturec.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. /*
  2. * Copyright 2011-2019 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause
  4. */
  5. #include <stdio.h>
  6. #include <bx/allocator.h>
  7. #include <bx/readerwriter.h>
  8. #include <bx/endian.h>
  9. #include <bx/math.h>
  10. #include <bimg/decode.h>
  11. #include <bimg/encode.h>
  12. #if 0
  13. # define DBG(_format, ...) fprintf(stderr, "" _format "\n", ##__VA_ARGS__)
  14. #else
  15. # define DBG(...) BX_NOOP()
  16. #endif // DEBUG
  17. #include <bx/bx.h>
  18. #include <bx/commandline.h>
  19. #include <bx/file.h>
  20. #include <string>
  21. #define BIMG_TEXTUREC_VERSION_MAJOR 1
  22. #define BIMG_TEXTUREC_VERSION_MINOR 18
  23. BX_ERROR_RESULT(TEXTRUREC_ERROR, BX_MAKEFOURCC('t', 'c', 0, 0) );
  24. struct Options
  25. {
  26. void dump()
  27. {
  28. DBG("Options:\n"
  29. "\t maxSize: %d\n"
  30. "\t mipSkip: %d\n"
  31. "\t edge: %f\n"
  32. "\t format: %s\n"
  33. "\t mips: %s\n"
  34. "\tnormalMap: %s\n"
  35. "\t iqa: %s\n"
  36. "\t pma: %s\n"
  37. "\t sdf: %s\n"
  38. "\t radiance: %s\n"
  39. "\t equirect: %s\n"
  40. "\t strip: %s\n"
  41. "\t linear: %s\n"
  42. , maxSize
  43. , mipSkip
  44. , edge
  45. , bimg::getName(format)
  46. , mips ? "true" : "false"
  47. , normalMap ? "true" : "false"
  48. , iqa ? "true" : "false"
  49. , pma ? "true" : "false"
  50. , sdf ? "true" : "false"
  51. , radiance ? "true" : "false"
  52. , equirect ? "true" : "false"
  53. , strip ? "true" : "false"
  54. , linear ? "true" : "false"
  55. );
  56. }
  57. uint32_t maxSize = UINT32_MAX;
  58. uint32_t mipSkip = 0;
  59. float edge = 0.0f;
  60. bimg::TextureFormat::Enum format = bimg::TextureFormat::Count;
  61. bimg::Quality::Enum quality = bimg::Quality::Default;
  62. bimg::LightingModel::Enum radiance = bimg::LightingModel::Count;
  63. bool mips = false;
  64. bool normalMap = false;
  65. bool equirect = false;
  66. bool strip = false;
  67. bool iqa = false;
  68. bool pma = false;
  69. bool sdf = false;
  70. bool alphaTest = false;
  71. bool linear = false;
  72. };
  73. void imageRgba32fNormalize(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  74. {
  75. const uint8_t* src = (const uint8_t*)_src;
  76. uint8_t* dst = (uint8_t*)_dst;
  77. for (uint32_t yy = 0, ystep = _srcPitch; yy < _height; ++yy, src += ystep)
  78. {
  79. const float* rgba = (const float*)&src[0];
  80. for (uint32_t xx = 0; xx < _width; ++xx, rgba += 4, dst += 16)
  81. {
  82. const bx::Vec3 xyz = bx::load<bx::Vec3>(rgba);
  83. bx::store(dst, bx::normalize(xyz) );
  84. }
  85. }
  86. }
  87. void imagePremultiplyAlpha(void* _inOut, uint32_t _width, uint32_t _height, uint32_t _depth, bimg::TextureFormat::Enum _format)
  88. {
  89. uint8_t* inOut = (uint8_t*)_inOut;
  90. uint32_t bpp = bimg::getBitsPerPixel(_format);
  91. uint32_t pitch = _width*bpp/8;
  92. bimg::PackFn pack = bimg::getPack(_format);
  93. bimg::UnpackFn unpack = bimg::getUnpack(_format);
  94. for (uint32_t zz = 0; zz < _depth; ++zz)
  95. {
  96. for (uint32_t yy = 0; yy < _height; ++yy)
  97. {
  98. for (uint32_t xx = 0; xx < _width; ++xx)
  99. {
  100. const uint32_t offset = yy*pitch + xx*bpp/8;
  101. float rgba[4];
  102. unpack(rgba, &inOut[offset]);
  103. const float alpha = rgba[3];
  104. rgba[0] = bx::toGamma(bx::toLinear(rgba[0]) * alpha);
  105. rgba[1] = bx::toGamma(bx::toLinear(rgba[1]) * alpha);
  106. rgba[2] = bx::toGamma(bx::toLinear(rgba[2]) * alpha);
  107. pack(&inOut[offset], rgba);
  108. }
  109. }
  110. }
  111. }
  112. bimg::ImageContainer* convert(bx::AllocatorI* _allocator, const void* _inputData, uint32_t _inputSize, const Options& _options, bx::Error* _err)
  113. {
  114. BX_ERROR_SCOPE(_err);
  115. const uint8_t* inputData = (uint8_t*)_inputData;
  116. bimg::ImageContainer* output = NULL;
  117. bimg::ImageContainer* input = bimg::imageParse(_allocator, inputData, _inputSize, bimg::TextureFormat::Count, _err);
  118. if (!_err->isOk() )
  119. {
  120. return NULL;
  121. }
  122. if (NULL != input)
  123. {
  124. bimg::TextureFormat::Enum inputFormat = input->m_format;
  125. bimg::TextureFormat::Enum outputFormat = input->m_format;
  126. if (bimg::TextureFormat::Count != _options.format)
  127. {
  128. outputFormat = _options.format;
  129. }
  130. if (_options.sdf)
  131. {
  132. outputFormat = bimg::TextureFormat::R8;
  133. }
  134. const bimg::ImageBlockInfo& inputBlockInfo = bimg::getBlockInfo(inputFormat);
  135. const bimg::ImageBlockInfo& outputBlockInfo = bimg::getBlockInfo(outputFormat);
  136. const uint32_t blockWidth = outputBlockInfo.blockWidth;
  137. const uint32_t blockHeight = outputBlockInfo.blockHeight;
  138. const uint32_t minBlockX = outputBlockInfo.minBlockX;
  139. const uint32_t minBlockY = outputBlockInfo.minBlockY;
  140. uint32_t outputWidth = bx::max(blockWidth * minBlockX, ( (input->m_width + blockWidth - 1) / blockWidth )*blockWidth);
  141. uint32_t outputHeight = bx::max(blockHeight * minBlockY, ( (input->m_height + blockHeight - 1) / blockHeight)*blockHeight);
  142. uint32_t outputDepth = input->m_depth;
  143. if (_options.mips
  144. && _options.mipSkip != 0)
  145. {
  146. for (uint32_t ii = 0; ii < _options.mipSkip; ++ii)
  147. {
  148. outputWidth = bx::max(blockWidth * minBlockX, ( ( (outputWidth>>1) + blockWidth - 1) / blockWidth )*blockWidth);
  149. outputHeight = bx::max(blockHeight * minBlockY, ( ( (outputHeight>>1) + blockHeight - 1) / blockHeight)*blockHeight);
  150. outputDepth = bx::max(outputDepth>>1, 1u);
  151. }
  152. }
  153. if (_options.equirect)
  154. {
  155. if (outputDepth == 1
  156. && outputWidth/2 == outputHeight)
  157. {
  158. if (outputWidth/2 > _options.maxSize)
  159. {
  160. outputWidth = _options.maxSize*4;
  161. outputHeight = _options.maxSize*2;
  162. }
  163. }
  164. else
  165. {
  166. bimg::imageFree(input);
  167. BX_ERROR_SET(_err, TEXTRUREC_ERROR, "Input image format is not equirectangular projection.");
  168. return NULL;
  169. }
  170. }
  171. else if (_options.strip)
  172. {
  173. if (outputDepth == 1
  174. && outputWidth/6 == outputHeight)
  175. {
  176. if (outputWidth/6 > _options.maxSize)
  177. {
  178. outputWidth = _options.maxSize*6;
  179. outputHeight = _options.maxSize;
  180. }
  181. }
  182. else
  183. {
  184. bimg::imageFree(input);
  185. BX_ERROR_SET(_err, TEXTRUREC_ERROR, "Input image format is not horizontal strip.");
  186. return NULL;
  187. }
  188. }
  189. else if (outputWidth > _options.maxSize
  190. || outputHeight > _options.maxSize
  191. || outputDepth > _options.maxSize)
  192. {
  193. if (outputDepth > outputWidth
  194. && outputDepth > outputHeight)
  195. {
  196. outputWidth = outputWidth * _options.maxSize / outputDepth;
  197. outputHeight = outputHeight * _options.maxSize / outputDepth;
  198. outputDepth = _options.maxSize;
  199. }
  200. else if (outputWidth > outputHeight)
  201. {
  202. outputDepth = outputDepth * _options.maxSize / outputWidth;
  203. outputHeight = outputHeight * _options.maxSize / outputWidth;
  204. outputWidth = _options.maxSize;
  205. }
  206. else
  207. {
  208. outputDepth = outputDepth * _options.maxSize / outputHeight;
  209. outputWidth = outputWidth * _options.maxSize / outputHeight;
  210. outputHeight = _options.maxSize;
  211. }
  212. }
  213. const bool needResize = false
  214. || input->m_width != outputWidth
  215. || input->m_height != outputHeight
  216. ;
  217. const bool passThru = true
  218. && !needResize
  219. && (1 < input->m_numMips) == _options.mips
  220. && !_options.sdf
  221. && !_options.alphaTest
  222. && !_options.normalMap
  223. && !(_options.equirect || _options.strip)
  224. && !_options.iqa
  225. && !_options.pma
  226. && (bimg::LightingModel::Count == _options.radiance)
  227. ;
  228. if (!_options.sdf
  229. && needResize)
  230. {
  231. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input, false);
  232. bimg::ImageContainer* dst = bimg::imageAlloc(
  233. _allocator
  234. , bimg::TextureFormat::RGBA32F
  235. , uint16_t(outputWidth)
  236. , uint16_t(outputHeight)
  237. , uint16_t(outputDepth)
  238. , input->m_numLayers
  239. , input->m_cubeMap
  240. , false
  241. );
  242. if (!_options.linear)
  243. {
  244. bimg::imageRgba32fToLinear(src);
  245. }
  246. bimg::imageResizeRgba32fLinear(dst, src);
  247. if (!_options.linear)
  248. {
  249. bimg::imageRgba32fToGamma(dst);
  250. }
  251. bimg::imageFree(src);
  252. bimg::imageFree(input);
  253. if (bimg::isCompressed(inputFormat) )
  254. {
  255. if (inputFormat == bimg::TextureFormat::BC6H)
  256. {
  257. inputFormat = bimg::TextureFormat::RGBA32F;
  258. }
  259. else
  260. {
  261. inputFormat = bimg::TextureFormat::RGBA8;
  262. }
  263. }
  264. input = bimg::imageConvert(_allocator, inputFormat, *dst);
  265. bimg::imageFree(dst);
  266. }
  267. if (passThru)
  268. {
  269. if (inputFormat != outputFormat
  270. && bimg::isCompressed(outputFormat) )
  271. {
  272. output = bimg::imageEncode(_allocator, outputFormat, _options.quality, *input);
  273. }
  274. else
  275. {
  276. output = bimg::imageConvert(_allocator, outputFormat, *input);
  277. }
  278. bimg::imageFree(input);
  279. return output;
  280. }
  281. if (_options.equirect
  282. || _options.strip)
  283. {
  284. bimg::ImageContainer* src = bimg::imageConvert(_allocator, bimg::TextureFormat::RGBA32F, *input);
  285. bimg::imageFree(input);
  286. bimg::ImageContainer* dst;
  287. if (outputWidth/2 == outputHeight)
  288. {
  289. dst = bimg::imageCubemapFromLatLongRgba32F(_allocator, *src, true, _err);
  290. bimg::imageFree(src);
  291. }
  292. else
  293. {
  294. dst = bimg::imageCubemapFromStripRgba32F(_allocator, *src, _err);
  295. bimg::imageFree(src);
  296. }
  297. if (!_err->isOk() )
  298. {
  299. return NULL;
  300. }
  301. input = bimg::imageConvert(_allocator, inputFormat, *dst);
  302. bimg::imageFree(dst);
  303. }
  304. if (bimg::LightingModel::Count != _options.radiance)
  305. {
  306. output = bimg::imageCubemapRadianceFilter(_allocator, *input, _options.radiance, _err);
  307. if (!_err->isOk() )
  308. {
  309. return NULL;
  310. }
  311. if (bimg::TextureFormat::RGBA32F != outputFormat)
  312. {
  313. bimg::ImageContainer* temp = bimg::imageEncode(_allocator, outputFormat, _options.quality, *output);
  314. bimg::imageFree(output);
  315. output = temp;
  316. }
  317. bimg::imageFree(input);
  318. return output;
  319. }
  320. output = bimg::imageAlloc(
  321. _allocator
  322. , outputFormat
  323. , uint16_t(input->m_width)
  324. , uint16_t(input->m_height)
  325. , uint16_t(input->m_depth)
  326. , input->m_numLayers
  327. , input->m_cubeMap
  328. , _options.mips
  329. );
  330. const uint8_t numMips = output->m_numMips;
  331. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  332. for (uint16_t side = 0; side < numSides && _err->isOk(); ++side)
  333. {
  334. bimg::ImageMip mip;
  335. if (bimg::imageGetRawData(*input, side, 0, input->m_data, input->m_size, mip) )
  336. {
  337. bimg::ImageMip dstMip;
  338. bimg::imageGetRawData(*output, side, 0, output->m_data, output->m_size, dstMip);
  339. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  340. void* temp = NULL;
  341. // Normal map.
  342. if (_options.normalMap)
  343. {
  344. uint32_t size = bimg::imageGetSize(
  345. NULL
  346. , uint16_t(dstMip.m_width)
  347. , uint16_t(dstMip.m_height)
  348. , 0
  349. , false
  350. , false
  351. , 1
  352. , bimg::TextureFormat::RGBA32F
  353. );
  354. temp = BX_ALLOC(_allocator, size);
  355. float* rgba = (float*)temp;
  356. float* rgbaDst = (float*)BX_ALLOC(_allocator, size);
  357. bimg::imageDecodeToRgba32f(_allocator
  358. , rgba
  359. , mip.m_data
  360. , dstMip.m_width
  361. , dstMip.m_height
  362. , dstMip.m_depth
  363. , dstMip.m_width*16
  364. , mip.m_format
  365. );
  366. if (bimg::TextureFormat::BC5 != mip.m_format)
  367. {
  368. for (uint32_t yy = 0; yy < mip.m_height; ++yy)
  369. {
  370. for (uint32_t xx = 0; xx < mip.m_width; ++xx)
  371. {
  372. const uint32_t offset = (yy*mip.m_width + xx) * 4;
  373. float* inout = &rgba[offset];
  374. inout[0] = inout[0] * 2.0f - 1.0f;
  375. inout[1] = inout[1] * 2.0f - 1.0f;
  376. inout[2] = inout[2] * 2.0f - 1.0f;
  377. inout[3] = inout[3] * 2.0f - 1.0f;
  378. }
  379. }
  380. }
  381. imageRgba32fNormalize(rgba
  382. , dstMip.m_width
  383. , dstMip.m_height
  384. , dstMip.m_width*16
  385. , rgba
  386. );
  387. bimg::imageRgba32f11to01(rgbaDst
  388. , dstMip.m_width
  389. , dstMip.m_height
  390. , dstMip.m_depth
  391. , dstMip.m_width*16
  392. , rgba
  393. );
  394. bimg::imageEncodeFromRgba32f(_allocator
  395. , dstData
  396. , rgbaDst
  397. , dstMip.m_width
  398. , dstMip.m_height
  399. , dstMip.m_depth
  400. , outputFormat
  401. , _options.quality
  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. , _options.quality
  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. 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. fprintf(stderr, "Error:\n%s\n\n", _error);
  786. if (!_showHelp)
  787. {
  788. return;
  789. }
  790. }
  791. fprintf(stderr
  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. fprintf(stderr
  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) Targa.\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. printf("Uncompressed formats:\n");
  896. for (int format = bimg::TextureFormat::Unknown + 1; format < bimg::TextureFormat::UnknownDepth; format++)
  897. printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  898. for (int format = bimg::TextureFormat::UnknownDepth + 1; format < bimg::TextureFormat::Count; format++)
  899. printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  900. printf("Compressed formats:\n");
  901. for (int format = 0; format < bimg::TextureFormat::Unknown; format++)
  902. printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  903. return bx::kExitSuccess;
  904. }
  905. const char* inputFileName = cmdLine.findOption('f');
  906. if (NULL == inputFileName)
  907. {
  908. help("Input file must be specified.");
  909. return bx::kExitFailure;
  910. }
  911. const char* outputFileName = cmdLine.findOption('o');
  912. if (NULL == outputFileName)
  913. {
  914. help("Output file must be specified.");
  915. return bx::kExitFailure;
  916. }
  917. bx::StringView saveAs = cmdLine.findOption("as");
  918. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".ktx") : saveAs;
  919. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".dds") : saveAs;
  920. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".png") : saveAs;
  921. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".exr") : saveAs;
  922. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".hdr") : saveAs;
  923. if (saveAs.isEmpty() )
  924. {
  925. help("Output file format must be specified.");
  926. return bx::kExitFailure;
  927. }
  928. Options options;
  929. const char* alphaRef = cmdLine.findOption("ref");
  930. if (NULL != alphaRef)
  931. {
  932. options.alphaTest = true;
  933. if (!bx::fromString(&options.edge, alphaRef))
  934. {
  935. options.edge = 0.5f;
  936. }
  937. }
  938. options.sdf = cmdLine.hasArg("sdf");
  939. options.mips = cmdLine.hasArg('m', "mips");
  940. options.normalMap = cmdLine.hasArg('n', "normalmap");
  941. options.equirect = cmdLine.hasArg("equirect");
  942. options.strip = cmdLine.hasArg("strip");
  943. options.iqa = cmdLine.hasArg("iqa");
  944. options.pma = cmdLine.hasArg("pma");
  945. options.linear = cmdLine.hasArg("linear");
  946. if (options.equirect
  947. && options.strip)
  948. {
  949. help("Image can't be equirect and strip at the same time.");
  950. return bx::kExitFailure;
  951. }
  952. const char* maxSize = cmdLine.findOption("max");
  953. if (NULL != maxSize)
  954. {
  955. if (!bx::fromString(&options.maxSize, maxSize) )
  956. {
  957. help("Parsing `--max` failed.");
  958. return bx::kExitFailure;
  959. }
  960. }
  961. const char* mipSkip = cmdLine.findOption("mipskip");
  962. if (NULL != mipSkip)
  963. {
  964. if (!bx::fromString(&options.mipSkip, mipSkip) )
  965. {
  966. help("Parsing `--mipskip` failed.");
  967. return bx::kExitFailure;
  968. }
  969. }
  970. options.format = bimg::TextureFormat::Count;
  971. const char* type = cmdLine.findOption('t');
  972. if (NULL != type)
  973. {
  974. options.format = bimg::getFormat(type);
  975. if (!bimg::isValid(options.format) )
  976. {
  977. help("Invalid format specified.");
  978. return bx::kExitFailure;
  979. }
  980. }
  981. if (!bx::strFindI(saveAs, "png").isEmpty() )
  982. {
  983. if (options.format == bimg::TextureFormat::Count)
  984. {
  985. options.format = bimg::TextureFormat::RGBA8;
  986. }
  987. else if (options.format != bimg::TextureFormat::RGBA8)
  988. {
  989. help("Output PNG format must be RGBA8.");
  990. return bx::kExitFailure;
  991. }
  992. }
  993. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  994. {
  995. if (options.format == bimg::TextureFormat::Count)
  996. {
  997. options.format = bimg::TextureFormat::RGBA16F;
  998. }
  999. else if (options.format != bimg::TextureFormat::RGBA16F)
  1000. {
  1001. help("Output EXR format must be RGBA16F.");
  1002. return bx::kExitFailure;
  1003. }
  1004. }
  1005. const char* quality = cmdLine.findOption('q');
  1006. if (NULL != quality)
  1007. {
  1008. switch (bx::toLower(quality[0]) )
  1009. {
  1010. case 'h': options.quality = bimg::Quality::Highest; break;
  1011. case 'f': options.quality = bimg::Quality::Fastest; break;
  1012. case 'd': options.quality = bimg::Quality::Default; break;
  1013. default:
  1014. help("Invalid quality specified.");
  1015. return bx::kExitFailure;
  1016. }
  1017. }
  1018. const char* radiance = cmdLine.findOption("radiance");
  1019. if (NULL != radiance)
  1020. {
  1021. if (0 == bx::strCmpI(radiance, "phong" ) ) { options.radiance = bimg::LightingModel::Phong; }
  1022. else if (0 == bx::strCmpI(radiance, "phongbrdf") ) { options.radiance = bimg::LightingModel::PhongBrdf; }
  1023. else if (0 == bx::strCmpI(radiance, "blinn" ) ) { options.radiance = bimg::LightingModel::Blinn; }
  1024. else if (0 == bx::strCmpI(radiance, "blinnbrdf") ) { options.radiance = bimg::LightingModel::BlinnBrdf; }
  1025. else if (0 == bx::strCmpI(radiance, "ggx" ) ) { options.radiance = bimg::LightingModel::Ggx; }
  1026. else
  1027. {
  1028. help("Invalid radiance lighting model specified.");
  1029. return bx::kExitFailure;
  1030. }
  1031. }
  1032. const bool validate = cmdLine.hasArg("validate");
  1033. bx::Error err;
  1034. bx::FileReader reader;
  1035. if (!bx::open(&reader, inputFileName, &err) )
  1036. {
  1037. help("Failed to open input file.", err);
  1038. return bx::kExitFailure;
  1039. }
  1040. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  1041. if (0 == inputSize)
  1042. {
  1043. help("Failed to read input file.", err);
  1044. return bx::kExitFailure;
  1045. }
  1046. bx::DefaultAllocator defaultAllocator;
  1047. AlignedAllocator allocator(&defaultAllocator, 16);
  1048. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1049. bx::read(&reader, inputData, inputSize, &err);
  1050. bx::close(&reader);
  1051. if (!err.isOk() )
  1052. {
  1053. help("Failed to read input file.", err);
  1054. return bx::kExitFailure;
  1055. }
  1056. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
  1057. BX_FREE(&allocator, inputData);
  1058. if (NULL != output)
  1059. {
  1060. bx::FileWriter writer;
  1061. if (bx::open(&writer, outputFileName, false, &err) )
  1062. {
  1063. if (!bx::strFindI(saveAs, "ktx").isEmpty() )
  1064. {
  1065. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size, &err);
  1066. }
  1067. else if (!bx::strFindI(saveAs, "dds").isEmpty() )
  1068. {
  1069. bimg::imageWriteDds(&writer, *output, output->m_data, output->m_size, &err);
  1070. }
  1071. else if (!bx::strFindI(saveAs, "png").isEmpty() )
  1072. {
  1073. if (output->m_format != bimg::TextureFormat::RGBA8)
  1074. {
  1075. help("Incompatible output texture format. Output PNG format must be RGBA8.", err);
  1076. return bx::kExitFailure;
  1077. }
  1078. bimg::ImageMip mip;
  1079. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1080. bimg::imageWritePng(&writer
  1081. , mip.m_width
  1082. , mip.m_height
  1083. , mip.m_width*4
  1084. , mip.m_data
  1085. , output->m_format
  1086. , false
  1087. , &err
  1088. );
  1089. }
  1090. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1091. {
  1092. bimg::ImageMip mip;
  1093. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1094. bimg::imageWriteExr(&writer
  1095. , mip.m_width
  1096. , mip.m_height
  1097. , mip.m_width*8
  1098. , mip.m_data
  1099. , output->m_format
  1100. , false
  1101. , &err
  1102. );
  1103. }
  1104. else if (!bx::strFindI(saveAs, "hdr").isEmpty() )
  1105. {
  1106. bimg::ImageMip mip;
  1107. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1108. bimg::imageWriteHdr(&writer
  1109. , mip.m_width
  1110. , mip.m_height
  1111. , mip.m_width*getBitsPerPixel(mip.m_format)/8
  1112. , mip.m_data
  1113. , output->m_format
  1114. , false
  1115. , &err
  1116. );
  1117. }
  1118. bx::close(&writer);
  1119. if (!err.isOk() )
  1120. {
  1121. help(NULL, err);
  1122. return bx::kExitFailure;
  1123. }
  1124. }
  1125. else
  1126. {
  1127. help("Failed to open output file.", err);
  1128. return bx::kExitFailure;
  1129. }
  1130. if (validate)
  1131. {
  1132. if (!bx::open(&reader, outputFileName, &err) )
  1133. {
  1134. help("Failed to validate file.", err);
  1135. return bx::kExitFailure;
  1136. }
  1137. inputSize = (uint32_t)bx::getSize(&reader);
  1138. if (0 == inputSize)
  1139. {
  1140. help("Failed to validate file.", err);
  1141. return bx::kExitFailure;
  1142. }
  1143. inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1144. bx::read(&reader, inputData, inputSize, &err);
  1145. bx::close(&reader);
  1146. bimg::ImageContainer* input = bimg::imageParse(&allocator, inputData, inputSize, bimg::TextureFormat::Count, &err);
  1147. if (!err.isOk() )
  1148. {
  1149. help("Failed to validate file.", err);
  1150. return bx::kExitFailure;
  1151. }
  1152. if (false
  1153. || input->m_format != output->m_format
  1154. || input->m_size != output->m_size
  1155. || input->m_width != output->m_width
  1156. || input->m_height != output->m_height
  1157. || input->m_depth != output->m_depth
  1158. || input->m_numLayers != output->m_numLayers
  1159. || input->m_numMips != output->m_numMips
  1160. || input->m_hasAlpha != output->m_hasAlpha
  1161. || input->m_cubeMap != output->m_cubeMap
  1162. )
  1163. {
  1164. help("Validation failed, image headers are different.");
  1165. return bx::kExitFailure;
  1166. }
  1167. {
  1168. const uint8_t numMips = output->m_numMips;
  1169. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  1170. for (uint8_t lod = 0; lod < numMips; ++lod)
  1171. {
  1172. for (uint16_t side = 0; side < numSides; ++side)
  1173. {
  1174. bimg::ImageMip srcMip;
  1175. bool hasSrc = bimg::imageGetRawData(*input, side, lod, input->m_data, input->m_size, srcMip);
  1176. bimg::ImageMip dstMip;
  1177. bool hasDst = bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  1178. if (false
  1179. || hasSrc != hasDst
  1180. || srcMip.m_size != dstMip.m_size
  1181. )
  1182. {
  1183. help("Validation failed, image mip/layer/side are different.");
  1184. return bx::kExitFailure;
  1185. }
  1186. if (0 != bx::memCmp(srcMip.m_data, dstMip.m_data, srcMip.m_size) )
  1187. {
  1188. help("Validation failed, image content are different.");
  1189. return bx::kExitFailure;
  1190. }
  1191. }
  1192. }
  1193. }
  1194. BX_FREE(&allocator, inputData);
  1195. }
  1196. bimg::imageFree(output);
  1197. }
  1198. else
  1199. {
  1200. help(NULL, err);
  1201. return bx::kExitFailure;
  1202. }
  1203. return bx::kExitSuccess;
  1204. }