1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
3 * Copyright (C) 2005, Jonathan Blandford <jrb@gnome.org>
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)
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.
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.
20 /* FIXME: Should probably buffer calls to libtiff with TIFFSetWarningHandler
25 #include <glib/gstdio.h>
29 #include "tiff-document.h"
30 #include "ev-document-misc.h"
31 #include "ev-document-thumbnails.h"
32 #include "ev-ps-exporter.h"
34 struct _TiffDocumentClass
36 GObjectClass parent_class;
41 GObject parent_instance;
45 EvOrientation orientation;
50 typedef struct _TiffDocumentClass TiffDocumentClass;
52 static void tiff_document_document_iface_init (EvDocumentIface *iface);
53 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
54 static void tiff_document_document_ps_exporter_iface_init (EvPSExporterIface *iface);
56 G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
57 { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
58 tiff_document_document_iface_init);
59 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
60 tiff_document_document_thumbnails_iface_init);
61 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
62 tiff_document_document_ps_exporter_iface_init);
65 static TIFFErrorHandler orig_error_handler = NULL;
66 static TIFFErrorHandler orig_warning_handler = NULL;
71 orig_error_handler = TIFFSetErrorHandler (NULL);
72 orig_warning_handler = TIFFSetWarningHandler (NULL);
78 TIFFSetErrorHandler (orig_error_handler);
79 TIFFSetWarningHandler (orig_warning_handler);
83 tiff_document_load (EvDocument *document,
87 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
92 /* FIXME: We could actually load uris */
93 filename = g_filename_from_uri (uri, NULL, error);
100 tiff = TIFFOpen (filename, "r");
104 /* FIXME: unused data? why bother here */
105 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
106 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
113 tiff_document->tiff = tiff;
120 tiff_document_save (EvDocument *document,
128 tiff_document_get_n_pages (EvDocument *document)
130 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
132 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
133 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
135 if (tiff_document->n_pages == -1)
138 tiff_document->n_pages = 0;
141 tiff_document->n_pages ++;
143 while (TIFFReadDirectory (tiff_document->tiff));
147 return tiff_document->n_pages;
151 tiff_document_get_page_size (EvDocument *document,
158 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
160 g_return_if_fail (TIFF_IS_DOCUMENT (document));
161 g_return_if_fail (tiff_document->tiff != NULL);
164 if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
170 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
171 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
172 TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res);
173 TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res);
174 h = h * (x_res / y_res);
176 if (tiff_document->orientation == EV_ORIENTATION_PORTRAIT ||
177 tiff_document->orientation == EV_ORIENTATION_UPSIDEDOWN) {
188 tiff_document_get_orientation (EvDocument *document)
190 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
192 return tiff_document->orientation;
196 tiff_document_set_orientation (EvDocument *document,
197 EvOrientation orientation)
199 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
201 tiff_document->orientation = orientation;
205 rotate_pixbuf (EvDocument *document, GdkPixbuf *pixbuf)
207 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
209 switch (tiff_document->orientation)
211 case EV_ORIENTATION_LANDSCAPE:
212 return gdk_pixbuf_rotate_simple (pixbuf, 90);
213 case EV_ORIENTATION_UPSIDEDOWN:
214 return gdk_pixbuf_rotate_simple (pixbuf, 180);
215 case EV_ORIENTATION_SEASCAPE:
216 return gdk_pixbuf_rotate_simple (pixbuf, 270);
218 return g_object_ref (pixbuf);
223 tiff_document_render_pixbuf (EvDocument *document,
226 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
229 gint rowstride, bytes;
230 guchar *pixels = NULL;
232 GdkPixbuf *scaled_pixbuf;
233 GdkPixbuf *rotated_pixbuf;
235 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
236 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
239 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1)
245 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width))
251 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height))
257 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res))
263 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res))
271 /* Sanity check the doc */
272 if (width <= 0 || height <= 0)
275 rowstride = width * 4;
276 if (rowstride / 4 != width)
280 bytes = height * rowstride;
281 if (bytes / rowstride != height)
285 pixels = g_try_malloc (bytes);
289 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
290 width, height, rowstride,
291 (GdkPixbufDestroyNotify) g_free, NULL);
293 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
294 TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
297 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
299 height * rc->scale * (x_res/y_res),
300 GDK_INTERP_BILINEAR);
301 g_object_unref (pixbuf);
303 rotated_pixbuf = rotate_pixbuf (document, scaled_pixbuf);
304 g_object_unref (scaled_pixbuf);
306 return rotated_pixbuf;
310 tiff_document_finalize (GObject *object)
312 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
314 TIFFClose (tiff_document->tiff);
316 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
320 tiff_document_class_init (TiffDocumentClass *klass)
322 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
324 gobject_class->finalize = tiff_document_finalize;
328 tiff_document_can_get_text (EvDocument *document)
333 static EvDocumentInfo *
334 tiff_document_get_info (EvDocument *document)
336 EvDocumentInfo *info;
338 info = g_new0 (EvDocumentInfo, 1);
339 info->fields_mask = 0;
345 tiff_document_document_iface_init (EvDocumentIface *iface)
347 iface->load = tiff_document_load;
348 iface->save = tiff_document_save;
349 iface->can_get_text = tiff_document_can_get_text;
350 iface->get_n_pages = tiff_document_get_n_pages;
351 iface->get_page_size = tiff_document_get_page_size;
352 iface->render_pixbuf = tiff_document_render_pixbuf;
353 iface->get_info = tiff_document_get_info;
354 iface->get_orientation = tiff_document_get_orientation;
355 iface->set_orientation = tiff_document_set_orientation;
359 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
368 tiff_document_get_page_size (EV_DOCUMENT (document),
372 rc = ev_render_context_new (EV_ORIENTATION_PORTRAIT, page, size/w);
373 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
374 g_object_unref (G_OBJECT (rc));
378 GdkPixbuf *tmp_pixbuf = pixbuf;
379 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
380 g_object_unref (tmp_pixbuf);
387 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
389 gint suggested_width,
396 tiff_document_get_page_size (EV_DOCUMENT (document),
399 g_return_if_fail (w > 0);
401 *width = suggested_width;
402 *height = (gint) (suggested_width * page_ratio);
406 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
408 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
409 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
412 /* postscript exporter implementation */
415 tiff_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
416 int first_page, int last_page)
418 TiffDocument *document = TIFF_DOCUMENT (exporter);
420 document->ps_export_file = g_fopen (filename, "w");
421 if (document->ps_export_file == NULL)
423 document->ps_export_pages = 0;
427 tiff_document_ps_export_do_page (EvPSExporter *exporter, int page)
429 TiffDocument *document = TIFF_DOCUMENT (exporter);
431 if (document->ps_export_file == NULL)
433 if (TIFFSetDirectory (document->tiff, page) != 1)
435 TIFF2PS (document->ps_export_file,
438 &document->ps_export_pages);
442 tiff_document_ps_export_end (EvPSExporter *exporter)
444 TiffDocument *document = TIFF_DOCUMENT (exporter);
446 if (document->ps_export_file == NULL)
448 if (document->ps_export_pages)
449 TIFFPSTail (document->ps_export_file,
450 document->ps_export_pages);
451 fclose (document->ps_export_file);
455 tiff_document_document_ps_exporter_iface_init (EvPSExporterIface *iface)
457 iface->begin = tiff_document_ps_export_begin;
458 iface->do_page = tiff_document_ps_export_do_page;
459 iface->end = tiff_document_ps_export_end;
463 tiff_document_init (TiffDocument *tiff_document)
465 tiff_document->n_pages = -1;
466 tiff_document->orientation = EV_ORIENTATION_PORTRAIT;