1 /*----------------------------------------------------------------------*
2  * File:	rxvtimg.h
3  *----------------------------------------------------------------------*
4  *
5  * All portions of code are copyright by their respective author/s.
6  * Copyright (c) 2012      Marc Lehmann <schmorp@schmorp.de>
7  * Copyright (c) 2012      Emanuele Giaquinta <e.giaquinta@glauco.it>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *---------------------------------------------------------------------*/
23 
24 #ifndef IMG_H
25 #define IMG_H
26 
27 #if HAVE_IMG
28 
29 #define float_to_component(d) (int32_t)((d) * 65535.99)
30 
31 #include <X11/extensions/Xrender.h>
32 
33 struct rxvt_img
34 {
35   typedef double nv;
36 
37   // *could* also hold the Pixmap itself
38   struct pixref
39   {
40     int cnt;
41     int w, h;
42     bool ours; // false if we don't own the pixmap at all
43 
pixrefrxvt_img::pixref44     pixref (int w, int h)
45     : cnt(1), w(w), h(h), ours(true)
46     {
47     }
48   };
49 
50   rxvt_display *d;
51   Pixmap pm;
52   pixref *ref; // shared refcnt
53   int x, y, w, h, repeat;
54   XRenderPictFormat *format;
55 
56   rxvt_img (rxvt_screen *screen, XRenderPictFormat *format, int x, int y, int width, int height, int repeat = RepeatNormal);
57   rxvt_img (rxvt_display *display, XRenderPictFormat *format, int x, int y, int width, int height, int repeat = RepeatNormal);
58   rxvt_img (const rxvt_img &img);
59   void alloc ();
60 
61 # if HAVE_PIXBUF
62   static rxvt_img *new_from_pixbuf (rxvt_screen *s, GdkPixbuf *pb); // from pixbuf
63   static rxvt_img *new_from_file (rxvt_screen *s, const char *filename); // via pixbuf
64 # endif
65   static rxvt_img *new_from_root (rxvt_screen *s); // get root pixmap
66 
67   ~rxvt_img ();
68 
stealrxvt_img69   Pixmap steal ()
70   {
71     ref->ours = false;
72     return pm;
73   }
74 
75   // inplace
moverxvt_img76   void move (int dx, int dy)
77   {
78     x += dx;
79     y += dy;
80   }
81 
repeat_moderxvt_img82   void repeat_mode (int repeat)
83   {
84     this->repeat = repeat;
85   }
86 
87   void unshare (); // prepare for write
88   void fill (const rgba &c);
89   void fill (const rgba &c, int x, int y, int w, int h);
90   void add_alpha ();
91   //void linear_gradient  (const XLinearGradient  *gradient, const XFixed *stops, const XRenderColor *colors, int nstops);
92   //void radial_gradient  (const XRadialGradient  *gradient, const XFixed *stops, const XRenderColor *colors, int nstops);
93   //void conical_gradient (const XConicalGradient *gradient, const XFixed *stops, const XRenderColor *colors, int nstops);
94 
95   void brightness (int32_t r, int32_t g, int32_t b, int32_t a);
96   void contrast (int32_t r, int32_t g, int32_t b, int32_t a);
97 
98   void brightness (nv r, nv g, nv b, nv a = 1.)
99   {
100     brightness (float_to_component (r),
101                 float_to_component (g),
102                 float_to_component (b),
103                 float_to_component (a));
104   }
105 
106   void contrast (nv r, nv g, nv b, nv a = 1.)
107   {
108     contrast (float_to_component (r),
109               float_to_component (g),
110               float_to_component (b),
111               float_to_component (a));
112   }
113 
114   void draw (rxvt_img *img, int op = PictOpOver, nv mask = 1.);
115 #if 0
116   void draw (rxvt_img *img, int op = PictOpOver, nv mask = 1.,
117              nv px, nv py, nv qx, nv qy, nv rx, nv ry, nv sx, nv sy);
118 #endif
119 
120   // copy
121   rxvt_img *reify (); // make x, y 0, make real width/height
122   rxvt_img *blur (int rh, int rv);
123   rxvt_img *clone ();
124   rxvt_img *sub_rect (int x, int y, int width, int height);
125   rxvt_img *transform (const nv matrix[3][3]);
126   rxvt_img *scale (int new_width, int new_height);
127   rxvt_img *rotate (int cx, int cy, nv phi);
128   rxvt_img *convert_format (XRenderPictFormat *format, const rgba &bg);
129   rxvt_img *tint (const rgba &c);
130   rxvt_img *shade (nv factor, rgba c = rgba (rgba::MAX_CC, rgba::MAX_CC, rgba::MAX_CC));
131   rxvt_img *filter (const char *name, int nparams = 0, nv *params = 0);
132   rxvt_img *muladd (nv mul, nv add); // general multiply and add, implemented as the biggest hack ever :/
133 
134   // egregious helper category
replacerxvt_img135   rxvt_img *replace (rxvt_img *&p)
136   {
137     delete p;
138     p = this;
139     return this;
140   }
141 
142   /* these are considered private */
143   void destroy ();
144   rxvt_img *new_empty ();
145   Picture picture ();
146   rxvt_img *transform (const nv *matrix);
147 };
148 
149 #endif
150 
151 #endif
152 
153