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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include "ev-document-misc.h"
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
36 ev_document_misc_get_thumbnail_frame (int width,
38 GdkPixbuf *source_pixbuf)
44 int width_r, height_r;
47 g_return_val_if_fail (GDK_IS_PIXBUF (source_pixbuf), NULL);
50 width_r = gdk_pixbuf_get_width (source_pixbuf);
51 height_r = gdk_pixbuf_get_height (source_pixbuf);
57 /* make sure no one is passing us garbage */
58 g_assert (width_r >= 0 && height_r >= 0);
60 retval = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
65 /* make it black and fill in the middle */
66 data = gdk_pixbuf_get_pixels (retval);
67 rowstride = gdk_pixbuf_get_rowstride (retval);
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);
73 /* copy the source pixbuf */
75 gdk_pixbuf_copy_area (source_pixbuf, 0, 0,
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;
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;
95 ev_document_misc_get_page_border_size (gint page_width,
103 if (page_width < 100) {
106 } else if (page_width < 500) {
117 ev_document_misc_paint_one_page (GdkDrawable *drawable,
122 gboolean inverted_colors)
124 GtkStyle *style = gtk_widget_get_style (widget);
125 GtkStateType state = gtk_widget_get_state (widget);
127 gdk_draw_rectangle (drawable,
128 highlight ? style->text_gc[state] : style->dark_gc[state],
134 gdk_draw_rectangle (drawable,
135 inverted_colors ? style->black_gc : style->white_gc,
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],
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],
151 area->x + area->width - (border->right - border->left),
153 border->right - border->left,
154 border->right - border->left);
159 ev_document_misc_surface_from_pixbuf (GdkPixbuf *pixbuf)
161 cairo_surface_t *surface;
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);
177 ev_document_misc_pixbuf_from_surface (cairo_surface_t *surface)
180 cairo_surface_t *image;
184 cairo_format_t surface_format;
185 gint pixbuf_n_channels;
186 gint pixbuf_rowstride;
187 guchar *pixbuf_pixels;
190 width = cairo_image_surface_get_width (surface);
191 height = cairo_image_surface_get_height (surface);
193 surface_format = cairo_image_surface_get_format (surface);
194 has_alpha = (surface_format == CAIRO_FORMAT_ARGB32);
196 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
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);
203 image = cairo_image_surface_create_for_data (pixbuf_pixels,
207 cr = cairo_create (image);
208 cairo_set_source_surface (cr, surface, 0, 0);
211 cairo_mask_surface (cr, surface, 0, 0);
216 cairo_surface_destroy (image);
218 for (y = 0; y < height; y++) {
219 guchar *p = pixbuf_pixels + y * pixbuf_rowstride;
221 for (x = 0; x < width; x++) {
224 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
228 p[3] = (has_alpha) ? p[3] : 0xff;
234 p[3] = (has_alpha) ? tmp : 0xff;
236 p += pixbuf_n_channels;
244 ev_document_misc_surface_rotate_and_scale (cairo_surface_t *surface,
249 cairo_surface_t *new_surface;
252 gint new_width = dest_width;
253 gint new_height = dest_height;
255 width = cairo_image_surface_get_width (surface);
256 height = cairo_image_surface_get_height (surface);
258 if (dest_width == width &&
259 dest_height == height &&
260 dest_rotation == 0) {
261 return cairo_surface_reference (surface);
264 if (dest_rotation == 90 || dest_rotation == 270) {
265 new_width = dest_height;
266 new_height = dest_width;
269 new_surface = cairo_surface_create_similar (surface,
270 cairo_surface_get_content (surface),
271 new_width, new_height);
273 cr = cairo_create (new_surface);
274 switch (dest_rotation) {
276 cairo_translate (cr, new_width, 0);
279 cairo_translate (cr, new_width, new_height);
282 cairo_translate (cr, 0, new_height);
285 cairo_translate (cr, 0, 0);
288 if (dest_width != width || dest_height != height) {
289 cairo_pattern_set_filter (cairo_get_source (cr), CAIRO_FILTER_BILINEAR);
291 (gdouble)dest_width / width,
292 (gdouble)dest_height / height);
295 cairo_rotate (cr, dest_rotation * G_PI / 180.0);
296 cairo_set_source_surface (cr, surface, 0, 0);
304 ev_document_misc_invert_surface (cairo_surface_t *surface) {
305 #if CAIRO_VERSION > CAIRO_VERSION_ENCODE(1, 9, 2)
308 cr = cairo_create (surface);
310 /* white + DIFFERENCE -> invert */
311 cairo_set_operator (cr, CAIRO_OPERATOR_DIFFERENCE);
312 cairo_set_source_rgb (cr, 1., 1., 1.);
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);
326 for (y = 0; y < height; y++) {
327 guchar *p = data + y * rowstride;
329 for (x = 0; x < width; x++) {
337 cairo_surface_mark_dirty (surface);
342 ev_document_misc_invert_pixbuf (GdkPixbuf *pixbuf)
345 guint width, height, x, y, rowstride, n_channels;
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);
351 /* First grab a pointer to the raw pixel data. */
352 data = gdk_pixbuf_get_pixels (pixbuf);
354 /* Find the number of bytes per row (could be padded). */
355 rowstride = gdk_pixbuf_get_rowstride (pixbuf);
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*/
372 ev_document_misc_get_screen_dpi (GdkScreen *screen)
376 /*diagonal in pixels*/
377 dp = hypot (gdk_screen_get_width (screen), gdk_screen_get_height (screen));
379 /*diagonal in inches*/
380 di = hypot (gdk_screen_get_width_mm(screen), gdk_screen_get_height_mm (screen)) / 25.4;