Image.h 4.5 KB

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