Image.h 4.1 KB

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