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