ImageLoader.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #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> m_data;
  18. };
  19. /// An image volume
  20. /// @memberof ImageLoader
  21. class ImageLoaderVolume
  22. {
  23. public:
  24. U32 m_width;
  25. U32 m_height;
  26. U32 m_depth;
  27. DynamicArray<U8> m_data;
  28. };
  29. /// Loads bitmaps from regular system files or resource files. Supported formats are .tga and .ankitex.
  30. class ImageLoader
  31. {
  32. public:
  33. ImageLoader(GenericMemoryPoolAllocator<U8> alloc)
  34. : m_alloc(alloc)
  35. {
  36. }
  37. ~ImageLoader()
  38. {
  39. destroy();
  40. }
  41. ImageBinaryColorFormat getColorFormat() const
  42. {
  43. ANKI_ASSERT(m_colorFormat != ImageBinaryColorFormat::NONE);
  44. return m_colorFormat;
  45. }
  46. ImageBinaryDataCompression getCompression() const
  47. {
  48. ANKI_ASSERT(m_compression != ImageBinaryDataCompression::NONE);
  49. return m_compression;
  50. }
  51. U32 getMipmapCount() const
  52. {
  53. ANKI_ASSERT(m_mipmapCount != 0);
  54. return m_mipmapCount;
  55. }
  56. U32 getWidth() const
  57. {
  58. return m_width;
  59. }
  60. U32 getHeight() const
  61. {
  62. return m_height;
  63. }
  64. U32 getDepth() const
  65. {
  66. ANKI_ASSERT(m_imageType == ImageBinaryType::_3D);
  67. return m_depth;
  68. }
  69. U32 getLayerCount() const
  70. {
  71. ANKI_ASSERT(m_imageType == ImageBinaryType::_2D_ARRAY);
  72. return m_layerCount;
  73. }
  74. ImageBinaryType getImageType() const
  75. {
  76. ANKI_ASSERT(m_imageType != ImageBinaryType::NONE);
  77. return m_imageType;
  78. }
  79. UVec2 getAstcBlockSize() const
  80. {
  81. ANKI_ASSERT(!!(m_compression & ImageBinaryDataCompression::ASTC));
  82. ANKI_ASSERT(m_astcBlockSize != UVec2(0u));
  83. return m_astcBlockSize;
  84. }
  85. const ImageLoaderSurface& getSurface(U32 level, U32 face, U32 layer) const;
  86. const ImageLoaderVolume& getVolume(U32 level) const;
  87. /// Load a resource image file.
  88. ANKI_USE_RESULT Error load(ResourceFilePtr file, const CString& filename, U32 maxImageSize = MAX_U32);
  89. /// Load a system image file.
  90. ANKI_USE_RESULT Error load(const CString& filename, U32 maxImageSize = MAX_U32);
  91. private:
  92. class FileInterface;
  93. class RsrcFile;
  94. class SystemFile;
  95. GenericMemoryPoolAllocator<U8> m_alloc;
  96. /// [mip][depth or face or layer]. Loader doesn't support cube arrays ATM so face and layer won't be used at the
  97. /// same time.
  98. DynamicArray<ImageLoaderSurface> m_surfaces;
  99. DynamicArray<ImageLoaderVolume> m_volumes;
  100. U32 m_mipmapCount = 0;
  101. U32 m_width = 0;
  102. U32 m_height = 0;
  103. U32 m_depth = 0;
  104. U32 m_layerCount = 0;
  105. UVec2 m_astcBlockSize = UVec2(0u);
  106. ImageBinaryDataCompression m_compression = ImageBinaryDataCompression::NONE;
  107. ImageBinaryColorFormat m_colorFormat = ImageBinaryColorFormat::NONE;
  108. ImageBinaryType m_imageType = ImageBinaryType::NONE;
  109. void destroy();
  110. static ANKI_USE_RESULT Error loadUncompressedTga(FileInterface& fs, U32& width, U32& height, U32& bpp,
  111. DynamicArray<U8>& data, GenericMemoryPoolAllocator<U8>& alloc);
  112. static ANKI_USE_RESULT Error loadCompressedTga(FileInterface& fs, U32& width, U32& height, U32& bpp,
  113. DynamicArray<U8>& data, GenericMemoryPoolAllocator<U8>& alloc);
  114. static ANKI_USE_RESULT Error loadTga(FileInterface& fs, U32& width, U32& height, U32& bpp, DynamicArray<U8>& data,
  115. GenericMemoryPoolAllocator<U8>& alloc);
  116. static ANKI_USE_RESULT Error loadStb(FileInterface& fs, U32& width, U32& height, DynamicArray<U8>& data,
  117. GenericMemoryPoolAllocator<U8>& alloc);
  118. static ANKI_USE_RESULT Error loadAnkiImage(FileInterface& file, U32 maxImageSize,
  119. ImageBinaryDataCompression& preferredCompression,
  120. DynamicArray<ImageLoaderSurface>& surfaces,
  121. DynamicArray<ImageLoaderVolume>& volumes,
  122. GenericMemoryPoolAllocator<U8>& alloc, U32& width, U32& height,
  123. U32& depth, U32& layerCount, U32& mipCount, ImageBinaryType& imageType,
  124. ImageBinaryColorFormat& colorFormat, UVec2& astcBlockSize);
  125. ANKI_USE_RESULT Error loadInternal(FileInterface& file, const CString& filename, U32 maxImageSize);
  126. };
  127. } // end namespace anki