]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-misc.c
Invert colors of pages when loading in inverted color mode
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20
21 #include <config.h>
22
23 #include <string.h>
24 #include <math.h>
25
26 #include <gtk/gtk.h>
27
28 #include "ev-document-misc.h"
29
30 /* Returns a new GdkPixbuf that is suitable for placing in the thumbnail view.
31  * It is four pixels wider and taller than the source.  If source_pixbuf is not
32  * NULL, then it will fill the return pixbuf with the contents of
33  * source_pixbuf.
34  */
35 GdkPixbuf *
36 ev_document_misc_get_thumbnail_frame (int        width,
37                                       int        height,
38                                       GdkPixbuf *source_pixbuf)
39 {
40         GdkPixbuf *retval;
41         guchar *data;
42         gint rowstride;
43         int i;
44         int width_r, height_r;
45
46         if (source_pixbuf)
47                 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
48
49         if (source_pixbuf) {
50                 width_r = gdk_pixbuf_get_width (source_pixbuf);
51                 height_r = gdk_pixbuf_get_height (source_pixbuf);
52         } else {
53                 width_r = width;
54                 height_r = height;
55         }
56
57         /* make sure no one is passing us garbage */
58         g_assert (width_r >= 0 && height_r >= 0);
59
60         retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
61                                  TRUE, 8,
62                                  width_r + 4,
63                                  height_r + 4);
64
65         /* make it black and fill in the middle */
66         data = gdk_pixbuf_get_pixels (retval);
67         rowstride = gdk_pixbuf_get_rowstride (retval);
68
69         gdk_pixbuf_fill (retval, 0x000000ff);
70         for (i = 1; i < height_r + 1; i++)
71                 memset (data + (rowstride * i) + 4, 0xffffffff, width_r * 4);
72
73         /* copy the source pixbuf */
74         if (source_pixbuf)
75                 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
76                                       width_r,
77                                       height_r,
78                                       retval,
79                                       1, 1);
80         /* Add the corner */
81         data [(width_r + 2) * 4 + 3] = 0;
82         data [(width_r + 3) * 4 + 3] = 0;
83         data [(width_r + 2) * 4 + (rowstride * 1) + 3] = 0;
84         data [(width_r + 3) * 4 + (rowstride * 1) + 3] = 0;
85
86         data [(height_r + 2) * rowstride + 3] = 0;
87         data [(height_r + 3) * rowstride + 3] = 0;
88         data [(height_r + 2) * rowstride + 4 + 3] = 0;
89         data [(height_r + 3) * rowstride + 4 + 3] = 0;
90
91         return retval;
92 }
93
94 void
95 ev_document_misc_get_page_border_size (gint       page_width,
96                                        gint       page_height,
97                                        GtkBorder *border)
98 {
99         g_assert (border);
100
101         border->left = 1;
102         border->top = 1;
103         if (page_width < 100) {
104                 border->right = 2;
105                 border->bottom = 2;
106         } else if (page_width < 500) {
107                 border->right = 3;
108                 border->bottom = 3;
109         } else {
110                 border->right = 4;
111                 border->bottom = 4;
112         }
113 }
114
115
116 void
117 ev_document_misc_paint_one_page (GdkDrawable  *drawable,
118                                  GtkWidget    *widget,
119                                  GdkRectangle *area,
120                                  GtkBorder    *border,
121                                  gboolean      highlight,
122                                  gboolean      inverted_colors)
123 {
124         GtkStyle    *style = gtk_widget_get_style (widget);
125         GtkStateType state = gtk_widget_get_state (widget);
126
127         gdk_draw_rectangle (drawable,
128                             highlight ? style->text_gc[state] : style->dark_gc[state],
129                             TRUE,
130                             area->x,
131                             area->y,
132                             area->width,
133                             area->height);
134         gdk_draw_rectangle (drawable,
135                             inverted_colors ? style->black_gc : style->white_gc,
136                             TRUE,
137                             area->x + border->left,
138                             area->y + border->top,
139                             area->width - (border->left + border->right),
140                             area->height - (border->top + border->bottom));
141         gdk_draw_rectangle (drawable,
142                             style->mid_gc[state],
143                             TRUE,
144                             area->x,
145                             area->y + area->height - (border->bottom - border->top),
146                             border->bottom - border->top,
147                             border->bottom - border->top);
148         gdk_draw_rectangle (drawable,
149                             style->mid_gc[state],
150                             TRUE,
151                             area->x + area->width - (border->right - border->left),
152                             area->y,
153                             border->right - border->left,
154                             border->right - border->left);
155
156 }
157
158 cairo_surface_t *
159 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
160 {
161         cairo_surface_t *surface;
162         cairo_t         *cr;
163
164         surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
165                                               CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
166                                               gdk_pixbuf_get_width (pixbuf),
167                                               gdk_pixbuf_get_height (pixbuf));
168         cr = cairo_create (surface);
169         gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
170         cairo_paint (cr);
171         cairo_destroy (cr);
172         
173         return surface;
174 }
175
176 GdkPixbuf *
177 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
178 {
179         GdkPixbuf       *pixbuf;
180         cairo_surface_t *image;
181         cairo_t         *cr;
182         gboolean         has_alpha;
183         gint             width, height;
184         cairo_format_t   surface_format;
185         gint             pixbuf_n_channels;
186         gint             pixbuf_rowstride;
187         guchar          *pixbuf_pixels;
188         gint             x, y;
189
190         width = cairo_image_surface_get_width (surface);
191         height = cairo_image_surface_get_height (surface);
192         
193         surface_format = cairo_image_surface_get_format (surface);
194         has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
195
196         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
197                                  TRUE, 8,
198                                  width, height);
199         pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
200         pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
201         pixbuf_pixels = gdk_pixbuf_get_pixels (pixbuf);
202
203         image = cairo_image_surface_create_for_data (pixbuf_pixels,
204                                                      surface_format,
205                                                      width, height,
206                                                      pixbuf_rowstride);
207         cr = cairo_create (image);
208         cairo_set_source_surface (cr, surface, 0, 0);
209
210         if (has_alpha)
211                 cairo_mask_surface (cr, surface, 0, 0);
212         else
213                 cairo_paint (cr);
214
215         cairo_destroy (cr);
216         cairo_surface_destroy (image);
217
218         for (y = 0; y < height; y++) {
219                 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
220
221                 for (x = 0; x < width; x++) {
222                         guchar tmp;
223                         
224 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
225                         tmp = p[0];
226                         p[0] = p[2];
227                         p[2] = tmp;
228                         p[3] = (has_alpha) ? p[3] : 0xff;
229 #else
230                         tmp = p[0];
231                         p[0] = p[1];
232                         p[1] = p[2];
233                         p[2] = p[3];
234                         p[3] = (has_alpha) ? tmp : 0xff;
235 #endif                  
236                         p += pixbuf_n_channels;
237                 }
238         }
239
240         return pixbuf;
241 }
242
243 cairo_surface_t *
244 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
245                                            gint             dest_width,
246                                            gint             dest_height,
247                                            gint             dest_rotation)
248 {
249         cairo_surface_t *new_surface;
250         cairo_t         *cr;
251         gint             width, height;
252         gint             new_width = dest_width;
253         gint             new_height = dest_height;
254
255         width = cairo_image_surface_get_width (surface);
256         height = cairo_image_surface_get_height (surface);
257         
258         if (dest_width == width &&
259             dest_height == height &&
260             dest_rotation == 0) {
261                 return cairo_surface_reference (surface);
262         }
263
264         if (dest_rotation == 90 || dest_rotation == 270) {
265                 new_width = dest_height;
266                 new_height = dest_width;
267         }
268
269         new_surface = cairo_surface_create_similar (surface,
270                                                     cairo_surface_get_content (surface),
271                                                     new_width, new_height);
272
273         cr = cairo_create (new_surface);
274         switch (dest_rotation) {
275                 case 90:
276                         cairo_translate (cr, new_width, 0);
277                         break;
278                 case 180:
279                         cairo_translate (cr, new_width, new_height);
280                         break;
281                 case 270:
282                         cairo_translate (cr, 0, new_height);
283                         break;
284                 default:
285                         cairo_translate (cr, 0, 0);
286         }
287         
288         if (dest_width != width || dest_height != height) {
289                 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
290                 cairo_scale (cr,
291                              (gdouble)dest_width / width,
292                              (gdouble)dest_height / height);
293         }
294         
295         cairo_rotate (cr, dest_rotation * G_PI / 180.0);
296         cairo_set_source_surface (cr, surface, 0, 0);
297         cairo_paint (cr);
298         cairo_destroy (cr);
299
300         return new_surface;
301 }
302
303 void
304 ev_document_misc_invert_surface (cairo_surface_t *surface) {
305 #if CAIRO_VERSION > CAIRO_VERSION_ENCODE(1, 9, 2)
306         cairo_t *cr;
307
308         cr = cairo_create (surface);
309
310         /* white + DIFFERENCE -> invert */
311         cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
312         cairo_set_source_rgb (cr, 1., 1., 1.);
313         cairo_paint(cr);
314         cairo_destroy (cr);
315 #else
316         guchar *data;
317         gint    rowstride;
318         gint    width, height;
319         gint    x, y;
320
321         data = cairo_image_surface_get_data (surface);
322         rowstride = cairo_image_surface_get_stride (surface);
323         width = cairo_image_surface_get_width (surface);
324         height = cairo_image_surface_get_height (surface);
325
326         for (y = 0; y < height; y++) {
327                 guchar *p = data + y * rowstride;
328
329                 for (x = 0; x < width; x++) {
330                         p[0] = 255 - p[0];
331                         p[1] = 255 - p[1];
332                         p[2] = 255 - p[2];
333                         p += 4;
334                 }
335         }
336
337         cairo_surface_mark_dirty (surface);
338 #endif
339 }
340
341 void
342 ev_document_misc_invert_pixbuf (GdkPixbuf *pixbuf)
343 {
344         guchar *data, *p;
345         guint   width, height, x, y, rowstride, n_channels;
346
347         n_channels = gdk_pixbuf_get_n_channels (pixbuf);
348         g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
349         g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
350
351         /* First grab a pointer to the raw pixel data. */
352         data = gdk_pixbuf_get_pixels (pixbuf);
353
354         /* Find the number of bytes per row (could be padded). */
355         rowstride = gdk_pixbuf_get_rowstride (pixbuf);
356
357         width = gdk_pixbuf_get_width (pixbuf);
358         height = gdk_pixbuf_get_height (pixbuf);
359         for (x = 0; x < width; x++) {
360                 for (y = 0; y < height; y++) {
361                         /* Calculate pixel's offset into the data array. */
362                         p = data + x * n_channels + y * rowstride;
363                         /* Change the RGB values*/
364                         p[0] = 255 - p[0];
365                         p[1] = 255 - p[1];
366                         p[2] = 255 - p[2];
367                 }
368         }
369 }
370
371 gdouble
372 ev_document_misc_get_screen_dpi (GdkScreen *screen)
373 {
374         gdouble dp, di;
375
376         /*diagonal in pixels*/
377         dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
378
379         /*diagonal in inches*/
380         di = hypot (gdk_screen_get_width_mm(screen), gdk_screen_get_height_mm (screen)) / 25.4;
381
382         return (dp / di);
383 }