ImageImporter.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Importer/ImageImporter.h>
  6. #include <AnKi/Importer/TinyExr.h>
  7. #include <AnKi/Gr/Common.h>
  8. #include <AnKi/Resource/Stb.h>
  9. #include <AnKi/Util/Process.h>
  10. #include <AnKi/Util/File.h>
  11. #include <AnKi/Util/Filesystem.h>
  12. namespace anki {
  13. static Atomic<U32> g_tempFileIndex = {1}; // Used to create random names
  14. namespace {
  15. class SurfaceOrVolumeData
  16. {
  17. public:
  18. ImporterDynamicArrayLarge<U8> m_pixels;
  19. ImporterDynamicArrayLarge<U8> m_s3tcPixels;
  20. ImporterDynamicArrayLarge<U8> m_astcPixels;
  21. };
  22. class Mipmap
  23. {
  24. public:
  25. /// One surface for each layer ore one per face or a single volume if it's a 3D texture.
  26. ImporterDynamicArray<SurfaceOrVolumeData> m_surfacesOrVolume;
  27. };
  28. /// Image importer context.
  29. class ImageImporterContext
  30. {
  31. public:
  32. ImporterDynamicArray<Mipmap> m_mipmaps;
  33. U32 m_width = 0;
  34. U32 m_height = 0;
  35. U32 m_depth = 0;
  36. U32 m_faceCount = 0;
  37. U32 m_layerCount = 0;
  38. U32 m_channelCount = 0;
  39. U32 m_pixelSize = 0; ///< Texel size of an uncompressed image.
  40. Bool m_hdr = false;
  41. Vec4 m_averageColor = Vec4(0.0f);
  42. };
  43. class DdsPixelFormat
  44. {
  45. public:
  46. U32 m_dwSize;
  47. U32 m_dwFlags;
  48. Array<char, 4> m_dwFourCC;
  49. U32 m_dwRGBBitCount;
  50. U32 m_dwRBitMask;
  51. U32 m_dwGBitMask;
  52. U32 m_dwBBitMask;
  53. U32 m_dwABitMask;
  54. };
  55. class DdsHeader
  56. {
  57. public:
  58. Array<U8, 4> m_magic;
  59. U32 m_dwSize;
  60. U32 m_dwFlags;
  61. U32 m_dwHeight;
  62. U32 m_dwWidth;
  63. U32 m_dwPitchOrLinearSize;
  64. U32 m_dwDepth;
  65. U32 m_dwMipMapCount;
  66. Array<U32, 11> m_dwReserved1;
  67. DdsPixelFormat m_ddspf;
  68. U32 m_dwCaps;
  69. U32 m_dwCaps2;
  70. U32 m_dwCaps3;
  71. U32 m_dwCaps4;
  72. U32 m_dwReserved2;
  73. };
  74. enum D3D10ResourceDimension
  75. {
  76. D3D10_RESOURCE_DIMENSION_UNKNOWN = 0,
  77. D3D10_RESOURCE_DIMENSION_BUFFER = 1,
  78. D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2,
  79. D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3,
  80. D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4
  81. };
  82. /// Extra header for some DDS formats.
  83. class DdsHeaderDxt10
  84. {
  85. public:
  86. U32 m_dxgiFormat;
  87. D3D10ResourceDimension m_resourceDimension;
  88. U32 m_miscFlag;
  89. U32 m_arraySize;
  90. U32 m_reserved;
  91. };
  92. class AstcHeader
  93. {
  94. public:
  95. Array<U8, 4> m_magic;
  96. U8 m_blockX;
  97. U8 m_blockY;
  98. U8 m_blockZ;
  99. Array<U8, 3> m_dimX;
  100. Array<U8, 3> m_dimY;
  101. Array<U8, 3> m_dimZ;
  102. };
  103. } // namespace
  104. static Error checkConfig(const ImageImporterConfig& config)
  105. {
  106. #define ANKI_CFG_ASSERT(x, message) \
  107. do \
  108. { \
  109. if(!(x)) \
  110. { \
  111. ANKI_IMPORTER_LOGE(message); \
  112. return Error::kUserData; \
  113. } \
  114. } while(false)
  115. // Filenames
  116. ANKI_CFG_ASSERT(config.m_outFilename.getLength() > 0, "Empty output filename");
  117. for(CString in : config.m_inputFilenames)
  118. {
  119. ANKI_CFG_ASSERT(in.getLength() > 0, "Empty input filename");
  120. }
  121. // Type
  122. ANKI_CFG_ASSERT(config.m_type != ImageBinaryType::kNone, "Wrong image type");
  123. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() == 1 || config.m_type != ImageBinaryType::k2D, "2D images require only one input image");
  124. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 1 || config.m_type != ImageBinaryType::k2DArray,
  125. "2D array images require more than one input image");
  126. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 1 || config.m_type != ImageBinaryType::k3D, "3D images require more than one input image");
  127. ANKI_CFG_ASSERT(config.m_inputFilenames.getSize() != 6 || config.m_type != ImageBinaryType::kCube, "Cube images require 6 input images");
  128. // Compressions
  129. ANKI_CFG_ASSERT(config.m_compressions != ImageBinaryDataCompression::kNone, "Missing output compressions");
  130. ANKI_CFG_ASSERT(config.m_compressions == ImageBinaryDataCompression::kRaw || config.m_type != ImageBinaryType::k3D, "Can't compress 3D textures");
  131. // ASTC
  132. if(!!(config.m_compressions & ImageBinaryDataCompression::kAstc))
  133. {
  134. ANKI_CFG_ASSERT(config.m_astcBlockSize == UVec2(4u) || config.m_astcBlockSize == UVec2(8u), "Incorrect ASTC block sizes");
  135. }
  136. // Mip size
  137. ANKI_CFG_ASSERT(config.m_minMipmapDimension >= 4, "Mimpap min dimension can be less than 4");
  138. // Color conversions
  139. ANKI_CFG_ASSERT(!(config.m_linearToSRgb && config.m_sRgbToLinear), "Can't have a conversion to sRGB and to linear at the same time");
  140. #undef ANKI_CFG_ASSERT
  141. return Error::kNone;
  142. }
  143. static Error identifyImage(CString filename, U32& width, U32& height, U32& channelCount, Bool& hdr)
  144. {
  145. I32 iwidth, iheight, ichannelCount;
  146. const I ok = stbi_info(filename.cstr(), &iwidth, &iheight, &ichannelCount);
  147. if(!ok)
  148. {
  149. ANKI_IMPORTER_LOGE("STB failed to load file: %s", filename.cstr());
  150. return Error::kFunctionFailed;
  151. }
  152. const I ihdr = stbi_is_hdr(filename.cstr());
  153. width = U32(iwidth);
  154. height = U32(iheight);
  155. channelCount = U32(ichannelCount);
  156. hdr = U32(ihdr);
  157. return Error::kNone;
  158. }
  159. static Error checkInputImages(const ImageImporterConfig& config, U32& width, U32& height, U32& channelCount, Bool& isHdr)
  160. {
  161. width = 0;
  162. height = 0;
  163. channelCount = 0;
  164. isHdr = false;
  165. for(U32 i = 0; i < config.m_inputFilenames.getSize(); ++i)
  166. {
  167. U32 nwidth, nheight, nchannelCount;
  168. Bool nhdr;
  169. ANKI_CHECK(identifyImage(config.m_inputFilenames[i], nwidth, nheight, nchannelCount, nhdr));
  170. if(i == 0)
  171. {
  172. width = nwidth;
  173. height = nheight;
  174. channelCount = nchannelCount;
  175. isHdr = nhdr;
  176. }
  177. else if(width != nwidth || height != nheight || channelCount != nchannelCount || isHdr != nhdr)
  178. {
  179. ANKI_IMPORTER_LOGE("Input image doesn't match previous input images: %s", config.m_inputFilenames[i].cstr());
  180. return Error::kUserData;
  181. }
  182. }
  183. ANKI_ASSERT(width > 0 && height > 0 && channelCount > 0);
  184. if(!isPowerOfTwo(width) || !isPowerOfTwo(height))
  185. {
  186. ANKI_IMPORTER_LOGE("Only power of two images are accepted");
  187. return Error::kUserData;
  188. }
  189. return Error::kNone;
  190. }
  191. static Error resizeImage(CString inImageFilename, U32 outWidth, U32 outHeight, CString tempDirectory, ImporterString& tmpFilename)
  192. {
  193. U32 inWidth, inHeight, channelCount;
  194. Bool hdr;
  195. ANKI_CHECK(identifyImage(inImageFilename, inWidth, inHeight, channelCount, hdr));
  196. // Load
  197. void* inPixels;
  198. if(!hdr)
  199. {
  200. I32 width, height;
  201. inPixels = stbi_load(inImageFilename.cstr(), &width, &height, nullptr, channelCount);
  202. }
  203. else
  204. {
  205. I32 width, height;
  206. inPixels = stbi_loadf(inImageFilename.cstr(), &width, &height, nullptr, channelCount);
  207. }
  208. if(!inPixels)
  209. {
  210. ANKI_IMPORTER_LOGE("STB load failed: %s", inImageFilename.cstr());
  211. return Error::kFunctionFailed;
  212. }
  213. // Resize
  214. I ok;
  215. ImporterDynamicArray<U8> outPixels;
  216. if(!hdr)
  217. {
  218. outPixels.resize(outWidth * outHeight * channelCount);
  219. ok = stbir_resize_uint8(static_cast<const U8*>(inPixels), inWidth, inHeight, 0, outPixels.getBegin(), outWidth, outHeight, 0, channelCount);
  220. }
  221. else
  222. {
  223. outPixels.resize(outWidth * outHeight * channelCount * sizeof(F32));
  224. ok = stbir_resize_float(static_cast<const F32*>(inPixels), inWidth, inHeight, 0, reinterpret_cast<F32*>(outPixels.getBegin()), outWidth,
  225. outHeight, 0, channelCount);
  226. }
  227. stbi_image_free(inPixels);
  228. if(!ok)
  229. {
  230. ANKI_IMPORTER_LOGE("stbir_resize_xxx() failed to resize the image: %s", inImageFilename.cstr());
  231. return Error::kFunctionFailed;
  232. }
  233. // Store
  234. tmpFilename.sprintf("%s/AnKiImageImporter_%u.%s", tempDirectory.cstr(), g_tempFileIndex.fetchAdd(1), (hdr) ? "exr" : "png");
  235. ANKI_IMPORTER_LOGV("Will store: %s", tmpFilename.cstr());
  236. if(!hdr)
  237. {
  238. ok = stbi_write_png(tmpFilename.cstr(), outWidth, outHeight, channelCount, outPixels.getBegin(), 0);
  239. }
  240. else
  241. {
  242. const I ret = SaveEXR(reinterpret_cast<const F32*>(outPixels.getBegin()), outWidth, outHeight, channelCount, 0, tmpFilename.cstr(), nullptr);
  243. ok = ret >= 0;
  244. }
  245. if(!ok)
  246. {
  247. ANKI_IMPORTER_LOGE("Failed to create: %s", tmpFilename.cstr());
  248. return Error::kFunctionFailed;
  249. }
  250. return Error::kNone;
  251. }
  252. static Vec3 linearToSRgb(Vec3 p)
  253. {
  254. Vec3 cutoff;
  255. cutoff.x = (p.x < 0.0031308f) ? 1.0f : 0.0f;
  256. cutoff.y = (p.y < 0.0031308f) ? 1.0f : 0.0f;
  257. cutoff.z = (p.z < 0.0031308f) ? 1.0f : 0.0f;
  258. const Vec3 higher = 1.055f * p.pow(1.0f / 2.4f) - 0.055f;
  259. const Vec3 lower = p * 12.92f;
  260. p = higher.lerp(lower, cutoff);
  261. return p;
  262. }
  263. static Vec3 sRgbToLinear(Vec3 p)
  264. {
  265. Vec3 cutoff;
  266. cutoff.x = (p.x < 0.04045f) ? 1.0f : 0.0f;
  267. cutoff.y = (p.y < 0.04045f) ? 1.0f : 0.0f;
  268. cutoff.z = (p.z < 0.04045f) ? 1.0f : 0.0f;
  269. const Vec3 higher = ((p + 0.055f) / 1.055f).pow(2.4f);
  270. const Vec3 lower = p / 12.92f;
  271. const Vec3 out = higher.lerp(lower, cutoff);
  272. return out;
  273. }
  274. template<typename TVec, typename TFunc>
  275. static void linearToSRgbBatch(WeakArray<TVec> pixels, TFunc func)
  276. {
  277. using S = typename TVec::Scalar;
  278. for(TVec& pixel : pixels)
  279. {
  280. Vec3 p;
  281. if(std::is_same<S, U8>::value)
  282. {
  283. p.x = F32(pixel.x) / 255.0f;
  284. p.y = F32(pixel.y) / 255.0f;
  285. p.z = F32(pixel.z) / 255.0f;
  286. }
  287. else
  288. {
  289. ANKI_ASSERT((std::is_same<S, F32>::value));
  290. p.x = F32(pixel.x);
  291. p.y = F32(pixel.y);
  292. p.z = F32(pixel.z);
  293. }
  294. p = func(p);
  295. if(std::is_same<S, U8>::value)
  296. {
  297. pixel.x = S(p.x * 255.0f);
  298. pixel.y = S(p.y * 255.0f);
  299. pixel.z = S(p.z * 255.0f);
  300. }
  301. else
  302. {
  303. pixel.x = S(p.x);
  304. pixel.y = S(p.y);
  305. pixel.z = S(p.z);
  306. }
  307. }
  308. }
  309. template<typename TVec>
  310. static Vec4 computeAverageColor(WeakArray<TVec> pixels)
  311. {
  312. Vec4 average(0.0f);
  313. const F32 weight = 1.0f / F32(pixels.getSize());
  314. constexpr Bool unorm = sizeof(pixels[0][0]) == 1;
  315. constexpr U32 componentCount = TVec::kComponentCount;
  316. static_assert(componentCount == 3 || componentCount == 4);
  317. for(const TVec& color : pixels)
  318. {
  319. Vec4 v;
  320. if constexpr(componentCount == 3)
  321. {
  322. v = Vec4(Vec3(color), 0.0f);
  323. }
  324. else
  325. {
  326. ANKI_ASSERT(componentCount == 4);
  327. v = Vec4(color);
  328. }
  329. if(unorm)
  330. {
  331. v /= 255.0f;
  332. }
  333. else
  334. {
  335. ANKI_ASSERT(sizeof(color[0]) == 4);
  336. }
  337. average += v * weight;
  338. }
  339. return (componentCount == 3) ? average.xyz1 : average;
  340. }
  341. static void applyScaleAndBias(WeakArray<Vec3> pixels, Vec3 scale, Vec3 bias)
  342. {
  343. for(Vec3& pixel : pixels)
  344. {
  345. pixel = pixel * scale + bias;
  346. }
  347. }
  348. static Error loadFirstMipmap(const ImageImporterConfig& config, ImageImporterContext& ctx)
  349. {
  350. ctx.m_mipmaps.emplaceBack();
  351. Mipmap& mip0 = ctx.m_mipmaps[0];
  352. if(ctx.m_depth > 1)
  353. {
  354. mip0.m_surfacesOrVolume.resize(1);
  355. mip0.m_surfacesOrVolume[0].m_pixels.resize(ctx.m_pixelSize * ctx.m_width * ctx.m_height * ctx.m_depth);
  356. }
  357. else
  358. {
  359. mip0.m_surfacesOrVolume.resize(ctx.m_faceCount * ctx.m_layerCount);
  360. ANKI_ASSERT(mip0.m_surfacesOrVolume.getSize() == config.m_inputFilenames.getSize());
  361. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  362. {
  363. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  364. {
  365. mip0.m_surfacesOrVolume[l * ctx.m_faceCount + f].m_pixels.resize(ctx.m_pixelSize * ctx.m_width * ctx.m_height);
  366. }
  367. }
  368. }
  369. for(U32 i = 0; i < config.m_inputFilenames.getSize(); ++i)
  370. {
  371. I32 width, height;
  372. stbi_set_flip_vertically_on_load_thread(config.m_flipImage);
  373. void* data;
  374. if(!ctx.m_hdr)
  375. {
  376. data = stbi_load(config.m_inputFilenames[i].cstr(), &width, &height, nullptr, ctx.m_channelCount);
  377. }
  378. else
  379. {
  380. data = stbi_loadf(config.m_inputFilenames[i].cstr(), &width, &height, nullptr, ctx.m_channelCount);
  381. }
  382. if(!data)
  383. {
  384. ANKI_IMPORTER_LOGE("STB load failed: %s", config.m_inputFilenames[i].cstr());
  385. return Error::kFunctionFailed;
  386. }
  387. const PtrSize dataSize = PtrSize(ctx.m_width) * ctx.m_height * ctx.m_pixelSize;
  388. // To conversions in place
  389. if(config.m_linearToSRgb)
  390. {
  391. ANKI_IMPORTER_LOGV("Will convert linear to sRGB");
  392. if(ctx.m_channelCount == 3)
  393. {
  394. if(!ctx.m_hdr)
  395. {
  396. linearToSRgbBatch(WeakArray<U8Vec3>(static_cast<U8Vec3*>(data), ctx.m_width * ctx.m_height), linearToSRgb);
  397. }
  398. else
  399. {
  400. linearToSRgbBatch(WeakArray<Vec3>(static_cast<Vec3*>(data), ctx.m_width * ctx.m_height), linearToSRgb);
  401. }
  402. }
  403. else
  404. {
  405. ANKI_ASSERT(ctx.m_channelCount == 4);
  406. if(!ctx.m_hdr)
  407. {
  408. linearToSRgbBatch(WeakArray<U8Vec4>(static_cast<U8Vec4*>(data), ctx.m_width * ctx.m_height), linearToSRgb);
  409. }
  410. else
  411. {
  412. linearToSRgbBatch(WeakArray<Vec4>(static_cast<Vec4*>(data), ctx.m_width * ctx.m_height), linearToSRgb);
  413. }
  414. }
  415. }
  416. else if(config.m_sRgbToLinear)
  417. {
  418. ANKI_IMPORTER_LOGV("Will convert sRGB to linear");
  419. if(ctx.m_channelCount == 3)
  420. {
  421. if(!ctx.m_hdr)
  422. {
  423. linearToSRgbBatch(WeakArray<U8Vec3>(static_cast<U8Vec3*>(data), ctx.m_width * ctx.m_height), sRgbToLinear);
  424. }
  425. else
  426. {
  427. linearToSRgbBatch(WeakArray<Vec3>(static_cast<Vec3*>(data), ctx.m_width * ctx.m_height), sRgbToLinear);
  428. }
  429. }
  430. else
  431. {
  432. ANKI_ASSERT(ctx.m_channelCount == 4);
  433. if(!ctx.m_hdr)
  434. {
  435. linearToSRgbBatch(WeakArray<U8Vec4>(static_cast<U8Vec4*>(data), ctx.m_width * ctx.m_height), sRgbToLinear);
  436. }
  437. else
  438. {
  439. linearToSRgbBatch(WeakArray<Vec4>(static_cast<Vec4*>(data), ctx.m_width * ctx.m_height), sRgbToLinear);
  440. }
  441. }
  442. }
  443. if(ctx.m_hdr && (config.m_hdrScale != Vec3(1.0f) || config.m_hdrBias != Vec3(0.0f)))
  444. {
  445. ANKI_IMPORTER_LOGV("Will apply scale and/or bias to the image");
  446. applyScaleAndBias(WeakArray(static_cast<Vec3*>(data), ctx.m_width * ctx.m_height), config.m_hdrScale, config.m_hdrBias);
  447. }
  448. if(ctx.m_depth > 1)
  449. {
  450. memcpy(mip0.m_surfacesOrVolume[0].m_pixels.getBegin() + i * dataSize, data, dataSize);
  451. }
  452. else
  453. {
  454. memcpy(mip0.m_surfacesOrVolume[i].m_pixels.getBegin(), data, dataSize);
  455. }
  456. Vec4 averageColor(0.0f);
  457. if(ctx.m_channelCount == 3)
  458. {
  459. if(!ctx.m_hdr)
  460. {
  461. averageColor = computeAverageColor(WeakArray<U8Vec3>(static_cast<U8Vec3*>(data), ctx.m_width * ctx.m_height));
  462. }
  463. else
  464. {
  465. averageColor = computeAverageColor(WeakArray<Vec3>(static_cast<Vec3*>(data), ctx.m_width * ctx.m_height));
  466. }
  467. }
  468. else
  469. {
  470. ANKI_ASSERT(ctx.m_channelCount == 4);
  471. if(!ctx.m_hdr)
  472. {
  473. averageColor = computeAverageColor(WeakArray<U8Vec4>(static_cast<U8Vec4*>(data), ctx.m_width * ctx.m_height));
  474. }
  475. else
  476. {
  477. averageColor = computeAverageColor(WeakArray<Vec4>(static_cast<Vec4*>(data), ctx.m_width * ctx.m_height));
  478. }
  479. }
  480. ctx.m_averageColor += averageColor / F32(config.m_inputFilenames.getSize());
  481. stbi_image_free(data);
  482. }
  483. return Error::kNone;
  484. }
  485. template<typename TStorageVec>
  486. static void generateSurfaceMipmap(ConstWeakArray<U8, PtrSize> inBuffer, U32 inWidth, U32 inHeight, WeakArray<U8, PtrSize> outBuffer)
  487. {
  488. constexpr U32 channelCount = TStorageVec::getSize();
  489. using FVecType = typename std::conditional_t<channelCount == 3, Vec3, Vec4>;
  490. const ConstWeakArray<TStorageVec, PtrSize> inPixels(reinterpret_cast<const TStorageVec*>(&inBuffer[0]),
  491. inBuffer.getSizeInBytes() / sizeof(TStorageVec));
  492. WeakArray<TStorageVec, PtrSize> outPixels(reinterpret_cast<TStorageVec*>(&outBuffer[0]), outBuffer.getSizeInBytes() / sizeof(TStorageVec));
  493. const U32 outWidth = inWidth >> 1;
  494. const U32 outHeight = inHeight >> 1;
  495. for(U32 h = 0; h < outHeight; ++h)
  496. {
  497. for(U32 w = 0; w < outWidth; ++w)
  498. {
  499. // Gather input
  500. FVecType average(0.0f);
  501. for(U32 y = 0; y < 2; ++y)
  502. {
  503. for(U32 x = 0; x < 2; ++x)
  504. {
  505. const U32 idx = (h * 2 + y) * inWidth + (w * 2 + x);
  506. const TStorageVec inPixel = inPixels[idx];
  507. for(U32 c = 0; c < channelCount; ++c)
  508. {
  509. average[c] += F32(inPixel[c]) * 0.25f;
  510. }
  511. }
  512. }
  513. TStorageVec uaverage;
  514. for(U32 c = 0; c < channelCount; ++c)
  515. {
  516. uaverage[c] = typename TStorageVec::Scalar(average[c]);
  517. }
  518. // Store
  519. const U32 idx = h * outWidth + w;
  520. outPixels[idx] = uaverage;
  521. }
  522. }
  523. }
  524. static Error compressS3tc(CString tempDirectory, CString compressonatorFilename, ConstWeakArray<U8, PtrSize> inPixels, U32 inWidth, U32 inHeight,
  525. U32 channelCount, Bool hdr, WeakArray<U8, PtrSize> outPixels)
  526. {
  527. ANKI_ASSERT(inPixels.getSizeInBytes() == PtrSize(inWidth) * inHeight * channelCount * ((hdr) ? sizeof(F32) : sizeof(U8)));
  528. ANKI_ASSERT(inWidth > 0 && isPowerOfTwo(inWidth) && inHeight > 0 && isPowerOfTwo(inHeight));
  529. ANKI_ASSERT(outPixels.getSizeInBytes() == PtrSize((hdr || channelCount == 4) ? 16 : 8) * (inWidth / 4) * (inHeight / 4));
  530. // Create a PNG image to feed to the compressor
  531. ImporterString tmpFilename;
  532. tmpFilename.sprintf("%s/AnKiImageImporter_%u.%s", tempDirectory.cstr(), g_tempFileIndex.fetchAdd(1), (hdr) ? "exr" : "png");
  533. ANKI_IMPORTER_LOGV("Will store: %s", tmpFilename.cstr());
  534. Bool saveTmpImageOk = false;
  535. if(!hdr)
  536. {
  537. const I ok = stbi_write_png(tmpFilename.cstr(), inWidth, inHeight, channelCount, inPixels.getBegin(), 0);
  538. saveTmpImageOk = !!ok;
  539. }
  540. else
  541. {
  542. const I ret = SaveEXR(reinterpret_cast<const F32*>(inPixels.getBegin()), inWidth, inHeight, channelCount, 0, tmpFilename.cstr(), nullptr);
  543. saveTmpImageOk = ret >= 0;
  544. }
  545. if(!saveTmpImageOk)
  546. {
  547. ANKI_IMPORTER_LOGE("Failed to create: %s", tmpFilename.cstr());
  548. return Error::kFunctionFailed;
  549. }
  550. CleanupFile tmpCleanup(tmpFilename);
  551. // Invoke the compressor process
  552. ImporterString ddsFilename;
  553. ddsFilename.sprintf("%s/AnKiImageImporter_%u.dds", tempDirectory.cstr(), g_tempFileIndex.fetchAdd(1));
  554. Process proc;
  555. Array<CString, 5> args;
  556. U32 argCount = 0;
  557. args[argCount++] = "-nomipmap";
  558. args[argCount++] = "-fd";
  559. args[argCount++] = (hdr) ? "BC6H" : ((channelCount == 3) ? "BC1" : "BC3");
  560. args[argCount++] = tmpFilename;
  561. args[argCount++] = ddsFilename;
  562. ANKI_IMPORTER_LOGV("Will invoke process: %s %s %s %s %s %s", compressonatorFilename.cstr(), args[0].cstr(), args[1].cstr(), args[2].cstr(),
  563. args[3].cstr(), args[4].cstr());
  564. ANKI_CHECK(proc.start(compressonatorFilename, args));
  565. CleanupFile ddsCleanup(ddsFilename);
  566. ProcessStatus status;
  567. I32 exitCode;
  568. ANKI_CHECK(proc.wait(60.0_sec, &status, &exitCode));
  569. if(!(status == ProcessStatus::kNotRunning && exitCode == 0))
  570. {
  571. String errStr;
  572. if(exitCode != 0)
  573. {
  574. ANKI_CHECK(proc.readFromStdout(errStr));
  575. }
  576. if(errStr.isEmpty())
  577. {
  578. errStr = "Unknown error";
  579. }
  580. ANKI_IMPORTER_LOGE("Invoking compressor process failed: %s", errStr.cstr());
  581. return Error::kFunctionFailed;
  582. }
  583. // Read the DDS file
  584. File ddsFile;
  585. ANKI_CHECK(ddsFile.open(ddsFilename, FileOpenFlag::kRead | FileOpenFlag::kBinary));
  586. DdsHeader ddsHeader;
  587. ANKI_CHECK(ddsFile.read(&ddsHeader, sizeof(DdsHeader)));
  588. if(!hdr && channelCount == 3 && memcmp(&ddsHeader.m_ddspf.m_dwFourCC[0], "DXT1", 4) != 0)
  589. {
  590. ANKI_IMPORTER_LOGE("Incorrect format. Expecting DXT1");
  591. return Error::kFunctionFailed;
  592. }
  593. if(!hdr && channelCount == 4 && memcmp(&ddsHeader.m_ddspf.m_dwFourCC[0], "DXT5", 4) != 0)
  594. {
  595. ANKI_IMPORTER_LOGE("Incorrect format. Expecting DXT5");
  596. return Error::kFunctionFailed;
  597. }
  598. if(hdr && memcmp(&ddsHeader.m_ddspf.m_dwFourCC[0], "DX10", 4) != 0)
  599. {
  600. ANKI_IMPORTER_LOGE("Incorrect format. Expecting BC6H");
  601. return Error::kFunctionFailed;
  602. }
  603. if(ddsHeader.m_dwWidth != inWidth || ddsHeader.m_dwHeight != inHeight)
  604. {
  605. ANKI_IMPORTER_LOGE("Incorrect DDS image size");
  606. return Error::kFunctionFailed;
  607. }
  608. if(hdr)
  609. {
  610. DdsHeaderDxt10 dxt10Header;
  611. ANKI_CHECK(ddsFile.read(&dxt10Header, sizeof(dxt10Header)));
  612. }
  613. ANKI_CHECK(ddsFile.read(outPixels.getBegin(), outPixels.getSizeInBytes()));
  614. return Error::kNone;
  615. }
  616. static Error compressAstc(CString tempDirectory, CString astcencFilename, ConstWeakArray<U8, PtrSize> inPixels, U32 inWidth, U32 inHeight,
  617. U32 inChannelCount, UVec2 blockSize, Bool hdr, WeakArray<U8, PtrSize> outPixels)
  618. {
  619. [[maybe_unused]] const PtrSize blockBytes = 16;
  620. ANKI_ASSERT(inPixels.getSizeInBytes() == PtrSize(inWidth) * inHeight * inChannelCount * ((hdr) ? sizeof(F32) : sizeof(U8)));
  621. ANKI_ASSERT(inWidth > 0 && isPowerOfTwo(inWidth) && inHeight > 0 && isPowerOfTwo(inHeight));
  622. ANKI_ASSERT(outPixels.getSizeInBytes() == blockBytes * (inWidth / blockSize.x) * (inHeight / blockSize.y));
  623. // Create a BMP image to feed to the astcebc
  624. ImporterString tmpFilename;
  625. tmpFilename.sprintf("%s/AnKiImageImporter_%u.%s", tempDirectory.cstr(), g_tempFileIndex.fetchAdd(1), (hdr) ? "exr" : "png");
  626. ANKI_IMPORTER_LOGV("Will store: %s", tmpFilename.cstr());
  627. Bool saveTmpImageOk = false;
  628. if(!hdr)
  629. {
  630. const I ok = stbi_write_png(tmpFilename.cstr(), inWidth, inHeight, inChannelCount, inPixels.getBegin(), 0);
  631. saveTmpImageOk = !!ok;
  632. }
  633. else
  634. {
  635. const I ret = SaveEXR(reinterpret_cast<const F32*>(inPixels.getBegin()), inWidth, inHeight, inChannelCount, 0, tmpFilename.cstr(), nullptr);
  636. saveTmpImageOk = ret >= 0;
  637. }
  638. if(!saveTmpImageOk)
  639. {
  640. ANKI_IMPORTER_LOGE("Failed to create: %s", tmpFilename.cstr());
  641. return Error::kFunctionFailed;
  642. }
  643. CleanupFile pngCleanup(tmpFilename);
  644. // Invoke the compressor process
  645. ImporterString astcFilename;
  646. astcFilename.sprintf("%s/AnKiImageImporter_%u.astc", tempDirectory.cstr(), g_tempFileIndex.fetchAdd(1));
  647. ImporterString blockStr;
  648. blockStr.sprintf("%ux%u", blockSize.x, blockSize.y);
  649. Process proc;
  650. Array<CString, 5> args;
  651. U32 argCount = 0;
  652. args[argCount++] = (!hdr) ? "-cl" : "-ch";
  653. args[argCount++] = tmpFilename;
  654. args[argCount++] = astcFilename;
  655. args[argCount++] = blockStr;
  656. args[argCount++] = "-fast";
  657. ANKI_IMPORTER_LOGV("Will invoke process: %s %s %s %s %s %s", astcencFilename.cstr(), args[0].cstr(), args[1].cstr(), args[2].cstr(),
  658. args[3].cstr(), args[4].cstr());
  659. ANKI_CHECK(proc.start(astcencFilename, args));
  660. CleanupFile astcCleanup(astcFilename);
  661. ProcessStatus status;
  662. I32 exitCode;
  663. ANKI_CHECK(proc.wait(60.0_sec, &status, &exitCode));
  664. if(!(status == ProcessStatus::kNotRunning && exitCode == 0))
  665. {
  666. String errStr;
  667. if(exitCode != 0)
  668. {
  669. ANKI_CHECK(proc.readFromStdout(errStr));
  670. }
  671. if(errStr.isEmpty())
  672. {
  673. errStr = "Unknown error";
  674. }
  675. ANKI_IMPORTER_LOGE("Invoking astcenc-avx2 process failed: %s", errStr.cstr());
  676. return Error::kFunctionFailed;
  677. }
  678. // Read the astc file
  679. File astcFile;
  680. ANKI_CHECK(astcFile.open(astcFilename, FileOpenFlag::kRead | FileOpenFlag::kBinary));
  681. AstcHeader header;
  682. ANKI_CHECK(astcFile.read(&header, sizeof(header)));
  683. auto unpackBytes = [](U8 a, U8 b, U8 c, U8 d) -> U32 {
  684. return (U32(a)) + (U32(b) << 8) + (U32(c) << 16) + (U32(d) << 24);
  685. };
  686. const U32 magicval = unpackBytes(header.m_magic[0], header.m_magic[1], header.m_magic[2], header.m_magic[3]);
  687. if(magicval != 0x5CA1AB13)
  688. {
  689. ANKI_IMPORTER_LOGE("astcenc-avx2 produced a file with wrong magic");
  690. return Error::kFunctionFailed;
  691. }
  692. const U32 blockx = max<U32>(header.m_blockX, 1u);
  693. const U32 blocky = max<U32>(header.m_blockY, 1u);
  694. const U32 blockz = max<U32>(header.m_blockZ, 1u);
  695. if(blockx != blockSize.x || blocky != blockSize.y || blockz != 1)
  696. {
  697. ANKI_IMPORTER_LOGE("astcenc-avx2 with wrong block size");
  698. return Error::kFunctionFailed;
  699. }
  700. const U32 dimx = unpackBytes(header.m_dimX[0], header.m_dimX[1], header.m_dimX[2], 0);
  701. const U32 dimy = unpackBytes(header.m_dimY[0], header.m_dimY[1], header.m_dimY[2], 0);
  702. const U32 dimz = unpackBytes(header.m_dimZ[0], header.m_dimZ[1], header.m_dimZ[2], 0);
  703. if(dimx != inWidth || dimy != inHeight || dimz != 1)
  704. {
  705. ANKI_IMPORTER_LOGE("astcenc-avx2 with wrong image size");
  706. return Error::kFunctionFailed;
  707. }
  708. ANKI_CHECK(astcFile.read(outPixels.getBegin(), outPixels.getSizeInBytes()));
  709. return Error::kNone;
  710. }
  711. static Error storeAnkiImage(const ImageImporterConfig& config, const ImageImporterContext& ctx)
  712. {
  713. ANKI_IMPORTER_LOGV("Storing to %s", config.m_outFilename.cstr());
  714. File outFile;
  715. ANKI_CHECK(outFile.open(config.m_outFilename, FileOpenFlag::kBinary | FileOpenFlag::kWrite));
  716. // Header
  717. ImageBinaryHeader header = {};
  718. memcpy(&header.m_magic[0], &kImageMagic[0], sizeof(header.m_magic));
  719. header.m_width = ctx.m_width;
  720. header.m_height = ctx.m_height;
  721. header.m_depthOrLayerCount = max(ctx.m_layerCount, ctx.m_depth);
  722. header.m_type = config.m_type;
  723. if(ctx.m_hdr)
  724. {
  725. header.m_colorFormat = (ctx.m_channelCount == 3) ? ImageBinaryColorFormat::kRgbFloat : ImageBinaryColorFormat::kRgbaFloat;
  726. }
  727. else
  728. {
  729. header.m_colorFormat = (ctx.m_channelCount == 3) ? ImageBinaryColorFormat::kRgb8 : ImageBinaryColorFormat::kRgba8;
  730. }
  731. header.m_compressionMask = config.m_compressions;
  732. header.m_isNormal = false;
  733. header.m_mipmapCount = U8(ctx.m_mipmaps.getSize());
  734. header.m_astcBlockSizeX = config.m_astcBlockSize.x;
  735. header.m_astcBlockSizeY = config.m_astcBlockSize.y;
  736. header.m_averageColor = {ctx.m_averageColor.x, ctx.m_averageColor.y, ctx.m_averageColor.z, ctx.m_averageColor.w};
  737. ANKI_CHECK(outFile.write(&header, sizeof(header)));
  738. // Write RAW
  739. if(!!(config.m_compressions & ImageBinaryDataCompression::kRaw))
  740. {
  741. ANKI_IMPORTER_LOGV("Storing RAW");
  742. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  743. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  744. {
  745. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  746. {
  747. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  748. {
  749. const U32 idx = l * ctx.m_faceCount + f;
  750. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_pixels;
  751. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  752. }
  753. }
  754. }
  755. }
  756. // Write S3TC
  757. if(!!(config.m_compressions & ImageBinaryDataCompression::kS3tc))
  758. {
  759. ANKI_IMPORTER_LOGV("Storing S3TC");
  760. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  761. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  762. {
  763. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  764. {
  765. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  766. {
  767. const U32 idx = l * ctx.m_faceCount + f;
  768. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_s3tcPixels;
  769. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  770. }
  771. }
  772. }
  773. }
  774. // Write ASTC
  775. if(!!(config.m_compressions & ImageBinaryDataCompression::kAstc))
  776. {
  777. ANKI_IMPORTER_LOGV("Storing ASTC");
  778. // for(I32 mip = I32(ctx.m_mipmaps.getSize()) - 1; mip >= 0; --mip)
  779. for(U32 mip = 0; mip < ctx.m_mipmaps.getSize(); ++mip)
  780. {
  781. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  782. {
  783. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  784. {
  785. const U32 idx = l * ctx.m_faceCount + f;
  786. const ConstWeakArray<U8, PtrSize> pixels = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx].m_astcPixels;
  787. ANKI_CHECK(outFile.write(&pixels[0], pixels.getSizeInBytes()));
  788. }
  789. }
  790. }
  791. }
  792. return Error::kNone;
  793. }
  794. static Error importImageInternal(const ImageImporterConfig& configOriginal)
  795. {
  796. ImageImporterConfig config = configOriginal;
  797. config.m_minMipmapDimension = max(config.m_minMipmapDimension, 4u);
  798. if(!!(config.m_compressions & ImageBinaryDataCompression::kAstc))
  799. {
  800. config.m_minMipmapDimension = max(config.m_minMipmapDimension, config.m_astcBlockSize.x);
  801. config.m_minMipmapDimension = max(config.m_minMipmapDimension, config.m_astcBlockSize.y);
  802. }
  803. // Checks
  804. ANKI_CHECK(checkConfig(config));
  805. U32 width, height, channelCount;
  806. Bool isHdr;
  807. ANKI_CHECK(checkInputImages(config, width, height, channelCount, isHdr));
  808. // Resize
  809. ImporterDynamicArray<ImporterString> newFilenames;
  810. ImporterDynamicArray<CString> newFilenamesCString;
  811. ImporterDynamicArray<CleanupFile> resizedImagesCleanup;
  812. if(width < config.m_minMipmapDimension || height < config.m_minMipmapDimension)
  813. {
  814. const U32 newWidth = max(width, config.m_minMipmapDimension);
  815. const U32 newHeight = max(height, config.m_minMipmapDimension);
  816. ANKI_IMPORTER_LOGV("Image is smaller than the min mipmap dimension. Will resize it to %ux%u", newWidth, newHeight);
  817. newFilenames.resize(config.m_inputFilenames.getSize());
  818. newFilenamesCString.resize(config.m_inputFilenames.getSize());
  819. for(U32 i = 0; i < config.m_inputFilenames.getSize(); ++i)
  820. {
  821. ANKI_CHECK(resizeImage(config.m_inputFilenames[i], newWidth, newHeight, config.m_tempDirectory, newFilenames[i]));
  822. newFilenamesCString[i] = newFilenames[i];
  823. resizedImagesCleanup.emplaceBack(newFilenames[i]);
  824. }
  825. // Override config
  826. config.m_inputFilenames = newFilenamesCString;
  827. width = newWidth;
  828. height = newHeight;
  829. }
  830. // Init image
  831. ImageImporterContext ctx;
  832. ctx.m_width = width;
  833. ctx.m_height = height;
  834. ctx.m_depth = (config.m_type == ImageBinaryType::k3D) ? config.m_inputFilenames.getSize() : 1;
  835. ctx.m_faceCount = (config.m_type == ImageBinaryType::kCube) ? 6 : 1;
  836. ctx.m_layerCount = (config.m_type == ImageBinaryType::k2DArray) ? config.m_inputFilenames.getSize() : 1;
  837. ctx.m_hdr = isHdr;
  838. U32 desiredChannelCount;
  839. if(isHdr && !!(config.m_compressions & ImageBinaryDataCompression::kS3tc))
  840. {
  841. // BC6H doesn't have a 4th channel
  842. if(channelCount != 3)
  843. {
  844. ANKI_IMPORTER_LOGW("Input images have alpha but that can't be supported with BC6H");
  845. }
  846. if(!!(config.m_compressions & ImageBinaryDataCompression::kRaw))
  847. {
  848. ANKI_IMPORTER_LOGE("Can't support both BC6H (which is 3 component) and RAW which requires 4 compoments");
  849. return Error::kUserData;
  850. }
  851. desiredChannelCount = 3;
  852. }
  853. else if(!!(config.m_compressions & ImageBinaryDataCompression::kRaw))
  854. {
  855. // Always ask for 4 components because desktop GPUs don't always like 3
  856. desiredChannelCount = 4;
  857. }
  858. else if(config.m_noAlpha || channelCount == 1)
  859. {
  860. // no alpha or 1 component grey
  861. desiredChannelCount = 3;
  862. }
  863. else if(channelCount == 2)
  864. {
  865. // grey with alpha
  866. desiredChannelCount = 4;
  867. }
  868. else
  869. {
  870. desiredChannelCount = channelCount;
  871. }
  872. ctx.m_channelCount = desiredChannelCount;
  873. ctx.m_pixelSize = ctx.m_channelCount * U32((isHdr) ? sizeof(F32) : sizeof(U8));
  874. // Load first mip from the files
  875. ANKI_CHECK(loadFirstMipmap(config, ctx));
  876. // Generate mipmaps
  877. const U8 mipCount = min(config.m_mipmapCount, (config.m_type == ImageBinaryType::k3D)
  878. ? computeMaxMipmapCount3d(width, height, ctx.m_depth, config.m_minMipmapDimension)
  879. : computeMaxMipmapCount2d(width, height, config.m_minMipmapDimension));
  880. for(U8 mip = 1; mip < mipCount; ++mip)
  881. {
  882. ctx.m_mipmaps.emplaceBack();
  883. if(config.m_type != ImageBinaryType::k3D)
  884. {
  885. ctx.m_mipmaps[mip].m_surfacesOrVolume.resize(ctx.m_faceCount * ctx.m_layerCount);
  886. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  887. {
  888. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  889. {
  890. const U32 idx = l * ctx.m_faceCount + f;
  891. const SurfaceOrVolumeData& inSurface = ctx.m_mipmaps[mip - 1].m_surfacesOrVolume[idx];
  892. SurfaceOrVolumeData& outSurface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  893. outSurface.m_pixels.resize((ctx.m_width >> mip) * (ctx.m_height >> mip) * ctx.m_pixelSize);
  894. if(ctx.m_channelCount == 3)
  895. {
  896. if(ctx.m_hdr)
  897. {
  898. generateSurfaceMipmap<Vec3>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels), ctx.m_width >> (mip - 1),
  899. ctx.m_height >> (mip - 1), WeakArray<U8, PtrSize>(outSurface.m_pixels));
  900. }
  901. else
  902. {
  903. generateSurfaceMipmap<U8Vec3>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels), ctx.m_width >> (mip - 1),
  904. ctx.m_height >> (mip - 1), WeakArray<U8, PtrSize>(outSurface.m_pixels));
  905. }
  906. }
  907. else
  908. {
  909. ANKI_ASSERT(ctx.m_channelCount == 4);
  910. if(ctx.m_hdr)
  911. {
  912. generateSurfaceMipmap<Vec4>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels), ctx.m_width >> (mip - 1),
  913. ctx.m_height >> (mip - 1), WeakArray<U8, PtrSize>(outSurface.m_pixels));
  914. }
  915. else
  916. {
  917. generateSurfaceMipmap<U8Vec4>(ConstWeakArray<U8, PtrSize>(inSurface.m_pixels), ctx.m_width >> (mip - 1),
  918. ctx.m_height >> (mip - 1), WeakArray<U8, PtrSize>(outSurface.m_pixels));
  919. }
  920. }
  921. }
  922. }
  923. }
  924. else
  925. {
  926. ANKI_ASSERT(!"TODO");
  927. }
  928. }
  929. // Compress
  930. if(!!(config.m_compressions & ImageBinaryDataCompression::kS3tc))
  931. {
  932. ANKI_IMPORTER_LOGV("Will compress in S3TC");
  933. for(U32 mip = 0; mip < mipCount; ++mip)
  934. {
  935. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  936. {
  937. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  938. {
  939. const U32 idx = l * ctx.m_faceCount + f;
  940. SurfaceOrVolumeData& surface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  941. const U32 width = ctx.m_width >> mip;
  942. const U32 height = ctx.m_height >> mip;
  943. const PtrSize blockSize = (ctx.m_hdr || ctx.m_channelCount == 4) ? 16 : 8;
  944. const PtrSize s3tcImageSize = blockSize * (width / 4) * (height / 4);
  945. surface.m_s3tcPixels.resize(s3tcImageSize);
  946. ANKI_CHECK(compressS3tc(config.m_tempDirectory, config.m_compressonatorFilename, ConstWeakArray<U8, PtrSize>(surface.m_pixels),
  947. width, height, ctx.m_channelCount, ctx.m_hdr, WeakArray<U8, PtrSize>(surface.m_s3tcPixels)));
  948. }
  949. }
  950. }
  951. }
  952. if(!!(config.m_compressions & ImageBinaryDataCompression::kAstc))
  953. {
  954. ANKI_IMPORTER_LOGV("Will compress in ASTC");
  955. for(U32 mip = 0; mip < mipCount; ++mip)
  956. {
  957. for(U32 l = 0; l < ctx.m_layerCount; ++l)
  958. {
  959. for(U32 f = 0; f < ctx.m_faceCount; ++f)
  960. {
  961. const U32 idx = l * ctx.m_faceCount + f;
  962. SurfaceOrVolumeData& surface = ctx.m_mipmaps[mip].m_surfacesOrVolume[idx];
  963. const U32 width = ctx.m_width >> mip;
  964. const U32 height = ctx.m_height >> mip;
  965. const PtrSize blockSize = 16;
  966. const PtrSize astcImageSize = blockSize * (width / config.m_astcBlockSize.x) * (height / config.m_astcBlockSize.y);
  967. surface.m_astcPixels.resize(astcImageSize);
  968. ANKI_CHECK(compressAstc(config.m_tempDirectory, config.m_astcencFilename, ConstWeakArray<U8, PtrSize>(surface.m_pixels), width,
  969. height, ctx.m_channelCount, config.m_astcBlockSize, ctx.m_hdr,
  970. WeakArray<U8, PtrSize>(surface.m_astcPixels)));
  971. }
  972. }
  973. }
  974. }
  975. if(!!(config.m_compressions & ImageBinaryDataCompression::kEtc))
  976. {
  977. ANKI_ASSERT(!"TODO");
  978. }
  979. // Store the image
  980. ANKI_CHECK(storeAnkiImage(config, ctx));
  981. return Error::kNone;
  982. }
  983. Error importImage(const ImageImporterConfig& config)
  984. {
  985. const Error err = importImageInternal(config);
  986. if(err)
  987. {
  988. ANKI_IMPORTER_LOGE("Image importing failed: %s", config.m_inputFilenames[0].cstr());
  989. return err;
  990. }
  991. return Error::kNone;
  992. }
  993. } // end namespace anki