Image.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef RESOURCE_IMAGE_H
  24. #define RESOURCE_IMAGE_H
  25. #include "Resource.h"
  26. #include "SharedPtr.h"
  27. #include "SharedArrayPtr.h"
  28. //! Supported compressed image formats
  29. enum CompressedFormat
  30. {
  31. CF_NONE = 0,
  32. CF_DXT1,
  33. CF_DXT3,
  34. CF_DXT5
  35. };
  36. //! Description of a compressed image mipmap level
  37. struct CompressedLevel
  38. {
  39. //! Construct as empty
  40. CompressedLevel() :
  41. mData(0),
  42. mWidth(0),
  43. mHeight(0),
  44. mBlockSize(0),
  45. mDataSize(0),
  46. mRowSize(0),
  47. mRows(0)
  48. {
  49. }
  50. //! Image data
  51. unsigned char* mData;
  52. //! Width
  53. int mWidth;
  54. //! Height
  55. int mHeight;
  56. //! Block size in bytes
  57. unsigned mBlockSize;
  58. //! Total data size in bytes
  59. unsigned mDataSize;
  60. //! Row size in bytes
  61. unsigned mRowSize;
  62. //! Number of rows
  63. unsigned mRows;
  64. };
  65. //! An image resource
  66. class Image : public Resource
  67. {
  68. DEFINE_TYPE(Image);
  69. public:
  70. //! Construct with name
  71. Image(const std::string& name = std::string());
  72. //! Construct with initial size and name
  73. Image(int width, int height, unsigned components, const std::string& name = std::string());
  74. //! Construct with initial size, pixel data and name
  75. Image(int width, int height, unsigned components, unsigned char* pixelData, const std::string& name = std::string());
  76. //! Destruct
  77. virtual ~Image();
  78. //! Load resource. Throw exception on error
  79. virtual void load(Deserializer& source, ResourceCache* cache = 0);
  80. //! Set size and number of color components
  81. void setSize(unsigned width, unsigned height, unsigned components);
  82. //! Set data
  83. void setData(const unsigned char* pixelData);
  84. //! Save in BMP format
  85. void saveBMP(const std::string& fileName);
  86. //! Save in TGA format
  87. void saveTGA(const std::string& fileName);
  88. //! Return width
  89. int getWidth() const { return mWidth; }
  90. //! Return height
  91. int getHeight() const { return mHeight; }
  92. //! Return number of color components
  93. unsigned getComponents() const { return mComponents; }
  94. //! Return pixel data
  95. unsigned char* getData() const { return mData; }
  96. //! Return whether is compressed
  97. bool isCompressed() const { return mCompressedFormat != CF_NONE; }
  98. //! Return compressed format
  99. CompressedFormat getCompressedFormat() const { return mCompressedFormat; }
  100. //! Return number of compressed mipmap levels
  101. unsigned getNumCompressedLevels() const { return mCompressedLevels; }
  102. //! Return next mipmap level by bilinear filtering
  103. SharedPtr<Image> getNextLevel() const;
  104. //! Return a compressed mipmap level
  105. CompressedLevel getCompressedLevel(unsigned index) const;
  106. private:
  107. //! Load an image file
  108. static unsigned char* loadImageFile(Deserializer& source, int& width, int& height, unsigned& components);
  109. //! Free an image file's pixel data
  110. static void freeImageFile(unsigned char* pixelData);
  111. //! Width
  112. int mWidth;
  113. //! Height
  114. int mHeight;
  115. //! Number of color components
  116. unsigned mComponents;
  117. //! Number of compressed mipmap levels
  118. unsigned mCompressedLevels;
  119. //! Compressed format
  120. CompressedFormat mCompressedFormat;
  121. //! Pixel data
  122. SharedArrayPtr<unsigned char> mData;
  123. };
  124. #endif // RESOURCE_IMAGE_H