]> www.fi.muni.cz Git - evince.git/blob - libdocument/ev-document-misc.c
Fix bytes order on big endian. Patch by Benjamin Jacobs. Fixes bug
[evince.git] / libdocument / ev-document-misc.c
1 /*
2  *  Copyright (c) 2007 Carlos Garcia Campos <carlosgc@gnome.org>
3  *  Copyright (C) 2000-2003 Marco Pesenti Gritti
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 #include <config.h>
21 #include "ev-document-misc.h"
22 #include <string.h>
23 #include <gtk/gtk.h>
24
25 /* Returns a new GdkPixbuf that is suitable for placing in the thumbnail view.
26  * It is four pixels wider and taller than the source.  If source_pixbuf is not
27  * NULL, then it will fill the return pixbuf with the contents of
28  * source_pixbuf.
29  */
30 GdkPixbuf *
31 ev_document_misc_get_thumbnail_frame (int        width,
32                                       int        height,
33                                       GdkPixbuf *source_pixbuf)
34 {
35         GdkPixbuf *retval;
36         guchar *data;
37         gint rowstride;
38         int i;
39         int width_r, height_r;
40
41         if (source_pixbuf)
42                 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
43
44         if (source_pixbuf) {
45                 width_r = gdk_pixbuf_get_width (source_pixbuf);
46                 height_r = gdk_pixbuf_get_height (source_pixbuf);
47         } else {
48                 width_r = width;
49                 height_r = height;
50         }
51
52         /* make sure no one is passing us garbage */
53         g_assert (width_r >= 0 && height_r >= 0);
54
55         retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
56                                  TRUE, 8,
57                                  width_r + 4,
58                                  height_r + 4);
59
60         /* make it black and fill in the middle */
61         data = gdk_pixbuf_get_pixels (retval);
62         rowstride = gdk_pixbuf_get_rowstride (retval);
63
64         gdk_pixbuf_fill (retval, 0x000000ff);
65         for (i = 1; i < height_r + 1; i++)
66                 memset (data + (rowstride * i) + 4, 0xffffffff, width_r * 4);
67
68         /* copy the source pixbuf */
69         if (source_pixbuf)
70                 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
71                                       width_r,
72                                       height_r,
73                                       retval,
74                                       1, 1);
75         /* Add the corner */
76         data [(width_r + 2) * 4 + 3] = 0;
77         data [(width_r + 3) * 4 + 3] = 0;
78         data [(width_r + 2) * 4 + (rowstride * 1) + 3] = 0;
79         data [(width_r + 3) * 4 + (rowstride * 1) + 3] = 0;
80
81         data [(height_r + 2) * rowstride + 3] = 0;
82         data [(height_r + 3) * rowstride + 3] = 0;
83         data [(height_r + 2) * rowstride + 4 + 3] = 0;
84         data [(height_r + 3) * rowstride + 4 + 3] = 0;
85
86         return retval;
87 }
88
89 void
90 ev_document_misc_get_page_border_size (gint       page_width,
91                                        gint       page_height,
92                                        GtkBorder *border)
93 {
94         g_assert (border);
95
96         border->left = 1;
97         border->top = 1;
98         if (page_width < 100) {
99                 border->right = 2;
100                 border->bottom = 2;
101         } else if (page_width < 500) {
102                 border->right = 3;
103                 border->bottom = 3;
104         } else {
105                 border->right = 4;
106                 border->bottom = 4;
107         }
108 }
109
110
111 void
112 ev_document_misc_paint_one_page (GdkDrawable  *drawable,
113                                  GtkWidget    *widget,
114                                  GdkRectangle *area,
115                                  GtkBorder    *border,
116                                  gboolean highlight)
117 {
118         gdk_draw_rectangle (drawable,
119                             highlight ?
120                                     widget->style->text_gc[widget->state] : widget->style->dark_gc[widget->state],
121                             TRUE,
122                             area->x,
123                             area->y,
124                             area->width,
125                             area->height);
126         gdk_draw_rectangle (drawable,
127                             widget->style->white_gc,
128                             TRUE,
129                             area->x + border->left,
130                             area->y + border->top,
131                             area->width - (border->left + border->right),
132                             area->height - (border->top + border->bottom));
133         gdk_draw_rectangle (drawable,
134                             widget->style->mid_gc[widget->state],
135                             TRUE,
136                             area->x,
137                             area->y + area->height - (border->bottom - border->top),
138                             border->bottom - border->top,
139                             border->bottom - border->top);
140         gdk_draw_rectangle (drawable,
141                             widget->style->mid_gc[widget->state],
142                             TRUE,
143                             area->x + area->width - (border->right - border->left),
144                             area->y,
145                             border->right - border->left,
146                             border->right - border->left);
147
148 }
149
150 cairo_surface_t *
151 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
152 {
153         cairo_surface_t *surface;
154         cairo_t         *cr;
155
156         surface = cairo_image_surface_create (gdk_pixbuf_get_has_alpha (pixbuf) ?
157                                               CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
158                                               gdk_pixbuf_get_width (pixbuf),
159                                               gdk_pixbuf_get_height (pixbuf));
160         cr = cairo_create (surface);
161         gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
162         cairo_paint (cr);
163         cairo_destroy (cr);
164         
165         return surface;
166 }
167
168 GdkPixbuf *
169 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
170 {
171         GdkPixbuf       *pixbuf;
172         cairo_surface_t *image;
173         cairo_t         *cr;
174         gboolean         has_alpha;
175         gint             width, height;
176         cairo_format_t   surface_format;
177         gint             pixbuf_n_channels;
178         gint             pixbuf_rowstride;
179         guchar          *pixbuf_pixels;
180         gint             x, y;
181
182         width = cairo_image_surface_get_width (surface);
183         height = cairo_image_surface_get_height (surface);
184         
185         surface_format = cairo_image_surface_get_format (surface);
186         has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
187
188         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
189                                  TRUE, 8,
190                                  width, height);
191         pixbuf_n_channels = gdk_pixbuf_get_n_channels (pixbuf);
192         pixbuf_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
193         pixbuf_pixels = gdk_pixbuf_get_pixels (pixbuf);
194
195         image = cairo_image_surface_create_for_data (pixbuf_pixels,
196                                                      surface_format,
197                                                      width, height,
198                                                      pixbuf_rowstride);
199         cr = cairo_create (image);
200         cairo_set_source_surface (cr, surface, 0, 0);
201
202         if (has_alpha)
203                 cairo_mask_surface (cr, surface, 0, 0);
204         else
205                 cairo_paint (cr);
206
207         cairo_destroy (cr);
208         cairo_surface_destroy (image);
209
210         for (y = 0; y < height; y++) {
211                 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
212
213                 for (x = 0; x < width; x++) {
214                         guchar tmp;
215                         
216 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
217                         tmp = p[0];
218                         p[0] = p[2];
219                         p[2] = tmp;
220                         p[3] = (has_alpha) ? p[3] : 0xff;
221 #else
222                         tmp = p[0];
223                         p[0] = p[1];
224                         p[1] = p[2];
225                         p[2] = p[3];
226                         p[3] = (has_alpha) ? tmp : 0xff;
227 #endif                  
228                         p += pixbuf_n_channels;
229                 }
230         }
231
232         return pixbuf;
233 }
234
235 cairo_surface_t *
236 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
237                                            gint             dest_width,
238                                            gint             dest_height,
239                                            gint             dest_rotation)
240 {
241         cairo_surface_t *new_surface;
242         cairo_t         *cr;
243         gint             width, height;
244         gint             new_width = dest_width;
245         gint             new_height = dest_height;
246
247         width = cairo_image_surface_get_width (surface);
248         height = cairo_image_surface_get_height (surface);
249         
250         if (dest_width == width &&
251             dest_height == height &&
252             dest_rotation == 0) {
253                 return cairo_surface_reference (surface);
254         }
255
256         if (dest_rotation == 90 || dest_rotation == 270) {
257                 new_width = dest_height;
258                 new_height = dest_width;
259         }
260
261         new_surface = cairo_surface_create_similar (surface,
262                                                     cairo_surface_get_content (surface),
263                                                     new_width, new_height);
264
265         cr = cairo_create (new_surface);
266         switch (dest_rotation) {
267                 case 90:
268                         cairo_translate (cr, new_width, 0);
269                         break;
270                 case 180:
271                         cairo_translate (cr, new_width, new_height);
272                         break;
273                 case 270:
274                         cairo_translate (cr, 0, new_height);
275                         break;
276                 default:
277                         cairo_translate (cr, 0, 0);
278         }
279         
280         if (dest_width != width || dest_height != height) {
281                 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
282                 cairo_scale (cr,
283                              (gdouble)dest_width / width,
284                              (gdouble)dest_height / height);
285         }
286         
287         cairo_rotate (cr, dest_rotation * G_PI / 180.0);
288         cairo_set_source_surface (cr, surface, 0, 0);
289         cairo_paint (cr);
290         cairo_destroy (cr);
291
292         return new_surface;
293 }
294