ImageLoader.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  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/util/Functions.h>
  9. #include <anki/util/Enum.h>
  10. namespace anki
  11. {
  12. /// Image loader.
  13. /// Used in Texture::load. Supported types: TGA and an AnKi specific format.
  14. class ImageLoader
  15. {
  16. public:
  17. /// Texture type
  18. enum class TextureType : U32
  19. {
  20. NONE,
  21. _2D,
  22. CUBE,
  23. _3D,
  24. _2D_ARRAY
  25. };
  26. /// The acceptable color types of AnKi
  27. enum class ColorFormat : U32
  28. {
  29. NONE,
  30. RGB8, ///< RGB
  31. RGBA8 ///< RGB plus alpha
  32. };
  33. /// The data compression
  34. enum class DataCompression : U32
  35. {
  36. NONE,
  37. RAW = 1 << 0,
  38. S3TC = 1 << 1,
  39. ETC = 1 << 2
  40. };
  41. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(DataCompression, friend)
  42. /// An image surface
  43. class Surface
  44. {
  45. public:
  46. U32 m_width;
  47. U32 m_height;
  48. U32 m_mipLevel;
  49. DArray<U8> m_data;
  50. };
  51. ImageLoader(GenericMemoryPoolAllocator<U8> alloc)
  52. : m_alloc(alloc)
  53. {
  54. }
  55. ~ImageLoader()
  56. {
  57. destroy();
  58. }
  59. ColorFormat getColorFormat() const
  60. {
  61. ANKI_ASSERT(m_colorFormat != ColorFormat::NONE);
  62. return m_colorFormat;
  63. }
  64. DataCompression getCompression() const
  65. {
  66. ANKI_ASSERT(m_compression != DataCompression::NONE);
  67. return m_compression;
  68. }
  69. U getMipLevelsCount() const
  70. {
  71. ANKI_ASSERT(m_mipLevels != 0);
  72. return m_mipLevels;
  73. }
  74. U getDepth() const
  75. {
  76. ANKI_ASSERT(m_depth != 0);
  77. return m_depth;
  78. }
  79. TextureType getTextureType() const
  80. {
  81. ANKI_ASSERT(m_textureType != TextureType::NONE);
  82. return m_textureType;
  83. }
  84. const Surface& getSurface(U mipLevel, U layer) const;
  85. GenericMemoryPoolAllocator<U8> getAllocator() const
  86. {
  87. return m_alloc;
  88. }
  89. /// Load an image file.
  90. ANKI_USE_RESULT Error load(ResourceFilePtr file,
  91. const CString& filename,
  92. U32 maxTextureSize = MAX_U32);
  93. Atomic<I32>& getRefcount()
  94. {
  95. return m_refcount;
  96. }
  97. private:
  98. GenericMemoryPoolAllocator<U8> m_alloc;
  99. Atomic<I32> m_refcount = {0};
  100. /// [mip][depthFace]
  101. DArray<Surface> m_surfaces;
  102. U8 m_mipLevels = 0;
  103. U8 m_depth = 0;
  104. DataCompression m_compression = DataCompression::NONE;
  105. ColorFormat m_colorFormat = ColorFormat::NONE;
  106. TextureType m_textureType = TextureType::NONE;
  107. void destroy();
  108. };
  109. } // end namespace anki