ImageLoader.cpp 18 KB

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