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