]> www.fi.muni.cz Git - evince.git/blob - backend/tiff/tiff-document.c
Handle document orientation in tiff backend. Fixes bug #548444.
[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 EvDocumentInfo *
405 tiff_document_get_info (EvDocument *document)
406 {
407         EvDocumentInfo *info;
408
409         info = g_new0 (EvDocumentInfo, 1);
410         info->fields_mask = 0;
411
412         return info;
413 }
414
415 static void
416 tiff_document_document_iface_init (EvDocumentIface *iface)
417 {
418         iface->load = tiff_document_load;
419         iface->save = tiff_document_save;
420         iface->get_n_pages = tiff_document_get_n_pages;
421         iface->get_page_size = tiff_document_get_page_size;
422         iface->render = tiff_document_render;
423         iface->get_info = tiff_document_get_info;
424 }
425
426 static GdkPixbuf *
427 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
428                                         EvRenderContext      *rc, 
429                                         gboolean              border)
430 {
431         GdkPixbuf *pixbuf;
432
433         pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
434         
435         if (border) {
436                 GdkPixbuf *tmp_pixbuf = pixbuf;
437                 
438                 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
439                 g_object_unref (tmp_pixbuf);
440         }
441         
442         return pixbuf;
443 }
444
445 static void
446 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
447                                          EvRenderContext      *rc, 
448                                          gint                 *width,
449                                          gint                 *height)
450 {
451         gdouble page_width, page_height;
452
453         tiff_document_get_page_size (EV_DOCUMENT (document),
454                                      rc->page,
455                                      &page_width, &page_height);
456
457         if (rc->rotation == 90 || rc->rotation == 270) {
458                 *width = (gint) (page_height * rc->scale);
459                 *height = (gint) (page_width * rc->scale);
460         } else {
461                 *width = (gint) (page_width * rc->scale);
462                 *height = (gint) (page_height * rc->scale);
463         }
464 }
465
466 static void
467 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
468 {
469         iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
470         iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
471 }
472
473 /* postscript exporter implementation */
474 static void
475 tiff_document_file_exporter_begin (EvFileExporter        *exporter,
476                                    EvFileExporterContext *fc)
477 {
478         TiffDocument *document = TIFF_DOCUMENT (exporter);
479
480         document->ps_export_ctx = tiff2ps_context_new(fc->filename);
481 }
482
483 static void
484 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
485 {
486         TiffDocument *document = TIFF_DOCUMENT (exporter);
487
488         if (document->ps_export_ctx == NULL)
489                 return;
490         if (TIFFSetDirectory (document->tiff, rc->page->index) != 1)
491                 return;
492         tiff2ps_process_page (document->ps_export_ctx, document->tiff,
493                               0, 0, 0, 0, 0);
494 }
495
496 static void
497 tiff_document_file_exporter_end (EvFileExporter *exporter)
498 {
499         TiffDocument *document = TIFF_DOCUMENT (exporter);
500
501         if (document->ps_export_ctx == NULL)
502                 return;
503         tiff2ps_context_finalize(document->ps_export_ctx);
504 }
505
506 static EvFileExporterCapabilities
507 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
508 {
509         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
510                 EV_FILE_EXPORTER_CAN_COPIES |
511                 EV_FILE_EXPORTER_CAN_COLLATE |
512                 EV_FILE_EXPORTER_CAN_REVERSE |
513                 EV_FILE_EXPORTER_CAN_GENERATE_PS;
514 }
515
516 static void
517 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
518 {
519         iface->begin = tiff_document_file_exporter_begin;
520         iface->do_page = tiff_document_file_exporter_do_page;
521         iface->end = tiff_document_file_exporter_end;
522         iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
523 }
524
525 static void
526 tiff_document_init (TiffDocument *tiff_document)
527 {
528         tiff_document->n_pages = -1;
529 }