2 Copyright (C) 2005 Fernando Herrera <fherrera@onirica.com>
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.
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.
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.
21 #include <evince-document.h>
28 #define THUMBNAIL_SIZE 128
30 static gint size = THUMBNAIL_SIZE;
31 static const gchar **file_arguments;
33 static const GOptionEntry goption_options[] = {
34 { "size", 's', 0, G_OPTION_ARG_INT, &size, NULL, "SIZE" },
35 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &file_arguments, NULL, "<input> <ouput>" },
47 evince_thumbnailer_get_document (const gchar *uri)
49 EvDocument *document = NULL;
52 document = ev_document_factory_get_document (uri, &error);
54 if (error->domain == EV_DOCUMENT_ERROR &&
55 error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
56 /* FIXME: Create a thumb for cryp docs */
68 evince_thumbnail_pngenc_get (EvDocument *document, const char *thumbnail, int size)
75 page = ev_document_get_page (document, 0);
77 ev_document_get_page_size (document, page, &width, &height);
79 rc = ev_render_context_new (page, 0, size / width);
80 pixbuf = ev_document_thumbnails_get_thumbnail (EV_DOCUMENT_THUMBNAILS (document),
83 g_object_unref (page);
86 const char *overlaid_icon_name = NULL;
88 if (overlaid_icon_name) {
89 GdkPixbuf *overlaid_pixbuf;
91 gchar *overlaid_icon_path = g_strdup_printf ("%s/%s", DATADIR, overlaid_icon_name);
92 overlaid_pixbuf = gdk_pixbuf_new_from_file (overlaid_icon_path, NULL);
93 g_free (overlaid_icon_path);
94 if (overlaid_pixbuf != NULL) {
95 int delta_height, delta_width;
97 delta_width = gdk_pixbuf_get_width (pixbuf) -
98 gdk_pixbuf_get_width (overlaid_pixbuf);
99 delta_height = gdk_pixbuf_get_height (pixbuf) -
100 gdk_pixbuf_get_height (overlaid_pixbuf);
102 gdk_pixbuf_composite (overlaid_pixbuf, pixbuf,
103 delta_width, delta_height,
104 gdk_pixbuf_get_width (overlaid_pixbuf),
105 gdk_pixbuf_get_height (overlaid_pixbuf),
106 delta_width, delta_height,
108 GDK_INTERP_NEAREST, 100);
110 g_object_unref (overlaid_pixbuf);
114 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
115 g_object_unref (pixbuf);
119 g_object_unref (pixbuf);
126 evince_thumbnail_pngenc_get_async (struct AsyncData *data)
128 ev_document_doc_mutex_lock ();
129 data->success = evince_thumbnail_pngenc_get (data->document,
132 ev_document_doc_mutex_unlock ();
134 g_idle_add ((GSourceFunc)gtk_main_quit, NULL);
140 print_usage (GOptionContext *context)
144 help = g_option_context_get_help (context, TRUE, NULL);
145 g_print ("%s", help);
150 main (int argc, char *argv[])
152 EvDocument *document;
153 GOptionContext *context;
158 GError *error = NULL;
160 context = g_option_context_new ("- GNOME Document Thumbnailer");
161 g_option_context_add_main_entries (context, goption_options, NULL);
163 if (!g_option_context_parse (context, &argc, &argv, &error)) {
164 g_printerr ("%s\n", error->message);
165 g_error_free (error);
166 print_usage (context);
167 g_option_context_free (context);
172 input = file_arguments ? file_arguments[0] : NULL;
173 output = input ? file_arguments[1] : NULL;
174 if (!input || !output) {
175 print_usage (context);
176 g_option_context_free (context);
181 g_option_context_free (context);
184 g_print ("Size cannot be smaller than 40 pixels\n");
188 input = file_arguments[0];
189 output = file_arguments[1];
193 if (!g_thread_supported ())
194 g_thread_init (NULL);
199 file = g_file_new_for_commandline_arg (input);
200 uri = g_file_get_uri (file);
201 document = evince_thumbnailer_get_document (uri);
203 g_object_unref (file);
211 if (!EV_IS_DOCUMENT_THUMBNAILS (document)) {
212 g_object_unref (document);
217 if (EV_IS_ASYNC_RENDERER (document)) {
218 struct AsyncData data;
220 gtk_init (&argc, &argv);
222 data.document = document;
223 data.output = output;
226 g_thread_create ((GThreadFunc) evince_thumbnail_pngenc_get_async,
231 g_object_unref (document);
234 return data.success ? 0 : -2;
237 if (!evince_thumbnail_pngenc_get (document, output, size)) {
238 g_object_unref (document);
243 g_object_unref (document);