]> www.fi.muni.cz Git - evince.git/blob - shell/ev-utils.c
Fix build warnings and compilation with dbus 0.35.2
[evince.git] / shell / ev-utils.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  *  Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  */
20
21 #include "ev-utils.h"
22 #include <math.h>
23
24 typedef struct
25 {
26   int size;
27   double *data;
28 } ConvFilter;
29
30 static double
31 gaussian (double x, double y, double r)
32 {
33     return ((1 / (2 * M_PI * r)) *
34             exp ((- (x * x + y * y)) / (2 * r * r)));
35 }
36
37 static ConvFilter *
38 create_blur_filter (int radius)
39 {
40   ConvFilter *filter;
41   int x, y;
42   double sum;
43   
44   filter = g_new0 (ConvFilter, 1);
45   filter->size = radius * 2 + 1;
46   filter->data = g_new (double, filter->size * filter->size);
47
48   sum = 0.0;
49   
50   for (y = 0 ; y < filter->size; y++)
51     {
52       for (x = 0 ; x < filter->size; x++)
53         {
54           sum += filter->data[y * filter->size + x] = gaussian (x - (filter->size >> 1),
55                                                                 y - (filter->size >> 1),
56                                                                 radius);
57         }
58     }
59
60   for (y = 0; y < filter->size; y++)
61     {
62       for (x = 0; x < filter->size; x++)
63         {
64           filter->data[y * filter->size + x] /= sum;
65         }
66     }
67
68   return filter;
69   
70 }
71
72 static GdkPixbuf *
73 create_shadow (GdkPixbuf *src, int blur_radius,
74                int x_offset, int y_offset, double opacity)
75 {
76   int x, y, i, j;
77   int width, height;
78   GdkPixbuf *dest;
79   static ConvFilter *filter = NULL;
80   int src_rowstride, dest_rowstride;
81   int src_bpp, dest_bpp;
82   
83   guchar *src_pixels, *dest_pixels;
84
85   if (!filter)
86     filter = create_blur_filter (blur_radius);
87
88   if (x_offset < 0)
89           x_offset = (blur_radius * 4) / 5;
90   
91   if (y_offset < 0)
92           y_offset = (blur_radius * 4) / 5;
93
94   
95   width = gdk_pixbuf_get_width (src) + blur_radius * 2 + x_offset;
96   height = gdk_pixbuf_get_height (src) + blur_radius * 2 + y_offset;
97
98   dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src), TRUE,
99                          gdk_pixbuf_get_bits_per_sample (src),
100                          width, height);
101   gdk_pixbuf_fill (dest, 0);  
102   src_pixels = gdk_pixbuf_get_pixels (src);
103   src_rowstride = gdk_pixbuf_get_rowstride (src);
104   src_bpp = gdk_pixbuf_get_has_alpha (src) ? 4 : 3;
105   
106   dest_pixels = gdk_pixbuf_get_pixels (dest);
107   dest_rowstride = gdk_pixbuf_get_rowstride (dest);
108   dest_bpp = gdk_pixbuf_get_has_alpha (dest) ? 4 : 3;
109   
110   for (y = 0; y < height; y++)
111     {
112       for (x = 0; x < width; x++)
113         {
114           int sumr = 0, sumg = 0, sumb = 0, suma = 0;
115
116           for (i = 0; i < filter->size; i++)
117             {
118               for (j = 0; j < filter->size; j++)
119                 {
120                   int src_x, src_y;
121
122                   src_y = -(blur_radius + x_offset) + y - (filter->size >> 1) + i;
123                   src_x = -(blur_radius + y_offset) + x - (filter->size >> 1) + j;
124
125                   if (src_y < 0 || src_y > gdk_pixbuf_get_height (src) ||
126                       src_x < 0 || src_x > gdk_pixbuf_get_width (src))
127                     continue;
128
129                   sumr += src_pixels [src_y * src_rowstride +
130                                       src_x * src_bpp + 0] *
131                     filter->data [i * filter->size + j];
132                   sumg += src_pixels [src_y * src_rowstride +
133                                       src_x * src_bpp + 1] * 
134                     filter->data [i * filter->size + j];
135
136                   sumb += src_pixels [src_y * src_rowstride +
137                                       src_x * src_bpp + 2] * 
138                     filter->data [i * filter->size + j];
139                   
140                   if (src_bpp == 4)
141                     suma += src_pixels [src_y * src_rowstride +
142                                         src_x * src_bpp + 3] *
143                     filter->data [i * filter->size + j];
144                   else
145                           suma += 0xff;
146                     
147                 }
148             }
149
150           if (dest_bpp == 4)
151             dest_pixels [y * dest_rowstride +
152                          x * dest_bpp + 3] = (suma * opacity) / (filter->size * filter->size);
153
154         }
155     }
156   
157   return dest;
158 }
159
160 GdkPixbuf *
161 ev_pixbuf_add_shadow (GdkPixbuf *src, int size,
162                       int x_offset, int y_offset, double opacity)
163 {
164   GdkPixbuf *dest;
165   
166   dest = create_shadow (src, size, x_offset, y_offset, opacity);
167
168   gdk_pixbuf_composite (src, dest,
169                         size, size,
170                         gdk_pixbuf_get_width (src),
171                         gdk_pixbuf_get_height (src),
172                         size, size,
173                         1.0, 1.0,
174                         GDK_INTERP_NEAREST, 255);
175
176   return dest;
177 }
178
179
180 /* Simple function to output the contents of a region.  Used solely for testing
181  * the region code.
182  */
183 void
184 ev_print_region_contents (GdkRegion *region)
185 {
186         GdkRectangle *rectangles = NULL;
187         gint n_rectangles, i;
188
189         if (region == NULL) {
190                 g_print ("<empty region>\n");
191                 return;
192         }
193
194         g_print ("<region %p>\n", region);
195         gdk_region_get_rectangles (region, &rectangles, &n_rectangles);
196         for (i = 0; i < n_rectangles; i++) {
197                 g_print ("\t(%d %d, %d %d) [%dx%d]\n",
198                          rectangles[i].x,
199                          rectangles[i].y,
200                          rectangles[i].x + rectangles[i].width,
201                          rectangles[i].y + rectangles[i].height,
202                          rectangles[i].width,
203                          rectangles[i].height);
204         }
205         g_free (rectangles);
206 }
207
208
209 #ifndef HAVE_G_FILE_SET_CONTENTS
210
211 #include <errno.h>
212 #include <unistd.h>
213 #include <sys/types.h>
214 #include <sys/wait.h>
215 #include <string.h>
216 #include <glib/gstdio.h>
217
218 static gboolean
219 rename_file (const char *old_name,
220              const char *new_name,
221              GError **err)
222 {
223   errno = 0;
224   if (g_rename (old_name, new_name) == -1)
225     {
226       return FALSE;
227     }
228   
229   return TRUE;
230 }
231
232 static gboolean
233 set_umask_permissions (int           fd,
234                        GError      **err)
235 {
236   /* All of this function is just to work around the fact that
237    * there is no way to get the umask without changing it.
238    *
239    * We can't just change-and-reset the umask because that would
240    * lead to a race condition if another thread tried to change
241    * the umask in between the getting and the setting of the umask.
242    * So we have to do the whole thing in a child process.
243    */
244   pid_t pid;
245
246   pid = fork ();
247   
248   if (pid == -1)
249     {
250       return FALSE;
251     }
252   else if (pid == 0)
253     {
254       /* child */
255       mode_t mask = umask (0666);
256
257       errno = 0;
258       if (fchmod (fd, 0666 & ~mask) == -1)
259         _exit (errno);
260       else
261         _exit (0);
262
263       return TRUE; /* To quiet gcc */
264     }
265   else
266     { 
267       /* parent */
268       int status;
269
270       errno = 0;
271       if (waitpid (pid, &status, 0) == -1)
272         {
273           return FALSE;
274         }
275
276       if (WIFEXITED (status))
277         {
278           if (WEXITSTATUS (status) == 0)
279             {
280               return TRUE;
281             }
282           else
283             {
284               return FALSE;
285             }
286         }
287       else if (WIFSIGNALED (status))
288         {
289           return FALSE;
290         }
291       else
292         {
293           return FALSE;
294         }
295     }
296 }
297
298 static gchar *
299 write_to_temp_file (const gchar *contents,
300                     gssize length,
301                     const gchar *template,
302                     GError **err)
303 {
304   gchar *tmp_name;
305   gchar *display_name;
306   gchar *retval;
307   FILE *file;
308   gint fd;
309
310   retval = NULL;
311   
312   tmp_name = g_strdup_printf ("%s.XXXXXX", template);
313
314   errno = 0;
315   fd = g_mkstemp (tmp_name);
316   display_name = g_filename_display_name (tmp_name);
317       
318   if (fd == -1)
319     {
320       goto out;
321     }
322
323   if (!set_umask_permissions (fd, err))
324     {
325       close (fd);
326       g_unlink (tmp_name);
327
328       goto out;
329     }
330   
331   errno = 0;
332   file = fdopen (fd, "wb");
333   if (!file)
334     {
335       close (fd);
336       g_unlink (tmp_name);
337       
338       goto out;
339     }
340
341   if (length > 0)
342     {
343       size_t n_written;
344       
345       errno = 0;
346
347       n_written = fwrite (contents, 1, length, file);
348
349       if (n_written < length)
350         {
351           fclose (file);
352           g_unlink (tmp_name);
353           
354           goto out;
355         }
356     }
357    
358   errno = 0;
359   if (fclose (file) == EOF)
360     { 
361       g_unlink (tmp_name);
362       
363       goto out;
364     }
365
366   retval = g_strdup (tmp_name);
367   
368  out:
369   g_free (tmp_name);
370   g_free (display_name);
371   
372   return retval;
373 }
374
375 gboolean
376 ev_file_set_contents (const gchar *filename,
377                       const gchar *contents,
378                       gssize         length,
379                       GError       **error)
380 {
381   gchar *tmp_filename;
382   gboolean retval;
383   GError *rename_error = NULL;
384   
385   g_return_val_if_fail (filename != NULL, FALSE);
386   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
387   g_return_val_if_fail (contents != NULL || length == 0, FALSE);
388   g_return_val_if_fail (length >= -1, FALSE);
389   
390   if (length == -1)
391     length = strlen (contents);
392
393   tmp_filename = write_to_temp_file (contents, length, filename, error);
394   
395   if (!tmp_filename)
396     {
397       retval = FALSE;
398       goto out;
399     }
400
401   if (!rename_file (tmp_filename, filename, &rename_error))
402     {
403       g_unlink (tmp_filename);
404       g_propagate_error (error, rename_error);
405       retval = FALSE;
406       goto out;
407     }
408
409   retval = TRUE;
410   
411  out:
412   g_free (tmp_filename);
413   return retval;
414 }
415
416 #endif /* HAVE_G_FILE_SET_CONTENTS */
417