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