Image.h 4.3 KB

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