ImageLoader.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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, UVec2& astcBlockSize)
  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. astcBlockSize = UVec2(header.m_astcBlockSizeX, header.m_astcBlockSizeY);
  342. U32 faceCount = 1;
  343. switch(header.m_type)
  344. {
  345. case ImageBinaryType::_2D:
  346. depth = 1;
  347. layerCount = 1;
  348. break;
  349. case ImageBinaryType::CUBE:
  350. depth = 1;
  351. layerCount = 1;
  352. faceCount = 6;
  353. break;
  354. case ImageBinaryType::_3D:
  355. depth = header.m_depthOrLayerCount;
  356. layerCount = 1;
  357. break;
  358. case ImageBinaryType::_2D_ARRAY:
  359. depth = 1;
  360. layerCount = header.m_depthOrLayerCount;
  361. break;
  362. default:
  363. ANKI_ASSERT(0);
  364. }
  365. //
  366. // Move file pointer
  367. //
  368. PtrSize skipSize = 0;
  369. if(preferredCompression == ImageBinaryDataCompression::RAW)
  370. {
  371. // Do nothing
  372. }
  373. else if(preferredCompression == ImageBinaryDataCompression::S3TC)
  374. {
  375. if(!!(header.m_compressionMask & ImageBinaryDataCompression::RAW))
  376. {
  377. // If raw compression is present then skip it
  378. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::RAW);
  379. }
  380. }
  381. else if(preferredCompression == ImageBinaryDataCompression::ETC)
  382. {
  383. if(!!(header.m_compressionMask & ImageBinaryDataCompression::RAW))
  384. {
  385. // If raw compression is present then skip it
  386. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::RAW);
  387. }
  388. if(!!(header.m_compressionMask & ImageBinaryDataCompression::S3TC))
  389. {
  390. // If s3tc compression is present then skip it
  391. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::S3TC);
  392. }
  393. }
  394. else if(preferredCompression == ImageBinaryDataCompression::ASTC)
  395. {
  396. if(!!(header.m_compressionMask & ImageBinaryDataCompression::RAW))
  397. {
  398. // If raw compression is present then skip it
  399. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::RAW);
  400. }
  401. if(!!(header.m_compressionMask & ImageBinaryDataCompression::S3TC))
  402. {
  403. // If s3tc compression is present then skip it
  404. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::S3TC);
  405. }
  406. if(!!(header.m_compressionMask & ImageBinaryDataCompression::ETC))
  407. {
  408. // If ETC compression is present then skip it
  409. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::ETC);
  410. }
  411. }
  412. if(skipSize)
  413. {
  414. ANKI_CHECK(file.seek(skipSize, FileSeekOrigin::CURRENT));
  415. }
  416. //
  417. // It's time to read
  418. //
  419. // Allocate the surfaces
  420. mipCount = 0;
  421. if(header.m_type != ImageBinaryType::_3D)
  422. {
  423. // Read all surfaces
  424. U32 mipWidth = header.m_width;
  425. U32 mipHeight = header.m_height;
  426. for(U32 mip = 0; mip < header.m_mipmapCount; mip++)
  427. {
  428. for(U32 l = 0; l < layerCount; l++)
  429. {
  430. for(U32 f = 0; f < faceCount; ++f)
  431. {
  432. const U32 dataSize =
  433. U32(calcSurfaceSize(mipWidth, mipHeight, preferredCompression, header.m_colorFormat,
  434. UVec2(header.m_astcBlockSizeX, header.m_astcBlockSizeY)));
  435. // Check if this mipmap can be skipped because of size
  436. if(max(mipWidth, mipHeight) <= maxImageSize || mip == header.m_mipmapCount - 1)
  437. {
  438. ImageLoaderSurface& surf = *surfaces.emplaceBack(alloc);
  439. surf.m_width = mipWidth;
  440. surf.m_height = mipHeight;
  441. surf.m_data.create(alloc, dataSize);
  442. ANKI_CHECK(file.read(&surf.m_data[0], dataSize));
  443. mipCount = max(header.m_mipmapCount - mip, mipCount);
  444. }
  445. else
  446. {
  447. ANKI_CHECK(file.seek(dataSize, FileSeekOrigin::CURRENT));
  448. }
  449. }
  450. }
  451. mipWidth /= 2;
  452. mipHeight /= 2;
  453. }
  454. width = surfaces[0].m_width;
  455. height = surfaces[0].m_height;
  456. depth = MAX_U32;
  457. }
  458. else
  459. {
  460. U32 mipWidth = header.m_width;
  461. U32 mipHeight = header.m_height;
  462. U32 mipDepth = header.m_depthOrLayerCount;
  463. for(U32 mip = 0; mip < header.m_mipmapCount; mip++)
  464. {
  465. const U32 dataSize =
  466. U32(calcVolumeSize(mipWidth, mipHeight, mipDepth, preferredCompression, header.m_colorFormat));
  467. // Check if this mipmap can be skipped because of size
  468. if(max(max(mipWidth, mipHeight), mipDepth) <= maxImageSize || mip == header.m_mipmapCount - 1)
  469. {
  470. ImageLoaderVolume& vol = *volumes.emplaceBack(alloc);
  471. vol.m_width = mipWidth;
  472. vol.m_height = mipHeight;
  473. vol.m_depth = mipDepth;
  474. vol.m_data.create(alloc, dataSize);
  475. ANKI_CHECK(file.read(&vol.m_data[0], dataSize));
  476. mipCount = max(header.m_mipmapCount - mip, mipCount);
  477. }
  478. else
  479. {
  480. ANKI_CHECK(file.seek(dataSize, FileSeekOrigin::CURRENT));
  481. }
  482. mipWidth /= 2;
  483. mipHeight /= 2;
  484. mipDepth /= 2;
  485. }
  486. width = volumes[0].m_width;
  487. height = volumes[0].m_height;
  488. depth = volumes[0].m_depth;
  489. }
  490. return Error::NONE;
  491. }
  492. Error ImageLoader::loadStb(FileInterface& fs, U32& width, U32& height, DynamicArray<U8>& data,
  493. GenericMemoryPoolAllocator<U8>& alloc)
  494. {
  495. // Read the file
  496. DynamicArrayAuto<U8> fileData = {alloc};
  497. const PtrSize fileSize = fs.getSize();
  498. fileData.create(U32(fileSize));
  499. ANKI_CHECK(fs.read(&fileData[0], fileSize));
  500. // Use STB to read the image
  501. int stbw, stbh, comp;
  502. stbi_set_flip_vertically_on_load_thread(true);
  503. U8* stbdata = reinterpret_cast<U8*>(stbi_load_from_memory(&fileData[0], I32(fileSize), &stbw, &stbh, &comp, 4));
  504. if(!stbdata)
  505. {
  506. ANKI_RESOURCE_LOGE("STB failed to read image");
  507. return Error::FUNCTION_FAILED;
  508. }
  509. // Store it
  510. width = U32(stbw);
  511. height = U32(stbh);
  512. data.create(alloc, width * height * 4);
  513. memcpy(&data[0], stbdata, data.getSize());
  514. // Cleanup
  515. stbi_image_free(stbdata);
  516. return Error::NONE;
  517. }
  518. Error ImageLoader::load(ResourceFilePtr rfile, const CString& filename, U32 maxImageSize)
  519. {
  520. RsrcFile file;
  521. file.m_rfile = rfile;
  522. const Error err = loadInternal(file, filename, maxImageSize);
  523. if(err)
  524. {
  525. ANKI_RESOURCE_LOGE("Failed to read image: %s", filename.cstr());
  526. }
  527. return err;
  528. }
  529. Error ImageLoader::load(const CString& filename, U32 maxImageSize)
  530. {
  531. SystemFile file;
  532. ANKI_CHECK(file.m_file.open(filename, FileOpenFlag::READ | FileOpenFlag::BINARY));
  533. const Error err = loadInternal(file, filename, maxImageSize);
  534. if(err)
  535. {
  536. ANKI_RESOURCE_LOGE("Failed to read image: %s", filename.cstr());
  537. }
  538. return err;
  539. }
  540. Error ImageLoader::loadInternal(FileInterface& file, const CString& filename, U32 maxImageSize)
  541. {
  542. // get the extension
  543. StringAuto ext(m_alloc);
  544. getFilepathExtension(filename, ext);
  545. if(ext.isEmpty())
  546. {
  547. ANKI_RESOURCE_LOGE("Failed to get filename extension");
  548. return Error::USER_DATA;
  549. }
  550. // load from this extension
  551. m_imageType = ImageBinaryType::_2D;
  552. m_compression = ImageBinaryDataCompression::RAW;
  553. if(ext == "tga")
  554. {
  555. m_surfaces.create(m_alloc, 1);
  556. m_mipmapCount = 1;
  557. m_depth = 1;
  558. m_layerCount = 1;
  559. U32 bpp = 0;
  560. ANKI_CHECK(loadTga(file, m_surfaces[0].m_width, m_surfaces[0].m_height, bpp, m_surfaces[0].m_data, m_alloc));
  561. m_width = m_surfaces[0].m_width;
  562. m_height = m_surfaces[0].m_height;
  563. if(bpp == 32)
  564. {
  565. m_colorFormat = ImageBinaryColorFormat::RGBA8;
  566. }
  567. else if(bpp == 24)
  568. {
  569. m_colorFormat = ImageBinaryColorFormat::RGB8;
  570. }
  571. else
  572. {
  573. ANKI_ASSERT(0);
  574. }
  575. }
  576. else if(ext == "ankitex")
  577. {
  578. #if ANKI_OS_ANDROID
  579. m_compression = ImageBinaryDataCompression::ASTC;
  580. #else
  581. m_compression = ImageBinaryDataCompression::S3TC;
  582. #endif
  583. ANKI_CHECK(loadAnkiImage(file, maxImageSize, m_compression, m_surfaces, m_volumes, m_alloc, m_width, m_height,
  584. m_depth, m_layerCount, m_mipmapCount, m_imageType, m_colorFormat, m_astcBlockSize));
  585. }
  586. else if(ext == "png" || ext == "jpg")
  587. {
  588. m_surfaces.create(m_alloc, 1);
  589. m_mipmapCount = 1;
  590. m_depth = 1;
  591. m_layerCount = 1;
  592. m_colorFormat = ImageBinaryColorFormat::RGBA8;
  593. ANKI_CHECK(loadStb(file, m_surfaces[0].m_width, m_surfaces[0].m_height, m_surfaces[0].m_data, m_alloc));
  594. m_width = m_surfaces[0].m_width;
  595. m_height = m_surfaces[0].m_height;
  596. }
  597. else
  598. {
  599. ANKI_RESOURCE_LOGE("Unsupported extension: %s", &ext[0]);
  600. return Error::USER_DATA;
  601. }
  602. return Error::NONE;
  603. }
  604. const ImageLoaderSurface& ImageLoader::getSurface(U32 level, U32 face, U32 layer) const
  605. {
  606. ANKI_ASSERT(level < m_mipmapCount);
  607. U32 idx = 0;
  608. switch(m_imageType)
  609. {
  610. case ImageBinaryType::_2D:
  611. idx = level;
  612. break;
  613. case ImageBinaryType::CUBE:
  614. ANKI_ASSERT(face < 6);
  615. idx = level * 6 + face;
  616. break;
  617. case ImageBinaryType::_3D:
  618. ANKI_ASSERT(0 && "Can't use that for 3D images");
  619. break;
  620. case ImageBinaryType::_2D_ARRAY:
  621. idx = level * m_layerCount + layer;
  622. break;
  623. default:
  624. ANKI_ASSERT(0);
  625. }
  626. return m_surfaces[idx];
  627. }
  628. const ImageLoaderVolume& ImageLoader::getVolume(U32 level) const
  629. {
  630. ANKI_ASSERT(m_imageType == ImageBinaryType::_3D);
  631. return m_volumes[level];
  632. }
  633. void ImageLoader::destroy()
  634. {
  635. for(ImageLoaderSurface& surf : m_surfaces)
  636. {
  637. surf.m_data.destroy(m_alloc);
  638. }
  639. m_surfaces.destroy(m_alloc);
  640. for(ImageLoaderVolume& v : m_volumes)
  641. {
  642. v.m_data.destroy(m_alloc);
  643. }
  644. m_volumes.destroy(m_alloc);
  645. }
  646. } // end namespace anki