ImageLoader.h 4.2 KB

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