]> www.fi.muni.cz Git - evince.git/blob - thumbnailer/evince-thumbnailer.c
s/pdf-document/ev-poppler
[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 <ev-poppler.h>
20
21 #include <libgnomevfs/gnome-vfs-mime-utils.h>
22 #include <libgnomevfs/gnome-vfs-uri.h>
23 #include <libgnomevfs/gnome-vfs-utils.h>
24 #include <libgnomevfs/gnome-vfs-init.h>
25
26 #include <ev-document.h>
27 #include <ev-document-thumbnails.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         char *mime_type;
38         GError *error;
39         GdkPixbuf *pixbuf;
40
41         mime_type = gnome_vfs_get_mime_type (uri);
42         if (mime_type == NULL)
43                 return FALSE;
44
45         if (!strcmp (mime_type, "application/pdf"))
46                 document = g_object_new (PDF_TYPE_DOCUMENT, NULL);
47         else
48                 return FALSE;
49
50         if (!ev_document_load (document, uri, &error)) {
51                 if (error->domain == EV_DOCUMENT_ERROR &&
52                     error->code == EV_DOCUMENT_ERROR_ENCRYPTED) {
53                         /* FIXME: Create a thumb for cryp docs */
54                 }
55                 g_error_free (error);
56                 return FALSE;
57         }
58
59         pixbuf = ev_document_thumbnails_get_thumbnail
60                         (EV_DOCUMENT_THUMBNAILS (document), 1, size, FALSE);
61         
62         if (pixbuf != NULL) {
63                 GdkPixbuf *pdflogo;
64
65                 pdflogo = gdk_pixbuf_new_from_file (DATADIR"/pdf-icon.png", NULL);
66                 if (pdflogo != NULL) {
67                         int delta_height, delta_width;
68
69                         delta_width = gdk_pixbuf_get_width (pixbuf) -
70                                       gdk_pixbuf_get_width (pdflogo);
71                         delta_height = gdk_pixbuf_get_height (pixbuf) -
72                                        gdk_pixbuf_get_height (pdflogo);
73
74                         gdk_pixbuf_composite (pdflogo, pixbuf,
75                                               delta_width, delta_height,
76                                               gdk_pixbuf_get_width (pdflogo),
77                                               gdk_pixbuf_get_height (pdflogo),
78                                               delta_width, delta_height,
79                                               1, 1,
80                                               GDK_INTERP_NEAREST, 100);
81
82                         gdk_pixbuf_unref  (pdflogo);
83                 }
84                 if (gdk_pixbuf_save (pixbuf, thumbnail, "png", NULL, NULL)) {
85                         gdk_pixbuf_unref  (pixbuf);
86                         g_object_unref (document);
87                         return TRUE;
88                 } else {
89                         gdk_pixbuf_unref  (pixbuf);
90                         g_object_unref (document);
91                 }
92         }
93         return FALSE;
94 }
95
96 int
97 main (int argc, char *argv[])
98 {
99         int res;
100         char *input, *output;
101         int size;
102         char *uri;
103
104         if (argc <= 2 || argc > 5 || strcmp (argv[1], "-h") == 0 ||
105             strcmp (argv[1], "--help") == 0) {
106                 g_print ("Usage: %s [-s <size>] <input> <output>\n", argv[0]);
107                 return -1;
108         }
109
110         res = gnome_vfs_init ();
111
112         if (!strcmp (argv[1], "-s")) {
113                 input = argv[3];
114                 output = argv[4];
115                 size = g_strtod (argv[2], NULL);
116         } else {
117                 input = argv[1];
118                 output = argv[2];
119                 size = THUMBNAIL_SIZE;
120         }
121
122         if (size < 40) {
123                 g_print ("Size cannot be smaller than 40 pixels\n");
124                 return -1;
125         }
126
127         uri = gnome_vfs_make_uri_from_shell_arg (input);
128
129         if (evince_thumbnail_pngenc_get (uri, output, size)) {
130                 g_free (uri);
131                 return 0;
132         } else {
133                 g_free (uri);
134                 return -2;
135         }
136 }