Image.h 4.8 KB

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