]> www.fi.muni.cz Git - evince.git/blob - tiff/tiff-document.c
Add Belarusian translation
[evince.git] / 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-ps-exporter.h"
32
33 #include <libgnomevfs/gnome-vfs-uri.h>
34 #include <libgnomevfs/gnome-vfs-utils.h>
35 #include <libgnomevfs/gnome-vfs-ops.h>
36 #include <libgnomevfs/gnome-vfs-xfer.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_ps_exporter_iface_init (EvPSExporterIface *iface);
59
60 G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
61                          { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
62                                                   tiff_document_document_iface_init);
63                            G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
64                                                   tiff_document_document_thumbnails_iface_init);
65                            G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
66                                                   tiff_document_document_ps_exporter_iface_init);
67                          });
68
69 static TIFFErrorHandler orig_error_handler = NULL;
70 static TIFFErrorHandler orig_warning_handler = NULL;
71
72 static void
73 push_handlers (void)
74 {
75   orig_error_handler = TIFFSetErrorHandler (NULL);
76   orig_warning_handler = TIFFSetWarningHandler (NULL);
77 }
78
79 static void
80 pop_handlers (void)
81 {
82   TIFFSetErrorHandler (orig_error_handler);
83   TIFFSetWarningHandler (orig_warning_handler);
84 }
85
86 static gboolean
87 tiff_document_load (EvDocument  *document,
88                     const char  *uri,
89                     GError     **error)
90 {
91   TiffDocument *tiff_document = TIFF_DOCUMENT (document);
92   gchar *filename;
93   TIFF *tiff;
94
95   push_handlers ();
96   filename = g_filename_from_uri (uri, NULL, error);
97   if (!filename)
98     {
99       pop_handlers ();
100       return FALSE;
101     }
102
103   tiff = TIFFOpen (filename, "r");
104   if (tiff)
105     {
106       guint32 w, h;
107       /* FIXME: unused data? why bother here */
108       TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
109       TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
110     }
111   if (!tiff)
112     {
113       pop_handlers ();
114       return FALSE;
115     }
116   tiff_document->tiff = tiff;
117   g_free (tiff_document->uri);
118   g_free (filename);
119   tiff_document->uri = g_strdup (uri);
120
121   pop_handlers ();
122   return TRUE;
123 }
124
125 static gboolean
126 tiff_document_save (EvDocument  *document,
127                       const char  *uri,
128                       GError     **error)
129 {               
130         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
131         GnomeVFSResult result;
132         GnomeVFSURI *source_uri;
133         GnomeVFSURI *target_uri;
134         
135         if (!tiff_document->uri)
136                 return FALSE;
137         
138         source_uri = gnome_vfs_uri_new (tiff_document->uri);
139         target_uri = gnome_vfs_uri_new (uri);
140
141         result = gnome_vfs_xfer_uri (source_uri, target_uri, 
142                                      GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
143                                      GNOME_VFS_XFER_ERROR_MODE_ABORT,
144                                      GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
145                                      NULL,
146                                      NULL);
147         gnome_vfs_uri_unref (target_uri);
148         gnome_vfs_uri_unref (source_uri);
149     
150         if (result != GNOME_VFS_OK)
151                 g_set_error (error,
152                              EV_DOCUMENT_ERROR,
153                              0,
154                              gnome_vfs_result_to_string (result));                      
155         return (result == GNOME_VFS_OK);
156 }
157
158 static int
159 tiff_document_get_n_pages (EvDocument  *document)
160 {
161   TiffDocument *tiff_document = TIFF_DOCUMENT (document);
162
163   g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
164   g_return_val_if_fail (tiff_document->tiff != NULL, 0);
165
166   if (tiff_document->n_pages == -1)
167     {
168       push_handlers ();
169       tiff_document->n_pages = 0;
170       do
171         {
172           tiff_document->n_pages ++;
173         }
174       while (TIFFReadDirectory (tiff_document->tiff));
175       pop_handlers ();
176     }
177
178   return tiff_document->n_pages;
179 }
180
181 static void
182 tiff_document_get_page_size (EvDocument   *document,
183                              int           page,
184                              double       *width,
185                              double       *height)
186 {
187   guint32 w, h;
188   gfloat x_res, y_res;
189   TiffDocument *tiff_document = TIFF_DOCUMENT (document);
190
191   g_return_if_fail (TIFF_IS_DOCUMENT (document));
192   g_return_if_fail (tiff_document->tiff != NULL);
193
194   push_handlers ();
195   if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
196     {
197       pop_handlers ();
198       return;
199     }
200
201   TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
202   TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
203   TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res);
204   TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res);
205   h = h * (x_res / y_res);
206
207   *width = w;
208   *height = h;
209
210   pop_handlers ();
211 }
212
213 static GdkPixbuf *
214 tiff_document_render_pixbuf (EvDocument      *document,
215                              EvRenderContext *rc)
216 {
217   TiffDocument *tiff_document = TIFF_DOCUMENT (document);
218   int width, height;
219   float x_res, y_res;
220   gint rowstride, bytes;
221   guchar *pixels = NULL;
222   GdkPixbuf *pixbuf;
223   GdkPixbuf *scaled_pixbuf;
224   GdkPixbuf *rotated_pixbuf;
225
226   g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
227   g_return_val_if_fail (tiff_document->tiff != NULL, 0);
228
229   push_handlers ();
230   if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1)
231     {
232       pop_handlers ();
233       return NULL;
234     }
235
236   if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width))
237     {
238       pop_handlers ();
239       return NULL;
240     }
241
242   if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height))
243     {
244       pop_handlers ();
245       return NULL;
246     }
247
248   if (!TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res))
249     {
250       pop_handlers ();
251       return NULL;
252     }
253
254   if (! TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res))
255     {
256       pop_handlers ();
257       return NULL;
258     }
259
260   pop_handlers ();
261
262   /* Sanity check the doc */
263   if (width <= 0 || height <= 0)
264     return NULL;                
265         
266   rowstride = width * 4;
267   if (rowstride / 4 != width)
268     /* overflow */
269     return NULL;                
270         
271   bytes = height * rowstride;
272   if (bytes / rowstride != height)
273     /* overflow */
274     return NULL;                
275
276   pixels = g_try_malloc (bytes);
277   if (!pixels)
278     return NULL;
279
280   pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
281                                      width, height, rowstride,
282                                      (GdkPixbufDestroyNotify) g_free, NULL);
283
284   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
285   TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
286   pop_handlers ();
287
288   scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
289                                            width * rc->scale,
290                                            height * rc->scale * (x_res/y_res),
291                                            GDK_INTERP_BILINEAR);
292   g_object_unref (pixbuf);
293
294   rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
295   g_object_unref (scaled_pixbuf);
296
297   return rotated_pixbuf;
298 }
299
300 static void
301 tiff_document_finalize (GObject *object)
302 {
303         TiffDocument *tiff_document = TIFF_DOCUMENT (object);
304
305         TIFFClose (tiff_document->tiff);
306         g_free (tiff_document->uri);
307
308         G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
309 }
310
311 static void
312 tiff_document_class_init (TiffDocumentClass *klass)
313 {
314         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
315
316         gobject_class->finalize = tiff_document_finalize;
317 }
318
319 static gboolean
320 tiff_document_can_get_text (EvDocument *document)
321 {
322         return FALSE;
323 }
324
325 static EvDocumentInfo *
326 tiff_document_get_info (EvDocument *document)
327 {
328         EvDocumentInfo *info;
329
330         info = g_new0 (EvDocumentInfo, 1);
331         info->fields_mask = 0;
332
333         return info;
334 }
335
336 static void
337 tiff_document_document_iface_init (EvDocumentIface *iface)
338 {
339         iface->load = tiff_document_load;
340         iface->save = tiff_document_save;
341         iface->can_get_text = tiff_document_can_get_text;
342         iface->get_n_pages = tiff_document_get_n_pages;
343         iface->get_page_size = tiff_document_get_page_size;
344         iface->render_pixbuf = tiff_document_render_pixbuf;
345         iface->get_info = tiff_document_get_info;
346 }
347
348 static GdkPixbuf *
349 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
350                                         gint                  page,
351                                         gint                  rotation,
352                                         gint                  size,
353                                         gboolean              border)
354 {
355   EvRenderContext *rc;
356   GdkPixbuf *pixbuf;
357   gdouble w, h;
358
359   tiff_document_get_page_size (EV_DOCUMENT (document),
360                                page,
361                                &w, &h);
362
363   rc = ev_render_context_new (rotation, page, size/w);
364   pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
365   g_object_unref (G_OBJECT (rc));
366
367   if (border)
368     {
369       GdkPixbuf *tmp_pixbuf = pixbuf;
370       pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf);
371       g_object_unref (tmp_pixbuf);
372     }
373
374   return pixbuf;
375 }
376
377 static void
378 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
379                                          gint                  page,
380                                          gint                  suggested_width,
381                                          gint                 *width,
382                                          gint                 *height)
383 {
384   gdouble page_ratio;
385   gdouble w, h;
386
387   tiff_document_get_page_size (EV_DOCUMENT (document),
388                                page,
389                                &w, &h);
390   g_return_if_fail (w > 0);
391   page_ratio = h/w;
392   *width = suggested_width;
393   *height = (gint) (suggested_width * page_ratio);
394 }
395
396 static void
397 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
398 {
399   iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
400   iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
401 }
402
403 /* postscript exporter implementation */
404
405 static void
406 tiff_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
407                                int first_page, int last_page,
408                                double width, double height, gboolean duplex)
409 {
410         TiffDocument *document = TIFF_DOCUMENT (exporter);
411
412         document->ps_export_ctx = tiff2ps_context_new(filename);
413 }
414
415 static void
416 tiff_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
417 {
418         TiffDocument *document = TIFF_DOCUMENT (exporter);
419
420         if (document->ps_export_ctx == NULL)
421                 return;
422         if (TIFFSetDirectory (document->tiff, rc->page) != 1)
423                 return;
424         tiff2ps_process_page (document->ps_export_ctx, document->tiff,
425                               0, 0, 0, 0, 0);
426 }
427
428 static void
429 tiff_document_ps_export_end (EvPSExporter *exporter)
430 {
431         TiffDocument *document = TIFF_DOCUMENT (exporter);
432
433         if (document->ps_export_ctx == NULL)
434                 return;
435         tiff2ps_context_finalize(document->ps_export_ctx);
436 }
437
438 static void
439 tiff_document_document_ps_exporter_iface_init (EvPSExporterIface *iface)
440 {
441         iface->begin = tiff_document_ps_export_begin;
442         iface->do_page = tiff_document_ps_export_do_page;
443         iface->end = tiff_document_ps_export_end;
444 }
445
446 static void
447 tiff_document_init (TiffDocument *tiff_document)
448 {
449   tiff_document->n_pages = -1;
450 }