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);