ASPiK SDK
cbitmap.h
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 
5 #ifndef __cbitmap__
6 #define __cbitmap__
7 
8 #include "vstguifwd.h"
9 #include "cpoint.h"
10 #include "crect.h"
11 #include "cresourcedescription.h"
12 #include "platform/iplatformbitmap.h"
13 #include <vector>
14 
15 namespace VSTGUI {
16 
17 //-----------------------------------------------------------------------------
18 // CBitmap Declaration
20 //-----------------------------------------------------------------------------
22 {
23 public:
25 
26  explicit CBitmap (const CResourceDescription& desc);
27  CBitmap (CCoord width, CCoord height);
28  CBitmap (CPoint size, double scaleFactor = 1.);
29  explicit CBitmap (const PlatformBitmapPtr& platformBitmap);
30  ~CBitmap () noexcept override = default;
31 
32  //-----------------------------------------------------------------------------
34  //-----------------------------------------------------------------------------
36  virtual void draw (CDrawContext* context, const CRect& rect, const CPoint& offset = CPoint (0, 0), float alpha = 1.f);
37 
38  CCoord getWidth () const;
39  CCoord getHeight () const;
40  CPoint getSize () const;
41 
42  bool isLoaded () const { return getPlatformBitmap () ? true : false; }
43 
44  const CResourceDescription& getResourceDescription () const { return resourceDesc; }
45 
46  PlatformBitmapPtr getPlatformBitmap () const;
47  void setPlatformBitmap (const PlatformBitmapPtr& bitmap);
48 
49  bool addBitmap (const PlatformBitmapPtr& platformBitmap);
50  PlatformBitmapPtr getBestPlatformBitmapForScaleFactor (double scaleFactor) const;
52 
53 //-----------------------------------------------------------------------------
54 protected:
55  CBitmap ();
56 
57  CResourceDescription resourceDesc;
58  using BitmapVector = std::vector<PlatformBitmapPtr>;
59  BitmapVector bitmaps;
60 };
61 
62 //-----------------------------------------------------------------------------
64 {
65  enum
66  {
67  kPartTopLeft,
68  kPartTop,
69  kPartTopRight,
70  kPartLeft,
71  kPartCenter,
72  kPartRight,
73  kPartBottomLeft,
74  kPartBottom,
75  kPartBottomRight,
76  kPartCount
77  };
78 
79  CCoord left {0.};
80  CCoord top {0.};
81  CCoord right {0.};
82  CCoord bottom {0.};
83 
84  CNinePartTiledDescription () = default;
85  CNinePartTiledDescription (CCoord left, CCoord top, CCoord right, CCoord bottom)
86  : left (left), top (top), right (right), bottom (bottom) {}
87 
88  //-----------------------------------------------------------------------------
89  inline void calcRects (const CRect& inBitmapRect, CRect outRect[kPartCount]) const
90  {
91  // Center
92  CRect myCenter = outRect[kPartCenter] (inBitmapRect.left + left,
93  inBitmapRect.top + top,
94  inBitmapRect.right - right,
95  inBitmapRect.bottom - bottom);
96 
97  // Edges
98  outRect[kPartTop] (myCenter.left, inBitmapRect.top, myCenter.right, myCenter.top);
99  outRect[kPartLeft] (inBitmapRect.left, myCenter.top, myCenter.left, myCenter.bottom);
100  outRect[kPartRight] (myCenter.right, myCenter.top, inBitmapRect.right, myCenter.bottom);
101  outRect[kPartBottom] (myCenter.left, myCenter.bottom, myCenter.right, inBitmapRect.bottom);
102 
103  // Corners
104  outRect[kPartTopLeft] (inBitmapRect.left, inBitmapRect.top, myCenter.left, myCenter.top);
105  outRect[kPartTopRight] (myCenter.right, inBitmapRect.top, inBitmapRect.right, myCenter.top);
106  outRect[kPartBottomLeft] (inBitmapRect.left, myCenter.bottom, myCenter.left, inBitmapRect.bottom);
107  outRect[kPartBottomRight] (myCenter.right, myCenter.bottom, inBitmapRect.right, inBitmapRect.bottom);
108  }
109 
110 };
111 
112 //-----------------------------------------------------------------------------
113 // CNinePartTiledBitmap Declaration
116 //-----------------------------------------------------------------------------
118 {
119 public:
121  CNinePartTiledBitmap (const PlatformBitmapPtr& platformBitmap, const CNinePartTiledDescription& offsets);
122  ~CNinePartTiledBitmap () noexcept override = default;
123 
124  //-----------------------------------------------------------------------------
126  //-----------------------------------------------------------------------------
128  void setPartOffsets (const CNinePartTiledDescription& partOffsets) { offsets = partOffsets; }
129  const CNinePartTiledDescription& getPartOffsets () const { return offsets; }
131 
132  void draw (CDrawContext* context, const CRect& rect, const CPoint& offset = CPoint (0, 0), float alpha = 1.f) override;
133 
134 //-----------------------------------------------------------------------------
135 protected:
137 };
138 
139 //------------------------------------------------------------------------
140 // CBitmapPixelAccess
143 //------------------------------------------------------------------------
145 {
146 public:
147  inline bool operator++ ();
148  inline bool setPosition (uint32_t x, uint32_t y);
149  inline uint32_t getX () const { return x; }
150  inline uint32_t getY () const { return y; }
151  virtual void getColor (CColor& c) const = 0;
152  virtual void setColor (const CColor& c) = 0;
153 
154  inline void getValue (uint32_t& value);
155  inline void setValue (uint32_t value);
156 
157  inline uint32_t getBitmapWidth () const { return maxX+1; }
158  inline uint32_t getBitmapHeight () const { return maxY+1; }
159 
160  inline IPlatformBitmapPixelAccess* getPlatformBitmapPixelAccess () const { return pixelAccess; }
164  static CBitmapPixelAccess* create (CBitmap* bitmap, bool alphaPremultiplied = true);
165 //-----------------------------------------------------------------------------
166 protected:
167  CBitmapPixelAccess ();
168  ~CBitmapPixelAccess () noexcept override = default;
169  void init (CBitmap* bitmap, IPlatformBitmapPixelAccess* pixelAccess);
170 
171  CBitmap* bitmap;
172  SharedPointer<IPlatformBitmapPixelAccess> pixelAccess;
173  uint8_t* currentPos;
174  uint8_t* address;
175  uint32_t bytesPerRow;
176  uint32_t maxX;
177  uint32_t maxY;
178  uint32_t x;
179  uint32_t y;
180 };
181 
182 //------------------------------------------------------------------------
183 inline bool CBitmapPixelAccess::operator++ ()
184 {
185  if (x < maxX)
186  {
187  x++;
188  currentPos += 4;
189  return true;
190  }
191  else if (y < maxY)
192  {
193  y++;
194  x = 0;
195  currentPos = address + y * bytesPerRow;
196  return true;
197  }
198  return false;
199 }
200 
201 //------------------------------------------------------------------------
202 inline bool CBitmapPixelAccess::setPosition (uint32_t _x, uint32_t _y)
203 {
204  if (_x > maxX || _y > maxY)
205  return false;
206  x = _x;
207  y = _y;
208  currentPos = address + y * bytesPerRow + x * 4;
209  return true;
210 }
211 
212 //------------------------------------------------------------------------
213 inline void CBitmapPixelAccess::getValue (uint32_t& value)
214 {
215  value = *(uint32_t*) (currentPos);
216 }
217 
218 //------------------------------------------------------------------------
219 inline void CBitmapPixelAccess::setValue (uint32_t value)
220 {
221  *(uint32_t*) (currentPos) = value;
222 }
223 
224 } // namespace VSTGUI
225 
226 #endif
Describes a resource by name or by ID.
Definition: cresourcedescription.h:16
CPoint getSize() const
get size of image
Definition: cbitmap.cpp:107
Rect structure.
Definition: crect.h:17
Definition: cbitmap.h:63
a nine-part tiled bitmap
Definition: cbitmap.h:117
uint32_t getY() const
return current y position
Definition: cbitmap.h:150
Definition: vstguibase.h:299
virtual void setColor(const CColor &c)=0
set color of current pixel
void setValue(uint32_t value)
set native color value
Definition: cbitmap.h:219
A drawing context encapsulates the drawing context of the underlying OS.
Definition: cdrawcontext.h:29
RGBA Color structure.
Definition: ccolor.h:15
bool isLoaded() const
check if image is loaded
Definition: cbitmap.h:42
void getValue(uint32_t &value)
get native color value
Definition: cbitmap.h:213
bool operator++()
advance position
Definition: cbitmap.h:183
static CBitmapPixelAccess * create(CBitmap *bitmap, bool alphaPremultiplied=true)
Definition: cbitmap.cpp:285
direct pixel access to a CBitmap
Definition: cbitmap.h:144
Encapsulates various platform depended kinds of bitmaps.
Definition: cbitmap.h:21
Definition: customcontrols.cpp:8
bool setPosition(uint32_t x, uint32_t y)
set current position
Definition: cbitmap.h:202
CCoord getHeight() const
get the height of the image
Definition: cbitmap.cpp:99
uint32_t getX() const
return current x position
Definition: cbitmap.h:149
Definition: vstguibase.h:247
virtual void getColor(CColor &c) const =0
get color of current pixel
Point structure.
Definition: cpoint.h:17
CCoord getWidth() const
get the width of the image
Definition: cbitmap.cpp:91