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