DISTRHO Plugin Framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.8KB

  1. // This file is part of VSTGUI. It is subject to the license terms
  2. // in the LICENSE file found in the top-level directory of this
  3. // distribution and at http://github.com/steinbergmedia/vstgui/LICENSE
  4. #pragma once
  5. #include "vstguifwd.h"
  6. #include "cpoint.h"
  7. #include "crect.h"
  8. #include "cresourcedescription.h"
  9. #include "platform/iplatformbitmap.h"
  10. #include <vector>
  11. namespace VSTGUI {
  12. //-----------------------------------------------------------------------------
  13. // CBitmap Declaration
  14. //! @brief Encapsulates various platform depended kinds of bitmaps
  15. //-----------------------------------------------------------------------------
  16. class CBitmap : public AtomicReferenceCounted
  17. {
  18. public:
  19. using PlatformBitmapPtr = SharedPointer<IPlatformBitmap>;
  20. /** Create an image from a resource identifier */
  21. explicit CBitmap (const CResourceDescription& desc);
  22. /** Create an image with a given size */
  23. CBitmap (CCoord width, CCoord height);
  24. /** Create an image with a given size and scale factor */
  25. CBitmap (CPoint size, double scaleFactor = 1.);
  26. explicit CBitmap (const PlatformBitmapPtr& platformBitmap);
  27. ~CBitmap () noexcept override = default;
  28. //-----------------------------------------------------------------------------
  29. /// @name CBitmap Methods
  30. //-----------------------------------------------------------------------------
  31. //@{
  32. virtual void draw (CDrawContext* context, const CRect& rect, const CPoint& offset = CPoint (0, 0), float alpha = 1.f);
  33. /** get the width of the image */
  34. CCoord getWidth () const;
  35. /** get the height of the image */
  36. CCoord getHeight () const;
  37. /** get size of image */
  38. CPoint getSize () const;
  39. /** check if image is loaded */
  40. bool isLoaded () const { return getPlatformBitmap () ? true : false; }
  41. const CResourceDescription& getResourceDescription () const { return resourceDesc; }
  42. PlatformBitmapPtr getPlatformBitmap () const;
  43. void setPlatformBitmap (const PlatformBitmapPtr& bitmap);
  44. bool addBitmap (const PlatformBitmapPtr& platformBitmap);
  45. PlatformBitmapPtr getBestPlatformBitmapForScaleFactor (double scaleFactor) const;
  46. //@}
  47. //-----------------------------------------------------------------------------
  48. protected:
  49. CBitmap ();
  50. CResourceDescription resourceDesc;
  51. using BitmapVector = std::vector<PlatformBitmapPtr>;
  52. BitmapVector bitmaps;
  53. };
  54. //-----------------------------------------------------------------------------
  55. struct CNinePartTiledDescription
  56. {
  57. enum
  58. {
  59. kPartTopLeft,
  60. kPartTop,
  61. kPartTopRight,
  62. kPartLeft,
  63. kPartCenter,
  64. kPartRight,
  65. kPartBottomLeft,
  66. kPartBottom,
  67. kPartBottomRight,
  68. kPartCount
  69. };
  70. CCoord left {0.};
  71. CCoord top {0.};
  72. CCoord right {0.};
  73. CCoord bottom {0.};
  74. CNinePartTiledDescription () = default;
  75. CNinePartTiledDescription (CCoord left, CCoord top, CCoord right, CCoord bottom)
  76. : left (left), top (top), right (right), bottom (bottom) {}
  77. //-----------------------------------------------------------------------------
  78. inline void calcRects (const CRect& inBitmapRect, CRect outRect[kPartCount]) const
  79. {
  80. // Center
  81. CRect myCenter = outRect[kPartCenter] (inBitmapRect.left + left,
  82. inBitmapRect.top + top,
  83. inBitmapRect.right - right,
  84. inBitmapRect.bottom - bottom);
  85. // Edges
  86. outRect[kPartTop] (myCenter.left, inBitmapRect.top, myCenter.right, myCenter.top);
  87. outRect[kPartLeft] (inBitmapRect.left, myCenter.top, myCenter.left, myCenter.bottom);
  88. outRect[kPartRight] (myCenter.right, myCenter.top, inBitmapRect.right, myCenter.bottom);
  89. outRect[kPartBottom] (myCenter.left, myCenter.bottom, myCenter.right, inBitmapRect.bottom);
  90. // Corners
  91. outRect[kPartTopLeft] (inBitmapRect.left, inBitmapRect.top, myCenter.left, myCenter.top);
  92. outRect[kPartTopRight] (myCenter.right, inBitmapRect.top, inBitmapRect.right, myCenter.top);
  93. outRect[kPartBottomLeft] (inBitmapRect.left, myCenter.bottom, myCenter.left, inBitmapRect.bottom);
  94. outRect[kPartBottomRight] (myCenter.right, myCenter.bottom, inBitmapRect.right, inBitmapRect.bottom);
  95. }
  96. };
  97. //-----------------------------------------------------------------------------
  98. // CNinePartTiledBitmap Declaration
  99. /// @brief a nine-part tiled bitmap
  100. /// @ingroup new_in_4_0
  101. //-----------------------------------------------------------------------------
  102. class CNinePartTiledBitmap : public CBitmap
  103. {
  104. public:
  105. CNinePartTiledBitmap (const CResourceDescription& desc, const CNinePartTiledDescription& offsets);
  106. CNinePartTiledBitmap (const PlatformBitmapPtr& platformBitmap, const CNinePartTiledDescription& offsets);
  107. ~CNinePartTiledBitmap () noexcept override = default;
  108. //-----------------------------------------------------------------------------
  109. /// @name Part Offsets
  110. //-----------------------------------------------------------------------------
  111. //@{
  112. void setPartOffsets (const CNinePartTiledDescription& partOffsets) { offsets = partOffsets; }
  113. const CNinePartTiledDescription& getPartOffsets () const { return offsets; }
  114. //@}
  115. void draw (CDrawContext* context, const CRect& rect, const CPoint& offset = CPoint (0, 0), float alpha = 1.f) override;
  116. //-----------------------------------------------------------------------------
  117. protected:
  118. CNinePartTiledDescription offsets;
  119. };
  120. //------------------------------------------------------------------------
  121. // CBitmapPixelAccess
  122. /// @brief direct pixel access to a CBitmap
  123. /// @ingroup new_in_4_0
  124. //------------------------------------------------------------------------
  125. class CBitmapPixelAccess : public AtomicReferenceCounted
  126. {
  127. public:
  128. /** advance position */
  129. inline bool operator++ ();
  130. /** set current position */
  131. inline bool setPosition (uint32_t x, uint32_t y);
  132. /** return current x position */
  133. inline uint32_t getX () const { return x; }
  134. /** return current y position */
  135. inline uint32_t getY () const { return y; }
  136. /** get color of current pixel */
  137. virtual void getColor (CColor& c) const = 0;
  138. /** set color of current pixel */
  139. virtual void setColor (const CColor& c) = 0;
  140. /** get native color value */
  141. inline void getValue (uint32_t& value);
  142. /** set native color value */
  143. inline void setValue (uint32_t value);
  144. inline uint32_t getBitmapWidth () const { return maxX+1; }
  145. inline uint32_t getBitmapHeight () const { return maxY+1; }
  146. inline IPlatformBitmapPixelAccess* getPlatformBitmapPixelAccess () const { return pixelAccess; }
  147. /** create an accessor.
  148. can return 0 if platform implementation does not support this.
  149. result needs to be forgotten before the CBitmap reflects the change to the pixels */
  150. static CBitmapPixelAccess* create (CBitmap* bitmap, bool alphaPremultiplied = true);
  151. //-----------------------------------------------------------------------------
  152. protected:
  153. CBitmapPixelAccess ();
  154. ~CBitmapPixelAccess () noexcept override = default;
  155. void init (CBitmap* bitmap, IPlatformBitmapPixelAccess* pixelAccess);
  156. CBitmap* bitmap;
  157. SharedPointer<IPlatformBitmapPixelAccess> pixelAccess;
  158. uint8_t* currentPos;
  159. uint8_t* address;
  160. uint32_t bytesPerRow;
  161. uint32_t maxX;
  162. uint32_t maxY;
  163. uint32_t x;
  164. uint32_t y;
  165. };
  166. //------------------------------------------------------------------------
  167. inline bool CBitmapPixelAccess::operator++ ()
  168. {
  169. if (x < maxX)
  170. {
  171. x++;
  172. currentPos += 4;
  173. return true;
  174. }
  175. else if (y < maxY)
  176. {
  177. y++;
  178. x = 0;
  179. currentPos = address + y * bytesPerRow;
  180. return true;
  181. }
  182. return false;
  183. }
  184. //------------------------------------------------------------------------
  185. inline bool CBitmapPixelAccess::setPosition (uint32_t _x, uint32_t _y)
  186. {
  187. if (_x > maxX || _y > maxY)
  188. return false;
  189. x = _x;
  190. y = _y;
  191. currentPos = address + y * bytesPerRow + x * 4;
  192. return true;
  193. }
  194. //------------------------------------------------------------------------
  195. inline void CBitmapPixelAccess::getValue (uint32_t& value)
  196. {
  197. value = *(uint32_t*) (currentPos);
  198. }
  199. //------------------------------------------------------------------------
  200. inline void CBitmapPixelAccess::setValue (uint32_t value)
  201. {
  202. *(uint32_t*) (currentPos) = value;
  203. }
  204. } // VSTGUI