]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-misc.c
[libdocument] Add a function to invert colors of an image cairo surface
[evince.git] / libdocument / ev-document-misc.c
1 /*
2  *  Copyright (C) 2009 Juanjo MarĂ­n <juanj.marin@juntadeandalucia.es>
3  *  Copyright (c) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
4  *  Copyright (C) 2000-2003 Marco Pesenti Gritti
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24
25 #include <gtk/gtk.h>
26
27 #include "ev-document-misc.h"
28
29 /* Returns a new GdkPixbuf that is suitable for placing in the thumbnail view.
30  * It is four pixels wider and taller than the source.  If source_pixbuf is not
31  * NULL, then it will fill the return pixbuf with the contents of
32  * source_pixbuf.
33  */
34 GdkPixbuf *
35 ev_document_misc_get_thumbnail_frame (int        width,
36                                       int        height,
37                                       GdkPixbuf *source_pixbuf)
38 {
39         GdkPixbuf *retval;
40         guchar *data;
41         gint rowstride;
42         int i;
43         int width_r, height_r;
44
45         if (source_pixbuf)
46                 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
47
48         if (source_pixbuf) {
49                 width_r = gdk_pixbuf_get_width (source_pixbuf);
50                 height_r = gdk_pixbuf_get_height (source_pixbuf);
51         } else {
52                 width_r = width;
53                 height_r = height;
54         }
55
56         /* make sure no one is passing us garbage */
57         g_assert (width_r >= 0 && height_r >= 0);
58
59         retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
60                                  TRUE, 8,
61                                  width_r + 4,
62                                  height_r + 4);
63
64         /* make it black and fill in the middle */
65         data = gdk_pixbuf_get_pixels (retval);
66         rowstride = gdk_pixbuf_get_rowstride (retval);
67
68         gdk_pixbuf_fill (retval, 0x000000ff);
69         for (i = 1; i < height_r + 1; i++)
70                 memset (data + (rowstride * i) + 4, 0xffffffff, width_r * 4);
71
72         /* copy the source pixbuf */
73         if (source_pixbuf)
74                 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
75                                       width_r,
76                                       height_r,
77                                       retval,
78                                       1, 1);
79         /* Add the corner */
80         data [(width_r + 2) * 4 + 3] = 0;
81         data [(width_r + 3) * 4 + 3] = 0;
82         data [(width_r + 2) * 4 + (rowstride * 1) + 3] = 0;
83         data [(width_r + 3) * 4 + (rowstride * 1) + 3] = 0;
84
85         data [(height_r + 2) * rowstride + 3] = 0;
86         data [(height_r + 3) * rowstride + 3] = 0;
87         data [(height_r + 2) * rowstride + 4 + 3] = 0;
88         data [(height_r + 3) * rowstride + 4 + 3] = 0;
89
90         return retval;
91 }
92
93 void
94 ev_document_misc_get_page_border_size (gint       page_width,
95                                        gint       page_height,
96                                        GtkBorder *border)
97 {
98         g_assert (border);
99
100         border->left = 1;
101         border->top = 1;
102         if (page_width < 100) {
103                 border->right = 2;
104                 border->bottom = 2;
105         } else if (page_width < 500) {
106                 border->right = 3;
107                 border->bottom = 3;
108         } else {
109                 border->right = 4;
110                 border->bottom = 4;
111         }
112 }
113
114
115 void
116 ev_document_misc_paint_one_page (GdkDrawable  *drawable,
117                                  GtkWidget    *widget,
118                                  GdkRectangle *area,
119                                  GtkBorder    *border,
120                                  gboolean highlight)
121 {
122         gdk_draw_rectangle (drawable,
123                             highlight ?
124                                     widget->style->text_gc[widget->state] : widget->style->dark_gc[widget->state],
125                             TRUE,
126                             area->x,
127                             area->y,
128                             area->width,
129                             area->height);
130         gdk_draw_rectangle (drawable,
131                             widget->style->white_gc,
132                             TRUE,
133                             area->x + border->left,
134                             area->y + border->top,
135                             area->width - (border->left + border->right),
136                             area->height - (border->top + border->bottom));
137         gdk_draw_rectangle (drawable,
138                             widget->style->mid_gc[widget->state],
139                             TRUE,
140                             area->x,
141                             area->y + area->height - (border->bottom - border->top),
142                             border->bottom - border->top,
143                             border->bottom - border->top);
144         gdk_draw_rectangle (drawable,
145                             widget->style->mid_gc[widget->state],
146                             TRUE,
147                             area->x + area->width - (border->right - border->left),
148                             area->y,
149                             border->right - border->left,
150                             border->right - border->left);
151
152 }
153
154 cairo_surface_t *
155 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
156 {
157         cairo_surface_t *surface;
158         cairo_t         *cr;
159
160         surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
161                                               CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
162                                               gdk_pixbuf_get_width (pixbuf),
163                                               gdk_pixbuf_get_height (pixbuf));
164         cr = cairo_create (surface);
165         gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
166         cairo_paint (cr);
167         cairo_destroy (cr);
168         
169         return surface;
170 }
171
172 GdkPixbuf *
173 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
174 {
175         GdkPixbuf       *pixbuf;
176         cairo_surface_t *image;
177         cairo_t         *cr;
178         gboolean         has_alpha;
179         gint             width, height;
180         cairo_format_t   surface_format;
181         gint             pixbuf_n_channels;
182         gint             pixbuf_rowstride;
183         guchar          *pixbuf_pixels;
184         gint             x, y;
185
186         width = cairo_image_surface_get_width (surface);
187         height = cairo_image_surface_get_height (surface);
188         
189         surface_format = cairo_image_surface_get_format (surface);
190         has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
191
192         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
193                                  TRUE, 8,
194                                  width, height);
195         pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
196         pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
197         pixbuf_pixels = gdk_pixbuf_get_pixels (pixbuf);
198
199         image = cairo_image_surface_create_for_data (pixbuf_pixels,
200                                                      surface_format,
201                                                      width, height,
202                                                      pixbuf_rowstride);
203         cr = cairo_create (image);
204         cairo_set_source_surface (cr, surface, 0, 0);
205
206         if (has_alpha)
207                 cairo_mask_surface (cr, surface, 0, 0);
208         else
209                 cairo_paint (cr);
210
211         cairo_destroy (cr);
212         cairo_surface_destroy (image);
213
214         for (y = 0; y < height; y++) {
215                 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
216
217                 for (x = 0; x < width; x++) {
218                         guchar tmp;
219                         
220 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
221                         tmp = p[0];
222                         p[0] = p[2];
223                         p[2] = tmp;
224                         p[3] = (has_alpha) ? p[3] : 0xff;
225 #else
226                         tmp = p[0];
227                         p[0] = p[1];
228                         p[1] = p[2];
229                         p[2] = p[3];
230                         p[3] = (has_alpha) ? tmp : 0xff;
231 #endif                  
232                         p += pixbuf_n_channels;
233                 }
234         }
235
236         return pixbuf;
237 }
238
239 cairo_surface_t *
240 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
241                                            gint             dest_width,
242                                            gint             dest_height,
243                                            gint             dest_rotation)
244 {
245         cairo_surface_t *new_surface;
246         cairo_t         *cr;
247         gint             width, height;
248         gint             new_width = dest_width;
249         gint             new_height = dest_height;
250
251         width = cairo_image_surface_get_width (surface);
252         height = cairo_image_surface_get_height (surface);
253         
254         if (dest_width == width &&
255             dest_height == height &&
256             dest_rotation == 0) {
257                 return cairo_surface_reference (surface);
258         }
259
260         if (dest_rotation == 90 || dest_rotation == 270) {
261                 new_width = dest_height;
262                 new_height = dest_width;
263         }
264
265         new_surface = cairo_surface_create_similar (surface,
266                                                     cairo_surface_get_content (surface),
267                                                     new_width, new_height);
268
269         cr = cairo_create (new_surface);
270         switch (dest_rotation) {
271                 case 90:
272                         cairo_translate (cr, new_width, 0);
273                         break;
274                 case 180:
275                         cairo_translate (cr, new_width, new_height);
276                         break;
277                 case 270:
278                         cairo_translate (cr, 0, new_height);
279                         break;
280                 default:
281                         cairo_translate (cr, 0, 0);
282         }
283         
284         if (dest_width != width || dest_height != height) {
285                 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
286                 cairo_scale (cr,
287                              (gdouble)dest_width / width,
288                              (gdouble)dest_height / height);
289         }
290         
291         cairo_rotate (cr, dest_rotation * G_PI / 180.0);
292         cairo_set_source_surface (cr, surface, 0, 0);
293         cairo_paint (cr);
294         cairo_destroy (cr);
295
296         return new_surface;
297 }
298
299 void
300 ev_document_misc_invert_surface (cairo_surface_t *surface) {
301 #if CAIRO_VERSION > CAIRO_VERSION_ENCODE(1, 9, 2)
302         cairo_t *cr;
303
304         cr = cairo_create (surface);
305
306         /* white + DIFFERENCE -> invert */
307         cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
308         cairo_set_source_rgb (cr, 1., 1., 1.);
309         cairo_paint(cr);
310         cairo_destroy (cr);
311 #else
312         guchar *data;
313         gint    rowstride;
314         gint    width, height;
315         gint    x, y;
316
317         data = cairo_image_surface_get_data (surface);
318         rowstride = cairo_image_surface_get_stride (surface);
319         width = cairo_image_surface_get_width (surface);
320         height = cairo_image_surface_get_height (surface);
321
322         for (y = 0; y < height; y++) {
323                 guchar *p = data + y * rowstride;
324
325                 for (x = 0; x < width; x++) {
326                         p[0] = 255 - p[0];
327                         p[1] = 255 - p[1];
328                         p[2] = 255 - p[2];
329                         p += 4;
330                 }
331         }
332
333         cairo_surface_mark_dirty (surface);
334 #endif
335 }
336
337 void
338 ev_document_misc_invert_pixbuf (GdkPixbuf *pixbuf)
339 {
340         guchar *data, *p;
341         guint   width, height, x, y, rowstride, n_channels;
342
343         n_channels = gdk_pixbuf_get_n_channels (pixbuf);
344         g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
345         g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
346
347         /* First grab a pointer to the raw pixel data. */
348         data = gdk_pixbuf_get_pixels (pixbuf);
349
350         /* Find the number of bytes per row (could be padded). */
351         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
352
353         width = gdk_pixbuf_get_width (pixbuf);
354         height = gdk_pixbuf_get_height (pixbuf);
355         for (x = 0; x < width; x++) {
356                 for (y = 0; y < height; y++) {
357                         /* Calculate pixel's offset into the data array. */
358                         p = data + x * n_channels + y * rowstride;
359                         /* Change the RGB values*/
360                         p[0] = 255 - p[0];
361                         p[1] = 255 - p[1];
362                         p[2] = 255 - p[2];
363                 }
364         }
365 }