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);
170 gint rowstride, bytes;
171 guchar *pixels = NULL;
173 GdkPixbuf *scaled_pixbuf;
175 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
176 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
179 if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
185 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width))
191 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height))
199 /* Sanity check the doc */
200 if (width <= 0 || height <= 0)
203 rowstride = width * 4;
204 if (rowstride / 4 != width)
208 bytes = height * rowstride;
209 if (bytes / rowstride != height)
213 pixels = g_try_malloc (bytes);
217 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
218 width, height, rowstride,
219 (GdkPixbufDestroyNotify) g_free, NULL);
221 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
222 TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
225 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
228 GDK_INTERP_BILINEAR);
229 g_object_unref (pixbuf);
231 return scaled_pixbuf;
235 tiff_document_finalize (GObject *object)
237 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
239 TIFFClose (tiff_document->tiff);
241 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
245 tiff_document_class_init (TiffDocumentClass *klass)
247 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
249 gobject_class->finalize = tiff_document_finalize;
253 tiff_document_can_get_text (EvDocument *document)
258 static EvDocumentInfo *
259 tiff_document_get_info (EvDocument *document)
261 EvDocumentInfo *info;
263 info = g_new0 (EvDocumentInfo, 1);
264 info->fields_mask = 0;
270 tiff_document_document_iface_init (EvDocumentIface *iface)
272 iface->load = tiff_document_load;
273 iface->save = tiff_document_save;
274 iface->can_get_text = tiff_document_can_get_text;
275 iface->get_n_pages = tiff_document_get_n_pages;
276 iface->get_page_size = tiff_document_get_page_size;
277 iface->render_pixbuf = tiff_document_render_pixbuf;
278 iface->get_info = tiff_document_get_info;
282 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
290 tiff_document_get_page_size (EV_DOCUMENT (document),
294 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document),
300 GdkPixbuf *tmp_pixbuf = pixbuf;
301 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
302 g_object_unref (tmp_pixbuf);
309 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
311 gint suggested_width,
318 tiff_document_get_page_size (EV_DOCUMENT (document),
321 g_return_if_fail (w > 0);
323 *width = suggested_width;
324 *height = (gint) (suggested_width * page_ratio);
328 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
330 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
331 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
336 tiff_document_init (TiffDocument *tiff_document)
338 tiff_document->n_pages = -1;