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