]> www.fi.muni.cz Git - evince.git/blob - backend/tiff/tiff-document.c
Include config.h. Bug #504721.
[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                            G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
63                                                   tiff_document_document_thumbnails_iface_init);
64                            G_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                              int           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) != 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         GdkPixbuf *pixbuf;
219         GdkPixbuf *scaled_pixbuf;
220         GdkPixbuf *rotated_pixbuf;
221         cairo_surface_t *surface;
222         cairo_surface_t *rotated_surface;
223         static const cairo_user_data_key_t key;
224         
225         g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
226         g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
227   
228         push_handlers ();
229         if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
230                 pop_handlers ();
231                 return NULL;
232         }
233
234         if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
235                 pop_handlers ();
236                 return NULL;
237         }
238
239         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
240                 pop_handlers ();
241                 return NULL;
242         }
243
244         tiff_document_get_resolution (tiff_document, &x_res, &y_res);
245         
246         pop_handlers ();
247   
248         /* Sanity check the doc */
249         if (width <= 0 || height <= 0)
250                 return NULL;                
251
252         rowstride = width * 4;
253         if (rowstride / 4 != width)
254                 /* overflow */
255                 return NULL;                
256         
257         bytes = height * rowstride;
258         if (bytes / rowstride != height)
259                 /* overflow */
260                 return NULL;                
261         
262         pixels = g_try_malloc (bytes);
263         if (!pixels)
264                 return NULL;
265         
266         surface = cairo_image_surface_create_for_data (pixels,
267                                                        CAIRO_FORMAT_RGB24,
268                                                        width, height,
269                                                        rowstride);
270         cairo_surface_set_user_data (surface, &key,
271                                      pixels, (cairo_destroy_func_t)g_free);
272
273         TIFFReadRGBAImageOriented (tiff_document->tiff,
274                                    width, height,
275                                    (uint32 *)pixels,
276                                    ORIENTATION_TOPLEFT, 1);
277         pop_handlers ();
278
279         /* Convert the format returned by libtiff to
280         * what cairo expects
281         */
282         p = pixels;
283         while (p < pixels + bytes) {
284                 uint32 pixel = *(uint32 *)p;
285                 int r = TIFFGetR(pixel);
286                 int g = TIFFGetG(pixel);
287                 int b = TIFFGetB(pixel);
288                 int a = TIFFGetA(pixel);
289                 
290                 *p++ = b;
291                 *p++ = g;
292                 *p++ = r;
293                 *p++ = a;
294         }
295
296         rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
297                                                                      (width * rc->scale) + 0.5,
298                                                                      (height * rc->scale * (x_res / y_res)) + 0.5,
299                                                                      rc->rotation);
300         cairo_surface_destroy (surface);
301         
302         return rotated_surface;
303 }
304
305 static GdkPixbuf *
306 tiff_document_render_pixbuf (EvDocument      *document,
307                              EvRenderContext *rc)
308 {
309         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
310         int width, height;
311         float x_res, y_res;
312         gint rowstride, bytes;
313         guchar *pixels = NULL;
314         GdkPixbuf *pixbuf;
315         GdkPixbuf *scaled_pixbuf;
316         GdkPixbuf *rotated_pixbuf;
317         
318         push_handlers ();
319         if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
320                 pop_handlers ();
321                 return NULL;
322         }
323
324         if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
325                 pop_handlers ();
326                 return NULL;
327         }
328
329         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
330                 pop_handlers ();
331                 return NULL;
332         }
333
334         tiff_document_get_resolution (tiff_document, &x_res, &y_res);
335         
336         pop_handlers ();
337   
338         /* Sanity check the doc */
339         if (width <= 0 || height <= 0)
340                 return NULL;                
341
342         rowstride = width * 4;
343         if (rowstride / 4 != width)
344                 /* overflow */
345                 return NULL;                
346         
347         bytes = height * rowstride;
348         if (bytes / rowstride != height)
349                 /* overflow */
350                 return NULL;                
351         
352         pixels = g_try_malloc (bytes);
353         if (!pixels)
354                 return NULL;
355         
356         pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
357                                            width, height, rowstride,
358                                            (GdkPixbufDestroyNotify) g_free, NULL);
359         TIFFReadRGBAImageOriented (tiff_document->tiff,
360                                    width, height,
361                                    (uint32 *)pixels,
362                                    ORIENTATION_TOPLEFT, 1);
363         pop_handlers ();
364
365         scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
366                                                  width * rc->scale,
367                                                  height * rc->scale * (x_res / y_res),
368                                                  GDK_INTERP_BILINEAR);
369         g_object_unref (pixbuf);
370         
371         rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
372         g_object_unref (scaled_pixbuf);
373         
374         return rotated_pixbuf;
375 }
376
377 static void
378 tiff_document_finalize (GObject *object)
379 {
380         TiffDocument *tiff_document = TIFF_DOCUMENT (object);
381
382         if (tiff_document->tiff)
383                 TIFFClose (tiff_document->tiff);
384         if (tiff_document->uri)
385                 g_free (tiff_document->uri);
386
387         G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
388 }
389
390 static void
391 tiff_document_class_init (TiffDocumentClass *klass)
392 {
393         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
394
395         gobject_class->finalize = tiff_document_finalize;
396 }
397
398 static EvDocumentInfo *
399 tiff_document_get_info (EvDocument *document)
400 {
401         EvDocumentInfo *info;
402
403         info = g_new0 (EvDocumentInfo, 1);
404         info->fields_mask = 0;
405
406         return info;
407 }
408
409 static void
410 tiff_document_document_iface_init (EvDocumentIface *iface)
411 {
412         iface->load = tiff_document_load;
413         iface->save = tiff_document_save;
414         iface->get_n_pages = tiff_document_get_n_pages;
415         iface->get_page_size = tiff_document_get_page_size;
416         iface->render = tiff_document_render;
417         iface->get_info = tiff_document_get_info;
418 }
419
420 static GdkPixbuf *
421 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
422                                         EvRenderContext      *rc, 
423                                         gboolean              border)
424 {
425         GdkPixbuf *pixbuf;
426
427         pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
428         
429         if (border) {
430                 GdkPixbuf *tmp_pixbuf = pixbuf;
431                 
432                 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
433                 g_object_unref (tmp_pixbuf);
434         }
435         
436         return pixbuf;
437 }
438
439 static void
440 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
441                                          EvRenderContext      *rc, 
442                                          gint                 *width,
443                                          gint                 *height)
444 {
445         gdouble page_width, page_height;
446
447         tiff_document_get_page_size (EV_DOCUMENT (document),
448                                      rc->page,
449                                      &page_width, &page_height);
450
451         if (rc->rotation == 90 || rc->rotation == 270) {
452                 *width = (gint) (page_height * rc->scale);
453                 *height = (gint) (page_width * rc->scale);
454         } else {
455                 *width = (gint) (page_width * rc->scale);
456                 *height = (gint) (page_height * rc->scale);
457         }
458 }
459
460 static void
461 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
462 {
463         iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
464         iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
465 }
466
467 /* postscript exporter implementation */
468 static void
469 tiff_document_file_exporter_begin (EvFileExporter        *exporter,
470                                    EvFileExporterContext *fc)
471 {
472         TiffDocument *document = TIFF_DOCUMENT (exporter);
473
474         document->ps_export_ctx = tiff2ps_context_new(fc->filename);
475 }
476
477 static void
478 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
479 {
480         TiffDocument *document = TIFF_DOCUMENT (exporter);
481
482         if (document->ps_export_ctx == NULL)
483                 return;
484         if (TIFFSetDirectory (document->tiff, rc->page) != 1)
485                 return;
486         tiff2ps_process_page (document->ps_export_ctx, document->tiff,
487                               0, 0, 0, 0, 0);
488 }
489
490 static void
491 tiff_document_file_exporter_end (EvFileExporter *exporter)
492 {
493         TiffDocument *document = TIFF_DOCUMENT (exporter);
494
495         if (document->ps_export_ctx == NULL)
496                 return;
497         tiff2ps_context_finalize(document->ps_export_ctx);
498 }
499
500 static EvFileExporterCapabilities
501 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
502 {
503         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
504                 EV_FILE_EXPORTER_CAN_COPIES |
505                 EV_FILE_EXPORTER_CAN_COLLATE |
506                 EV_FILE_EXPORTER_CAN_REVERSE |
507                 EV_FILE_EXPORTER_CAN_GENERATE_PS;
508 }
509
510 static void
511 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
512 {
513         iface->begin = tiff_document_file_exporter_begin;
514         iface->do_page = tiff_document_file_exporter_do_page;
515         iface->end = tiff_document_file_exporter_end;
516         iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
517 }
518
519 static void
520 tiff_document_init (TiffDocument *tiff_document)
521 {
522         tiff_document->n_pages = -1;
523 }