]> www.fi.muni.cz Git - evince.git/blob - backend/tiff/tiff-document.c
d1473a17bbd3d94908305a030f91d7a4eba8e361
[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 "config.h"
24
25 #include <stdio.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include "tiffio.h"
30 #include "tiff2ps.h"
31 #include "tiff-document.h"
32 #include "ev-document-misc.h"
33 #include "ev-document-thumbnails.h"
34 #include "ev-file-exporter.h"
35 #include "ev-file-helpers.h"
36
37 struct _TiffDocumentClass
38 {
39   GObjectClass parent_class;
40 };
41
42 struct _TiffDocument
43 {
44   GObject parent_instance;
45
46   TIFF *tiff;
47   gint n_pages;
48   TIFF2PSContext *ps_export_ctx;
49   
50   gchar *uri;
51 };
52
53 typedef struct _TiffDocumentClass TiffDocumentClass;
54
55 static void tiff_document_document_iface_init (EvDocumentIface *iface);
56 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
57 static void tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface);
58
59 EV_BACKEND_REGISTER_WITH_CODE (TiffDocument, tiff_document,
60                          {
61                            G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
62                                                   tiff_document_document_thumbnails_iface_init);
63                            G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
64                                                   tiff_document_document_file_exporter_iface_init);
65                          });
66
67 static TIFFErrorHandler orig_error_handler = NULL;
68 static TIFFErrorHandler orig_warning_handler = NULL;
69
70 static void
71 push_handlers (void)
72 {
73         orig_error_handler = TIFFSetErrorHandler (NULL);
74         orig_warning_handler = TIFFSetWarningHandler (NULL);
75 }
76
77 static void
78 pop_handlers (void)
79 {
80         TIFFSetErrorHandler (orig_error_handler);
81         TIFFSetWarningHandler (orig_warning_handler);
82 }
83
84 static gboolean
85 tiff_document_load (EvDocument  *document,
86                     const char  *uri,
87                     GError     **error)
88 {
89         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
90         gchar *filename;
91         TIFF *tiff;
92         
93         push_handlers ();
94         filename = g_filename_from_uri (uri, NULL, error);
95         if (!filename) {
96                 pop_handlers ();
97                 return FALSE;
98         }
99         
100         tiff = TIFFOpen (filename, "r");
101         if (tiff) {
102                 guint32 w, h;
103                 
104                 /* FIXME: unused data? why bother here */
105                 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
106                 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
107         }
108         
109         if (!tiff) {
110                 pop_handlers ();
111                 return FALSE;
112         }
113         
114         tiff_document->tiff = tiff;
115         g_free (tiff_document->uri);
116         g_free (filename);
117         tiff_document->uri = g_strdup (uri);
118         
119         pop_handlers ();
120         return TRUE;
121 }
122
123 static gboolean
124 tiff_document_save (EvDocument  *document,
125                     const char  *uri,
126                     GError     **error)
127 {               
128         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
129
130         return ev_xfer_uri_simple (tiff_document->uri, uri, error); 
131 }
132
133 static int
134 tiff_document_get_n_pages (EvDocument  *document)
135 {
136         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
137         
138         g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
139         g_return_val_if_fail (tiff_document->tiff != NULL, 0);
140         
141         if (tiff_document->n_pages == -1) {
142                 push_handlers ();
143                 tiff_document->n_pages = 0;
144                 
145                 do {
146                         tiff_document->n_pages ++;
147                 }
148                 while (TIFFReadDirectory (tiff_document->tiff));
149                 pop_handlers ();
150         }
151
152         return tiff_document->n_pages;
153 }
154
155 static void
156 tiff_document_get_resolution (TiffDocument *tiff_document,
157                               gfloat       *x_res,
158                               gfloat       *y_res)
159 {
160         gfloat x = 72.0, y = 72.0;
161         gushort unit;
162         
163         if (TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x) &&
164             TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y)) {
165                 if (TIFFGetFieldDefaulted (tiff_document->tiff, TIFFTAG_RESOLUTIONUNIT, &unit)) {
166                         if (unit == RESUNIT_CENTIMETER) {
167                                 x *= 2.54;
168                                 y *= 2.54;
169                         }
170                 }
171         }
172
173         *x_res = x;
174         *y_res = y;
175 }
176
177 static void
178 tiff_document_get_page_size (EvDocument   *document,
179                              int           page,
180                              double       *width,
181                              double       *height)
182 {
183         guint32 w, h;
184         gfloat x_res, y_res;
185         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
186         
187         g_return_if_fail (TIFF_IS_DOCUMENT (document));
188         g_return_if_fail (tiff_document->tiff != NULL);
189         
190         push_handlers ();
191         if (TIFFSetDirectory (tiff_document->tiff, page) != 1) {
192                 pop_handlers ();
193                 return;
194         }
195         
196         TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
197         TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
198         tiff_document_get_resolution (tiff_document, &x_res, &y_res);
199         h = h * (x_res / y_res);
200         
201         *width = w;
202         *height = h;
203         
204         pop_handlers ();
205 }
206
207 static cairo_surface_t *
208 tiff_document_render (EvDocument      *document,
209                       EvRenderContext *rc)
210 {
211         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
212         int width, height;
213         float x_res, y_res;
214         gint rowstride, bytes;
215         guchar *pixels = NULL;
216         guchar *p;
217         GdkPixbuf *pixbuf;
218         GdkPixbuf *scaled_pixbuf;
219         GdkPixbuf *rotated_pixbuf;
220         cairo_surface_t *surface;
221         cairo_surface_t *rotated_surface;
222         static const cairo_user_data_key_t key;
223         
224         g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
225         g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
226   
227         push_handlers ();
228         if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
229                 pop_handlers ();
230                 return NULL;
231         }
232
233         if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
234                 pop_handlers ();
235                 return NULL;
236         }
237
238         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
239                 pop_handlers ();
240                 return NULL;
241         }
242
243         tiff_document_get_resolution (tiff_document, &x_res, &y_res);
244         
245         pop_handlers ();
246   
247         /* Sanity check the doc */
248         if (width <= 0 || height <= 0)
249                 return NULL;                
250
251         rowstride = width * 4;
252         if (rowstride / 4 != width)
253                 /* overflow */
254                 return NULL;                
255         
256         bytes = height * rowstride;
257         if (bytes / rowstride != height)
258                 /* overflow */
259                 return NULL;                
260         
261         pixels = g_try_malloc (bytes);
262         if (!pixels)
263                 return NULL;
264         
265         surface = cairo_image_surface_create_for_data (pixels,
266                                                        CAIRO_FORMAT_RGB24,
267                                                        width, height,
268                                                        rowstride);
269         cairo_surface_set_user_data (surface, &key,
270                                      pixels, (cairo_destroy_func_t)g_free);
271
272         TIFFReadRGBAImageOriented (tiff_document->tiff,
273                                    width, height,
274                                    (uint32 *)pixels,
275                                    ORIENTATION_TOPLEFT, 1);
276         pop_handlers ();
277
278         /* Convert the format returned by libtiff to
279         * what cairo expects
280         */
281         p = pixels;
282         while (p < pixels + bytes) {
283                 uint32 pixel = *(uint32 *)p;
284                 int r = TIFFGetR(pixel);
285                 int g = TIFFGetG(pixel);
286                 int b = TIFFGetB(pixel);
287                 int a = TIFFGetA(pixel);
288                 
289                 *p++ = b;
290                 *p++ = g;
291                 *p++ = r;
292                 *p++ = a;
293         }
294
295         rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
296                                                                      (width * rc->scale) + 0.5,
297                                                                      (height * rc->scale * (x_res / y_res)) + 0.5,
298                                                                      rc->rotation);
299         cairo_surface_destroy (surface);
300         
301         return rotated_surface;
302 }
303
304 static GdkPixbuf *
305 tiff_document_render_pixbuf (EvDocument      *document,
306                              EvRenderContext *rc)
307 {
308         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
309         int width, height;
310         float x_res, y_res;
311         gint rowstride, bytes;
312         guchar *pixels = NULL;
313         GdkPixbuf *pixbuf;
314         GdkPixbuf *scaled_pixbuf;
315         GdkPixbuf *rotated_pixbuf;
316         
317         push_handlers ();
318         if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
319                 pop_handlers ();
320                 return NULL;
321         }
322
323         if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
324                 pop_handlers ();
325                 return NULL;
326         }
327
328         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
329                 pop_handlers ();
330                 return NULL;
331         }
332
333         tiff_document_get_resolution (tiff_document, &x_res, &y_res);
334         
335         pop_handlers ();
336   
337         /* Sanity check the doc */
338         if (width <= 0 || height <= 0)
339                 return NULL;                
340
341         rowstride = width * 4;
342         if (rowstride / 4 != width)
343                 /* overflow */
344                 return NULL;                
345         
346         bytes = height * rowstride;
347         if (bytes / rowstride != height)
348                 /* overflow */
349                 return NULL;                
350         
351         pixels = g_try_malloc (bytes);
352         if (!pixels)
353                 return NULL;
354         
355         pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
356                                            width, height, rowstride,
357                                            (GdkPixbufDestroyNotify) g_free, NULL);
358         TIFFReadRGBAImageOriented (tiff_document->tiff,
359                                    width, height,
360                                    (uint32 *)pixels,
361                                    ORIENTATION_TOPLEFT, 1);
362         pop_handlers ();
363
364         scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
365                                                  width * rc->scale,
366                                                  height * rc->scale * (x_res / y_res),
367                                                  GDK_INTERP_BILINEAR);
368         g_object_unref (pixbuf);
369         
370         rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
371         g_object_unref (scaled_pixbuf);
372         
373         return rotated_pixbuf;
374 }
375
376 static void
377 tiff_document_finalize (GObject *object)
378 {
379         TiffDocument *tiff_document = TIFF_DOCUMENT (object);
380
381         if (tiff_document->tiff)
382                 TIFFClose (tiff_document->tiff);
383         if (tiff_document->uri)
384                 g_free (tiff_document->uri);
385
386         G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
387 }
388
389 static void
390 tiff_document_class_init (TiffDocumentClass *klass)
391 {
392         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
393
394         gobject_class->finalize = tiff_document_finalize;
395 }
396
397 static EvDocumentInfo *
398 tiff_document_get_info (EvDocument *document)
399 {
400         EvDocumentInfo *info;
401
402         info = g_new0 (EvDocumentInfo, 1);
403         info->fields_mask = 0;
404
405         return info;
406 }
407
408 static void
409 tiff_document_document_iface_init (EvDocumentIface *iface)
410 {
411         iface->load = tiff_document_load;
412         iface->save = tiff_document_save;
413         iface->get_n_pages = tiff_document_get_n_pages;
414         iface->get_page_size = tiff_document_get_page_size;
415         iface->render = tiff_document_render;
416         iface->get_info = tiff_document_get_info;
417 }
418
419 static GdkPixbuf *
420 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
421                                         EvRenderContext      *rc, 
422                                         gboolean              border)
423 {
424         GdkPixbuf *pixbuf;
425
426         pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
427         
428         if (border) {
429                 GdkPixbuf *tmp_pixbuf = pixbuf;
430                 
431                 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
432                 g_object_unref (tmp_pixbuf);
433         }
434         
435         return pixbuf;
436 }
437
438 static void
439 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
440                                          EvRenderContext      *rc, 
441                                          gint                 *width,
442                                          gint                 *height)
443 {
444         gdouble page_width, page_height;
445
446         tiff_document_get_page_size (EV_DOCUMENT (document),
447                                      rc->page,
448                                      &page_width, &page_height);
449
450         if (rc->rotation == 90 || rc->rotation == 270) {
451                 *width = (gint) (page_height * rc->scale);
452                 *height = (gint) (page_width * rc->scale);
453         } else {
454                 *width = (gint) (page_width * rc->scale);
455                 *height = (gint) (page_height * rc->scale);
456         }
457 }
458
459 static void
460 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
461 {
462         iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
463         iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
464 }
465
466 /* postscript exporter implementation */
467 static void
468 tiff_document_file_exporter_begin (EvFileExporter        *exporter,
469                                    EvFileExporterContext *fc)
470 {
471         TiffDocument *document = TIFF_DOCUMENT (exporter);
472
473         document->ps_export_ctx = tiff2ps_context_new(fc->filename);
474 }
475
476 static void
477 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
478 {
479         TiffDocument *document = TIFF_DOCUMENT (exporter);
480
481         if (document->ps_export_ctx == NULL)
482                 return;
483         if (TIFFSetDirectory (document->tiff, rc->page) != 1)
484                 return;
485         tiff2ps_process_page (document->ps_export_ctx, document->tiff,
486                               0, 0, 0, 0, 0);
487 }
488
489 static void
490 tiff_document_file_exporter_end (EvFileExporter *exporter)
491 {
492         TiffDocument *document = TIFF_DOCUMENT (exporter);
493
494         if (document->ps_export_ctx == NULL)
495                 return;
496         tiff2ps_context_finalize(document->ps_export_ctx);
497 }
498
499 static EvFileExporterCapabilities
500 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
501 {
502         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
503                 EV_FILE_EXPORTER_CAN_COPIES |
504                 EV_FILE_EXPORTER_CAN_COLLATE |
505                 EV_FILE_EXPORTER_CAN_REVERSE |
506                 EV_FILE_EXPORTER_CAN_GENERATE_PS;
507 }
508
509 static void
510 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
511 {
512         iface->begin = tiff_document_file_exporter_begin;
513         iface->do_page = tiff_document_file_exporter_do_page;
514         iface->end = tiff_document_file_exporter_end;
515         iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
516 }
517
518 static void
519 tiff_document_init (TiffDocument *tiff_document)
520 {
521         tiff_document->n_pages = -1;
522 }