]> www.fi.muni.cz Git - evince.git/blob - shell/ev-utils.c
Add thumbnail support.
[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