]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
Use an EvRenderContext for rendering thumbnails instead of a suggested
[evince.git] / thumbnailer / evince-thumbnailer.c
1 /*
2    Copyright (C) 2005 Fernando Herrera <fherrera@onirica.com>
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include <libgnomevfs/gnome-vfs-mime-utils.h>
20 #include <libgnomevfs/gnome-vfs-uri.h>
21 #include <libgnomevfs/gnome-vfs-utils.h>
22 #include <libgnomevfs/gnome-vfs-init.h>
23 #include <libgnomevfs/gnome-vfs-ops.h>
24
25 #include <ev-document.h>
26 #include <ev-document-thumbnails.h>
27 #include <ev-document-factory.h>
28
29 #include <string.h>
30
31 #define THUMBNAIL_SIZE 128
32
33 static gboolean
34 evince_thumbnail_pngenc_get (const char *uri, const char *thumbnail, int size)
35 {
36         EvDocument *document = NULL;
37         EvRenderContext *rc;
38         double width, height;
39         GError *error = NULL;
40         GdkPixbuf *pixbuf;
41
42         document = ev_document_factory_get_document  (uri, &error);
43
44         if (error) {
45                 if (error->domain == EV_DOCUMENT_ERROR &&
46                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
47                         /* FIXME: Create a thumb for cryp docs */
48                         g_error_free (error);
49                         return FALSE;
50                 }
51                 g_error_free (error);
52                 return FALSE;
53         }
54         
55         if (document == NULL) {
56                 return FALSE;
57         }
58
59         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
60                 return FALSE;
61         }
62
63         ev_document_get_page_size (document, 0, &width, &height);
64         rc = ev_render_context_new (0, 0, THUMBNAIL_SIZE / width);
65
66         pixbuf = ev_document_thumbnails_get_thumbnail
67                         (EV_DOCUMENT_THUMBNAILS (document), rc, FALSE);
68         
69         g_object_unref (rc);
70         
71         if (pixbuf != NULL) {
72                 const char *overlaid_icon_name = NULL;
73
74                 if (overlaid_icon_name) {
75                         GdkPixbuf *overlaid_pixbuf;
76
77                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
78                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
79                         g_free (overlaid_icon_path);
80                         if (overlaid_pixbuf != NULL) {
81                                 int delta_height, delta_width;
82                                 
83                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
84                                         gdk_pixbuf_get_width (overlaid_pixbuf);
85                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
86                                         gdk_pixbuf_get_height (overlaid_pixbuf);
87                                 
88                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
89                                                       delta_width, delta_height,
90                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
91                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
92                                                       delta_width, delta_height,
93                                                       1, 1,
94                                                       GDK_INTERP_NEAREST, 100);
95                                 
96                                 g_object_unref  (overlaid_pixbuf);
97                         }
98                 }
99                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
100                         g_object_unref  (pixbuf);
101                         g_object_unref (document);
102                         return TRUE;
103                 } else {
104                         g_object_unref  (pixbuf);
105                         g_object_unref (document);
106                 }
107         }
108         return FALSE;
109 }
110
111 int
112 main (int argc, char *argv[])
113 {
114         int res;
115         char *input, *output;
116         int size;
117         char *uri;
118
119         if (argc <= 2 || argc > 5 || strcmp (argv[1], "-h") == 0 ||
120             strcmp (argv[1], "--help") == 0) {
121                 g_print ("Usage: %s [-s <size>] <input> <output>\n", argv[0]);
122                 return -1;
123         }
124
125         res = gnome_vfs_init ();
126
127         if (!strcmp (argv[1], "-s")) {
128                 input = argv[3];
129                 output = argv[4];
130                 size = g_strtod (argv[2], NULL);
131         } else {
132                 input = argv[1];
133                 output = argv[2];
134                 size = THUMBNAIL_SIZE;
135         }
136
137         if (size < 40) {
138                 g_print ("Size cannot be smaller than 40 pixels\n");
139                 return -1;
140         }
141
142         uri = gnome_vfs_make_uri_from_shell_arg (input);
143
144         if (evince_thumbnail_pngenc_get (uri, output, size)) {
145                 g_free (uri);
146                 return 0;
147         } else {
148                 g_free (uri);
149                 return -2;
150         }
151 }