]> www.fi.muni.cz Git - evince.git/blob - backend/tiff/tiff-document.c
Remove EvDocumentThumbnails interface
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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-file-exporter.h"
35 #include "ev-file-helpers.h"
36
37 struct _TiffDocumentClass
38 {
39   EvDocumentClass parent_class;
40 };
41
42 struct _TiffDocument
43 {
44   EvDocument parent_instance;
45
46   TIFF *tiff;
47   gint n_pages;
48   TIFF2PSContext *ps_export_ctx;
49   
50   gchar *uri;
51 };
52
53 typedef struct _TiffDocumentClass TiffDocumentClass;
54
55 static void tiff_document_document_file_exporter_iface_init (EvFileExporterInterface *iface);
56
57 EV_BACKEND_REGISTER_WITH_CODE (TiffDocument, tiff_document,
58                          {
59                            EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
60                                                            tiff_document_document_file_exporter_iface_init);
61                          });
62
63 static TIFFErrorHandler orig_error_handler = NULL;
64 static TIFFErrorHandler orig_warning_handler = NULL;
65
66 static void
67 push_handlers (void)
68 {
69         orig_error_handler = TIFFSetErrorHandler (NULL);
70         orig_warning_handler = TIFFSetWarningHandler (NULL);
71 }
72
73 static void
74 pop_handlers (void)
75 {
76         TIFFSetErrorHandler (orig_error_handler);
77         TIFFSetWarningHandler (orig_warning_handler);
78 }
79
80 static gboolean
81 tiff_document_load (EvDocument  *document,
82                     const char  *uri,
83                     GError     **error)
84 {
85         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
86         gchar *filename;
87         TIFF *tiff;
88         
89         filename = g_filename_from_uri (uri, NULL, error);
90         if (!filename)
91                 return FALSE;
92         
93         push_handlers ();
94         tiff = TIFFOpen (filename, "r");
95         if (tiff) {
96                 guint32 w, h;
97                 
98                 /* FIXME: unused data? why bother here */
99                 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
100                 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
101         }
102         
103         if (!tiff) {
104                 pop_handlers ();
105
106                 g_set_error_literal (error,
107                                      EV_DOCUMENT_ERROR,
108                                      EV_DOCUMENT_ERROR_INVALID,
109                                      _("Invalid document"));
110
111                 g_free (filename);
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                              EvPage     *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->index) != 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         int orientation;
219         cairo_surface_t *surface;
220         cairo_surface_t *rotated_surface;
221         static const cairo_user_data_key_t key;
222         
223         g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
224         g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
225   
226         push_handlers ();
227         if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
228                 pop_handlers ();
229                 return NULL;
230         }
231
232         if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
233                 pop_handlers ();
234                 return NULL;
235         }
236
237         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
238                 pop_handlers ();
239                 return NULL;
240         }
241
242         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_ORIENTATION, &orientation)) {
243                 orientation = ORIENTATION_TOPLEFT;
244         }
245
246         tiff_document_get_resolution (tiff_document, &x_res, &y_res);
247         
248         pop_handlers ();
249   
250         /* Sanity check the doc */
251         if (width <= 0 || height <= 0)
252                 return NULL;                
253
254 #ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
255         rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
256 #else
257         rowstride = width * 4;
258 #endif
259         if (rowstride / 4 != width)
260                 /* overflow, or cairo was changed in an unsupported way */
261                 return NULL;                
262         
263         bytes = height * rowstride;
264         if (bytes / rowstride != height)
265                 /* overflow */
266                 return NULL;                
267         
268         pixels = g_try_malloc (bytes);
269         if (!pixels)
270                 return NULL;
271         
272         surface = cairo_image_surface_create_for_data (pixels,
273                                                        CAIRO_FORMAT_RGB24,
274                                                        width, height,
275                                                        rowstride);
276         cairo_surface_set_user_data (surface, &key,
277                                      pixels, (cairo_destroy_func_t)g_free);
278
279         TIFFReadRGBAImageOriented (tiff_document->tiff,
280                                    width, height,
281                                    (uint32 *)pixels,
282                                    orientation, 1);
283         pop_handlers ();
284
285         /* Convert the format returned by libtiff to
286         * what cairo expects
287         */
288         p = pixels;
289         while (p < pixels + bytes) {
290                 guint32 *pixel = (guint32*)p;
291                 guint8 r = TIFFGetR(*pixel);
292                 guint8 g = TIFFGetG(*pixel);
293                 guint8 b = TIFFGetB(*pixel);
294                 guint8 a = TIFFGetA(*pixel);
295
296                 *pixel = (a << 24) | (r << 16) | (g << 8) | b;
297
298                 p += 4;
299         }
300
301         rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
302                                                                      (width * rc->scale) + 0.5,
303                                                                      (height * rc->scale * (x_res / y_res)) + 0.5,
304                                                                      rc->rotation);
305         cairo_surface_destroy (surface);
306         
307         return rotated_surface;
308 }
309
310 static GdkPixbuf *
311 tiff_document_get_thumbnail (EvDocument      *document,
312                              EvRenderContext *rc)
313 {
314         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
315         int width, height;
316         float x_res, y_res;
317         gint rowstride, bytes;
318         guchar *pixels = NULL;
319         GdkPixbuf *pixbuf;
320         GdkPixbuf *scaled_pixbuf;
321         GdkPixbuf *rotated_pixbuf;
322         
323         push_handlers ();
324         if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
325                 pop_handlers ();
326                 return NULL;
327         }
328
329         if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
330                 pop_handlers ();
331                 return NULL;
332         }
333
334         if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
335                 pop_handlers ();
336                 return NULL;
337         }
338
339         tiff_document_get_resolution (tiff_document, &x_res, &y_res);
340         
341         pop_handlers ();
342   
343         /* Sanity check the doc */
344         if (width <= 0 || height <= 0)
345                 return NULL;                
346
347         rowstride = width * 4;
348         if (rowstride / 4 != width)
349                 /* overflow */
350                 return NULL;                
351         
352         bytes = height * rowstride;
353         if (bytes / rowstride != height)
354                 /* overflow */
355                 return NULL;                
356         
357         pixels = g_try_malloc (bytes);
358         if (!pixels)
359                 return NULL;
360         
361         pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
362                                            width, height, rowstride,
363                                            (GdkPixbufDestroyNotify) g_free, NULL);
364         TIFFReadRGBAImageOriented (tiff_document->tiff,
365                                    width, height,
366                                    (uint32 *)pixels,
367                                    ORIENTATION_TOPLEFT, 1);
368         pop_handlers ();
369
370         scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
371                                                  width * rc->scale,
372                                                  height * rc->scale * (x_res / y_res),
373                                                  GDK_INTERP_BILINEAR);
374         g_object_unref (pixbuf);
375         
376         rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
377         g_object_unref (scaled_pixbuf);
378         
379         return rotated_pixbuf;
380 }
381
382 static gchar *
383 tiff_document_get_page_label (EvDocument *document,
384                               EvPage     *page)
385 {
386         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
387         static gchar *label;
388
389         if (TIFFGetField (tiff_document->tiff, TIFFTAG_PAGENAME, &label) &&
390             g_utf8_validate (label, -1, NULL)) {
391                 return g_strdup (label);
392         }
393
394         return NULL;
395 }
396
397 static void
398 tiff_document_finalize (GObject *object)
399 {
400         TiffDocument *tiff_document = TIFF_DOCUMENT (object);
401
402         if (tiff_document->tiff)
403                 TIFFClose (tiff_document->tiff);
404         if (tiff_document->uri)
405                 g_free (tiff_document->uri);
406
407         G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
408 }
409
410 static void
411 tiff_document_class_init (TiffDocumentClass *klass)
412 {
413         GObjectClass    *gobject_class = G_OBJECT_CLASS (klass);
414         EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
415
416         gobject_class->finalize = tiff_document_finalize;
417
418         ev_document_class->load = tiff_document_load;
419         ev_document_class->save = tiff_document_save;
420         ev_document_class->get_n_pages = tiff_document_get_n_pages;
421         ev_document_class->get_page_size = tiff_document_get_page_size;
422         ev_document_class->render = tiff_document_render;
423         ev_document_class->get_thumbnail = tiff_document_get_thumbnail;
424         ev_document_class->get_page_label = tiff_document_get_page_label;
425 }
426
427 /* postscript exporter implementation */
428 static void
429 tiff_document_file_exporter_begin (EvFileExporter        *exporter,
430                                    EvFileExporterContext *fc)
431 {
432         TiffDocument *document = TIFF_DOCUMENT (exporter);
433
434         document->ps_export_ctx = tiff2ps_context_new(fc->filename);
435 }
436
437 static void
438 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
439 {
440         TiffDocument *document = TIFF_DOCUMENT (exporter);
441
442         if (document->ps_export_ctx == NULL)
443                 return;
444         if (TIFFSetDirectory (document->tiff, rc->page->index) != 1)
445                 return;
446         tiff2ps_process_page (document->ps_export_ctx, document->tiff,
447                               0, 0, 0, 0, 0);
448 }
449
450 static void
451 tiff_document_file_exporter_end (EvFileExporter *exporter)
452 {
453         TiffDocument *document = TIFF_DOCUMENT (exporter);
454
455         if (document->ps_export_ctx == NULL)
456                 return;
457         tiff2ps_context_finalize(document->ps_export_ctx);
458 }
459
460 static EvFileExporterCapabilities
461 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
462 {
463         return  EV_FILE_EXPORTER_CAN_PAGE_SET |
464                 EV_FILE_EXPORTER_CAN_COPIES |
465                 EV_FILE_EXPORTER_CAN_COLLATE |
466                 EV_FILE_EXPORTER_CAN_REVERSE |
467                 EV_FILE_EXPORTER_CAN_GENERATE_PS;
468 }
469
470 static void
471 tiff_document_document_file_exporter_iface_init (EvFileExporterInterface *iface)
472 {
473         iface->begin = tiff_document_file_exporter_begin;
474         iface->do_page = tiff_document_file_exporter_do_page;
475         iface->end = tiff_document_file_exporter_end;
476         iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
477 }
478
479 static void
480 tiff_document_init (TiffDocument *tiff_document)
481 {
482         tiff_document->n_pages = -1;
483 }