try make bootstrap work in one pass for other people..
[mikachu/openbox.git] / src / Image.hh
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // Image.hh for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifndef   __Image_hh
25 #define   __Image_hh
26
27 extern "C" {
28 #include <X11/Xlib.h>
29 #include <X11/Xutil.h>
30 }
31
32 #include <list>
33
34 #include "Timer.hh"
35 #include "BaseDisplay.hh"
36 #include "Color.hh"
37
38 class BImageControl;
39 class BTexture;
40 class BImageCache;
41
42 class BImage {
43 private:
44   BImageControl *control;
45   bool interlaced;
46   XColor *colors;
47
48   BColor from, to;
49   int red_offset, green_offset, blue_offset, red_bits, green_bits, blue_bits,
50     ncolors, cpc, cpccpc;
51   unsigned char *red, *green, *blue, *red_table, *green_table, *blue_table;
52   unsigned int width, height, *xtable, *ytable;
53
54   void TrueColorDither(unsigned int bit_depth, int bytes_per_line,
55                        unsigned char *pixel_data);
56   void PseudoColorDither(int bytes_per_line, unsigned char *pixel_data);
57 #ifdef ORDEREDPSEUDO
58   void OrderedPseudoColorDither(int bytes_per_line, unsigned char *pixel_data);
59 #endif
60
61   Pixmap renderPixmap(void);
62
63   XImage *renderXImage(void);
64
65   void invert(void);
66   void bevel1(void);
67   void bevel2(void);
68   void dgradient(void);
69   void egradient(void);
70   void hgradient(void);
71   void pgradient(void);
72   void rgradient(void);
73   void vgradient(void);
74   void cdgradient(void);
75   void pcgradient(void);
76
77
78 public:
79   BImage(BImageControl *c, unsigned int w, unsigned int h);
80   ~BImage(void);
81
82   Pixmap render(const BTexture &texture);
83   Pixmap render_solid(const BTexture &texture);
84   Pixmap render_gradient(const BTexture &texture);
85
86   // static methods for the builtin cache
87   static unsigned long maximumCacheSize(void);
88   static void setMaximumCacheSize(const unsigned long cache_max);
89
90   static unsigned long cacheTimeout(void);
91   static void setCacheTimeout(const unsigned long cache_timeout);
92
93 private:
94   // global image cache
95   static BImageCache *imagecache;
96 };
97
98
99 class BImageControl : public TimeoutHandler {
100 public:
101   struct CachedImage {
102     Pixmap pixmap;
103
104     unsigned int count, width, height;
105     unsigned long pixel1, pixel2, texture;
106   };
107
108   BImageControl(BaseDisplay *dpy, const ScreenInfo *scrn,
109                 bool _dither= False, int _cpc = 4,
110                 unsigned long cache_timeout = 300000l,
111                 unsigned long cmax = 200l);
112   virtual ~BImageControl(void);
113
114   inline BaseDisplay *getBaseDisplay(void) const { return basedisplay; }
115
116   inline bool doDither(void) { return dither; }
117
118   inline const ScreenInfo *getScreenInfo(void) { return screeninfo; }
119
120   inline Window getDrawable(void) const { return window; }
121
122   inline Visual *getVisual(void) { return screeninfo->getVisual(); }
123
124   inline int getBitsPerPixel(void) const { return bits_per_pixel; }
125   inline int getDepth(void) const { return screen_depth; }
126   inline int getColorsPerChannel(void) const
127     { return colors_per_channel; }
128
129   unsigned long getSqrt(unsigned int x);
130
131   Pixmap renderImage(unsigned int width, unsigned int height,
132                      const BTexture &texture);
133
134   void installRootColormap(void);
135   void removeImage(Pixmap pixmap);
136   void getColorTables(unsigned char **rmt, unsigned char **gmt,
137                       unsigned char **bmt,
138                       int *roff, int *goff, int *boff,
139                       int *rbit, int *gbit, int *bbit);
140   void getXColorTable(XColor **c, int *n);
141   void getGradientBuffers(unsigned int w, unsigned int h,
142                           unsigned int **xbuf, unsigned int **ybuf);
143   void setDither(bool d) { dither = d; }
144   void setColorsPerChannel(int cpc);
145
146   virtual void timeout(void);
147
148 private:
149   bool dither;
150   BaseDisplay *basedisplay;
151   const ScreenInfo *screeninfo;
152 #ifdef    TIMEDCACHE
153   BTimer *timer;
154 #endif // TIMEDCACHE
155
156   Colormap colormap;
157
158   Window window;
159   XColor *colors;
160   int colors_per_channel, ncolors, screen_number, screen_depth,
161     bits_per_pixel, red_offset, green_offset, blue_offset,
162     red_bits, green_bits, blue_bits;
163   unsigned char red_color_table[256], green_color_table[256],
164     blue_color_table[256];
165   unsigned int *grad_xbuffer, *grad_ybuffer, grad_buffer_width,
166     grad_buffer_height;
167   unsigned long *sqrt_table, cache_max;
168
169   typedef std::list<CachedImage> CacheContainer;
170   CacheContainer cache;
171
172   Pixmap searchCache(const unsigned int width, const unsigned int height,
173                      const unsigned long texture,
174                      const BColor &c1, const BColor &c2);
175 };
176
177
178 #endif // __Image_hh
179