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;
41 typedef struct _TiffDocumentClass TiffDocumentClass;
43 static void tiff_document_document_iface_init (EvDocumentIface *iface);
44 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
46 G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
47 { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
48 tiff_document_document_iface_init);
49 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
50 tiff_document_document_thumbnails_iface_init);
53 static TIFFErrorHandler orig_error_handler = NULL;
54 static TIFFErrorHandler orig_warning_handler = NULL;
59 orig_error_handler = TIFFSetErrorHandler (NULL);
60 orig_warning_handler = TIFFSetWarningHandler (NULL);
66 TIFFSetErrorHandler (orig_error_handler);
67 TIFFSetWarningHandler (orig_warning_handler);
71 tiff_document_load (EvDocument *document,
75 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
80 /* FIXME: We could actually load uris */
81 filename = g_filename_from_uri (uri, NULL, error);
88 tiff = TIFFOpen (filename, "r");
93 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
94 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
101 tiff_document->tiff = tiff;
108 tiff_document_save (EvDocument *document,
116 tiff_document_get_n_pages (EvDocument *document)
118 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
120 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
121 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
123 if (tiff_document->n_pages == -1)
126 tiff_document->n_pages = 0;
129 tiff_document->n_pages ++;
131 while (TIFFReadDirectory (tiff_document->tiff));
135 return tiff_document->n_pages;
139 tiff_document_get_page_size (EvDocument *document,
145 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
147 g_return_if_fail (TIFF_IS_DOCUMENT (document));
148 g_return_if_fail (tiff_document->tiff != NULL);
151 if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
157 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
158 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
166 tiff_document_render_pixbuf (EvDocument *document, int page, double scale)
168 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
171 GdkPixbuf *scaled_pixbuf;
173 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
174 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
177 if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
183 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width);
184 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height);
186 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
187 TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
190 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
193 GDK_INTERP_BILINEAR);
194 g_object_unref (pixbuf);
196 return scaled_pixbuf;
200 tiff_document_finalize (GObject *object)
202 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
204 TIFFClose (tiff_document->tiff);
206 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
210 tiff_document_class_init (TiffDocumentClass *klass)
212 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
214 gobject_class->finalize = tiff_document_finalize;
218 tiff_document_can_get_text (EvDocument *document)
223 static EvDocumentInfo *
224 tiff_document_get_info (EvDocument *document)
226 EvDocumentInfo *info;
228 info = g_new0 (EvDocumentInfo, 1);
229 info->fields_mask = 0;
235 tiff_document_document_iface_init (EvDocumentIface *iface)
237 iface->load = tiff_document_load;
238 iface->save = tiff_document_save;
239 iface->can_get_text = tiff_document_can_get_text;
240 iface->get_n_pages = tiff_document_get_n_pages;
241 iface->get_page_size = tiff_document_get_page_size;
242 iface->render_pixbuf = tiff_document_render_pixbuf;
243 iface->get_info = tiff_document_get_info;
247 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
255 tiff_document_get_page_size (EV_DOCUMENT (document),
259 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document),
265 GdkPixbuf *tmp_pixbuf = pixbuf;
266 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
267 g_object_unref (tmp_pixbuf);
274 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
276 gint suggested_width,
283 tiff_document_get_page_size (EV_DOCUMENT (document),
286 g_return_if_fail (w > 0);
288 *width = suggested_width;
289 *height = (gint) (suggested_width * page_ratio);
293 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
295 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
296 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
301 tiff_document_init (TiffDocument *tiff_document)
303 tiff_document->n_pages = -1;