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
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)
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.
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.
27 #include "ev-document-misc.h"
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
35 ev_document_misc_get_thumbnail_frame (int width,
37 GdkPixbuf *source_pixbuf)
43 int width_r, height_r;
46 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
49 width_r = gdk_pixbuf_get_width (source_pixbuf);
50 height_r = gdk_pixbuf_get_height (source_pixbuf);
56 /* make sure no one is passing us garbage */
57 g_assert (width_r >= 0 && height_r >= 0);
59 retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
64 /* make it black and fill in the middle */
65 data = gdk_pixbuf_get_pixels (retval);
66 rowstride = gdk_pixbuf_get_rowstride (retval);
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);
72 /* copy the source pixbuf */
74 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
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;
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;
94 ev_document_misc_get_page_border_size (gint page_width,
102 if (page_width < 100) {
105 } else if (page_width < 500) {
116 ev_document_misc_paint_one_page (GdkDrawable *drawable,
122 gdk_draw_rectangle (drawable,
124 widget->style->text_gc[widget->state] : widget->style->dark_gc[widget->state],
130 gdk_draw_rectangle (drawable,
131 widget->style->white_gc,
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],
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],
147 area->x + area->width - (border->right - border->left),
149 border->right - border->left,
150 border->right - border->left);
155 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
157 cairo_surface_t *surface;
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);
173 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
176 cairo_surface_t *image;
180 cairo_format_t surface_format;
181 gint pixbuf_n_channels;
182 gint pixbuf_rowstride;
183 guchar *pixbuf_pixels;
186 width = cairo_image_surface_get_width (surface);
187 height = cairo_image_surface_get_height (surface);
189 surface_format = cairo_image_surface_get_format (surface);
190 has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
192 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
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);
199 image = cairo_image_surface_create_for_data (pixbuf_pixels,
203 cr = cairo_create (image);
204 cairo_set_source_surface (cr, surface, 0, 0);
207 cairo_mask_surface (cr, surface, 0, 0);
212 cairo_surface_destroy (image);
214 for (y = 0; y < height; y++) {
215 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
217 for (x = 0; x < width; x++) {
220 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
224 p[3] = (has_alpha) ? p[3] : 0xff;
230 p[3] = (has_alpha) ? tmp : 0xff;
232 p += pixbuf_n_channels;
240 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
245 cairo_surface_t *new_surface;
248 gint new_width = dest_width;
249 gint new_height = dest_height;
251 width = cairo_image_surface_get_width (surface);
252 height = cairo_image_surface_get_height (surface);
254 if (dest_width == width &&
255 dest_height == height &&
256 dest_rotation == 0) {
257 return cairo_surface_reference (surface);
260 if (dest_rotation == 90 || dest_rotation == 270) {
261 new_width = dest_height;
262 new_height = dest_width;
265 new_surface = cairo_surface_create_similar (surface,
266 cairo_surface_get_content (surface),
267 new_width, new_height);
269 cr = cairo_create (new_surface);
270 switch (dest_rotation) {
272 cairo_translate (cr, new_width, 0);
275 cairo_translate (cr, new_width, new_height);
278 cairo_translate (cr, 0, new_height);
281 cairo_translate (cr, 0, 0);
284 if (dest_width != width || dest_height != height) {
285 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
287 (gdouble)dest_width / width,
288 (gdouble)dest_height / height);
291 cairo_rotate (cr, dest_rotation * G_PI / 180.0);
292 cairo_set_source_surface (cr, surface, 0, 0);
300 ev_document_misc_invert_pixbuf (GdkPixbuf *pixbuf)
303 guint width, height, x, y, rowstride, n_channels;
305 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
306 g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
307 g_assert (gdk_pixbuf_get_bits_per_sample (pixbuf) == 8);
309 /* First grab a pointer to the raw pixel data. */
310 data = gdk_pixbuf_get_pixels (pixbuf);
312 /* Find the number of bytes per row (could be padded). */
313 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
315 width = gdk_pixbuf_get_width (pixbuf);
316 height = gdk_pixbuf_get_height (pixbuf);
317 for (x = 0; x < width; x++) {
318 for (y = 0; y < height; y++) {
319 /* Calculate pixel's offset into the data array. */
320 p = data + x * n_channels + y * rowstride;
321 /* Change the RGB values*/