Btk
pixels.hpp
1 #if !defined(_BTK_PIXELS_HPP_)
2 #define _BTK_PIXELS_HPP_
3 #include <string_view>
4 #include <cstdlib>
5 #include <cstddef>
6 #include <iosfwd>
7 #include <SDL2/SDL_pixels.h>
8 #include <SDL2/SDL_surface.h>
9 #include "defs.hpp"
10 #include "rect.hpp"
11 struct SDL_Surface;
12 struct BtkTexture;
13 namespace Btk{
14  class RWops;
19  struct Color:public SDL_Color{
20  Color() = default;
21  Color(Uint8 r,Uint8 g,Uint8 b,Uint8 a = 255){
22  this->r = r;
23  this->g = g;
24  this->b = b;
25  this->a = a;
26  }
27  Color(const SDL_Color &c):Color(c.r,c.g,c.b,c.a){}
28  Color(const Color &) = default;
29  Color &operator =(const Color &) = default;
30  };
31  //PixelBuffer
32  class BTKAPI PixBuf{
33  public:
34  PixBuf(SDL_Surface *s = nullptr):surf(s){};//empty
35  PixBuf(int w,int h,Uint32 format);//Create a buffer
36  //LoadFromFile
37  PixBuf(std::string_view file);
38  //Move construct
39  PixBuf(const PixBuf &) = delete;
40  PixBuf(PixBuf && s){
41  surf = s.surf;
42  s.surf = nullptr;
43  };
44  ~PixBuf();
45  //Get a copy of this PixBuf
46  PixBuf clone() const;
47  //Get a ref of this PixBuf
48  PixBuf ref() const;
49  //operators
50 
51  //save PixBuf
52  void save_png(RWops &,int quality);
53  void save_jpg(RWops &,int quality);
54  void save_bmp(RWops &);
55 
56  void save_png(std::string_view fname,int quality);
57  void save_jpg(std::string_view fname,int quality);
58  void save_bmp(std::string_view fname);
59 
60  PixBuf &operator =(SDL_Surface *);//assign
61  PixBuf &operator =(PixBuf &&);//assign
62  //check empty
63  bool empty() const noexcept{
64  return surf == nullptr;
65  }
66  //Get informations
67  Size size() const noexcept{
68  return {surf->w,surf->h};
69  }
70  int w() const noexcept{
71  return surf->w;
72  }
73  int h() const noexcept{
74  return surf->h;
75  }
76  int refcount() const noexcept{
77  return surf->refcount;
78  }
79  int pitch() const noexcept{
80  return surf->pitch;
81  }
82  //return its pixels size
83  size_t pixels_size() const noexcept{
84  return pitch() * h();
85  }
86  //must lock it before access its pixels
87  bool must_lock() const noexcept{
88  return SDL_MUSTLOCK(surf);
89  }
90  //get pixels
91  template<class T = void>
92  T *pixels() const noexcept{
93  return static_cast<T*>(surf->pixels);
94  }
95  SDL_Surface *get() const noexcept{
96  return surf;
97  }
98  SDL_Surface *operator ->() const noexcept{
99  return surf;
100  }
101  //Lock and Unlock
102  void lock() const;
103  void unlock() const noexcept;
104  //Set RLE
105  void set_rle(bool val = true);
112  PixBuf convert(Uint32 fmt) const;
113  //Some static method to load image
114  static PixBuf FromFile(std::string_view file);
115  static PixBuf FromFile(FILE *f);
116  static PixBuf FromMem(const void *mem,size_t size);
117  static PixBuf FromRWops(RWops &);
118  static PixBuf FromXPMArray(const char *const*);
119  private:
120  SDL_Surface *surf;
121  };
126  struct PixelFormat{
127  PixelFormat() = default;
128  PixelFormat(Uint32 val):fmt(val){}
129 
130  operator Uint32() const noexcept{
131  return fmt;
132  }
133  Uint32 fmt;
134  };
139  class BTKAPI PixFmt{
140  public:
141  //Create a pixel format
142  PixFmt(Uint32 pix_fmt);
143  PixFmt(const PixFmt &pixfmt):
144  PixFmt(pixfmt.fmt->format){};
145  PixFmt(PixFmt &&f){
146  fmt = f.fmt;
147  f.fmt = nullptr;
148  }
149  ~PixFmt();
150 
151  //Map RPG
152  Uint32 map_rgb (Uint8 r,Uint8 g,Uint8 b) const;
153  Uint32 map_rgba(Uint8 r,Uint8 g,Uint8 b,Uint8 a) const;
154 
155  Uint32 map_rgb(Color color) const{
156  return map_rgb(
157  color.r,
158  color.g,
159  color.b
160  );
161  };
162  Uint32 map_rgba(Color color) const{
163  return map_rgba(
164  color.r,
165  color.g,
166  color.b,
167  color.a
168  );
169  };
170  //Get its name
171  std::string_view name() const;
172  SDL_PixelFormat *operator ->() const noexcept{
173  return fmt;
174  }
175  operator Uint32() const noexcept{
176  return fmt->format;
177  };
178  private:
179  SDL_PixelFormat *fmt;
180  };
185  enum class TextureAccess:int{
186  Static,
187  Streaming,
188  Target
189  };
190  //RendererTexture
191  class BTKAPI Texture{
192  public:
197  struct Information{
198  TextureAccess access;//< Texture access
199  PixelFormat format;//< Pixels format
200  int w;
201  int h;
202  };
203  public:
204  Texture(BtkTexture *t = nullptr):texture(t){};
205  Texture(const Texture &) = delete;
206  Texture(Texture &&t){
207  texture = t.texture;
208  t.texture = nullptr;
209  }
210  ~Texture();
216  Size size() const{
217  auto inf = information();
218  return {inf.w,inf.h};
219  }
220  int w() const{
221  return size().w;
222  }
223  int h() const{
224  return size().h;
225  }
226  //check is empty
227  bool empty() const noexcept{
228  return texture == nullptr;
229  }
230  //assign
231  Texture &operator =(BtkTexture*);
232  Texture &operator =(Texture &&);
233  //Get pointer
234  BtkTexture *get() const noexcept{
235  return texture;
236  }
245  void update(const Rect *r,void *pixels,int pitch);
253  void lock(const Rect *rect,void **pixels,int *pitch);
254 
255  void lock(const Rect &rect,void **pixels,int *pitch){
256  lock(&rect,pixels,pitch);
257  }
258  void lock(void **pixels,int *pitch){
259  lock(nullptr,pixels,pitch);
260  }
261  void update(const Rect &r,void *pixels,int pitch){
262  update(&r,pixels,pitch);
263  }
264  void update(void *pixels,int pitch){
265  update(nullptr,pixels,pitch);
266  }
271  void unlock();
277  Information information() const;
278  private:
279  BtkTexture *texture;
280  friend struct Renderer;
281  };
286  class BTKAPI Gif{
287  public:
288  Gif():pimpl(nullptr){};
289  ~Gif();
290  static Gif FromRwops(RWops &);
291  private:
292  void *pimpl;
293  };
299  template<class T>
300  struct LockGuard{
301  LockGuard(T &m):mtx(m){
302  mtx.lock();
303  }
304  LockGuard(const LockGuard &) = delete;
305  ~LockGuard(){
306  mtx.unlock();
307  }
308  T &mtx;
309  };
313  template<>
315  LockGuard(Texture &t,const Rect *rect = nullptr):texture(t){
316  texture.lock(rect,&pixels,&pitch);
317  }
318  LockGuard(Texture &t,const Rect &rect):
319  texture(t){
320 
321  texture.lock(rect,&pixels,&pitch);
322  }
323  LockGuard(const LockGuard &) = delete;
324  ~LockGuard(){
325  texture.unlock();
326  }
327  Texture &texture;
328  void * pixels;
329  int pitch;
330  };
331 
332  template<class T>
333  using lock_guard = LockGuard<T>;
334 };
335 
336 #endif // _BTK_PIXELS_HPP_
Definition: pixels.hpp:191
a SDL_Rect with methods
Definition: rect.hpp:10
This header include many useful containers.
Definition: async.hpp:7
TextureAccess
TextureAccess(same def in SDL_render.h)
Definition: pixels.hpp:185
Texture&#39;s information.
Definition: pixels.hpp:197
Definition: pixels.hpp:32
Color structure.
Definition: pixels.hpp:19
Pixels format.
Definition: pixels.hpp:126
Size size() const
Get the size(w and h)
Definition: pixels.hpp:216
Generic LockGuard.
Definition: pixels.hpp:300
Gif Decoding class.
Definition: pixels.hpp:286
Pixels format detail.
Definition: pixels.hpp:139
Size of a Widget or Window.
Definition: rect.hpp:104
Definition: rwops.hpp:10