ImageLoader.cpp 20 KB

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