ImageLoader.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Resource/Common.h>
  7. #include <AnKi/Resource/ResourceFilesystem.h>
  8. #include <AnKi/Resource/ImageBinary.h>
  9. namespace anki {
  10. /// An image surface
  11. /// @memberof ImageLoader
  12. class ImageLoaderSurface
  13. {
  14. public:
  15. U32 m_width;
  16. U32 m_height;
  17. DynamicArray<U8, MemoryPoolPtrWrapper<BaseMemoryPool>, PtrSize> m_data;
  18. ImageLoaderSurface(MemoryPoolPtrWrapper<BaseMemoryPool> pool)
  19. : m_data(pool)
  20. {
  21. }
  22. };
  23. /// An image volume
  24. /// @memberof ImageLoader
  25. class ImageLoaderVolume
  26. {
  27. public:
  28. U32 m_width;
  29. U32 m_height;
  30. U32 m_depth;
  31. DynamicArray<U8, MemoryPoolPtrWrapper<BaseMemoryPool>, PtrSize> m_data;
  32. ImageLoaderVolume(MemoryPoolPtrWrapper<BaseMemoryPool> pool)
  33. : m_data(pool)
  34. {
  35. }
  36. };
  37. /// Loads bitmaps from regular system files or resource files. Supported formats are .tga and .ankitex.
  38. class ImageLoader
  39. {
  40. public:
  41. ImageLoader(BaseMemoryPool* pool)
  42. : m_surfaces(pool)
  43. , m_volumes(pool)
  44. {
  45. ANKI_ASSERT(pool);
  46. }
  47. ~ImageLoader() = default;
  48. ImageBinaryColorFormat getColorFormat() const
  49. {
  50. ANKI_ASSERT(m_colorFormat != ImageBinaryColorFormat::kNone);
  51. return m_colorFormat;
  52. }
  53. ImageBinaryDataCompression getCompression() const
  54. {
  55. ANKI_ASSERT(m_compression != ImageBinaryDataCompression::kNone);
  56. return m_compression;
  57. }
  58. U32 getMipmapCount() const
  59. {
  60. ANKI_ASSERT(m_mipmapCount != 0);
  61. return m_mipmapCount;
  62. }
  63. U32 getWidth() const
  64. {
  65. return m_width;
  66. }
  67. U32 getHeight() const
  68. {
  69. return m_height;
  70. }
  71. U32 getDepth() const
  72. {
  73. ANKI_ASSERT(m_imageType == ImageBinaryType::k3D);
  74. return m_depth;
  75. }
  76. U32 getLayerCount() const
  77. {
  78. ANKI_ASSERT(m_imageType == ImageBinaryType::k2DArray);
  79. return m_layerCount;
  80. }
  81. ImageBinaryType getImageType() const
  82. {
  83. ANKI_ASSERT(m_imageType != ImageBinaryType::kNone);
  84. return m_imageType;
  85. }
  86. UVec2 getAstcBlockSize() const
  87. {
  88. ANKI_ASSERT(!!(m_compression & ImageBinaryDataCompression::kAstc));
  89. ANKI_ASSERT(m_astcBlockSize != UVec2(0u));
  90. return m_astcBlockSize;
  91. }
  92. const ImageLoaderSurface& getSurface(U32 level, U32 face, U32 layer) const;
  93. const ImageLoaderVolume& getVolume(U32 level) const;
  94. /// Load a resource image file.
  95. Error load(ResourceFilePtr file, const CString& filename, U32 maxImageSize = kMaxU32);
  96. /// Load a system image file.
  97. Error load(const CString& filename, U32 maxImageSize = kMaxU32);
  98. private:
  99. class FileInterface;
  100. class RsrcFile;
  101. class SystemFile;
  102. /// [mip][depth or face or layer]. Loader doesn't support cube arrays ATM so face and layer won't be used at the
  103. /// same time.
  104. DynamicArray<ImageLoaderSurface, MemoryPoolPtrWrapper<BaseMemoryPool>> m_surfaces;
  105. DynamicArray<ImageLoaderVolume, MemoryPoolPtrWrapper<BaseMemoryPool>> m_volumes;
  106. U32 m_mipmapCount = 0;
  107. U32 m_width = 0;
  108. U32 m_height = 0;
  109. U32 m_depth = 0;
  110. U32 m_layerCount = 0;
  111. UVec2 m_astcBlockSize = UVec2(0u);
  112. ImageBinaryDataCompression m_compression = ImageBinaryDataCompression::kNone;
  113. ImageBinaryColorFormat m_colorFormat = ImageBinaryColorFormat::kNone;
  114. ImageBinaryType m_imageType = ImageBinaryType::kNone;
  115. void destroy();
  116. static Error loadStb(Bool isFloat, FileInterface& fs, U32& width, U32& height,
  117. DynamicArray<U8, MemoryPoolPtrWrapper<BaseMemoryPool>, PtrSize>& data);
  118. static Error loadAnkiImage(FileInterface& file, U32 maxImageSize, ImageBinaryDataCompression& preferredCompression,
  119. DynamicArray<ImageLoaderSurface, MemoryPoolPtrWrapper<BaseMemoryPool>>& surfaces,
  120. DynamicArray<ImageLoaderVolume, MemoryPoolPtrWrapper<BaseMemoryPool>>& volumes, U32& width, U32& height, U32& depth,
  121. U32& layerCount, U32& mipCount, ImageBinaryType& imageType, ImageBinaryColorFormat& colorFormat, UVec2& astcBlockSize);
  122. Error loadInternal(FileInterface& file, const CString& filename, U32 maxImageSize);
  123. };
  124. } // end namespace anki