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
28 #include "tiff-document.h"
29 #include "ev-document-misc.h"
30 #include "ev-document-thumbnails.h"
31 #include "ev-ps-exporter.h"
33 #include <libgnomevfs/gnome-vfs-uri.h>
34 #include <libgnomevfs/gnome-vfs-utils.h>
35 #include <libgnomevfs/gnome-vfs-ops.h>
36 #include <libgnomevfs/gnome-vfs-xfer.h>
38 struct _TiffDocumentClass
40 GObjectClass parent_class;
45 GObject parent_instance;
49 TIFF2PSContext *ps_export_ctx;
54 typedef struct _TiffDocumentClass TiffDocumentClass;
56 static void tiff_document_document_iface_init (EvDocumentIface *iface);
57 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
58 static void tiff_document_document_ps_exporter_iface_init (EvPSExporterIface *iface);
60 G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
61 { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
62 tiff_document_document_iface_init);
63 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
64 tiff_document_document_thumbnails_iface_init);
65 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
66 tiff_document_document_ps_exporter_iface_init);
69 static TIFFErrorHandler orig_error_handler = NULL;
70 static TIFFErrorHandler orig_warning_handler = NULL;
75 orig_error_handler = TIFFSetErrorHandler (NULL);
76 orig_warning_handler = TIFFSetWarningHandler (NULL);
82 TIFFSetErrorHandler (orig_error_handler);
83 TIFFSetWarningHandler (orig_warning_handler);
87 tiff_document_load (EvDocument *document,
91 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
96 /* FIXME: We could actually load uris */
97 filename = g_filename_from_uri (uri, NULL, error);
104 tiff = TIFFOpen (filename, "r");
108 /* FIXME: unused data? why bother here */
109 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
110 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
117 tiff_document->tiff = tiff;
118 g_free (tiff_document->uri);
119 tiff_document->uri = g_strdup (uri);
126 tiff_document_save (EvDocument *document,
130 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
131 GnomeVFSResult result;
132 GnomeVFSURI *source_uri;
133 GnomeVFSURI *target_uri;
135 if (!tiff_document->uri)
138 source_uri = gnome_vfs_uri_new (tiff_document->uri);
139 target_uri = gnome_vfs_uri_new (uri);
141 result = gnome_vfs_xfer_uri (source_uri, target_uri,
142 GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
143 GNOME_VFS_XFER_ERROR_MODE_ABORT,
144 GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
147 gnome_vfs_uri_unref (target_uri);
148 gnome_vfs_uri_unref (source_uri);
150 if (result != GNOME_VFS_OK)
154 gnome_vfs_result_to_string (result));
155 return (result == GNOME_VFS_OK);
159 tiff_document_get_n_pages (EvDocument *document)
161 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
163 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
164 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
166 if (tiff_document->n_pages == -1)
169 tiff_document->n_pages = 0;
172 tiff_document->n_pages ++;
174 while (TIFFReadDirectory (tiff_document->tiff));
178 return tiff_document->n_pages;
182 tiff_document_get_page_size (EvDocument *document,
189 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
191 g_return_if_fail (TIFF_IS_DOCUMENT (document));
192 g_return_if_fail (tiff_document->tiff != NULL);
195 if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
201 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
202 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
203 TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res);
204 TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res);
205 h = h * (x_res / y_res);
214 tiff_document_render_pixbuf (EvDocument *document,
217 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
220 gint rowstride, bytes;
221 guchar *pixels = NULL;
223 GdkPixbuf *scaled_pixbuf;
224 GdkPixbuf *rotated_pixbuf;
226 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
227 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
230 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1)
236 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width))
242 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height))
248 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res))
254 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res))
262 /* Sanity check the doc */
263 if (width <= 0 || height <= 0)
266 rowstride = width * 4;
267 if (rowstride / 4 != width)
271 bytes = height * rowstride;
272 if (bytes / rowstride != height)
276 pixels = g_try_malloc (bytes);
280 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
281 width, height, rowstride,
282 (GdkPixbufDestroyNotify) g_free, NULL);
284 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
285 TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
288 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
290 height * rc->scale * (x_res/y_res),
291 GDK_INTERP_BILINEAR);
292 g_object_unref (pixbuf);
294 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
295 g_object_unref (scaled_pixbuf);
297 return rotated_pixbuf;
301 tiff_document_finalize (GObject *object)
303 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
305 TIFFClose (tiff_document->tiff);
306 g_free (tiff_document->uri);
308 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
312 tiff_document_class_init (TiffDocumentClass *klass)
314 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
316 gobject_class->finalize = tiff_document_finalize;
320 tiff_document_can_get_text (EvDocument *document)
325 static EvDocumentInfo *
326 tiff_document_get_info (EvDocument *document)
328 EvDocumentInfo *info;
330 info = g_new0 (EvDocumentInfo, 1);
331 info->fields_mask = 0;
337 tiff_document_document_iface_init (EvDocumentIface *iface)
339 iface->load = tiff_document_load;
340 iface->save = tiff_document_save;
341 iface->can_get_text = tiff_document_can_get_text;
342 iface->get_n_pages = tiff_document_get_n_pages;
343 iface->get_page_size = tiff_document_get_page_size;
344 iface->render_pixbuf = tiff_document_render_pixbuf;
345 iface->get_info = tiff_document_get_info;
349 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
359 tiff_document_get_page_size (EV_DOCUMENT (document),
363 rc = ev_render_context_new (rotation, page, size/w);
364 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
365 g_object_unref (G_OBJECT (rc));
369 GdkPixbuf *tmp_pixbuf = pixbuf;
370 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf);
371 g_object_unref (tmp_pixbuf);
378 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
380 gint suggested_width,
387 tiff_document_get_page_size (EV_DOCUMENT (document),
390 g_return_if_fail (w > 0);
392 *width = suggested_width;
393 *height = (gint) (suggested_width * page_ratio);
397 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
399 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
400 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
403 /* postscript exporter implementation */
406 tiff_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
407 int first_page, int last_page,
408 double width, double height, gboolean duplex)
410 TiffDocument *document = TIFF_DOCUMENT (exporter);
412 document->ps_export_ctx = tiff2ps_context_new(filename);
416 tiff_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
418 TiffDocument *document = TIFF_DOCUMENT (exporter);
420 if (document->ps_export_ctx == NULL)
422 if (TIFFSetDirectory (document->tiff, rc->page) != 1)
424 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
429 tiff_document_ps_export_end (EvPSExporter *exporter)
431 TiffDocument *document = TIFF_DOCUMENT (exporter);
433 if (document->ps_export_ctx == NULL)
435 tiff2ps_context_finalize(document->ps_export_ctx);
439 tiff_document_document_ps_exporter_iface_init (EvPSExporterIface *iface)
441 iface->begin = tiff_document_ps_export_begin;
442 iface->do_page = tiff_document_ps_export_do_page;
443 iface->end = tiff_document_ps_export_end;
447 tiff_document_init (TiffDocument *tiff_document)
449 tiff_document->n_pages = -1;