2 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
4 * Copyright (C) 2005, Jonathan Blandford <jrb@gnome.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 /* FIXME: Shoudl probably buffer calls to libtiff with TIFFSetWarningHandler
24 #include "tiff-document.h"
25 #include "ev-document-thumbnails.h"
26 #include "ev-document-misc.h"
28 struct _TiffDocumentClass
30 GObjectClass parent_class;
35 GObject parent_instance;
39 EvOrientation orientation;
42 typedef struct _TiffDocumentClass TiffDocumentClass;
44 static void tiff_document_document_iface_init (EvDocumentIface *iface);
45 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
47 G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
48 { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
49 tiff_document_document_iface_init);
50 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
51 tiff_document_document_thumbnails_iface_init);
54 static TIFFErrorHandler orig_error_handler = NULL;
55 static TIFFErrorHandler orig_warning_handler = NULL;
60 orig_error_handler = TIFFSetErrorHandler (NULL);
61 orig_warning_handler = TIFFSetWarningHandler (NULL);
67 TIFFSetErrorHandler (orig_error_handler);
68 TIFFSetWarningHandler (orig_warning_handler);
72 tiff_document_load (EvDocument *document,
76 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
81 /* FIXME: We could actually load uris */
82 filename = g_filename_from_uri (uri, NULL, error);
89 tiff = TIFFOpen (filename, "r");
94 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
95 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
102 tiff_document->tiff = tiff;
109 tiff_document_save (EvDocument *document,
117 tiff_document_get_n_pages (EvDocument *document)
119 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
121 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
122 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
124 if (tiff_document->n_pages == -1)
127 tiff_document->n_pages = 0;
130 tiff_document->n_pages ++;
132 while (TIFFReadDirectory (tiff_document->tiff));
136 return tiff_document->n_pages;
140 tiff_document_get_page_size (EvDocument *document,
146 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
148 g_return_if_fail (TIFF_IS_DOCUMENT (document));
149 g_return_if_fail (tiff_document->tiff != NULL);
152 if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
158 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
159 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
161 if (tiff_document->orientation == EV_ORIENTATION_PORTRAIT ||
162 tiff_document->orientation == EV_ORIENTATION_UPSIDEDOWN) {
173 tiff_document_get_orientation (EvDocument *document)
175 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
177 return tiff_document->orientation;
181 tiff_document_set_orientation (EvDocument *document,
182 EvOrientation orientation)
184 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
186 tiff_document->orientation = orientation;
190 rotate_pixbuf (EvDocument *document, GdkPixbuf *pixbuf)
192 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
194 switch (tiff_document->orientation)
196 case EV_ORIENTATION_LANDSCAPE:
197 return gdk_pixbuf_rotate_simple (pixbuf, 90);
198 case EV_ORIENTATION_UPSIDEDOWN:
199 return gdk_pixbuf_rotate_simple (pixbuf, 180);
200 case EV_ORIENTATION_SEASCAPE:
201 return gdk_pixbuf_rotate_simple (pixbuf, 270);
203 return g_object_ref (pixbuf);
208 tiff_document_render_pixbuf (EvDocument *document, int page, double scale)
210 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
212 gint rowstride, bytes;
213 guchar *pixels = NULL;
215 GdkPixbuf *scaled_pixbuf;
216 GdkPixbuf *rotated_pixbuf;
218 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
219 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
222 if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
228 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width))
234 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height))
242 /* Sanity check the doc */
243 if (width <= 0 || height <= 0)
246 rowstride = width * 4;
247 if (rowstride / 4 != width)
251 bytes = height * rowstride;
252 if (bytes / rowstride != height)
256 pixels = g_try_malloc (bytes);
260 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
261 width, height, rowstride,
262 (GdkPixbufDestroyNotify) g_free, NULL);
264 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
265 TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
268 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
271 GDK_INTERP_BILINEAR);
272 g_object_unref (pixbuf);
274 rotated_pixbuf = rotate_pixbuf (document, scaled_pixbuf);
275 g_object_unref (scaled_pixbuf);
277 return rotated_pixbuf;
281 tiff_document_finalize (GObject *object)
283 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
285 TIFFClose (tiff_document->tiff);
287 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
291 tiff_document_class_init (TiffDocumentClass *klass)
293 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
295 gobject_class->finalize = tiff_document_finalize;
299 tiff_document_can_get_text (EvDocument *document)
304 static EvDocumentInfo *
305 tiff_document_get_info (EvDocument *document)
307 EvDocumentInfo *info;
309 info = g_new0 (EvDocumentInfo, 1);
310 info->fields_mask = 0;
316 tiff_document_document_iface_init (EvDocumentIface *iface)
318 iface->load = tiff_document_load;
319 iface->save = tiff_document_save;
320 iface->can_get_text = tiff_document_can_get_text;
321 iface->get_n_pages = tiff_document_get_n_pages;
322 iface->get_page_size = tiff_document_get_page_size;
323 iface->render_pixbuf = tiff_document_render_pixbuf;
324 iface->get_info = tiff_document_get_info;
325 iface->get_orientation = tiff_document_get_orientation;
326 iface->set_orientation = tiff_document_set_orientation;
330 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
338 tiff_document_get_page_size (EV_DOCUMENT (document),
342 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document),
348 GdkPixbuf *tmp_pixbuf = pixbuf;
349 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
350 g_object_unref (tmp_pixbuf);
357 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
359 gint suggested_width,
366 tiff_document_get_page_size (EV_DOCUMENT (document),
369 g_return_if_fail (w > 0);
371 *width = suggested_width;
372 *height = (gint) (suggested_width * page_ratio);
376 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
378 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
379 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
384 tiff_document_init (TiffDocument *tiff_document)
386 tiff_document->n_pages = -1;
387 tiff_document->orientation = EV_ORIENTATION_PORTRAIT;