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