]> www.fi.muni.cz Git - evince.git/blob - shell/ev-utils.c
587dd73664e914392582d1fc5c479beced82f1f0
[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 #ifndef HAVE_G_FILE_SET_CONTENTS
181
182 #include <errno.h>
183 #include <unistd.h>
184 #include <sys/types.h>
185 #include <sys/wait.h>
186 #include <string.h>
187
188 static gboolean
189 rename_file (const char *old_name,
190              const char *new_name,
191              GError **err)
192 {
193   errno = 0;
194   if (g_rename (old_name, new_name) == -1)
195     {
196       return FALSE;
197     }
198   
199   return TRUE;
200 }
201
202 static gboolean
203 set_umask_permissions (int           fd,
204                        GError      **err)
205 {
206   /* All of this function is just to work around the fact that
207    * there is no way to get the umask without changing it.
208    *
209    * We can't just change-and-reset the umask because that would
210    * lead to a race condition if another thread tried to change
211    * the umask in between the getting and the setting of the umask.
212    * So we have to do the whole thing in a child process.
213    */
214
215   int save_errno;
216   pid_t pid;
217
218   pid = fork ();
219   
220   if (pid == -1)
221     {
222       return FALSE;
223     }
224   else if (pid == 0)
225     {
226       /* child */
227       mode_t mask = umask (0666);
228
229       errno = 0;
230       if (fchmod (fd, 0666 & ~mask) == -1)
231         _exit (errno);
232       else
233         _exit (0);
234
235       return TRUE; /* To quiet gcc */
236     }
237   else
238     { 
239       /* parent */
240       int status;
241
242       errno = 0;
243       if (waitpid (pid, &status, 0) == -1)
244         {
245           return FALSE;
246         }
247
248       if (WIFEXITED (status))
249         {
250           save_errno = WEXITSTATUS (status);
251
252           if (save_errno == 0)
253             {
254               return TRUE;
255             }
256           else
257             {
258               return FALSE;
259             }
260         }
261       else if (WIFSIGNALED (status))
262         {
263           return FALSE;
264         }
265       else
266         {
267           return FALSE;
268         }
269     }
270 }
271
272 static gchar *
273 write_to_temp_file (const gchar *contents,
274                     gssize length,
275                     const gchar *template,
276                     GError **err)
277 {
278   gchar *tmp_name;
279   gchar *display_name;
280   gchar *retval;
281   FILE *file;
282   gint fd;
283   int save_errno;
284
285   retval = NULL;
286   
287   tmp_name = g_strdup_printf ("%s.XXXXXX", template);
288
289   errno = 0;
290   fd = g_mkstemp (tmp_name);
291   display_name = g_filename_display_name (tmp_name);
292       
293   if (fd == -1)
294     {
295       goto out;
296     }
297
298   if (!set_umask_permissions (fd, err))
299     {
300       close (fd);
301       g_unlink (tmp_name);
302
303       goto out;
304     }
305   
306   errno = 0;
307   file = fdopen (fd, "wb");
308   if (!file)
309     {
310       close (fd);
311       g_unlink (tmp_name);
312       
313       goto out;
314     }
315
316   if (length > 0)
317     {
318       size_t n_written;
319       
320       errno = 0;
321
322       n_written = fwrite (contents, 1, length, file);
323
324       if (n_written < length)
325         {
326           fclose (file);
327           g_unlink (tmp_name);
328           
329           goto out;
330         }
331     }
332    
333   errno = 0;
334   if (fclose (file) == EOF)
335     { 
336       g_unlink (tmp_name);
337       
338       goto out;
339     }
340
341   retval = g_strdup (tmp_name);
342   
343  out:
344   g_free (tmp_name);
345   g_free (display_name);
346   
347   return retval;
348 }
349
350 gboolean
351 ev_file_set_contents (const gchar *filename,
352                       const gchar *contents,
353                       gssize         length,
354                       GError       **error)
355 {
356   gchar *tmp_filename;
357   gboolean retval;
358   GError *rename_error = NULL;
359   
360   g_return_val_if_fail (filename != NULL, FALSE);
361   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
362   g_return_val_if_fail (contents != NULL || length == 0, FALSE);
363   g_return_val_if_fail (length >= -1, FALSE);
364   
365   if (length == -1)
366     length = strlen (contents);
367
368   tmp_filename = write_to_temp_file (contents, length, filename, error);
369   
370   if (!tmp_filename)
371     {
372       retval = FALSE;
373       goto out;
374     }
375
376   if (!rename_file (tmp_filename, filename, &rename_error))
377     {
378       g_unlink (tmp_filename);
379       g_propagate_error (error, rename_error);
380       retval = FALSE;
381       goto out;
382     }
383
384   retval = TRUE;
385   
386  out:
387   g_free (tmp_filename);
388   return retval;
389 }
390
391 #endif /* HAVE_G_FILE_SET_CONTENTS */
392