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