ImageLoader.cpp 18 KB

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