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. static const U8 tgaHeaderUncompressed[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  11. static const U8 tgaHeaderCompressed[12] = {0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  12. /// Get the size in bytes of a single surface
  13. static PtrSize calcSurfaceSize(const U32 width32, const U32 height32, const ImageBinaryDataCompression comp,
  14. const ImageBinaryColorFormat cf, UVec2 astcBlockSize)
  15. {
  16. const PtrSize width = width32;
  17. const PtrSize height = height32;
  18. PtrSize out = 0;
  19. ANKI_ASSERT(width >= 4 || height >= 4);
  20. switch(comp)
  21. {
  22. case ImageBinaryDataCompression::RAW:
  23. out = width * height * ((cf == ImageBinaryColorFormat::RGB8) ? 3 : 4);
  24. break;
  25. case ImageBinaryDataCompression::S3TC:
  26. out = (width / 4) * (height / 4) * ((cf == ImageBinaryColorFormat::RGB8) ? 8 : 16); // block size
  27. break;
  28. case ImageBinaryDataCompression::ETC:
  29. out = (width / 4) * (height / 4) * 8;
  30. break;
  31. case ImageBinaryDataCompression::ASTC:
  32. out = (width / astcBlockSize.x()) * (height / astcBlockSize.y()) * 16;
  33. break;
  34. default:
  35. ANKI_ASSERT(0);
  36. }
  37. ANKI_ASSERT(out > 0);
  38. return out;
  39. }
  40. /// Get the size in bytes of a single volume
  41. static PtrSize calcVolumeSize(const U width, const U height, const U depth, const ImageBinaryDataCompression comp,
  42. const ImageBinaryColorFormat cf)
  43. {
  44. PtrSize out = 0;
  45. ANKI_ASSERT(width >= 4 || height >= 4 || depth >= 4);
  46. switch(comp)
  47. {
  48. case ImageBinaryDataCompression::RAW:
  49. out = width * height * depth * ((cf == ImageBinaryColorFormat::RGB8) ? 3 : 4);
  50. break;
  51. default:
  52. ANKI_ASSERT(0);
  53. }
  54. ANKI_ASSERT(out > 0);
  55. return out;
  56. }
  57. /// Calculate the size of a compressed or uncomressed color data
  58. static PtrSize calcSizeOfSegment(const ImageBinaryHeader& header, ImageBinaryDataCompression comp)
  59. {
  60. PtrSize out = 0;
  61. U32 width = header.m_width;
  62. U32 height = header.m_height;
  63. U32 mips = header.m_mipmapCount;
  64. ANKI_ASSERT(mips > 0);
  65. if(header.m_type != ImageBinaryType::_3D)
  66. {
  67. U32 surfCountPerMip = 0;
  68. switch(header.m_type)
  69. {
  70. case ImageBinaryType::_2D:
  71. surfCountPerMip = 1;
  72. break;
  73. case ImageBinaryType::CUBE:
  74. surfCountPerMip = 6;
  75. break;
  76. case ImageBinaryType::_2D_ARRAY:
  77. surfCountPerMip = header.m_depthOrLayerCount;
  78. break;
  79. default:
  80. ANKI_ASSERT(0);
  81. break;
  82. }
  83. while(mips-- != 0)
  84. {
  85. out += calcSurfaceSize(width, height, comp, header.m_colorFormat,
  86. UVec2(header.m_astcBlockSizeX, header.m_astcBlockSizeY))
  87. * surfCountPerMip;
  88. width /= 2;
  89. height /= 2;
  90. }
  91. }
  92. else
  93. {
  94. U depth = header.m_depthOrLayerCount;
  95. while(mips-- != 0)
  96. {
  97. out += calcVolumeSize(width, height, depth, comp, header.m_colorFormat);
  98. width /= 2;
  99. height /= 2;
  100. depth /= 2;
  101. }
  102. }
  103. return out;
  104. }
  105. class ImageLoader::FileInterface
  106. {
  107. public:
  108. virtual ANKI_USE_RESULT Error read(void* buff, PtrSize size) = 0;
  109. virtual ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) = 0;
  110. virtual PtrSize getSize() const
  111. {
  112. ANKI_ASSERT(!"Not Implemented");
  113. return MAX_PTR_SIZE;
  114. }
  115. };
  116. class ImageLoader::RsrcFile : public FileInterface
  117. {
  118. public:
  119. ResourceFilePtr m_rfile;
  120. ANKI_USE_RESULT Error read(void* buff, PtrSize size) final
  121. {
  122. return m_rfile->read(buff, size);
  123. }
  124. ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) final
  125. {
  126. return m_rfile->seek(offset, origin);
  127. }
  128. PtrSize getSize() const final
  129. {
  130. return m_rfile->getSize();
  131. }
  132. };
  133. class ImageLoader::SystemFile : public FileInterface
  134. {
  135. public:
  136. File m_file;
  137. ANKI_USE_RESULT Error read(void* buff, PtrSize size) final
  138. {
  139. return m_file.read(buff, size);
  140. }
  141. ANKI_USE_RESULT Error seek(PtrSize offset, FileSeekOrigin origin) final
  142. {
  143. return m_file.seek(offset, origin);
  144. }
  145. PtrSize getSize() const final
  146. {
  147. return m_file.getSize();
  148. }
  149. };
  150. Error ImageLoader::loadUncompressedTga(FileInterface& fs, U32& width, U32& height, U32& bpp, DynamicArray<U8>& data,
  151. GenericMemoryPoolAllocator<U8>& alloc)
  152. {
  153. Array<U8, 6> header6;
  154. // read the info from header
  155. ANKI_CHECK(fs.read((char*)&header6[0], sizeof(header6)));
  156. width = header6[1] * 256 + header6[0];
  157. height = header6[3] * 256 + header6[2];
  158. bpp = header6[4];
  159. if((width == 0) || (height == 0) || ((bpp != 24) && (bpp != 32)))
  160. {
  161. ANKI_RESOURCE_LOGE("Invalid image information");
  162. return Error::USER_DATA;
  163. }
  164. // read the data
  165. U32 bytesPerPxl = (bpp / 8);
  166. U32 imageSize = bytesPerPxl * width * height;
  167. data.create(alloc, imageSize);
  168. ANKI_CHECK(fs.read(reinterpret_cast<char*>(&data[0]), imageSize));
  169. // swap red with blue
  170. for(U32 i = 0; i < imageSize; i += bytesPerPxl)
  171. {
  172. U8 temp = data[i];
  173. data[i] = data[i + 2];
  174. data[i + 2] = temp;
  175. }
  176. return Error::NONE;
  177. }
  178. Error ImageLoader::loadCompressedTga(FileInterface& fs, U32& width, U32& height, U32& bpp, DynamicArray<U8>& data,
  179. GenericMemoryPoolAllocator<U8>& alloc)
  180. {
  181. Array<U8, 6> header6;
  182. ANKI_CHECK(fs.read(reinterpret_cast<char*>(&header6[0]), sizeof(header6)));
  183. width = header6[1] * 256 + header6[0];
  184. height = header6[3] * 256 + header6[2];
  185. bpp = header6[4];
  186. if((width <= 0) || (height <= 0) || ((bpp != 24) && (bpp != 32)))
  187. {
  188. ANKI_RESOURCE_LOGE("Invalid image information");
  189. return Error::USER_DATA;
  190. }
  191. U32 bytesPerPxl = (bpp / 8);
  192. U32 imageSize = bytesPerPxl * width * height;
  193. data.create(alloc, imageSize);
  194. U32 pixelcount = height * width;
  195. U32 currentpixel = 0;
  196. U32 currentbyte = 0;
  197. U8 colorbuffer[4];
  198. do
  199. {
  200. U8 chunkheader = 0;
  201. ANKI_CHECK(fs.read(&chunkheader, sizeof(U8)));
  202. if(chunkheader < 128)
  203. {
  204. chunkheader++;
  205. for(int counter = 0; counter < chunkheader; counter++)
  206. {
  207. ANKI_CHECK(fs.read((char*)&colorbuffer[0], bytesPerPxl));
  208. data[currentbyte] = colorbuffer[2];
  209. data[currentbyte + 1] = colorbuffer[1];
  210. data[currentbyte + 2] = colorbuffer[0];
  211. if(bytesPerPxl == 4)
  212. {
  213. data[currentbyte + 3] = colorbuffer[3];
  214. }
  215. currentbyte += bytesPerPxl;
  216. currentpixel++;
  217. if(currentpixel > pixelcount)
  218. {
  219. ANKI_RESOURCE_LOGE("Too many pixels read");
  220. return Error::USER_DATA;
  221. }
  222. }
  223. }
  224. else
  225. {
  226. chunkheader = U8(chunkheader - 127);
  227. ANKI_CHECK(fs.read(&colorbuffer[0], bytesPerPxl));
  228. for(U32 counter = 0; counter < chunkheader; counter++)
  229. {
  230. data[currentbyte] = colorbuffer[2];
  231. data[currentbyte + 1] = colorbuffer[1];
  232. data[currentbyte + 2] = colorbuffer[0];
  233. if(bytesPerPxl == 4)
  234. {
  235. data[currentbyte + 3] = colorbuffer[3];
  236. }
  237. currentbyte += bytesPerPxl;
  238. currentpixel++;
  239. if(currentpixel > pixelcount)
  240. {
  241. ANKI_RESOURCE_LOGE("Too many pixels read");
  242. data.destroy(alloc);
  243. return Error::USER_DATA;
  244. }
  245. }
  246. }
  247. } while(currentpixel < pixelcount);
  248. return Error::NONE;
  249. }
  250. Error ImageLoader::loadTga(FileInterface& fs, U32& width, U32& height, U32& bpp, DynamicArray<U8>& data,
  251. GenericMemoryPoolAllocator<U8>& alloc)
  252. {
  253. char myTgaHeader[12];
  254. ANKI_CHECK(fs.read(&myTgaHeader[0], sizeof(myTgaHeader)));
  255. if(memcmp(tgaHeaderUncompressed, &myTgaHeader[0], sizeof(myTgaHeader)) == 0)
  256. {
  257. ANKI_CHECK(loadUncompressedTga(fs, width, height, bpp, data, alloc));
  258. }
  259. else if(std::memcmp(tgaHeaderCompressed, &myTgaHeader[0], sizeof(myTgaHeader)) == 0)
  260. {
  261. ANKI_CHECK(loadCompressedTga(fs, width, height, bpp, data, alloc));
  262. }
  263. else
  264. {
  265. ANKI_RESOURCE_LOGE("Invalid image header");
  266. return Error::USER_DATA;
  267. }
  268. if(bpp != 32 && bpp != 24)
  269. {
  270. ANKI_RESOURCE_LOGE("Invalid bpp");
  271. return Error::USER_DATA;
  272. }
  273. return Error::NONE;
  274. }
  275. Error ImageLoader::loadAnkiImage(FileInterface& file, U32 maxImageSize,
  276. ImageBinaryDataCompression& preferredCompression,
  277. DynamicArray<ImageLoaderSurface>& surfaces, DynamicArray<ImageLoaderVolume>& volumes,
  278. GenericMemoryPoolAllocator<U8>& alloc, U32& width, U32& height, U32& depth,
  279. U32& layerCount, U32& mipCount, ImageBinaryType& imageType,
  280. ImageBinaryColorFormat& colorFormat, UVec2& astcBlockSize)
  281. {
  282. //
  283. // Read and check the header
  284. //
  285. ImageBinaryHeader header;
  286. ANKI_CHECK(file.read(&header, sizeof(ImageBinaryHeader)));
  287. if(std::memcmp(&header.m_magic[0], IMAGE_MAGIC, sizeof(IMAGE_MAGIC) - 1) != 0)
  288. {
  289. ANKI_RESOURCE_LOGE("Wrong magic word");
  290. return Error::USER_DATA;
  291. }
  292. if(header.m_width == 0 || !isPowerOfTwo(header.m_width) || header.m_width > 4096 || header.m_height == 0
  293. || !isPowerOfTwo(header.m_height) || header.m_height > 4096)
  294. {
  295. ANKI_RESOURCE_LOGE("Incorrect width/height value");
  296. return Error::USER_DATA;
  297. }
  298. if(header.m_depthOrLayerCount < 1 || header.m_depthOrLayerCount > 4096)
  299. {
  300. ANKI_RESOURCE_LOGE("Zero or too big depth or layerCount");
  301. return Error::USER_DATA;
  302. }
  303. if(header.m_type < ImageBinaryType::_2D || header.m_type > ImageBinaryType::_2D_ARRAY)
  304. {
  305. ANKI_RESOURCE_LOGE("Incorrect header: image type");
  306. return Error::USER_DATA;
  307. }
  308. if(header.m_colorFormat < ImageBinaryColorFormat::RGB8 || header.m_colorFormat > ImageBinaryColorFormat::RGBA8)
  309. {
  310. ANKI_RESOURCE_LOGE("Incorrect header: color format");
  311. return Error::USER_DATA;
  312. }
  313. if(!!(header.m_compressionMask & ImageBinaryDataCompression::ASTC))
  314. {
  315. if((header.m_astcBlockSizeX != 8 && header.m_astcBlockSizeX != 4)
  316. || (header.m_astcBlockSizeY != 8 && header.m_astcBlockSizeY != 4))
  317. {
  318. ANKI_RESOURCE_LOGE("Incorrect header: ASTC block size");
  319. return Error::USER_DATA;
  320. }
  321. }
  322. if(!(header.m_compressionMask & preferredCompression))
  323. {
  324. // Fallback
  325. preferredCompression = ImageBinaryDataCompression::RAW;
  326. if(!(header.m_compressionMask & preferredCompression))
  327. {
  328. ANKI_RESOURCE_LOGE("File does not contain raw compression");
  329. return Error::USER_DATA;
  330. }
  331. }
  332. if(header.m_isNormal != 0 && header.m_isNormal != 1)
  333. {
  334. ANKI_RESOURCE_LOGE("Incorrect header: normal");
  335. return Error::USER_DATA;
  336. }
  337. // Set a few things
  338. colorFormat = header.m_colorFormat;
  339. imageType = header.m_type;
  340. astcBlockSize = UVec2(header.m_astcBlockSizeX, header.m_astcBlockSizeY);
  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, m_astcBlockSize));
  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