ubit
uhardima.hpp
1 /* ***********************************************************************
2  *
3  * uhardima.hpp
4  * Ubit GUI Toolkit - Version 6.0
5  * (C) 1999-2008 Eric Lecolinet | ENST Paris | www.enst.fr/~elc/ubit
6  *
7  * ***********************************************************************
8  * COPYRIGHT NOTICE :
9  * THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY AND WITHOUT EVEN THE
10  * IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
11  * YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU
12  * GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
13  * EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.
14  * SEE FILES 'COPYRIGHT' AND 'COPYING' FOR MORE DETAILS.
15  * ***********************************************************************/
16 
17 #ifndef _uhardima_hpp_
18 #define _uhardima_hpp_ 1
19 #include <ubit/ubit_features.h>
20 #include <ubit/udisp.hpp>
21 #if UBIT_WITH_GL
22 # include <ubit/ugl.hpp>
23 #endif
24 
25 #if WITH_2D_GRAPHICS
26 
27 #if UBIT_WITH_X11
28 # include <X11/Xlib.h>
29 # include <X11/Xutil.h>
30 #define SysPutPixel(I,X,Y,P) XPutPixel(I,X,Y,P)
31 #define SysGetPixel(I,X,Y) XGetPixel(I,X,Y)
32 #define USysNull None
33 typedef XImage* USysIma;
34 typedef Pixmap USysPixmap;
35 typedef unsigned long USysPixel;
36 typedef XColor USysColor;
37 typedef XPoint USysPoint;
38 
39 #elif UBIT_WITH_GDK
40 # include <gdk/gdk.h>
41 #define SysPutPixel(I,X,Y,P) gdk_image_put_pixel(I,X,Y,P)
42 #define SysGetPixel(I,X,Y) gdk_image_get_pixel(I,X,Y)
43 #define USysNull NULL
44 typedef GdkImage* USysIma;
45 typedef GdkPixmap* USysPixmap;
46 typedef guint32 USysPixel;
47 typedef GdkColor USysColor;
48 typedef GdkPoint USysPoint;
49 #endif
50 
51 #endif
52 
53 namespace ubit {
54 
55  struct UImaGIF {
56  static int read(UHardIma&, const UStr& fname, int wmax, int hmax);
57  };
58 
59  struct UImaJPEG {
60  static int read(UHardIma&, const UStr& fname, int wmax, int hmax);
61  };
62 
63  struct UImaXPM {
64  static int read(UHardIma&, const UStr& fname, int wmax, int hmax);
65  static int readFromData(UHardIma&, const char** xpm_data, int wmax, int hmax);
66  };
67 
68  /* ==================================================== ===== ======= */
71  class UHardIma {
72  public:
73  virtual ~UHardIma();
74 
75  virtual int read(const UStr& filename, const char* filetype = null,
76  int max_w = 0, int max_h = 0);
82  virtual int readFromData(const char **xpm_data, int max_w = 0, int max_h = 0);
84 
85  virtual void setRaster(int width, int height, int alpha_hint) = 0;
94 #if WITH_2D_GRAPHICS
95  virtual void setRasterAndAdopt(USysIma image, USysIma shapemask) = 0;
104 #endif
105 
106  virtual UHardIma* createScaledClone(float xscale, float yscale, UDisp* = 0) = 0;
108 
109  virtual int getWidth() const {return width;}
110  virtual int getHeight() const {return height;}
111  virtual int getBpp() const {return bpp;}
112 
113  virtual int getTransparency() const {return transparency;}
115 
116  UDisp* getDisp() const {return disp;}
117  virtual float getScale() const = 0;
118  virtual bool isRealized() const = 0;
119 
120  protected:
121  friend class UIma;
122  friend class UGraphCtxX11;
123  friend class UGraphCtxGDK;
124  UDisp* disp;
125  int width, height;
126  unsigned char bpp;
127  unsigned char transparency;
128  UHardIma() {};
129  UHardIma& operator=(const UHardIma&);
130  };
131 
132  /* ==================================================== ===== ======= */
133 #if UBIT_WITH_GL
134 
136  class UHardImaGL : public UHardIma {
137  public:
138  UHardImaGL(UDisp*, int width = 0, int height = 0, int transparency_hint = 24);
143  virtual ~UHardImaGL();
144 
145  virtual bool isRealized() const;
146 
147  virtual void setRaster(int width, int height, int transparency_hint);
148 
149  virtual void setRasterAndAdopt(unsigned char* pixels, int width, int height);
158 #if WITH_2D_GRAPHICS
159  virtual void setRasterAndAdopt(USysIma ima, USysIma imashape);
160 #endif
161 
162  virtual UHardIma* createScaledClone(float xscale, float yscale, UDisp* = 0);
163 
164  unsigned char* getPixels() {return pixels;}
165  // USysPixel getPixel(int x, int y);
166  // void setPixel(USysPixel p, int x, int y);
167 
168  virtual float getScale() const {return 1.;}
169 
170  GLuint getTexID() const {return texid;}
171 
172  protected:
173  friend class UGlcontext;
174  // inherited int width, height; ATTENTION: taille LOGIQUE de l'image, pas de
175  // la texture, qui est toujours une puissance de 2!
176  unsigned char* pixels; // image data for OpenGL
177  mutable GLuint texid; // texture ID
178  void createTexFromPixels() const;
179  };
180 
181 #endif // UBIT_WITH_GL
182 
183  /* ==================================================== ===== ======= */
184 
185 #if WITH_2D_GRAPHICS
186 
190  class UHardIma2D : public UHardIma {
191  public:
192  UHardIma2D(UDisp*, int width = 0, int height = 0, int transparency_hint = 0);
197  virtual ~UHardIma2D();
198 
199  virtual void setRaster(int width, int height, int transparency_hint);
200  virtual void setRasterAndAdopt(USysIma ima, USysIma shape);
201 
202  virtual UHardIma* createScaledClone(float xscale, float yscale, UDisp* = 0);
203 
204 #if UBIT_WITH_GDK
205  unsigned long getPixel(int x, int y) {return gdk_image_get_pixel(sys_ima, x, y);}
206  void setPixel(unsigned long p, int x, int y) {gdk_image_put_pixel(sys_ima, p, x, y);}
207 #elif UBIT_WITH_X11
208  unsigned long getPixel(int x, int y) {return XGetPixel(sys_ima, x, y);}
209  void setPixel(unsigned long p, int x, int y) {XPutPixel(sys_ima, p, x, y);}
210 #endif
211 
212  virtual float getScale() const {return scale;}
213 
214  USysIma getIma() {return sys_ima;}
215  USysIma getImaShape() {return sys_imashape;}
216  virtual bool isRealized() const {return sys_ima != null;}
217 
218  // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219 
220  static USysIma createEmptySysIma(UDisp*, int width, int height, int depth);
228  static USysIma createScaledSysIma(UDisp* to_nd, UDisp* from_nd,
229  USysIma from_ima, float xscale, float yscale);
238  static bool blendSysIma(UDisp*, USysIma i1, USysIma i2, float alpha);
243  static bool blendSysIma(UDisp*, USysIma i1, USysPixel p2, float alpha);
248  static USysPixel* allocSysColors(UDisp*, USysColor* colors, int col_count);
250 
251  protected:
252  friend class UIma;
253  friend class UX11context;
254  USysIma sys_ima, sys_imashape;
255  float scale;
256  };
257 
258 
259  /* ==================================================== ===== ======= */
262  class UHardPix {
263  public:
264  UHardPix(UDisp*, UHardIma2D* image = null);
269  virtual ~UHardPix();
270 
271  virtual void setRasterFrom(const USysIma image, const USysIma shapemask = 0);
280  virtual int getWidth() const {return width;}
281  virtual int getHeight() const {return height;}
282  virtual int getBpp() const {return bpp;}
283  virtual int getTransparency() const {return transparency;}
284  UDisp* getDisp() const {return disp;}
285  virtual float getScale() const {return scale;}
286  virtual bool isRealized() const {return sys_pix != USysNull;}
287 
288  USysPixmap getPixmap() {return sys_pix;}
289  USysPixmap getPixmapShape() {return sys_pixshape;}
290 
291  private:
292  friend class UPix;
293  friend class UX11context;
294  UDisp* disp;
295  int width, height;
296  unsigned char bpp;
297  unsigned char transparency;
298  USysPixmap sys_pix, sys_pixshape;
299  float scale;
300  };
301 
302 #endif
303 
304 }
305 #endif
Image.
Definition: uima.hpp:50
Display Context.
Definition: udisp.hpp:44
[Impl] image implementation (encapsulates system resources).
Definition: uhardima.hpp:71
virtual int getTransparency() const
0 (opaque), 1(bitmask) or >1(alpha channel).
Definition: uhardima.hpp:113
Definition: uhardima.hpp:63
Definition: uhardfont.hpp:31
Definition: uhardima.hpp:59
Definition: uhardima.hpp:55
Pixmap Image.
Definition: upix.hpp:31
Ubit String.
Definition: ustr.hpp:72