ImageLoader.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. // Copyright (C) 2009-2021, 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/Resource/ImageLoader.h>
  6. #include <AnKi/Resource/Stb.h>
  7. #include <AnKi/Util/Logger.h>
  8. #include <AnKi/Util/Filesystem.h>
  9. namespace anki
  10. {
  11. static const U8 tgaHeaderUncompressed[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  12. static const U8 tgaHeaderCompressed[12] = {0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  13. /// Get the size in bytes of a single surface
  14. static PtrSize calcSurfaceSize(const U32 width32, const U32 height32, const ImageBinaryDataCompression comp,
  15. const ImageBinaryColorFormat cf)
  16. {
  17. const PtrSize width = width32;
  18. const PtrSize height = height32;
  19. PtrSize out = 0;
  20. ANKI_ASSERT(width >= 4 || height >= 4);
  21. switch(comp)
  22. {
  23. case ImageBinaryDataCompression::RAW:
  24. out = width * height * ((cf == ImageBinaryColorFormat::RGB8) ? 3 : 4);
  25. break;
  26. case ImageBinaryDataCompression::S3TC:
  27. out = (width / 4) * (height / 4) * ((cf == ImageBinaryColorFormat::RGB8) ? 8 : 16); // block size
  28. break;
  29. case ImageBinaryDataCompression::ETC:
  30. out = (width / 4) * (height / 4) * 8;
  31. break;
  32. default:
  33. ANKI_ASSERT(0);
  34. }
  35. ANKI_ASSERT(out > 0);
  36. return out;
  37. }
  38. /// Get the size in bytes of a single volume
  39. static PtrSize calcVolumeSize(const U width, const U height, const U depth, const ImageBinaryDataCompression comp,
  40. const ImageBinaryColorFormat cf)
  41. {
  42. PtrSize out = 0;
  43. ANKI_ASSERT(width >= 4 || height >= 4 || depth >= 4);
  44. switch(comp)
  45. {
  46. case ImageBinaryDataCompression::RAW:
  47. out = width * height * depth * ((cf == ImageBinaryColorFormat::RGB8) ? 3 : 4);
  48. break;
  49. default:
  50. ANKI_ASSERT(0);
  51. }
  52. ANKI_ASSERT(out > 0);
  53. return out;
  54. }
  55. /// Calculate the size of a compressed or uncomressed color data
  56. static PtrSize calcSizeOfSegment(const ImageBinaryHeader& header, ImageBinaryDataCompression comp)
  57. {
  58. PtrSize out = 0;
  59. U32 width = header.m_width;
  60. U32 height = header.m_height;
  61. U32 mips = header.m_mipmapCount;
  62. ANKI_ASSERT(mips > 0);
  63. if(header.m_type != ImageBinaryType::_3D)
  64. {
  65. U32 surfCountPerMip = 0;
  66. switch(header.m_type)
  67. {
  68. case ImageBinaryType::_2D:
  69. surfCountPerMip = 1;
  70. break;
  71. case ImageBinaryType::CUBE:
  72. surfCountPerMip = 6;
  73. break;
  74. case ImageBinaryType::_2D_ARRAY:
  75. surfCountPerMip = header.m_depthOrLayerCount;
  76. break;
  77. default:
  78. ANKI_ASSERT(0);
  79. break;
  80. }
  81. while(mips-- != 0)
  82. {
  83. out += calcSurfaceSize(width, height, comp, header.m_colorFormat) * surfCountPerMip;
  84. width /= 2;
  85. height /= 2;
  86. }
  87. }
  88. else
  89. {
  90. U depth = header.m_depthOrLayerCount;
  91. while(mips-- != 0)
  92. {
  93. out += calcVolumeSize(width, height, depth, comp, header.m_colorFormat);
  94. width /= 2;
  95. height /= 2;
  96. depth /= 2;
  97. }
  98. }
  99. return out;
  100. }
  101. class ImageLoader::FileInterface
  102. {
  103. public:
  104. virtual ANKI_USE_RESULT Error read(void* buff, PtrSize size) = 0;
  105. virtual ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) = 0;
  106. virtual PtrSize getSize() const
  107. {
  108. ANKI_ASSERT(!"Not Implemented");
  109. return MAX_PTR_SIZE;
  110. }
  111. };
  112. class ImageLoader::RsrcFile : public FileInterface
  113. {
  114. public:
  115. ResourceFilePtr m_rfile;
  116. ANKI_USE_RESULT Error read(void* buff, PtrSize size) final
  117. {
  118. return m_rfile->read(buff, size);
  119. }
  120. ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) final
  121. {
  122. return m_rfile->seek(offset, origin);
  123. }
  124. PtrSize getSize() const final
  125. {
  126. return m_rfile->getSize();
  127. }
  128. };
  129. class ImageLoader::SystemFile : public FileInterface
  130. {
  131. public:
  132. File m_file;
  133. ANKI_USE_RESULT Error read(void* buff, PtrSize size) final
  134. {
  135. return m_file.read(buff, size);
  136. }
  137. ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) final
  138. {
  139. return m_file.seek(offset, origin);
  140. }
  141. PtrSize getSize() const final
  142. {
  143. return m_file.getSize();
  144. }
  145. };
  146. Error ImageLoader::loadUncompressedTga(FileInterface& fs, U32& width, U32& height, U32& bpp, DynamicArray<U8>& data,
  147. GenericMemoryPoolAllocator<U8>& alloc)
  148. {
  149. Array<U8, 6> header6;
  150. // read the info from header
  151. ANKI_CHECK(fs.read((char*)&header6[0], sizeof(header6)));
  152. width = header6[1] * 256 + header6[0];
  153. height = header6[3] * 256 + header6[2];
  154. bpp = header6[4];
  155. if((width == 0) || (height == 0) || ((bpp != 24) && (bpp != 32)))
  156. {
  157. ANKI_RESOURCE_LOGE("Invalid image information");
  158. return Error::USER_DATA;
  159. }
  160. // read the data
  161. U32 bytesPerPxl = (bpp / 8);
  162. U32 imageSize = bytesPerPxl * width * height;
  163. data.create(alloc, imageSize);
  164. ANKI_CHECK(fs.read(reinterpret_cast<char*>(&data[0]), imageSize));
  165. // swap red with blue
  166. for(U32 i = 0; i < imageSize; i += bytesPerPxl)
  167. {
  168. U8 temp = data[i];
  169. data[i] = data[i + 2];
  170. data[i + 2] = temp;
  171. }
  172. return Error::NONE;
  173. }
  174. Error ImageLoader::loadCompressedTga(FileInterface& fs, U32& width, U32& height, U32& bpp, DynamicArray<U8>& data,
  175. GenericMemoryPoolAllocator<U8>& alloc)
  176. {
  177. Array<U8, 6> header6;
  178. ANKI_CHECK(fs.read(reinterpret_cast<char*>(&header6[0]), sizeof(header6)));
  179. width = header6[1] * 256 + header6[0];
  180. height = header6[3] * 256 + header6[2];
  181. bpp = header6[4];
  182. if((width <= 0) || (height <= 0) || ((bpp != 24) && (bpp != 32)))
  183. {
  184. ANKI_RESOURCE_LOGE("Invalid image information");
  185. return Error::USER_DATA;
  186. }
  187. U32 bytesPerPxl = (bpp / 8);
  188. U32 imageSize = bytesPerPxl * width * height;
  189. data.create(alloc, imageSize);
  190. U32 pixelcount = height * width;
  191. U32 currentpixel = 0;
  192. U32 currentbyte = 0;
  193. U8 colorbuffer[4];
  194. do
  195. {
  196. U8 chunkheader = 0;
  197. ANKI_CHECK(fs.read(&chunkheader, sizeof(U8)));
  198. if(chunkheader < 128)
  199. {
  200. chunkheader++;
  201. for(int counter = 0; counter < chunkheader; counter++)
  202. {
  203. ANKI_CHECK(fs.read((char*)&colorbuffer[0], bytesPerPxl));
  204. data[currentbyte] = colorbuffer[2];
  205. data[currentbyte + 1] = colorbuffer[1];
  206. data[currentbyte + 2] = colorbuffer[0];
  207. if(bytesPerPxl == 4)
  208. {
  209. data[currentbyte + 3] = colorbuffer[3];
  210. }
  211. currentbyte += bytesPerPxl;
  212. currentpixel++;
  213. if(currentpixel > pixelcount)
  214. {
  215. ANKI_RESOURCE_LOGE("Too many pixels read");
  216. return Error::USER_DATA;
  217. }
  218. }
  219. }
  220. else
  221. {
  222. chunkheader = U8(chunkheader - 127);
  223. ANKI_CHECK(fs.read(&colorbuffer[0], bytesPerPxl));
  224. for(U32 counter = 0; counter < chunkheader; counter++)
  225. {
  226. data[currentbyte] = colorbuffer[2];
  227. data[currentbyte + 1] = colorbuffer[1];
  228. data[currentbyte + 2] = colorbuffer[0];
  229. if(bytesPerPxl == 4)
  230. {
  231. data[currentbyte + 3] = colorbuffer[3];
  232. }
  233. currentbyte += bytesPerPxl;
  234. currentpixel++;
  235. if(currentpixel > pixelcount)
  236. {
  237. ANKI_RESOURCE_LOGE("Too many pixels read");
  238. data.destroy(alloc);
  239. return Error::USER_DATA;
  240. }
  241. }
  242. }
  243. } while(currentpixel < pixelcount);
  244. return Error::NONE;
  245. }
  246. Error ImageLoader::loadTga(FileInterface& fs, U32& width, U32& height, U32& bpp, DynamicArray<U8>& data,
  247. GenericMemoryPoolAllocator<U8>& alloc)
  248. {
  249. char myTgaHeader[12];
  250. ANKI_CHECK(fs.read(&myTgaHeader[0], sizeof(myTgaHeader)));
  251. if(memcmp(tgaHeaderUncompressed, &myTgaHeader[0], sizeof(myTgaHeader)) == 0)
  252. {
  253. ANKI_CHECK(loadUncompressedTga(fs, width, height, bpp, data, alloc));
  254. }
  255. else if(std::memcmp(tgaHeaderCompressed, &myTgaHeader[0], sizeof(myTgaHeader)) == 0)
  256. {
  257. ANKI_CHECK(loadCompressedTga(fs, width, height, bpp, data, alloc));
  258. }
  259. else
  260. {
  261. ANKI_RESOURCE_LOGE("Invalid image header");
  262. return Error::USER_DATA;
  263. }
  264. if(bpp != 32 && bpp != 24)
  265. {
  266. ANKI_RESOURCE_LOGE("Invalid bpp");
  267. return Error::USER_DATA;
  268. }
  269. return Error::NONE;
  270. }
  271. Error ImageLoader::loadAnkiImage(FileInterface& file, U32 maxImageSize,
  272. ImageBinaryDataCompression& preferredCompression,
  273. DynamicArray<ImageLoaderSurface>& surfaces, DynamicArray<ImageLoaderVolume>& volumes,
  274. GenericMemoryPoolAllocator<U8>& alloc, U32& width, U32& height, U32& depth,
  275. U32& layerCount, U32& mipCount, ImageBinaryType& imageType,
  276. ImageBinaryColorFormat& colorFormat)
  277. {
  278. //
  279. // Read and check the header
  280. //
  281. ImageBinaryHeader header;
  282. ANKI_CHECK(file.read(&header, sizeof(ImageBinaryHeader)));
  283. if(std::memcmp(&header.m_magic[0], IMAGE_MAGIC, sizeof(IMAGE_MAGIC - 1)) != 0)
  284. {
  285. ANKI_RESOURCE_LOGE("Wrong magic word");
  286. return Error::USER_DATA;
  287. }
  288. if(header.m_width == 0 || !isPowerOfTwo(header.m_width) || header.m_width > 4096 || header.m_height == 0
  289. || !isPowerOfTwo(header.m_height) || header.m_height > 4096)
  290. {
  291. ANKI_RESOURCE_LOGE("Incorrect width/height value");
  292. return Error::USER_DATA;
  293. }
  294. if(header.m_depthOrLayerCount < 1 || header.m_depthOrLayerCount > 4096)
  295. {
  296. ANKI_RESOURCE_LOGE("Zero or too big depth or layerCount");
  297. return Error::USER_DATA;
  298. }
  299. if(header.m_type < ImageBinaryType::_2D || header.m_type > ImageBinaryType::_2D_ARRAY)
  300. {
  301. ANKI_RESOURCE_LOGE("Incorrect header: image type");
  302. return Error::USER_DATA;
  303. }
  304. if(header.m_colorFormat < ImageBinaryColorFormat::RGB8 || header.m_colorFormat > ImageBinaryColorFormat::RGBA8)
  305. {
  306. ANKI_RESOURCE_LOGE("Incorrect header: color format");
  307. return Error::USER_DATA;
  308. }
  309. if((header.m_compressionMask & preferredCompression) == ImageBinaryDataCompression::NONE)
  310. {
  311. // Fallback
  312. preferredCompression = ImageBinaryDataCompression::RAW;
  313. if((header.m_compressionMask & preferredCompression) == ImageBinaryDataCompression::NONE)
  314. {
  315. ANKI_RESOURCE_LOGE("File does not contain raw compression");
  316. return Error::USER_DATA;
  317. }
  318. }
  319. if(header.m_isNormal != 0 && header.m_isNormal != 1)
  320. {
  321. ANKI_RESOURCE_LOGE("Incorrect header: normal");
  322. return Error::USER_DATA;
  323. }
  324. // Set a few things
  325. colorFormat = header.m_colorFormat;
  326. imageType = header.m_type;
  327. U32 faceCount = 1;
  328. switch(header.m_type)
  329. {
  330. case ImageBinaryType::_2D:
  331. depth = 1;
  332. layerCount = 1;
  333. break;
  334. case ImageBinaryType::CUBE:
  335. depth = 1;
  336. layerCount = 1;
  337. faceCount = 6;
  338. break;
  339. case ImageBinaryType::_3D:
  340. depth = header.m_depthOrLayerCount;
  341. layerCount = 1;
  342. break;
  343. case ImageBinaryType::_2D_ARRAY:
  344. depth = 1;
  345. layerCount = header.m_depthOrLayerCount;
  346. break;
  347. default:
  348. ANKI_ASSERT(0);
  349. }
  350. //
  351. // Move file pointer
  352. //
  353. if(preferredCompression == ImageBinaryDataCompression::RAW)
  354. {
  355. // Do nothing
  356. }
  357. else if(preferredCompression == ImageBinaryDataCompression::S3TC)
  358. {
  359. if((header.m_compressionMask & ImageBinaryDataCompression::RAW) != ImageBinaryDataCompression::NONE)
  360. {
  361. // If raw compression is present then skip it
  362. ANKI_CHECK(file.seek(calcSizeOfSegment(header, ImageBinaryDataCompression::RAW), FileSeekOrigin::CURRENT));
  363. }
  364. }
  365. else if(preferredCompression == ImageBinaryDataCompression::ETC)
  366. {
  367. if((header.m_compressionMask & ImageBinaryDataCompression::RAW) != ImageBinaryDataCompression::NONE)
  368. {
  369. // If raw compression is present then skip it
  370. ANKI_CHECK(file.seek(calcSizeOfSegment(header, ImageBinaryDataCompression::RAW), FileSeekOrigin::CURRENT));
  371. }
  372. if((header.m_compressionMask & ImageBinaryDataCompression::S3TC) != ImageBinaryDataCompression::NONE)
  373. {
  374. // If s3tc compression is present then skip it
  375. ANKI_CHECK(file.seek(calcSizeOfSegment(header, ImageBinaryDataCompression::S3TC), FileSeekOrigin::CURRENT));
  376. }
  377. }
  378. //
  379. // It's time to read
  380. //
  381. // Allocate the surfaces
  382. mipCount = 0;
  383. if(header.m_type != ImageBinaryType::_3D)
  384. {
  385. // Read all surfaces
  386. U32 mipWidth = header.m_width;
  387. U32 mipHeight = header.m_height;
  388. for(U32 mip = 0; mip < header.m_mipmapCount; mip++)
  389. {
  390. for(U32 l = 0; l < layerCount; l++)
  391. {
  392. for(U32 f = 0; f < faceCount; ++f)
  393. {
  394. const U32 dataSize =
  395. U32(calcSurfaceSize(mipWidth, mipHeight, preferredCompression, header.m_colorFormat));
  396. // Check if this mipmap can be skipped because of size
  397. if(max(mipWidth, mipHeight) <= maxImageSize || mip == header.m_mipmapCount - 1)
  398. {
  399. ImageLoaderSurface& surf = *surfaces.emplaceBack(alloc);
  400. surf.m_width = mipWidth;
  401. surf.m_height = mipHeight;
  402. surf.m_data.create(alloc, dataSize);
  403. ANKI_CHECK(file.read(&surf.m_data[0], dataSize));
  404. mipCount = max(header.m_mipmapCount - mip, mipCount);
  405. }
  406. else
  407. {
  408. ANKI_CHECK(file.seek(dataSize, FileSeekOrigin::CURRENT));
  409. }
  410. }
  411. }
  412. mipWidth /= 2;
  413. mipHeight /= 2;
  414. }
  415. width = surfaces[0].m_width;
  416. height = surfaces[0].m_height;
  417. depth = MAX_U32;
  418. }
  419. else
  420. {
  421. U32 mipWidth = header.m_width;
  422. U32 mipHeight = header.m_height;
  423. U32 mipDepth = header.m_depthOrLayerCount;
  424. for(U32 mip = 0; mip < header.m_mipmapCount; mip++)
  425. {
  426. const U32 dataSize =
  427. U32(calcVolumeSize(mipWidth, mipHeight, mipDepth, preferredCompression, header.m_colorFormat));
  428. // Check if this mipmap can be skipped because of size
  429. if(max(max(mipWidth, mipHeight), mipDepth) <= maxImageSize || mip == header.m_mipmapCount - 1)
  430. {
  431. ImageLoaderVolume& vol = *volumes.emplaceBack(alloc);
  432. vol.m_width = mipWidth;
  433. vol.m_height = mipHeight;
  434. vol.m_depth = mipDepth;
  435. vol.m_data.create(alloc, dataSize);
  436. ANKI_CHECK(file.read(&vol.m_data[0], dataSize));
  437. mipCount = max(header.m_mipmapCount - mip, mipCount);
  438. }
  439. else
  440. {
  441. ANKI_CHECK(file.seek(dataSize, FileSeekOrigin::CURRENT));
  442. }
  443. mipWidth /= 2;
  444. mipHeight /= 2;
  445. mipDepth /= 2;
  446. }
  447. width = volumes[0].m_width;
  448. height = volumes[0].m_height;
  449. depth = volumes[0].m_depth;
  450. }
  451. return Error::NONE;
  452. }
  453. Error ImageLoader::loadStb(FileInterface& fs, U32& width, U32& height, DynamicArray<U8>& data,
  454. GenericMemoryPoolAllocator<U8>& alloc)
  455. {
  456. // Read the file
  457. DynamicArrayAuto<U8> fileData = {alloc};
  458. const PtrSize fileSize = fs.getSize();
  459. fileData.create(U32(fileSize));
  460. ANKI_CHECK(fs.read(&fileData[0], fileSize));
  461. // Use STB to read the image
  462. int stbw, stbh, comp;
  463. U8* stbdata = reinterpret_cast<U8*>(stbi_load_from_memory(&fileData[0], I32(fileSize), &stbw, &stbh, &comp, 4));
  464. if(!stbdata)
  465. {
  466. ANKI_RESOURCE_LOGE("STB failed to read image");
  467. return Error::FUNCTION_FAILED;
  468. }
  469. // Store it
  470. width = U32(stbw);
  471. height = U32(stbh);
  472. data.create(alloc, width * height * 4);
  473. memcpy(&data[0], stbdata, data.getSize());
  474. // Cleanup
  475. stbi_image_free(stbdata);
  476. return Error::NONE;
  477. }
  478. Error ImageLoader::load(ResourceFilePtr rfile, const CString& filename, U32 maxImageSize)
  479. {
  480. RsrcFile file;
  481. file.m_rfile = rfile;
  482. const Error err = loadInternal(file, filename, maxImageSize);
  483. if(err)
  484. {
  485. ANKI_RESOURCE_LOGE("Failed to read image: %s", filename.cstr());
  486. }
  487. return err;
  488. }
  489. Error ImageLoader::load(const CString& filename, U32 maxImageSize)
  490. {
  491. SystemFile file;
  492. ANKI_CHECK(file.m_file.open(filename, FileOpenFlag::READ | FileOpenFlag::BINARY));
  493. const Error err = loadInternal(file, filename, maxImageSize);
  494. if(err)
  495. {
  496. ANKI_RESOURCE_LOGE("Failed to read image: %s", filename.cstr());
  497. }
  498. return err;
  499. }
  500. Error ImageLoader::loadInternal(FileInterface& file, const CString& filename, U32 maxImageSize)
  501. {
  502. // get the extension
  503. StringAuto ext(m_alloc);
  504. getFilepathExtension(filename, ext);
  505. if(ext.isEmpty())
  506. {
  507. ANKI_RESOURCE_LOGE("Failed to get filename extension");
  508. return Error::USER_DATA;
  509. }
  510. // load from this extension
  511. m_imageType = ImageBinaryType::_2D;
  512. m_compression = ImageBinaryDataCompression::RAW;
  513. if(ext == "tga")
  514. {
  515. m_surfaces.create(m_alloc, 1);
  516. m_mipmapCount = 1;
  517. m_depth = 1;
  518. m_layerCount = 1;
  519. U32 bpp = 0;
  520. ANKI_CHECK(loadTga(file, m_surfaces[0].m_width, m_surfaces[0].m_height, bpp, m_surfaces[0].m_data, m_alloc));
  521. m_width = m_surfaces[0].m_width;
  522. m_height = m_surfaces[0].m_height;
  523. if(bpp == 32)
  524. {
  525. m_colorFormat = ImageBinaryColorFormat::RGBA8;
  526. }
  527. else if(bpp == 24)
  528. {
  529. m_colorFormat = ImageBinaryColorFormat::RGB8;
  530. }
  531. else
  532. {
  533. ANKI_ASSERT(0);
  534. }
  535. }
  536. else if(ext == "ankitex")
  537. {
  538. #if 0
  539. compression = ImageBinaryDataCompression::RAW;
  540. #else
  541. m_compression = ImageBinaryDataCompression::S3TC;
  542. #endif
  543. ANKI_CHECK(loadAnkiImage(file, maxImageSize, m_compression, m_surfaces, m_volumes, m_alloc, m_width, m_height,
  544. m_depth, m_layerCount, m_mipmapCount, m_imageType, m_colorFormat));
  545. }
  546. else if(ext == "png" || ext == "jpg")
  547. {
  548. m_surfaces.create(m_alloc, 1);
  549. m_mipmapCount = 1;
  550. m_depth = 1;
  551. m_layerCount = 1;
  552. m_colorFormat = ImageBinaryColorFormat::RGBA8;
  553. ANKI_CHECK(loadStb(file, m_surfaces[0].m_width, m_surfaces[0].m_height, m_surfaces[0].m_data, m_alloc));
  554. m_width = m_surfaces[0].m_width;
  555. m_height = m_surfaces[0].m_height;
  556. }
  557. else
  558. {
  559. ANKI_RESOURCE_LOGE("Unsupported extension: %s", &ext[0]);
  560. return Error::USER_DATA;
  561. }
  562. return Error::NONE;
  563. }
  564. const ImageLoaderSurface& ImageLoader::getSurface(U32 level, U32 face, U32 layer) const
  565. {
  566. ANKI_ASSERT(level < m_mipmapCount);
  567. U32 idx = 0;
  568. switch(m_imageType)
  569. {
  570. case ImageBinaryType::_2D:
  571. idx = level;
  572. break;
  573. case ImageBinaryType::CUBE:
  574. ANKI_ASSERT(face < 6);
  575. idx = level * 6 + face;
  576. break;
  577. case ImageBinaryType::_3D:
  578. ANKI_ASSERT(0 && "Can't use that for 3D images");
  579. break;
  580. case ImageBinaryType::_2D_ARRAY:
  581. idx = level * m_layerCount + layer;
  582. break;
  583. default:
  584. ANKI_ASSERT(0);
  585. }
  586. return m_surfaces[idx];
  587. }
  588. const ImageLoaderVolume& ImageLoader::getVolume(U32 level) const
  589. {
  590. ANKI_ASSERT(m_imageType == ImageBinaryType::_3D);
  591. return m_volumes[level];
  592. }
  593. void ImageLoader::destroy()
  594. {
  595. for(ImageLoaderSurface& surf : m_surfaces)
  596. {
  597. surf.m_data.destroy(m_alloc);
  598. }
  599. m_surfaces.destroy(m_alloc);
  600. for(ImageLoaderVolume& v : m_volumes)
  601. {
  602. v.m_data.destroy(m_alloc);
  603. }
  604. m_volumes.destroy(m_alloc);
  605. }
  606. } // end namespace anki