1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
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)
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.
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.
31 gaussian (double x, double y, double r)
33 return ((1 / (2 * M_PI * r)) *
34 exp ((- (x * x + y * y)) / (2 * r * r)));
38 create_blur_filter (int radius)
44 filter = g_new0 (ConvFilter, 1);
45 filter->size = radius * 2 + 1;
46 filter->data = g_new (double, filter->size * filter->size);
50 for (y = 0 ; y < filter->size; y++)
52 for (x = 0 ; x < filter->size; x++)
54 sum += filter->data[y * filter->size + x] = gaussian (x - (filter->size >> 1),
55 y - (filter->size >> 1),
60 for (y = 0; y < filter->size; y++)
62 for (x = 0; x < filter->size; x++)
64 filter->data[y * filter->size + x] /= sum;
73 create_shadow (GdkPixbuf *src, int blur_radius,
74 int x_offset, int y_offset, double opacity)
79 static ConvFilter *filter = NULL;
80 int src_rowstride, dest_rowstride;
81 int src_bpp, dest_bpp;
83 guchar *src_pixels, *dest_pixels;
86 filter = create_blur_filter (blur_radius);
89 x_offset = (blur_radius * 4) / 5;
92 y_offset = (blur_radius * 4) / 5;
95 width = gdk_pixbuf_get_width (src) + blur_radius * 2 + x_offset;
96 height = gdk_pixbuf_get_height (src) + blur_radius * 2 + y_offset;
98 dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src), TRUE,
99 gdk_pixbuf_get_bits_per_sample (src),
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;
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;
110 for (y = 0; y < height; y++)
112 for (x = 0; x < width; x++)
114 int sumr = 0, sumg = 0, sumb = 0, suma = 0;
116 for (i = 0; i < filter->size; i++)
118 for (j = 0; j < filter->size; j++)
122 src_y = -(blur_radius + x_offset) + y - (filter->size >> 1) + i;
123 src_x = -(blur_radius + y_offset) + x - (filter->size >> 1) + j;
125 if (src_y < 0 || src_y > gdk_pixbuf_get_height (src) ||
126 src_x < 0 || src_x > gdk_pixbuf_get_width (src))
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];
136 sumb += src_pixels [src_y * src_rowstride +
137 src_x * src_bpp + 2] *
138 filter->data [i * filter->size + j];
141 suma += src_pixels [src_y * src_rowstride +
142 src_x * src_bpp + 3] *
143 filter->data [i * filter->size + j];
151 dest_pixels [y * dest_rowstride +
152 x * dest_bpp + 3] = (suma * opacity) / (filter->size * filter->size);
161 ev_pixbuf_add_shadow (GdkPixbuf *src, int size,
162 int x_offset, int y_offset, double opacity)
166 dest = create_shadow (src, size, x_offset, y_offset, opacity);
168 gdk_pixbuf_composite (src, dest,
170 gdk_pixbuf_get_width (src),
171 gdk_pixbuf_get_height (src),
174 GDK_INTERP_NEAREST, 255);
180 /* Simple function to output the contents of a region. Used solely for testing
184 ev_print_region_contents (GdkRegion *region)
186 GdkRectangle *rectangles = NULL;
187 gint n_rectangles, i;
189 if (region == NULL) {
190 g_print ("<empty region>\n");
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",
200 rectangles[i].x + rectangles[i].width,
201 rectangles[i].y + rectangles[i].height,
203 rectangles[i].height);
209 #ifndef HAVE_G_FILE_SET_CONTENTS
213 #include <sys/types.h>
214 #include <sys/wait.h>
218 rename_file (const char *old_name,
219 const char *new_name,
223 if (g_rename (old_name, new_name) == -1)
232 set_umask_permissions (int fd,
235 /* All of this function is just to work around the fact that
236 * there is no way to get the umask without changing it.
238 * We can't just change-and-reset the umask because that would
239 * lead to a race condition if another thread tried to change
240 * the umask in between the getting and the setting of the umask.
241 * So we have to do the whole thing in a child process.
256 mode_t mask = umask (0666);
259 if (fchmod (fd, 0666 & ~mask) == -1)
264 return TRUE; /* To quiet gcc */
272 if (waitpid (pid, &status, 0) == -1)
277 if (WIFEXITED (status))
279 save_errno = WEXITSTATUS (status);
290 else if (WIFSIGNALED (status))
302 write_to_temp_file (const gchar *contents,
304 const gchar *template,
316 tmp_name = g_strdup_printf ("%s.XXXXXX", template);
319 fd = g_mkstemp (tmp_name);
320 display_name = g_filename_display_name (tmp_name);
327 if (!set_umask_permissions (fd, err))
336 file = fdopen (fd, "wb");
351 n_written = fwrite (contents, 1, length, file);
353 if (n_written < length)
363 if (fclose (file) == EOF)
370 retval = g_strdup (tmp_name);
374 g_free (display_name);
380 ev_file_set_contents (const gchar *filename,
381 const gchar *contents,
387 GError *rename_error = NULL;
389 g_return_val_if_fail (filename != NULL, FALSE);
390 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
391 g_return_val_if_fail (contents != NULL || length == 0, FALSE);
392 g_return_val_if_fail (length >= -1, FALSE);
395 length = strlen (contents);
397 tmp_filename = write_to_temp_file (contents, length, filename, error);
405 if (!rename_file (tmp_filename, filename, &rename_error))
407 g_unlink (tmp_filename);
408 g_propagate_error (error, rename_error);
416 g_free (tmp_filename);
420 #endif /* HAVE_G_FILE_SET_CONTENTS */