ImageLoader.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. 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. static PtrSize calcRawTexelSize(const ImageBinaryColorFormat cf)
  13. {
  14. PtrSize out;
  15. switch(cf)
  16. {
  17. case ImageBinaryColorFormat::RGB8:
  18. out = 3;
  19. break;
  20. case ImageBinaryColorFormat::RGBA8:
  21. out = 4;
  22. break;
  23. case ImageBinaryColorFormat::SRGB8:
  24. out = 3;
  25. break;
  26. case ImageBinaryColorFormat::RGBF32:
  27. out = 3 * sizeof(F32);
  28. break;
  29. case ImageBinaryColorFormat::RGBAF32:
  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::RGB8:
  44. out = 8;
  45. break;
  46. case ImageBinaryColorFormat::RGBA8:
  47. out = 16;
  48. break;
  49. case ImageBinaryColorFormat::SRGB8:
  50. out = 8;
  51. break;
  52. case ImageBinaryColorFormat::RGBF32:
  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::RAW:
  72. out = width * height * calcRawTexelSize(cf);
  73. break;
  74. case ImageBinaryDataCompression::S3TC:
  75. out = (width / 4) * (height / 4) * calcS3tcBlockSize(cf);
  76. break;
  77. case ImageBinaryDataCompression::ETC:
  78. out = (width / 4) * (height / 4) * 8;
  79. break;
  80. case ImageBinaryDataCompression::ASTC:
  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::RAW:
  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::_3D)
  115. {
  116. U32 surfCountPerMip = 0;
  117. switch(header.m_type)
  118. {
  119. case ImageBinaryType::_2D:
  120. surfCountPerMip = 1;
  121. break;
  122. case ImageBinaryType::CUBE:
  123. surfCountPerMip = 6;
  124. break;
  125. case ImageBinaryType::_2D_ARRAY:
  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 MAX_PTR_SIZE;
  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::USER_DATA;
  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::NONE;
  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::USER_DATA;
  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::USER_DATA;
  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::USER_DATA;
  293. }
  294. }
  295. }
  296. } while(currentPixel < pixelCount);
  297. return Error::NONE;
  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(tgaHeaderUncompressed, &myTgaHeader[0], sizeof(myTgaHeader)) == 0)
  305. {
  306. ANKI_CHECK(loadUncompressedTga(fs, width, height, bpp, data, alloc));
  307. }
  308. else if(std::memcmp(tgaHeaderCompressed, &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::USER_DATA;
  316. }
  317. if(bpp != 32 && bpp != 24)
  318. {
  319. ANKI_RESOURCE_LOGE("Invalid bpp");
  320. return Error::USER_DATA;
  321. }
  322. return Error::NONE;
  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], IMAGE_MAGIC, sizeof(IMAGE_MAGIC) - 1) != 0)
  337. {
  338. ANKI_RESOURCE_LOGE("Wrong magic word");
  339. return Error::USER_DATA;
  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::USER_DATA;
  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::USER_DATA;
  351. }
  352. if(header.m_type < ImageBinaryType::_2D || header.m_type > ImageBinaryType::_2D_ARRAY)
  353. {
  354. ANKI_RESOURCE_LOGE("Incorrect header: image type");
  355. return Error::USER_DATA;
  356. }
  357. if(header.m_colorFormat < ImageBinaryColorFormat::RGB8 || header.m_colorFormat > ImageBinaryColorFormat::RGBAF32)
  358. {
  359. ANKI_RESOURCE_LOGE("Incorrect header: color format");
  360. return Error::USER_DATA;
  361. }
  362. if(!!(header.m_compressionMask & ImageBinaryDataCompression::ASTC))
  363. {
  364. if((header.m_astcBlockSizeX != 8 && header.m_astcBlockSizeX != 4)
  365. || (header.m_astcBlockSizeY != 8 && header.m_astcBlockSizeY != 4))
  366. {
  367. ANKI_RESOURCE_LOGE("Incorrect header: ASTC block size");
  368. return Error::USER_DATA;
  369. }
  370. }
  371. if(!(header.m_compressionMask & preferredCompression))
  372. {
  373. // Fallback
  374. preferredCompression = ImageBinaryDataCompression::RAW;
  375. if(!(header.m_compressionMask & preferredCompression))
  376. {
  377. ANKI_RESOURCE_LOGE("File does not contain raw compression");
  378. return Error::USER_DATA;
  379. }
  380. }
  381. if(header.m_isNormal != 0 && header.m_isNormal != 1)
  382. {
  383. ANKI_RESOURCE_LOGE("Incorrect header: normal");
  384. return Error::USER_DATA;
  385. }
  386. // Set a few things
  387. colorFormat = header.m_colorFormat;
  388. imageType = header.m_type;
  389. astcBlockSize = UVec2(header.m_astcBlockSizeX, header.m_astcBlockSizeY);
  390. U32 faceCount = 1;
  391. switch(header.m_type)
  392. {
  393. case ImageBinaryType::_2D:
  394. depth = 1;
  395. layerCount = 1;
  396. break;
  397. case ImageBinaryType::CUBE:
  398. depth = 1;
  399. layerCount = 1;
  400. faceCount = 6;
  401. break;
  402. case ImageBinaryType::_3D:
  403. depth = header.m_depthOrLayerCount;
  404. layerCount = 1;
  405. break;
  406. case ImageBinaryType::_2D_ARRAY:
  407. depth = 1;
  408. layerCount = header.m_depthOrLayerCount;
  409. break;
  410. default:
  411. ANKI_ASSERT(0);
  412. }
  413. //
  414. // Move file pointer
  415. //
  416. PtrSize skipSize = 0;
  417. if(preferredCompression == ImageBinaryDataCompression::RAW)
  418. {
  419. // Do nothing
  420. }
  421. else if(preferredCompression == ImageBinaryDataCompression::S3TC)
  422. {
  423. if(!!(header.m_compressionMask & ImageBinaryDataCompression::RAW))
  424. {
  425. // If raw compression is present then skip it
  426. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::RAW);
  427. }
  428. }
  429. else if(preferredCompression == ImageBinaryDataCompression::ETC)
  430. {
  431. if(!!(header.m_compressionMask & ImageBinaryDataCompression::RAW))
  432. {
  433. // If raw compression is present then skip it
  434. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::RAW);
  435. }
  436. if(!!(header.m_compressionMask & ImageBinaryDataCompression::S3TC))
  437. {
  438. // If s3tc compression is present then skip it
  439. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::S3TC);
  440. }
  441. }
  442. else if(preferredCompression == ImageBinaryDataCompression::ASTC)
  443. {
  444. if(!!(header.m_compressionMask & ImageBinaryDataCompression::RAW))
  445. {
  446. // If raw compression is present then skip it
  447. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::RAW);
  448. }
  449. if(!!(header.m_compressionMask & ImageBinaryDataCompression::S3TC))
  450. {
  451. // If s3tc compression is present then skip it
  452. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::S3TC);
  453. }
  454. if(!!(header.m_compressionMask & ImageBinaryDataCompression::ETC))
  455. {
  456. // If ETC compression is present then skip it
  457. skipSize += calcSizeOfSegment(header, ImageBinaryDataCompression::ETC);
  458. }
  459. }
  460. if(skipSize)
  461. {
  462. ANKI_CHECK(file.seek(skipSize, FileSeekOrigin::CURRENT));
  463. }
  464. //
  465. // It's time to read
  466. //
  467. // Allocate the surfaces
  468. mipCount = 0;
  469. if(header.m_type != ImageBinaryType::_3D)
  470. {
  471. // Read all surfaces
  472. U32 mipWidth = header.m_width;
  473. U32 mipHeight = header.m_height;
  474. for(U32 mip = 0; mip < header.m_mipmapCount; mip++)
  475. {
  476. for(U32 l = 0; l < layerCount; l++)
  477. {
  478. for(U32 f = 0; f < faceCount; ++f)
  479. {
  480. const PtrSize dataSize =
  481. calcSurfaceSize(mipWidth, mipHeight, preferredCompression, header.m_colorFormat,
  482. UVec2(header.m_astcBlockSizeX, header.m_astcBlockSizeY));
  483. // Check if this mipmap can be skipped because of size
  484. if(max(mipWidth, mipHeight) <= maxImageSize || mip == header.m_mipmapCount - 1)
  485. {
  486. ImageLoaderSurface& surf = *surfaces.emplaceBack(alloc);
  487. surf.m_width = mipWidth;
  488. surf.m_height = mipHeight;
  489. surf.m_data.create(alloc, dataSize);
  490. ANKI_CHECK(file.read(&surf.m_data[0], dataSize));
  491. mipCount = max(header.m_mipmapCount - mip, mipCount);
  492. }
  493. else
  494. {
  495. ANKI_CHECK(file.seek(dataSize, FileSeekOrigin::CURRENT));
  496. }
  497. }
  498. }
  499. mipWidth /= 2;
  500. mipHeight /= 2;
  501. }
  502. width = surfaces[0].m_width;
  503. height = surfaces[0].m_height;
  504. depth = MAX_U32;
  505. }
  506. else
  507. {
  508. U32 mipWidth = header.m_width;
  509. U32 mipHeight = header.m_height;
  510. U32 mipDepth = header.m_depthOrLayerCount;
  511. for(U32 mip = 0; mip < header.m_mipmapCount; mip++)
  512. {
  513. const U32 dataSize =
  514. U32(calcVolumeSize(mipWidth, mipHeight, mipDepth, preferredCompression, header.m_colorFormat));
  515. // Check if this mipmap can be skipped because of size
  516. if(max(max(mipWidth, mipHeight), mipDepth) <= maxImageSize || mip == header.m_mipmapCount - 1)
  517. {
  518. ImageLoaderVolume& vol = *volumes.emplaceBack(alloc);
  519. vol.m_width = mipWidth;
  520. vol.m_height = mipHeight;
  521. vol.m_depth = mipDepth;
  522. vol.m_data.create(alloc, dataSize);
  523. ANKI_CHECK(file.read(&vol.m_data[0], dataSize));
  524. mipCount = max(header.m_mipmapCount - mip, mipCount);
  525. }
  526. else
  527. {
  528. ANKI_CHECK(file.seek(dataSize, FileSeekOrigin::CURRENT));
  529. }
  530. mipWidth /= 2;
  531. mipHeight /= 2;
  532. mipDepth /= 2;
  533. }
  534. width = volumes[0].m_width;
  535. height = volumes[0].m_height;
  536. depth = volumes[0].m_depth;
  537. }
  538. return Error::NONE;
  539. }
  540. Error ImageLoader::loadStb(Bool isFloat, FileInterface& fs, U32& width, U32& height, DynamicArray<U8, PtrSize>& data,
  541. GenericMemoryPoolAllocator<U8>& alloc)
  542. {
  543. // Read the file
  544. DynamicArrayAuto<U8, PtrSize> fileData(alloc);
  545. const PtrSize fileSize = fs.getSize();
  546. fileData.create(fileSize);
  547. ANKI_CHECK(fs.read(&fileData[0], fileSize));
  548. // Use STB to read the image
  549. int stbw, stbh, comp;
  550. stbi_set_flip_vertically_on_load_thread(true);
  551. U8* stbdata;
  552. if(isFloat)
  553. {
  554. stbdata = reinterpret_cast<U8*>(stbi_loadf_from_memory(&fileData[0], I32(fileSize), &stbw, &stbh, &comp, 4));
  555. }
  556. else
  557. {
  558. stbdata = reinterpret_cast<U8*>(stbi_load_from_memory(&fileData[0], I32(fileSize), &stbw, &stbh, &comp, 4));
  559. }
  560. if(!stbdata)
  561. {
  562. ANKI_RESOURCE_LOGE("STB failed to read image");
  563. return Error::FUNCTION_FAILED;
  564. }
  565. // Store it
  566. width = U32(stbw);
  567. height = U32(stbh);
  568. const U32 componentSize = (isFloat) ? sizeof(F32) : sizeof(U8);
  569. data.create(alloc, width * height * 4 * componentSize);
  570. memcpy(&data[0], stbdata, data.getSize());
  571. // Cleanup
  572. stbi_image_free(stbdata);
  573. return Error::NONE;
  574. }
  575. Error ImageLoader::load(ResourceFilePtr rfile, const CString& filename, U32 maxImageSize)
  576. {
  577. RsrcFile file;
  578. file.m_rfile = rfile;
  579. const Error err = loadInternal(file, filename, maxImageSize);
  580. if(err)
  581. {
  582. ANKI_RESOURCE_LOGE("Failed to read image: %s", filename.cstr());
  583. }
  584. return err;
  585. }
  586. Error ImageLoader::load(const CString& filename, U32 maxImageSize)
  587. {
  588. SystemFile file;
  589. ANKI_CHECK(file.m_file.open(filename, FileOpenFlag::READ | FileOpenFlag::BINARY));
  590. const Error err = loadInternal(file, filename, maxImageSize);
  591. if(err)
  592. {
  593. ANKI_RESOURCE_LOGE("Failed to read image: %s", filename.cstr());
  594. }
  595. return err;
  596. }
  597. Error ImageLoader::loadInternal(FileInterface& file, const CString& filename, U32 maxImageSize)
  598. {
  599. // get the extension
  600. StringAuto ext(m_alloc);
  601. getFilepathExtension(filename, ext);
  602. if(ext.isEmpty())
  603. {
  604. ANKI_RESOURCE_LOGE("Failed to get filename extension");
  605. return Error::USER_DATA;
  606. }
  607. // load from this extension
  608. m_imageType = ImageBinaryType::_2D;
  609. m_compression = ImageBinaryDataCompression::RAW;
  610. if(ext == "tga")
  611. {
  612. m_surfaces.create(m_alloc, 1);
  613. m_mipmapCount = 1;
  614. m_depth = 1;
  615. m_layerCount = 1;
  616. U32 bpp = 0;
  617. ANKI_CHECK(loadTga(file, m_surfaces[0].m_width, m_surfaces[0].m_height, bpp, m_surfaces[0].m_data, m_alloc));
  618. m_width = m_surfaces[0].m_width;
  619. m_height = m_surfaces[0].m_height;
  620. if(bpp == 32)
  621. {
  622. m_colorFormat = ImageBinaryColorFormat::RGBA8;
  623. }
  624. else if(bpp == 24)
  625. {
  626. m_colorFormat = ImageBinaryColorFormat::RGB8;
  627. }
  628. else
  629. {
  630. ANKI_ASSERT(0);
  631. }
  632. }
  633. else if(ext == "ankitex")
  634. {
  635. #if ANKI_PLATFORM_MOBILE
  636. m_compression = ImageBinaryDataCompression::ASTC;
  637. #else
  638. m_compression = ImageBinaryDataCompression::S3TC;
  639. #endif
  640. ANKI_CHECK(loadAnkiImage(file, maxImageSize, m_compression, m_surfaces, m_volumes, m_alloc, m_width, m_height,
  641. m_depth, m_layerCount, m_mipmapCount, m_imageType, m_colorFormat, m_astcBlockSize));
  642. }
  643. else if(ext == "png" || ext == "jpg")
  644. {
  645. m_surfaces.create(m_alloc, 1);
  646. m_mipmapCount = 1;
  647. m_depth = 1;
  648. m_layerCount = 1;
  649. m_colorFormat = ImageBinaryColorFormat::RGBA8;
  650. ANKI_CHECK(loadStb(false, file, m_surfaces[0].m_width, m_surfaces[0].m_height, m_surfaces[0].m_data, m_alloc));
  651. m_width = m_surfaces[0].m_width;
  652. m_height = m_surfaces[0].m_height;
  653. }
  654. else if(ext == "hdr")
  655. {
  656. m_surfaces.create(m_alloc, 1);
  657. m_mipmapCount = 1;
  658. m_depth = 1;
  659. m_layerCount = 1;
  660. m_colorFormat = ImageBinaryColorFormat::RGBAF32;
  661. ANKI_CHECK(loadStb(true, file, m_surfaces[0].m_width, m_surfaces[0].m_height, m_surfaces[0].m_data, m_alloc));
  662. m_width = m_surfaces[0].m_width;
  663. m_height = m_surfaces[0].m_height;
  664. }
  665. else
  666. {
  667. ANKI_RESOURCE_LOGE("Unsupported extension: %s", &ext[0]);
  668. return Error::USER_DATA;
  669. }
  670. return Error::NONE;
  671. }
  672. const ImageLoaderSurface& ImageLoader::getSurface(U32 level, U32 face, U32 layer) const
  673. {
  674. ANKI_ASSERT(level < m_mipmapCount);
  675. U32 idx = 0;
  676. switch(m_imageType)
  677. {
  678. case ImageBinaryType::_2D:
  679. idx = level;
  680. break;
  681. case ImageBinaryType::CUBE:
  682. ANKI_ASSERT(face < 6);
  683. idx = level * 6 + face;
  684. break;
  685. case ImageBinaryType::_3D:
  686. ANKI_ASSERT(0 && "Can't use that for 3D images");
  687. break;
  688. case ImageBinaryType::_2D_ARRAY:
  689. idx = level * m_layerCount + layer;
  690. break;
  691. default:
  692. ANKI_ASSERT(0);
  693. }
  694. return m_surfaces[idx];
  695. }
  696. const ImageLoaderVolume& ImageLoader::getVolume(U32 level) const
  697. {
  698. ANKI_ASSERT(m_imageType == ImageBinaryType::_3D);
  699. return m_volumes[level];
  700. }
  701. void ImageLoader::destroy()
  702. {
  703. for(ImageLoaderSurface& surf : m_surfaces)
  704. {
  705. surf.m_data.destroy(m_alloc);
  706. }
  707. m_surfaces.destroy(m_alloc);
  708. for(ImageLoaderVolume& v : m_volumes)
  709. {
  710. v.m_data.destroy(m_alloc);
  711. }
  712. m_volumes.destroy(m_alloc);
  713. }
  714. } // end namespace anki