Image.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "ArrayPtr.h"
  24. #include "Resource.h"
  25. struct SDL_Surface;
  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 URHO3D_API 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. /// Flip image vertically.
  92. void FlipVertical();
  93. /// Save in BMP format. Return true if successful.
  94. bool SaveBMP(const String& fileName);
  95. /// Save in PNG format. Return true if successful.
  96. bool SavePNG(const String& fileName);
  97. /// Save in TGA format. Return true if successful.
  98. bool SaveTGA(const String& fileName);
  99. /// Save in JPG format with compression quality. Return true if successful.
  100. bool SaveJPG(const String& fileName, int quality);
  101. /// Create SDL surface from the image, or null if failed. Specify rect to only return partial image. Only RGB images are supported. You must free the surface yourself.
  102. SDL_Surface* GetSDLSurface(const IntRect& rect = IntRect::ZERO) const;
  103. /// Return width.
  104. int GetWidth() const { return width_; }
  105. /// Return height.
  106. int GetHeight() const { return height_; }
  107. /// Return number of color components.
  108. unsigned GetComponents() const { return components_; }
  109. /// Return pixel data.
  110. unsigned char* GetData() const { return data_; }
  111. /// Return whether is compressed.
  112. bool IsCompressed() const { return compressedFormat_ != CF_NONE; }
  113. /// Return compressed format.
  114. CompressedFormat GetCompressedFormat() const { return compressedFormat_; }
  115. /// Return number of compressed mip levels.
  116. unsigned GetNumCompressedLevels() const { return numCompressedLevels_; }
  117. /// Return next mip level by bilinear filtering.
  118. SharedPtr<Image> GetNextLevel() const;
  119. /// Return a compressed mip level.
  120. CompressedLevel GetCompressedLevel(unsigned index) const;
  121. private:
  122. /// Decode an image using stb_image.
  123. static unsigned char* GetImageData(Deserializer& source, int& width, int& height, unsigned& components);
  124. /// Free an image file's pixel data.
  125. static void FreeImageData(unsigned char* pixelData);
  126. /// Width.
  127. int width_;
  128. /// Height.
  129. int height_;
  130. /// Number of color components.
  131. unsigned components_;
  132. /// Number of compressed mip levels.
  133. unsigned numCompressedLevels_;
  134. /// Compressed format.
  135. CompressedFormat compressedFormat_;
  136. /// Pixel data.
  137. SharedArrayPtr<unsigned char> data_;
  138. };
  139. }