Image.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef ANKI_RESOURCE_IMAGE_H
  2. #define ANKI_RESOURCE_IMAGE_H
  3. #include "anki/util/Functions.h"
  4. #include "anki/util/Vector.h"
  5. #include <iosfwd>
  6. #include <cstdint>
  7. namespace anki {
  8. /// Image class.
  9. /// Used in Texture::load. Supported types: TGA and PNG
  10. class Image
  11. {
  12. public:
  13. /// The acceptable color types of AnKi
  14. enum ColorType
  15. {
  16. CT_R, ///< Red only
  17. CT_RGB, ///< RGB
  18. CT_RGBA ///< RGB plus alpha
  19. };
  20. /// The data compression
  21. enum DataCompression
  22. {
  23. DC_NONE,
  24. DC_DXT1,
  25. DC_DXT3,
  26. DC_DXT5
  27. };
  28. /// Do nothing
  29. Image()
  30. {}
  31. /// Load an image
  32. /// @param[in] filename The image file to load
  33. /// @exception Exception
  34. Image(const char* filename)
  35. {
  36. load(filename);
  37. }
  38. /// Do nothing
  39. ~Image()
  40. {}
  41. /// @name Accessors
  42. /// @{
  43. uint32_t getWidth() const
  44. {
  45. return width;
  46. }
  47. uint32_t getHeight() const
  48. {
  49. return height;
  50. }
  51. ColorType getColorType() const
  52. {
  53. return type;
  54. }
  55. const uint8_t* getData() const
  56. {
  57. return &data[0];
  58. }
  59. /// Get image size in bytes
  60. size_t getDataSize() const
  61. {
  62. return getVectorSizeInBytes(data);
  63. }
  64. DataCompression getDataCompression() const
  65. {
  66. return dataCompression;
  67. }
  68. /// @}
  69. /// Load an image file
  70. /// @param[in] filename The file to load
  71. /// @exception Exception
  72. void load(const char* filename);
  73. private:
  74. uint32_t width = 0; ///< Image width
  75. uint32_t height = 0; ///< Image height
  76. ColorType type; ///< Image color type
  77. Vector<uint8_t> data; ///< Image data
  78. DataCompression dataCompression;
  79. /// @name TGA headers
  80. /// @{
  81. static uint8_t tgaHeaderUncompressed[12];
  82. static uint8_t tgaHeaderCompressed[12];
  83. /// @}
  84. /// Load a TGA
  85. /// @param[in] filename The file to load
  86. /// @exception Exception
  87. void loadTga(const char* filename);
  88. /// Used by loadTga
  89. /// @param[in] fs The input
  90. /// @param[out] bpp Bits per pixel
  91. /// @exception Exception
  92. void loadUncompressedTga(std::fstream& fs, uint32_t& bpp);
  93. /// Used by loadTga
  94. /// @param[in] fs The input
  95. /// @param[out] bpp Bits per pixel
  96. /// @exception Exception
  97. void loadCompressedTga(std::fstream& fs, uint32_t& bpp);
  98. /// Load PNG. Dont throw exception because libpng is in C
  99. /// @param[in] filename The file to load
  100. /// @param[out] err The error message
  101. /// @return true on success
  102. bool loadPng(const char* filename, std::string& err) throw();
  103. /// Load a DDS file
  104. /// @param[in] filename The file to load
  105. void loadDds(const char* filename);
  106. };
  107. } // end namespace anki
  108. #endif