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