texturec.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378
  1. /*
  2. * Copyright 2011-2020 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, ...) 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/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-2020 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 bx::StringView _str, const bx::Error& _err)
  843. {
  844. std::string str;
  845. if (!_str.isEmpty() )
  846. {
  847. str.append(_str.getPtr(), _str.getTerm() - _str.getPtr() );
  848. str.append(": ");
  849. }
  850. const bx::StringView& sv = _err.getMessage();
  851. str.append("'");
  852. str.append(sv.getPtr(), sv.getTerm() - sv.getPtr() );
  853. str.append("'");
  854. help(str.c_str(), false);
  855. }
  856. class AlignedAllocator : public bx::AllocatorI
  857. {
  858. public:
  859. AlignedAllocator(bx::AllocatorI* _allocator, size_t _minAlignment)
  860. : m_allocator(_allocator)
  861. , m_minAlignment(_minAlignment)
  862. {
  863. }
  864. virtual void* realloc(
  865. void* _ptr
  866. , size_t _size
  867. , size_t _align
  868. , const char* _file
  869. , uint32_t _line
  870. )
  871. {
  872. return m_allocator->realloc(_ptr, _size, bx::max(_align, m_minAlignment), _file, _line);
  873. }
  874. bx::AllocatorI* m_allocator;
  875. size_t m_minAlignment;
  876. };
  877. int main(int _argc, const char* _argv[])
  878. {
  879. bx::CommandLine cmdLine(_argc, _argv);
  880. if (cmdLine.hasArg('v', "version") )
  881. {
  882. bx::printf(
  883. "texturec, bgfx texture compiler tool, version %d.%d.%d.\n"
  884. , BIMG_TEXTUREC_VERSION_MAJOR
  885. , BIMG_TEXTUREC_VERSION_MINOR
  886. , BIMG_API_VERSION
  887. );
  888. return bx::kExitSuccess;
  889. }
  890. if (cmdLine.hasArg('h', "help") )
  891. {
  892. help();
  893. return bx::kExitFailure;
  894. }
  895. if (cmdLine.hasArg("formats"))
  896. {
  897. bx::printf("Uncompressed formats:\n");
  898. for (int format = bimg::TextureFormat::Unknown + 1; format < bimg::TextureFormat::UnknownDepth; format++)
  899. {
  900. bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  901. }
  902. for (int format = bimg::TextureFormat::UnknownDepth + 1; format < bimg::TextureFormat::Count; format++)
  903. {
  904. bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  905. }
  906. bx::printf("Compressed formats:\n");
  907. for (int format = 0; format < bimg::TextureFormat::Unknown; format++)
  908. {
  909. bx::printf(" %s\n", bimg::getName((bimg::TextureFormat::Enum) format));
  910. }
  911. return bx::kExitSuccess;
  912. }
  913. const char* inputFileName = cmdLine.findOption('f');
  914. if (NULL == inputFileName)
  915. {
  916. help("Input file must be specified.");
  917. return bx::kExitFailure;
  918. }
  919. const char* outputFileName = cmdLine.findOption('o');
  920. if (NULL == outputFileName)
  921. {
  922. help("Output file must be specified.");
  923. return bx::kExitFailure;
  924. }
  925. bx::StringView saveAs = cmdLine.findOption("as");
  926. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".ktx") : saveAs;
  927. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".dds") : saveAs;
  928. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".png") : saveAs;
  929. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".exr") : saveAs;
  930. saveAs = saveAs.isEmpty() ? bx::strFindI(outputFileName, ".hdr") : saveAs;
  931. if (saveAs.isEmpty() )
  932. {
  933. help("Output file format must be specified.");
  934. return bx::kExitFailure;
  935. }
  936. Options options;
  937. const char* alphaRef = cmdLine.findOption("ref");
  938. if (NULL != alphaRef)
  939. {
  940. options.alphaTest = true;
  941. if (!bx::fromString(&options.edge, alphaRef))
  942. {
  943. options.edge = 0.5f;
  944. }
  945. }
  946. options.sdf = cmdLine.hasArg("sdf");
  947. options.mips = cmdLine.hasArg('m', "mips");
  948. options.normalMap = cmdLine.hasArg('n', "normalmap");
  949. options.equirect = cmdLine.hasArg("equirect");
  950. options.strip = cmdLine.hasArg("strip");
  951. options.iqa = cmdLine.hasArg("iqa");
  952. options.pma = cmdLine.hasArg("pma");
  953. options.linear = cmdLine.hasArg("linear");
  954. if (options.equirect
  955. && options.strip)
  956. {
  957. help("Image can't be equirect and strip at the same time.");
  958. return bx::kExitFailure;
  959. }
  960. const char* maxSize = cmdLine.findOption("max");
  961. if (NULL != maxSize)
  962. {
  963. if (!bx::fromString(&options.maxSize, maxSize) )
  964. {
  965. help("Parsing `--max` failed.");
  966. return bx::kExitFailure;
  967. }
  968. }
  969. const char* mipSkip = cmdLine.findOption("mipskip");
  970. if (NULL != mipSkip)
  971. {
  972. if (!bx::fromString(&options.mipSkip, mipSkip) )
  973. {
  974. help("Parsing `--mipskip` failed.");
  975. return bx::kExitFailure;
  976. }
  977. }
  978. options.format = bimg::TextureFormat::Count;
  979. const char* type = cmdLine.findOption('t');
  980. if (NULL != type)
  981. {
  982. options.format = bimg::getFormat(type);
  983. if (!bimg::isValid(options.format) )
  984. {
  985. help("Invalid format specified.");
  986. return bx::kExitFailure;
  987. }
  988. }
  989. if (!bx::strFindI(saveAs, "png").isEmpty() )
  990. {
  991. if (options.format == bimg::TextureFormat::Count)
  992. {
  993. options.format = bimg::TextureFormat::RGBA8;
  994. }
  995. else if (options.format != bimg::TextureFormat::RGBA8)
  996. {
  997. help("Output PNG format must be RGBA8.");
  998. return bx::kExitFailure;
  999. }
  1000. }
  1001. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1002. {
  1003. if (options.format == bimg::TextureFormat::Count)
  1004. {
  1005. options.format = bimg::TextureFormat::RGBA16F;
  1006. }
  1007. else if (options.format != bimg::TextureFormat::RGBA16F)
  1008. {
  1009. help("Output EXR format must be RGBA16F.");
  1010. return bx::kExitFailure;
  1011. }
  1012. }
  1013. const char* quality = cmdLine.findOption('q');
  1014. if (NULL != quality)
  1015. {
  1016. switch (bx::toLower(quality[0]) )
  1017. {
  1018. case 'h': options.quality = bimg::Quality::Highest; break;
  1019. case 'f': options.quality = bimg::Quality::Fastest; break;
  1020. case 'd': options.quality = bimg::Quality::Default; break;
  1021. default:
  1022. help("Invalid quality specified.");
  1023. return bx::kExitFailure;
  1024. }
  1025. }
  1026. const char* radiance = cmdLine.findOption("radiance");
  1027. if (NULL != radiance)
  1028. {
  1029. if (0 == bx::strCmpI(radiance, "phong" ) ) { options.radiance = bimg::LightingModel::Phong; }
  1030. else if (0 == bx::strCmpI(radiance, "phongbrdf") ) { options.radiance = bimg::LightingModel::PhongBrdf; }
  1031. else if (0 == bx::strCmpI(radiance, "blinn" ) ) { options.radiance = bimg::LightingModel::Blinn; }
  1032. else if (0 == bx::strCmpI(radiance, "blinnbrdf") ) { options.radiance = bimg::LightingModel::BlinnBrdf; }
  1033. else if (0 == bx::strCmpI(radiance, "ggx" ) ) { options.radiance = bimg::LightingModel::Ggx; }
  1034. else
  1035. {
  1036. help("Invalid radiance lighting model specified.");
  1037. return bx::kExitFailure;
  1038. }
  1039. }
  1040. const bool validate = cmdLine.hasArg("validate");
  1041. bx::Error err;
  1042. bx::FileReader reader;
  1043. if (!bx::open(&reader, inputFileName, &err) )
  1044. {
  1045. help("Failed to open input file.", err);
  1046. return bx::kExitFailure;
  1047. }
  1048. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  1049. if (0 == inputSize)
  1050. {
  1051. help("Failed to read input file.", err);
  1052. return bx::kExitFailure;
  1053. }
  1054. bx::DefaultAllocator defaultAllocator;
  1055. AlignedAllocator allocator(&defaultAllocator, 16);
  1056. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1057. bx::read(&reader, inputData, inputSize, &err);
  1058. bx::close(&reader);
  1059. if (!err.isOk() )
  1060. {
  1061. help("Failed to read input file.", err);
  1062. return bx::kExitFailure;
  1063. }
  1064. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
  1065. BX_FREE(&allocator, inputData);
  1066. if (NULL != output)
  1067. {
  1068. bx::FileWriter writer;
  1069. if (bx::open(&writer, outputFileName, false, &err) )
  1070. {
  1071. if (!bx::strFindI(saveAs, "ktx").isEmpty() )
  1072. {
  1073. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size, &err);
  1074. }
  1075. else if (!bx::strFindI(saveAs, "dds").isEmpty() )
  1076. {
  1077. bimg::imageWriteDds(&writer, *output, output->m_data, output->m_size, &err);
  1078. }
  1079. else if (!bx::strFindI(saveAs, "png").isEmpty() )
  1080. {
  1081. if (output->m_format != bimg::TextureFormat::RGBA8)
  1082. {
  1083. help("Incompatible output texture format. Output PNG format must be RGBA8.", err);
  1084. return bx::kExitFailure;
  1085. }
  1086. bimg::ImageMip mip;
  1087. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1088. bimg::imageWritePng(&writer
  1089. , mip.m_width
  1090. , mip.m_height
  1091. , mip.m_width*4
  1092. , mip.m_data
  1093. , output->m_format
  1094. , false
  1095. , &err
  1096. );
  1097. }
  1098. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1099. {
  1100. bimg::ImageMip mip;
  1101. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1102. bimg::imageWriteExr(&writer
  1103. , mip.m_width
  1104. , mip.m_height
  1105. , mip.m_width*8
  1106. , mip.m_data
  1107. , output->m_format
  1108. , false
  1109. , &err
  1110. );
  1111. }
  1112. else if (!bx::strFindI(saveAs, "hdr").isEmpty() )
  1113. {
  1114. bimg::ImageMip mip;
  1115. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1116. bimg::imageWriteHdr(&writer
  1117. , mip.m_width
  1118. , mip.m_height
  1119. , mip.m_width*getBitsPerPixel(mip.m_format)/8
  1120. , mip.m_data
  1121. , output->m_format
  1122. , false
  1123. , &err
  1124. );
  1125. }
  1126. bx::close(&writer);
  1127. if (!err.isOk() )
  1128. {
  1129. help("", err);
  1130. return bx::kExitFailure;
  1131. }
  1132. }
  1133. else
  1134. {
  1135. help("Failed to open output file.", err);
  1136. return bx::kExitFailure;
  1137. }
  1138. if (validate)
  1139. {
  1140. if (!bx::open(&reader, outputFileName, &err) )
  1141. {
  1142. help("Failed to validate file.", err);
  1143. return bx::kExitFailure;
  1144. }
  1145. inputSize = (uint32_t)bx::getSize(&reader);
  1146. if (0 == inputSize)
  1147. {
  1148. help("Failed to validate file.", err);
  1149. return bx::kExitFailure;
  1150. }
  1151. inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1152. bx::read(&reader, inputData, inputSize, &err);
  1153. bx::close(&reader);
  1154. bimg::ImageContainer* input = bimg::imageParse(&allocator, inputData, inputSize, bimg::TextureFormat::Count, &err);
  1155. if (!err.isOk() )
  1156. {
  1157. help("Failed to validate file.", err);
  1158. return bx::kExitFailure;
  1159. }
  1160. if (false
  1161. || input->m_format != output->m_format
  1162. || input->m_size != output->m_size
  1163. || input->m_width != output->m_width
  1164. || input->m_height != output->m_height
  1165. || input->m_depth != output->m_depth
  1166. || input->m_numLayers != output->m_numLayers
  1167. || input->m_numMips != output->m_numMips
  1168. || input->m_hasAlpha != output->m_hasAlpha
  1169. || input->m_cubeMap != output->m_cubeMap
  1170. )
  1171. {
  1172. help("Validation failed, image headers are different.");
  1173. return bx::kExitFailure;
  1174. }
  1175. {
  1176. const uint8_t numMips = output->m_numMips;
  1177. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  1178. for (uint8_t lod = 0; lod < numMips; ++lod)
  1179. {
  1180. for (uint16_t side = 0; side < numSides; ++side)
  1181. {
  1182. bimg::ImageMip srcMip;
  1183. bool hasSrc = bimg::imageGetRawData(*input, side, lod, input->m_data, input->m_size, srcMip);
  1184. bimg::ImageMip dstMip;
  1185. bool hasDst = bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  1186. if (false
  1187. || hasSrc != hasDst
  1188. || srcMip.m_size != dstMip.m_size
  1189. )
  1190. {
  1191. help("Validation failed, image mip/layer/side are different.");
  1192. return bx::kExitFailure;
  1193. }
  1194. if (0 != bx::memCmp(srcMip.m_data, dstMip.m_data, srcMip.m_size) )
  1195. {
  1196. help("Validation failed, image content are different.");
  1197. return bx::kExitFailure;
  1198. }
  1199. }
  1200. }
  1201. }
  1202. BX_FREE(&allocator, inputData);
  1203. }
  1204. bimg::imageFree(output);
  1205. }
  1206. else
  1207. {
  1208. help("Failed to create output", err);
  1209. return bx::kExitFailure;
  1210. }
  1211. return bx::kExitSuccess;
  1212. }