]> www.fi.muni.cz Git - evince.git/blob - backend/tiff/tiff-document.c
Use an EvRenderContext for rendering thumbnails instead of a suggested
[evince.git] / backend / tiff / tiff-document.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2 /*
3  * Copyright (C) 2005, Jonathan Blandford <jrb@gnome.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /* FIXME: Should probably buffer calls to libtiff with TIFFSetWarningHandler
21  */
22
23 #include <stdio.h>
24 #include <glib.h>
25
26 #include "tiffio.h"
27 #include "tiff2ps.h"
28 #include "tiff-document.h"
29 #include "ev-document-misc.h"
30 #include "ev-document-thumbnails.h"
31 #include "ev-file-exporter.h"
32
33 struct _TiffDocumentClass
34 {
35   GObjectClass parent_class;
36 };
37
38 struct _TiffDocument
39 {
40   GObject parent_instance;
41
42   TIFF *tiff;
43   gint n_pages;
44   TIFF2PSContext *ps_export_ctx;
45   
46   gchar *uri;
47 };
48
49 typedef struct _TiffDocumentClass TiffDocumentClass;
50
51 static void tiff_document_document_iface_init (EvDocumentIface *iface);
52 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
53 static void tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface);
54
55 G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
56                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
57                                                   tiff_document_document_iface_init);
58                            G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
59                                                   tiff_document_document_thumbnails_iface_init);
60                            G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
61                                                   tiff_document_document_file_exporter_iface_init);
62                          });
63
64 static TIFFErrorHandler orig_error_handler = NULL;
65 static TIFFErrorHandler orig_warning_handler = NULL;
66
67 static void
68 push_handlers (void)
69 {
70         orig_error_handler = TIFFSetErrorHandler (NULL);
71         orig_warning_handler = TIFFSetWarningHandler (NULL);
72 }
73
74 static void
75 pop_handlers (void)
76 {
77         TIFFSetErrorHandler (orig_error_handler);
78         TIFFSetWarningHandler (orig_warning_handler);
79 }
80
81 static gboolean
82 tiff_document_load (EvDocument  *document,
83                     const char  *uri,
84                     GError     **error)
85 {
86         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
87         gchar *filename;
88         TIFF *tiff;
89         
90         push_handlers ();
91         filename = g_filename_from_uri (uri, NULL, error);
92         if (!filename) {
93                 pop_handlers ();
94                 return FALSE;
95         }
96         
97         tiff = TIFFOpen (filename, "r");
98         if (tiff) {
99                 guint32 w, h;
100                 
101                 /* FIXME: unused data? why bother here */
102                 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
103                 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
104         }
105         
106         if (!tiff) {
107                 pop_handlers ();
108                 return FALSE;
109         }
110         
111         tiff_document->tiff = tiff;
112         g_free (tiff_document->uri);
113         g_free (filename);
114         tiff_document->uri = g_strdup (uri);
115         
116         pop_handlers ();
117         return TRUE;
118 }
119
120 static gboolean
121 tiff_document_save (EvDocument  *document,
122                     const char  *uri,
123                     GError     **error)
124 {               
125         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
126
127         return ev_xfer_uri_simple (tiff_document->uri, uri, error); 
128 }
129
130 static int
131 tiff_document_get_n_pages (EvDocument  *document)
132 {
133         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
134         
135         g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
136         g_return_val_if_fail (tiff_document->tiff != NULL, 0);
137         
138         if (tiff_document->n_pages == -1) {
139                 push_handlers ();
140                 tiff_document->n_pages = 0;
141                 
142                 do {
143                         tiff_document->n_pages ++;
144                 }
145                 while (TIFFReadDirectory (tiff_document->tiff));
146                 pop_handlers ();
147         }
148
149         return tiff_document->n_pages;
150 }
151
152 static void
153 tiff_document_get_page_size (EvDocument   *document,
154                              int           page,
155                              double       *width,
156                              double       *height)
157 {
158         guint32 w, h;
159         gfloat x_res, y_res;
160         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
161         
162         g_return_if_fail (TIFF_IS_DOCUMENT (document));
163         g_return_if_fail (tiff_document->tiff != NULL);
164         
165         push_handlers ();
166         if (TIFFSetDirectory (tiff_document->tiff, page) != 1) {
167                 pop_handlers ();
168                 return;
169         }
170         
171         TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
172         TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
173         TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res);
174         TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res);
175         h = h * (x_res / y_res);
176         
177         *width = w;
178         *height = h;
179         
180         pop_handlers ();
181 }
182
183 static GdkPixbuf *
184 tiff_document_render_pixbuf (EvDocument      *document,
185                              EvRenderContext *rc)
186 {
187         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
188         int width, height;
189         float x_res, y_res;
190         gint rowstride, bytes;
191         guchar *pixels = NULL;
192         GdkPixbuf *pixbuf;
193         GdkPixbuf *scaled_pixbuf;
194         GdkPixbuf *rotated_pixbuf;
195         
196         g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
197         g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
198   
199         push_handlers ();
200         if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
201                 pop_handlers ();
202                 return NULL;
203         }
204
205         if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
206                 pop_handlers ();
207                 return NULL;
208         }
209
210         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
211                 pop_handlers ();
212                 return NULL;
213         }
214
215         if (!TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res)) {
216                 pop_handlers ();
217                 return NULL;
218         }
219
220         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res)) {
221                 pop_handlers ();
222                 return NULL;
223         }
224
225         pop_handlers ();
226   
227         /* Sanity check the doc */
228         if (width <= 0 || height <= 0)
229                 return NULL;                
230         
231         rowstride = width * 4;
232         if (rowstride / 4 != width)
233                 /* overflow */
234                 return NULL;                
235         
236         bytes = height * rowstride;
237         if (bytes / rowstride != height)
238                 /* overflow */
239                 return NULL;                
240         
241         pixels = g_try_malloc (bytes);
242         if (!pixels)
243                 return NULL;
244         
245         pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
246                                            width, height, rowstride,
247                                            (GdkPixbufDestroyNotify) g_free, NULL);
248         
249         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
250         TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
251         pop_handlers ();
252         
253         scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
254                                                  width * rc->scale,
255                                                  height * rc->scale * (x_res/y_res),
256                                                  GDK_INTERP_BILINEAR);
257         g_object_unref (pixbuf);
258         
259         rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
260         g_object_unref (scaled_pixbuf);
261         
262         return rotated_pixbuf;
263 }
264
265 static void
266 tiff_document_finalize (GObject *object)
267 {
268         TiffDocument *tiff_document = TIFF_DOCUMENT (object);
269
270         if (tiff_document->tiff)
271                 TIFFClose (tiff_document->tiff);
272         if (tiff_document->uri)
273                 g_free (tiff_document->uri);
274
275         G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
276 }
277
278 static void
279 tiff_document_class_init (TiffDocumentClass *klass)
280 {
281         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
282
283         gobject_class->finalize = tiff_document_finalize;
284 }
285
286 static gboolean
287 tiff_document_can_get_text (EvDocument *document)
288 {
289         return FALSE;
290 }
291
292 static EvDocumentInfo *
293 tiff_document_get_info (EvDocument *document)
294 {
295         EvDocumentInfo *info;
296
297         info = g_new0 (EvDocumentInfo, 1);
298         info->fields_mask = 0;
299
300         return info;
301 }
302
303 static void
304 tiff_document_document_iface_init (EvDocumentIface *iface)
305 {
306         iface->load = tiff_document_load;
307         iface->save = tiff_document_save;
308         iface->can_get_text = tiff_document_can_get_text;
309         iface->get_n_pages = tiff_document_get_n_pages;
310         iface->get_page_size = tiff_document_get_page_size;
311         iface->render_pixbuf = tiff_document_render_pixbuf;
312         iface->get_info = tiff_document_get_info;
313 }
314
315 static GdkPixbuf *
316 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
317                                         EvRenderContext      *rc, 
318                                         gboolean              border)
319 {
320         GdkPixbuf *pixbuf;
321
322         pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
323         
324         if (border) {
325                 GdkPixbuf *tmp_pixbuf = pixbuf;
326                 
327                 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
328                 g_object_unref (tmp_pixbuf);
329         }
330         
331         return pixbuf;
332 }
333
334 static void
335 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
336                                          EvRenderContext      *rc, 
337                                          gint                 *width,
338                                          gint                 *height)
339 {
340         gdouble page_width, page_height;
341
342         tiff_document_get_page_size (EV_DOCUMENT (document),
343                                      rc->page,
344                                      &page_width, &page_height);
345
346         if (rc->rotation == 90 || rc->rotation == 270) {
347                 *width = (gint) (page_height * rc->scale);
348                 *height = (gint) (page_width * rc->scale);
349         } else {
350                 *width = (gint) (page_width * rc->scale);
351                 *height = (gint) (page_height * rc->scale);
352         }
353 }
354
355 static void
356 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
357 {
358         iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
359         iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
360 }
361
362 /* postscript exporter implementation */
363
364 static gboolean
365 tiff_document_file_exporter_format_supported (EvFileExporter      *exporter,
366                                               EvFileExporterFormat format)
367 {
368         return (format == EV_FILE_FORMAT_PS);
369 }
370
371 static void
372 tiff_document_file_exporter_begin (EvFileExporter      *exporter,
373                                    EvFileExporterFormat format,
374                                    const char          *filename,
375                                    int                  first_page,
376                                    int                  last_page,
377                                    double               width,
378                                    double               height,
379                                    gboolean             duplex)
380 {
381         TiffDocument *document = TIFF_DOCUMENT (exporter);
382
383         document->ps_export_ctx = tiff2ps_context_new(filename);
384 }
385
386 static void
387 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
388 {
389         TiffDocument *document = TIFF_DOCUMENT (exporter);
390
391         if (document->ps_export_ctx == NULL)
392                 return;
393         if (TIFFSetDirectory (document->tiff, rc->page) != 1)
394                 return;
395         tiff2ps_process_page (document->ps_export_ctx, document->tiff,
396                               0, 0, 0, 0, 0);
397 }
398
399 static void
400 tiff_document_file_exporter_end (EvFileExporter *exporter)
401 {
402         TiffDocument *document = TIFF_DOCUMENT (exporter);
403
404         if (document->ps_export_ctx == NULL)
405                 return;
406         tiff2ps_context_finalize(document->ps_export_ctx);
407 }
408
409 static void
410 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
411 {
412         iface->format_supported = tiff_document_file_exporter_format_supported;
413         iface->begin = tiff_document_file_exporter_begin;
414         iface->do_page = tiff_document_file_exporter_do_page;
415         iface->end = tiff_document_file_exporter_end;
416 }
417
418 static void
419 tiff_document_init (TiffDocument *tiff_document)
420 {
421         tiff_document->n_pages = -1;
422 }