texturec.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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. (Implies --linear)\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. // Normal maps are always linear
  961. if (options.normalMap)
  962. {
  963. options.linear = true;
  964. }
  965. const char* maxSize = cmdLine.findOption("max");
  966. if (NULL != maxSize)
  967. {
  968. if (!bx::fromString(&options.maxSize, maxSize) )
  969. {
  970. help("Parsing `--max` failed.");
  971. return bx::kExitFailure;
  972. }
  973. }
  974. const char* mipSkip = cmdLine.findOption("mipskip");
  975. if (NULL != mipSkip)
  976. {
  977. if (!bx::fromString(&options.mipSkip, mipSkip) )
  978. {
  979. help("Parsing `--mipskip` failed.");
  980. return bx::kExitFailure;
  981. }
  982. }
  983. options.format = bimg::TextureFormat::Count;
  984. const char* type = cmdLine.findOption('t');
  985. if (NULL != type)
  986. {
  987. options.format = bimg::getFormat(type);
  988. if (!bimg::isValid(options.format) )
  989. {
  990. help("Invalid format specified.");
  991. return bx::kExitFailure;
  992. }
  993. }
  994. if (!bx::strFindI(saveAs, "png").isEmpty() )
  995. {
  996. if (options.format == bimg::TextureFormat::Count)
  997. {
  998. options.format = bimg::TextureFormat::RGBA8;
  999. }
  1000. else if (options.format != bimg::TextureFormat::RGBA8)
  1001. {
  1002. help("Output PNG format must be RGBA8.");
  1003. return bx::kExitFailure;
  1004. }
  1005. }
  1006. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1007. {
  1008. if (options.format == bimg::TextureFormat::Count)
  1009. {
  1010. options.format = bimg::TextureFormat::RGBA16F;
  1011. }
  1012. else if (options.format != bimg::TextureFormat::RGBA16F)
  1013. {
  1014. help("Output EXR format must be RGBA16F.");
  1015. return bx::kExitFailure;
  1016. }
  1017. }
  1018. const char* quality = cmdLine.findOption('q');
  1019. if (NULL != quality)
  1020. {
  1021. switch (bx::toLower(quality[0]) )
  1022. {
  1023. case 'h': options.quality = bimg::Quality::Highest; break;
  1024. case 'f': options.quality = bimg::Quality::Fastest; break;
  1025. case 'd': options.quality = bimg::Quality::Default; break;
  1026. default:
  1027. help("Invalid quality specified.");
  1028. return bx::kExitFailure;
  1029. }
  1030. }
  1031. const char* radiance = cmdLine.findOption("radiance");
  1032. if (NULL != radiance)
  1033. {
  1034. if (0 == bx::strCmpI(radiance, "phong" ) ) { options.radiance = bimg::LightingModel::Phong; }
  1035. else if (0 == bx::strCmpI(radiance, "phongbrdf") ) { options.radiance = bimg::LightingModel::PhongBrdf; }
  1036. else if (0 == bx::strCmpI(radiance, "blinn" ) ) { options.radiance = bimg::LightingModel::Blinn; }
  1037. else if (0 == bx::strCmpI(radiance, "blinnbrdf") ) { options.radiance = bimg::LightingModel::BlinnBrdf; }
  1038. else if (0 == bx::strCmpI(radiance, "ggx" ) ) { options.radiance = bimg::LightingModel::Ggx; }
  1039. else
  1040. {
  1041. help("Invalid radiance lighting model specified.");
  1042. return bx::kExitFailure;
  1043. }
  1044. }
  1045. const bool validate = cmdLine.hasArg("validate");
  1046. bx::Error err;
  1047. bx::FileReader reader;
  1048. if (!bx::open(&reader, inputFileName, &err) )
  1049. {
  1050. help("Failed to open input file.", err);
  1051. return bx::kExitFailure;
  1052. }
  1053. uint32_t inputSize = (uint32_t)bx::getSize(&reader);
  1054. if (0 == inputSize)
  1055. {
  1056. help("Failed to read input file.", err);
  1057. return bx::kExitFailure;
  1058. }
  1059. bx::DefaultAllocator defaultAllocator;
  1060. AlignedAllocator allocator(&defaultAllocator, 16);
  1061. uint8_t* inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1062. bx::read(&reader, inputData, inputSize, &err);
  1063. bx::close(&reader);
  1064. if (!err.isOk() )
  1065. {
  1066. help("Failed to read input file.", err);
  1067. return bx::kExitFailure;
  1068. }
  1069. bimg::ImageContainer* output = convert(&allocator, inputData, inputSize, options, &err);
  1070. BX_FREE(&allocator, inputData);
  1071. if (NULL != output)
  1072. {
  1073. output->m_srgb = !options.linear;
  1074. bx::FileWriter writer;
  1075. if (bx::open(&writer, outputFileName, false, &err) )
  1076. {
  1077. if (!bx::strFindI(saveAs, "ktx").isEmpty() )
  1078. {
  1079. bimg::imageWriteKtx(&writer, *output, output->m_data, output->m_size, &err);
  1080. }
  1081. else if (!bx::strFindI(saveAs, "dds").isEmpty() )
  1082. {
  1083. bimg::imageWriteDds(&writer, *output, output->m_data, output->m_size, &err);
  1084. }
  1085. else if (!bx::strFindI(saveAs, "png").isEmpty() )
  1086. {
  1087. if (output->m_format != bimg::TextureFormat::RGBA8)
  1088. {
  1089. help("Incompatible output texture format. Output PNG format must be RGBA8.", err);
  1090. return bx::kExitFailure;
  1091. }
  1092. bimg::ImageMip mip;
  1093. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1094. bimg::imageWritePng(&writer
  1095. , mip.m_width
  1096. , mip.m_height
  1097. , mip.m_width*4
  1098. , mip.m_data
  1099. , output->m_format
  1100. , false
  1101. , &err
  1102. );
  1103. }
  1104. else if (!bx::strFindI(saveAs, "exr").isEmpty() )
  1105. {
  1106. bimg::ImageMip mip;
  1107. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1108. bimg::imageWriteExr(&writer
  1109. , mip.m_width
  1110. , mip.m_height
  1111. , mip.m_width*8
  1112. , mip.m_data
  1113. , output->m_format
  1114. , false
  1115. , &err
  1116. );
  1117. }
  1118. else if (!bx::strFindI(saveAs, "hdr").isEmpty() )
  1119. {
  1120. bimg::ImageMip mip;
  1121. bimg::imageGetRawData(*output, 0, 0, output->m_data, output->m_size, mip);
  1122. bimg::imageWriteHdr(&writer
  1123. , mip.m_width
  1124. , mip.m_height
  1125. , mip.m_width*getBitsPerPixel(mip.m_format)/8
  1126. , mip.m_data
  1127. , output->m_format
  1128. , false
  1129. , &err
  1130. );
  1131. }
  1132. bx::close(&writer);
  1133. if (!err.isOk() )
  1134. {
  1135. help("", err);
  1136. return bx::kExitFailure;
  1137. }
  1138. }
  1139. else
  1140. {
  1141. help("Failed to open output file.", err);
  1142. return bx::kExitFailure;
  1143. }
  1144. if (validate)
  1145. {
  1146. if (!bx::open(&reader, outputFileName, &err) )
  1147. {
  1148. help("Failed to validate file.", err);
  1149. return bx::kExitFailure;
  1150. }
  1151. inputSize = (uint32_t)bx::getSize(&reader);
  1152. if (0 == inputSize)
  1153. {
  1154. help("Failed to validate file.", err);
  1155. return bx::kExitFailure;
  1156. }
  1157. inputData = (uint8_t*)BX_ALLOC(&allocator, inputSize);
  1158. bx::read(&reader, inputData, inputSize, &err);
  1159. bx::close(&reader);
  1160. bimg::ImageContainer* input = bimg::imageParse(&allocator, inputData, inputSize, bimg::TextureFormat::Count, &err);
  1161. if (!err.isOk() )
  1162. {
  1163. help("Failed to validate file.", err);
  1164. return bx::kExitFailure;
  1165. }
  1166. if (false
  1167. || input->m_format != output->m_format
  1168. || input->m_size != output->m_size
  1169. || input->m_width != output->m_width
  1170. || input->m_height != output->m_height
  1171. || input->m_depth != output->m_depth
  1172. || input->m_numLayers != output->m_numLayers
  1173. || input->m_numMips != output->m_numMips
  1174. || input->m_hasAlpha != output->m_hasAlpha
  1175. || input->m_cubeMap != output->m_cubeMap
  1176. )
  1177. {
  1178. help("Validation failed, image headers are different.");
  1179. return bx::kExitFailure;
  1180. }
  1181. {
  1182. const uint8_t numMips = output->m_numMips;
  1183. const uint16_t numSides = output->m_numLayers * (output->m_cubeMap ? 6 : 1);
  1184. for (uint8_t lod = 0; lod < numMips; ++lod)
  1185. {
  1186. for (uint16_t side = 0; side < numSides; ++side)
  1187. {
  1188. bimg::ImageMip srcMip;
  1189. bool hasSrc = bimg::imageGetRawData(*input, side, lod, input->m_data, input->m_size, srcMip);
  1190. bimg::ImageMip dstMip;
  1191. bool hasDst = bimg::imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  1192. if (false
  1193. || hasSrc != hasDst
  1194. || srcMip.m_size != dstMip.m_size
  1195. )
  1196. {
  1197. help("Validation failed, image mip/layer/side are different.");
  1198. return bx::kExitFailure;
  1199. }
  1200. if (0 != bx::memCmp(srcMip.m_data, dstMip.m_data, srcMip.m_size) )
  1201. {
  1202. help("Validation failed, image content are different.");
  1203. return bx::kExitFailure;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. BX_FREE(&allocator, inputData);
  1209. }
  1210. bimg::imageFree(output);
  1211. }
  1212. else
  1213. {
  1214. help("Failed to create output", err);
  1215. return bx::kExitFailure;
  1216. }
  1217. return bx::kExitSuccess;
  1218. }