]> www.fi.muni.cz Git - evince.git/blob - tiff/tiff-document.c
Really make use of the orientation bit of the render context. Use the
[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 EvOrientation
180 tiff_document_get_orientation (EvDocument *document)
181 {
182         return EV_ORIENTATION_PORTRAIT;
183 }
184
185 static GdkPixbuf *
186 rotate_pixbuf (EvDocument *document, EvOrientation orientation, GdkPixbuf *pixbuf)
187 {
188         TiffDocument *tiff_document = TIFF_DOCUMENT (document);
189
190         switch (orientation)
191         {
192                 case EV_ORIENTATION_LANDSCAPE:
193                         return gdk_pixbuf_rotate_simple (pixbuf, 90);
194                 case EV_ORIENTATION_UPSIDEDOWN:
195                         return gdk_pixbuf_rotate_simple (pixbuf, 180);
196                 case EV_ORIENTATION_SEASCAPE:
197                         return gdk_pixbuf_rotate_simple (pixbuf, 270);
198                 default:
199                         return g_object_ref (pixbuf);
200         }
201 }
202
203 static GdkPixbuf *
204 tiff_document_render_pixbuf (EvDocument      *document,
205                              EvRenderContext *rc)
206 {
207   TiffDocument *tiff_document = TIFF_DOCUMENT (document);
208   int width, height;
209   float x_res, y_res;
210   gint rowstride, bytes;
211   guchar *pixels = NULL;
212   GdkPixbuf *pixbuf;
213   GdkPixbuf *scaled_pixbuf;
214   GdkPixbuf *rotated_pixbuf;
215
216   g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
217   g_return_val_if_fail (tiff_document->tiff != NULL, 0);
218
219   push_handlers ();
220   if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1)
221     {
222       pop_handlers ();
223       return NULL;
224     }
225
226   if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width))
227     {
228       pop_handlers ();
229       return NULL;
230     }
231
232   if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height))
233     {
234       pop_handlers ();
235       return NULL;
236     }
237
238   if (!TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res))
239     {
240       pop_handlers ();
241       return NULL;
242     }
243
244   if (! TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res))
245     {
246       pop_handlers ();
247       return NULL;
248     }
249
250   pop_handlers ();
251
252   /* Sanity check the doc */
253   if (width <= 0 || height <= 0)
254     return NULL;                
255         
256   rowstride = width * 4;
257   if (rowstride / 4 != width)
258     /* overflow */
259     return NULL;                
260         
261   bytes = height * rowstride;
262   if (bytes / rowstride != height)
263     /* overflow */
264     return NULL;                
265
266   pixels = g_try_malloc (bytes);
267   if (!pixels)
268     return NULL;
269
270   pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
271                                      width, height, rowstride,
272                                      (GdkPixbufDestroyNotify) g_free, NULL);
273
274   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
275   TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
276   pop_handlers ();
277
278   scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
279                                            width * rc->scale,
280                                            height * rc->scale * (x_res/y_res),
281                                            GDK_INTERP_BILINEAR);
282   g_object_unref (pixbuf);
283
284   rotated_pixbuf = rotate_pixbuf (document, rc->orientation, scaled_pixbuf);
285   g_object_unref (scaled_pixbuf);
286
287   return rotated_pixbuf;
288 }
289
290 static void
291 tiff_document_finalize (GObject *object)
292 {
293         TiffDocument *tiff_document = TIFF_DOCUMENT (object);
294
295         TIFFClose (tiff_document->tiff);
296
297         G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
298 }
299
300 static void
301 tiff_document_class_init (TiffDocumentClass *klass)
302 {
303         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
304
305         gobject_class->finalize = tiff_document_finalize;
306 }
307
308 static gboolean
309 tiff_document_can_get_text (EvDocument *document)
310 {
311         return FALSE;
312 }
313
314 static EvDocumentInfo *
315 tiff_document_get_info (EvDocument *document)
316 {
317         EvDocumentInfo *info;
318
319         info = g_new0 (EvDocumentInfo, 1);
320         info->fields_mask = 0;
321
322         return info;
323 }
324
325 static void
326 tiff_document_document_iface_init (EvDocumentIface *iface)
327 {
328         iface->load = tiff_document_load;
329         iface->save = tiff_document_save;
330         iface->can_get_text = tiff_document_can_get_text;
331         iface->get_n_pages = tiff_document_get_n_pages;
332         iface->get_page_size = tiff_document_get_page_size;
333         iface->render_pixbuf = tiff_document_render_pixbuf;
334         iface->get_info = tiff_document_get_info;
335         iface->get_orientation = tiff_document_get_orientation;
336 }
337
338 static GdkPixbuf *
339 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
340                                         gint                  page,
341                                         EvOrientation         orientation,
342                                         gint                  size,
343                                         gboolean              border)
344 {
345   EvRenderContext *rc;
346   GdkPixbuf *pixbuf;
347   gdouble w, h;
348
349   tiff_document_get_page_size (EV_DOCUMENT (document),
350                                page,
351                                &w, &h);
352
353   rc = ev_render_context_new (EV_ORIENTATION_PORTRAIT, page, size/w);
354   pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
355   g_object_unref (G_OBJECT (rc));
356
357   if (border)
358     {
359       GdkPixbuf *tmp_pixbuf = pixbuf;
360       pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
361       g_object_unref (tmp_pixbuf);
362     }
363
364   return pixbuf;
365 }
366
367 static void
368 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
369                                          gint                  page,
370                                          gint                  suggested_width,
371                                          gint                 *width,
372                                          gint                 *height)
373 {
374   gdouble page_ratio;
375   gdouble w, h;
376
377   tiff_document_get_page_size (EV_DOCUMENT (document),
378                                page,
379                                &w, &h);
380   g_return_if_fail (w > 0);
381   page_ratio = h/w;
382   *width = suggested_width;
383   *height = (gint) (suggested_width * page_ratio);
384 }
385
386 static void
387 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
388 {
389   iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
390   iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
391 }
392
393 /* postscript exporter implementation */
394
395 static void
396 tiff_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
397                                int first_page, int last_page,
398                                double width, double height, gboolean duplex)
399 {
400         TiffDocument *document = TIFF_DOCUMENT (exporter);
401
402         document->ps_export_ctx = tiff2ps_context_new(filename);
403 }
404
405 static void
406 tiff_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
407 {
408         TiffDocument *document = TIFF_DOCUMENT (exporter);
409
410         if (document->ps_export_ctx == NULL)
411                 return;
412         if (TIFFSetDirectory (document->tiff, rc->page) != 1)
413                 return;
414         tiff2ps_process_page (document->ps_export_ctx, document->tiff,
415                               0, 0, 0, 0, 0);
416 }
417
418 static void
419 tiff_document_ps_export_end (EvPSExporter *exporter)
420 {
421         TiffDocument *document = TIFF_DOCUMENT (exporter);
422
423         if (document->ps_export_ctx == NULL)
424                 return;
425         tiff2ps_context_finalize(document->ps_export_ctx);
426 }
427
428 static void
429 tiff_document_document_ps_exporter_iface_init (EvPSExporterIface *iface)
430 {
431         iface->begin = tiff_document_ps_export_begin;
432         iface->do_page = tiff_document_ps_export_do_page;
433         iface->end = tiff_document_ps_export_end;
434 }
435
436 static void
437 tiff_document_init (TiffDocument *tiff_document)
438 {
439   tiff_document->n_pages = -1;
440 }