]>
Dogcows Code - chaz/tint2/blob - src/util/common.c
1 /**************************************************************************
3 * Tint2 : common windows function
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <X11/extensions/Xrender.h>
35 void copy_file(const char *pathSrc
, const char *pathDest
)
37 FILE *fileSrc
, *fileDest
;
41 fileSrc
= fopen(pathSrc
, "rb");
42 if (fileSrc
== NULL
) return;
44 fileDest
= fopen(pathDest
, "wb");
45 if (fileDest
== NULL
) return;
47 while ((nb
= fread(line
, 1, 100, fileSrc
)) > 0) fwrite(line
, 1, nb
, fileDest
);
54 int parse_line (const char *line
, char **key
, char **value
)
58 /* Skip useless lines */
59 if ((line
[0] == '#') || (line
[0] == '\n')) return 0;
60 if (!(a
= strchr (line
, '='))) return 0;
62 /* overwrite '=' with '\0' */
67 /* overwrite '\n' with '\0' if '\n' present */
68 if ((b
= strchr (a
, '\n'))) b
[0] = '\0';
78 int hex_char_to_int (char c
)
82 if (c
>= '0' && c
<= '9') r
= c
- '0';
83 else if (c
>= 'a' && c
<= 'f') r
= c
- 'a' + 10;
84 else if (c
>= 'A' && c
<= 'F') r
= c
- 'A' + 10;
91 int hex_to_rgb (char *hex
, int *r
, int *g
, int *b
)
95 if (hex
== NULL
|| hex
[0] != '#') return (0);
99 *r
= hex_char_to_int (hex
[1]);
100 *g
= hex_char_to_int (hex
[2]);
101 *b
= hex_char_to_int (hex
[3]);
103 else if (len
== 6 + 1) {
104 *r
= hex_char_to_int (hex
[1]) * 16 + hex_char_to_int (hex
[2]);
105 *g
= hex_char_to_int (hex
[3]) * 16 + hex_char_to_int (hex
[4]);
106 *b
= hex_char_to_int (hex
[5]) * 16 + hex_char_to_int (hex
[6]);
108 else if (len
== 12 + 1) {
109 *r
= hex_char_to_int (hex
[1]) * 16 + hex_char_to_int (hex
[2]);
110 *g
= hex_char_to_int (hex
[5]) * 16 + hex_char_to_int (hex
[6]);
111 *b
= hex_char_to_int (hex
[9]) * 16 + hex_char_to_int (hex
[10]);
119 void get_color (char *hex
, double *rgb
)
122 hex_to_rgb (hex
, &r
, &g
, &b
);
124 rgb
[0] = (r
/ 255.0);
125 rgb
[1] = (g
/ 255.0);
126 rgb
[2] = (b
/ 255.0);
130 void adjust_asb(DATA32
*data
, int w
, int h
, int alpha
, float satur
, float bright
)
133 unsigned int a
, r
, g
, b
, argb
;
136 float h2
, f
, p
, q
, t
;
137 float hue
, saturation
, brightness
;
138 float redc
, greenc
, bluec
;
140 for(y
= 0; y
< h
; y
++) {
141 for(id
= y
* w
, x
= 0; x
< w
; x
++, id
++) {
143 a
= (argb
>> 24) & 0xff;
144 // transparent => nothing to do.
145 if (a
== 0) continue;
146 r
= (argb
>> 16) & 0xff;
147 g
= (argb
>> 8) & 0xff;
150 // convert RGB to HSB
151 cmax
= (r
> g
) ? r
: g
;
152 if (b
> cmax
) cmax
= b
;
153 cmin
= (r
< g
) ? r
: g
;
154 if (b
< cmin
) cmin
= b
;
155 brightness
= ((float)cmax
) / 255.0f
;
157 saturation
= ((float)(cmax
- cmin
)) / ((float)cmax
);
163 redc
= ((float)(cmax
- r
)) / ((float)(cmax
- cmin
));
164 greenc
= ((float)(cmax
- g
)) / ((float)(cmax
- cmin
));
165 bluec
= ((float)(cmax
- b
)) / ((float)(cmax
- cmin
));
167 hue
= bluec
- greenc
;
169 hue
= 2.0f
+ redc
- bluec
;
171 hue
= 4.0f
+ greenc
- redc
;
179 if (saturation
< 0.0) saturation
= 0.0;
180 if (saturation
> 1.0) saturation
= 1.0;
181 brightness
+= bright
;
182 if (brightness
< 0.0) brightness
= 0.0;
183 if (brightness
> 1.0) brightness
= 1.0;
187 // convert HSB to RGB
188 if (saturation
== 0) {
189 r
= g
= b
= (int)(brightness
* 255.0f
+ 0.5f
);
191 h2
= (hue
- (int)hue
) * 6.0f
;
193 p
= brightness
* (1.0f
- saturation
);
194 q
= brightness
* (1.0f
- saturation
* f
);
195 t
= brightness
* (1.0f
- (saturation
* (1.0f
- f
)));
198 r
= (int)(brightness
* 255.0f
+ 0.5f
);
199 g
= (int)(t
* 255.0f
+ 0.5f
);
200 b
= (int)(p
* 255.0f
+ 0.5f
);
203 r
= (int)(q
* 255.0f
+ 0.5f
);
204 g
= (int)(brightness
* 255.0f
+ 0.5f
);
205 b
= (int)(p
* 255.0f
+ 0.5f
);
208 r
= (int)(p
* 255.0f
+ 0.5f
);
209 g
= (int)(brightness
* 255.0f
+ 0.5f
);
210 b
= (int)(t
* 255.0f
+ 0.5f
);
213 r
= (int)(p
* 255.0f
+ 0.5f
);
214 g
= (int)(q
* 255.0f
+ 0.5f
);
215 b
= (int)(brightness
* 255.0f
+ 0.5f
);
218 r
= (int)(t
* 255.0f
+ 0.5f
);
219 g
= (int)(p
* 255.0f
+ 0.5f
);
220 b
= (int)(brightness
* 255.0f
+ 0.5f
);
223 r
= (int)(brightness
* 255.0f
+ 0.5f
);
224 g
= (int)(p
* 255.0f
+ 0.5f
);
225 b
= (int)(q
* 255.0f
+ 0.5f
);
231 argb
= (argb
<< 8) + r
;
232 argb
= (argb
<< 8) + g
;
233 argb
= (argb
<< 8) + b
;
240 void createHeuristicMask(DATA32
* data
, int w
, int h
)
242 unsigned char* udata
= (unsigned char*)data
;
247 for (i
=0; i
<h
*w
; ++i
) {
248 if ( abs(b
-*udata
)<5 && abs(g
-*(udata
+1))<5 && abs(r
-*(udata
+2))<5 ) {
256 void render_image(Drawable d
, int x
, int y
, int w
, int h
)
258 // in real_transparency mode imlib_render_image_on_drawable does not the right thing, because
259 // the operation is IMLIB_OP_COPY, but we would need IMLIB_OP_OVER (which does not exist)
260 // Therefore we have to do it with the XRender extension (i.e. copy what imlib is doing internally)
261 // But first we need to render the image onto itself with PictOpIn to adjust the colors to the alpha channel
262 Pixmap pmap_tmp
= XCreatePixmap(server
.dsp
, server
.root_win
, w
, h
, 32);
263 imlib_context_set_drawable(pmap_tmp
);
264 imlib_context_set_blend(0);
265 imlib_render_image_on_drawable(0, 0);
266 Picture pict_image
= XRenderCreatePicture(server
.dsp
, pmap_tmp
, XRenderFindStandardFormat(server
.dsp
, PictStandardARGB32
), 0, 0);
267 Picture pict_drawable
= XRenderCreatePicture(server
.dsp
, d
, XRenderFindVisualFormat(server
.dsp
, server
.visual
), 0, 0);
268 XRenderComposite(server
.dsp
, PictOpIn
, pict_image
, None
, pict_image
, 0, 0, 0, 0, 0, 0, w
, h
);
269 XRenderComposite(server
.dsp
, PictOpOver
, pict_image
, None
, pict_drawable
, 0, 0, 0, 0, x
, y
, w
, h
);
270 XFreePixmap(server
.dsp
, pmap_tmp
);
271 XRenderFreePicture(server
.dsp
, pict_image
);
272 XRenderFreePicture(server
.dsp
, pict_drawable
);
This page took 0.047435 seconds and 4 git commands to generate.