Image.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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. #pragma once
  24. #include "ArrayPtr.h"
  25. #include "Resource.h"
  26. /// Supported compressed image formats.
  27. enum CompressedFormat
  28. {
  29. CF_NONE = 0,
  30. CF_DXT1,
  31. CF_DXT3,
  32. CF_DXT5,
  33. CF_ETC1,
  34. CF_PVRTC_RGB_2BPP,
  35. CF_PVRTC_RGBA_2BPP,
  36. CF_PVRTC_RGB_4BPP,
  37. CF_PVRTC_RGBA_4BPP,
  38. };
  39. /// Compressed image mip level.
  40. struct CompressedLevel
  41. {
  42. /// Construct empty.
  43. CompressedLevel() :
  44. data_(0),
  45. width_(0),
  46. height_(0),
  47. blockSize_(0),
  48. dataSize_(0),
  49. rowSize_(0),
  50. rows_(0)
  51. {
  52. }
  53. /// Decompress to RGBA. The destination buffer required is width * height * 4 bytes. Return true if successful.
  54. bool Decompress(unsigned char* dest);
  55. /// Compressed image data.
  56. unsigned char* data_;
  57. /// Compression format.
  58. CompressedFormat format_;
  59. /// Width.
  60. int width_;
  61. /// Height.
  62. int height_;
  63. /// Block size in bytes.
  64. unsigned blockSize_;
  65. /// Total data size in bytes.
  66. unsigned dataSize_;
  67. /// Row size in bytes.
  68. unsigned rowSize_;
  69. /// Number of rows.
  70. unsigned rows_;
  71. };
  72. /// %Image resource.
  73. class Image : public Resource
  74. {
  75. OBJECT(Image);
  76. public:
  77. /// Construct empty.
  78. Image(Context* context);
  79. /// Destruct.
  80. virtual ~Image();
  81. /// Register object factory.
  82. static void RegisterObject(Context* context);
  83. /// Load resource. Return true if successful.
  84. virtual bool Load(Deserializer& source);
  85. /// %Set size and number of color components.
  86. void SetSize(int width, int height, unsigned components);
  87. /// %Set data.
  88. void SetData(const unsigned char* pixelData);
  89. /// Save in BMP format. Return true if successful.
  90. bool SaveBMP(const String& fileName);
  91. /// Save in TGA format. Return true if successful.
  92. bool SaveTGA(const String& fileName);
  93. /// Return width.
  94. int GetWidth() const { return width_; }
  95. /// Return height.
  96. int GetHeight() const { return height_; }
  97. /// Return number of color components.
  98. unsigned GetComponents() const { return components_; }
  99. /// Return pixel data.
  100. unsigned char* GetData() const { return data_; }
  101. /// Return whether is compressed.
  102. bool IsCompressed() const { return compressedFormat_ != CF_NONE; }
  103. /// Return compressed format.
  104. CompressedFormat GetCompressedFormat() const { return compressedFormat_; }
  105. /// Return number of compressed mip levels.
  106. unsigned GetNumCompressedLevels() const { return numCompressedLevels_; }
  107. /// Return next mip level by bilinear filtering.
  108. SharedPtr<Image> GetNextLevel() const;
  109. /// Return a compressed mip level.
  110. CompressedLevel GetCompressedLevel(unsigned index) const;
  111. private:
  112. /// Decode an image using stb_image.
  113. static unsigned char* GetImageData(Deserializer& source, int& width, int& height, unsigned& components);
  114. /// Free an image file's pixel data.
  115. static void FreeImageData(unsigned char* pixelData);
  116. /// Width.
  117. int width_;
  118. /// Height.
  119. int height_;
  120. /// Number of color components.
  121. unsigned components_;
  122. /// Number of compressed mip levels.
  123. unsigned numCompressedLevels_;
  124. /// Compressed format.
  125. CompressedFormat compressedFormat_;
  126. /// Pixel data.
  127. SharedArrayPtr<unsigned char> data_;
  128. };