]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
b3c8285ee3d83debba1e41578b18f0f4a7a21124
[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 <config.h>
20
21 #include <ev-document.h>
22 #include <ev-document-thumbnails.h>
23 #include <ev-async-renderer.h>
24 #include <ev-document-factory.h>
25 #include <ev-backends-manager.h>
26
27 #include <gio/gio.h>
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #define THUMBNAIL_SIZE 128
33
34 struct AsyncData {
35         EvDocument  *document;
36         const gchar *output;
37         gint         size;
38         gboolean     success;
39 };
40
41 static EvDocument *
42 evince_thumbnailer_get_document (const gchar *uri)
43 {
44         EvDocument *document = NULL;
45         GError     *error = NULL;
46
47         document = ev_document_factory_get_document  (uri, &error);
48         if (error) {
49                 if (error->domain == EV_DOCUMENT_ERROR &&
50                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
51                         /* FIXME: Create a thumb for cryp docs */
52                         g_error_free (error);
53                         return NULL;
54                 }
55                 g_error_free (error);
56                 return NULL;
57         }
58         
59         return document;
60 }
61
62 static gboolean
63 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
64 {
65         EvRenderContext *rc;
66         double width, height;
67         GdkPixbuf *pixbuf;
68         EvPage *page;
69
70         page = ev_document_get_page (document, 0);
71         
72         ev_document_get_page_size (document, page, &width, &height);
73
74         rc = ev_render_context_new (page, 0, size / width);
75         pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
76                                                        rc, FALSE);
77         g_object_unref (rc);
78         g_object_unref (page);
79         
80         if (pixbuf != NULL) {
81                 const char *overlaid_icon_name = NULL;
82
83                 if (overlaid_icon_name) {
84                         GdkPixbuf *overlaid_pixbuf;
85
86                         gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
87                         overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
88                         g_free (overlaid_icon_path);
89                         if (overlaid_pixbuf != NULL) {
90                                 int delta_height, delta_width;
91                                 
92                                 delta_width = gdk_pixbuf_get_width (pixbuf) -
93                                         gdk_pixbuf_get_width (overlaid_pixbuf);
94                                 delta_height = gdk_pixbuf_get_height (pixbuf) -
95                                         gdk_pixbuf_get_height (overlaid_pixbuf);
96                                 
97                                 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
98                                                       delta_width, delta_height,
99                                                       gdk_pixbuf_get_width (overlaid_pixbuf),
100                                                       gdk_pixbuf_get_height (overlaid_pixbuf),
101                                                       delta_width, delta_height,
102                                                       1, 1,
103                                                       GDK_INTERP_NEAREST, 100);
104                                 
105                                 g_object_unref  (overlaid_pixbuf);
106                         }
107                 }
108                 
109                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
110                         g_object_unref  (pixbuf);
111                         return TRUE;
112                 }
113
114                 g_object_unref  (pixbuf);
115         }
116         
117         return FALSE;
118 }
119
120 static gpointer
121 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
122 {
123         ev_document_doc_mutex_lock ();
124         data->success = evince_thumbnail_pngenc_get (data->document,
125                                                      data->output,
126                                                      data->size);
127         ev_document_doc_mutex_unlock ();
128         
129         g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
130         
131         return NULL;
132 }
133
134 int
135 main (int argc, char *argv[])
136 {
137         EvDocument *document;
138         const char *input;
139         const char *output;
140         int         size;
141         char       *uri;
142         GFile      *file;
143
144         if (argc <= 2 || argc > 5 || strcmp (argv[1], "-h") == 0 ||
145             strcmp (argv[1], "--help") == 0) {
146                 g_print ("Usage: %s [-s <size>] <input> <output>\n", argv[0]);
147                 return -1;
148         }
149
150         if (!strcmp (argv[1], "-s")) {
151                 input = argv[3];
152                 output = argv[4];
153                 size = atoi (argv[2]);
154         } else {
155                 input = argv[1];
156                 output = argv[2];
157                 size = THUMBNAIL_SIZE;
158         }
159
160         if (size < 40) {
161                 g_print ("Size cannot be smaller than 40 pixels\n");
162                 return -1;
163         }
164
165         g_type_init ();
166
167         if (!g_thread_supported ())
168                 g_thread_init (NULL);
169
170         ev_backends_manager_init ();
171
172         file = g_file_new_for_commandline_arg (input);
173         uri = g_file_get_uri (file);
174         document = evince_thumbnailer_get_document (uri);
175
176         g_object_unref (file);
177         g_free (uri);
178
179         if (!document) {
180                 ev_backends_manager_shutdown ();
181                 return -2;
182         }
183
184         if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
185                 g_object_unref (document);
186                 ev_backends_manager_shutdown ();
187                 return -2;
188         }
189
190         if (EV_IS_ASYNC_RENDERER (document)) {
191                 struct AsyncData data;
192
193                 gtk_init (&argc, &argv);
194                 
195                 data.document = document;
196                 data.output = output;
197                 data.size = size;
198
199                 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
200                                  &data, FALSE, NULL);
201                 
202                 gtk_main ();
203
204                 g_object_unref (document);
205                 ev_backends_manager_shutdown ();
206
207                 return data.success ? 0 : -2;
208         }
209
210         if (!evince_thumbnail_pngenc_get (document, output, size)) {
211                 g_object_unref (document);
212                 ev_backends_manager_shutdown ();
213                 return -2;
214         }
215
216         g_object_unref (document);
217         ev_backends_manager_shutdown ();
218
219         return 0;
220 }