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 struct _TiffDocumentClass
35 GObjectClass parent_class;
40 GObject parent_instance;
44 TIFF2PSContext *ps_export_ctx;
47 typedef struct _TiffDocumentClass TiffDocumentClass;
49 static void tiff_document_document_iface_init (EvDocumentIface *iface);
50 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
51 static void tiff_document_document_ps_exporter_iface_init (EvPSExporterIface *iface);
53 G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
54 { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
55 tiff_document_document_iface_init);
56 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
57 tiff_document_document_thumbnails_iface_init);
58 G_IMPLEMENT_INTERFACE (EV_TYPE_PS_EXPORTER,
59 tiff_document_document_ps_exporter_iface_init);
62 static TIFFErrorHandler orig_error_handler = NULL;
63 static TIFFErrorHandler orig_warning_handler = NULL;
68 orig_error_handler = TIFFSetErrorHandler (NULL);
69 orig_warning_handler = TIFFSetWarningHandler (NULL);
75 TIFFSetErrorHandler (orig_error_handler);
76 TIFFSetWarningHandler (orig_warning_handler);
80 tiff_document_load (EvDocument *document,
84 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
89 /* FIXME: We could actually load uris */
90 filename = g_filename_from_uri (uri, NULL, error);
97 tiff = TIFFOpen (filename, "r");
101 /* FIXME: unused data? why bother here */
102 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
103 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
110 tiff_document->tiff = tiff;
117 tiff_document_save (EvDocument *document,
125 tiff_document_get_n_pages (EvDocument *document)
127 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
129 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
130 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
132 if (tiff_document->n_pages == -1)
135 tiff_document->n_pages = 0;
138 tiff_document->n_pages ++;
140 while (TIFFReadDirectory (tiff_document->tiff));
144 return tiff_document->n_pages;
148 tiff_document_get_page_size (EvDocument *document,
155 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
157 g_return_if_fail (TIFF_IS_DOCUMENT (document));
158 g_return_if_fail (tiff_document->tiff != NULL);
161 if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
167 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
168 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
169 TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res);
170 TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res);
171 h = h * (x_res / y_res);
180 tiff_document_get_orientation (EvDocument *document)
182 return EV_ORIENTATION_PORTRAIT;
186 rotate_pixbuf (EvDocument *document, EvOrientation orientation, GdkPixbuf *pixbuf)
188 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
192 case EV_ORIENTATION_LANDSCAPE:
193 return gdk_pixbuf_rotate_simple (pixbuf, 90);
194 case EV_ORIENTATION_UPSIDEDOWN:
195 return gdk_pixbuf_rotate_simple (pixbuf, 180);
196 case EV_ORIENTATION_SEASCAPE:
197 return gdk_pixbuf_rotate_simple (pixbuf, 270);
199 return g_object_ref (pixbuf);
204 tiff_document_render_pixbuf (EvDocument *document,
207 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
210 gint rowstride, bytes;
211 guchar *pixels = NULL;
213 GdkPixbuf *scaled_pixbuf;
214 GdkPixbuf *rotated_pixbuf;
216 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
217 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
220 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1)
226 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width))
232 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height))
238 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res))
244 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res))
252 /* Sanity check the doc */
253 if (width <= 0 || height <= 0)
256 rowstride = width * 4;
257 if (rowstride / 4 != width)
261 bytes = height * rowstride;
262 if (bytes / rowstride != height)
266 pixels = g_try_malloc (bytes);
270 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
271 width, height, rowstride,
272 (GdkPixbufDestroyNotify) g_free, NULL);
274 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
275 TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
278 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
280 height * rc->scale * (x_res/y_res),
281 GDK_INTERP_BILINEAR);
282 g_object_unref (pixbuf);
284 rotated_pixbuf = rotate_pixbuf (document, rc->orientation, scaled_pixbuf);
285 g_object_unref (scaled_pixbuf);
287 return rotated_pixbuf;
291 tiff_document_finalize (GObject *object)
293 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
295 TIFFClose (tiff_document->tiff);
297 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
301 tiff_document_class_init (TiffDocumentClass *klass)
303 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
305 gobject_class->finalize = tiff_document_finalize;
309 tiff_document_can_get_text (EvDocument *document)
314 static EvDocumentInfo *
315 tiff_document_get_info (EvDocument *document)
317 EvDocumentInfo *info;
319 info = g_new0 (EvDocumentInfo, 1);
320 info->fields_mask = 0;
326 tiff_document_document_iface_init (EvDocumentIface *iface)
328 iface->load = tiff_document_load;
329 iface->save = tiff_document_save;
330 iface->can_get_text = tiff_document_can_get_text;
331 iface->get_n_pages = tiff_document_get_n_pages;
332 iface->get_page_size = tiff_document_get_page_size;
333 iface->render_pixbuf = tiff_document_render_pixbuf;
334 iface->get_info = tiff_document_get_info;
335 iface->get_orientation = tiff_document_get_orientation;
339 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
341 EvOrientation orientation,
349 tiff_document_get_page_size (EV_DOCUMENT (document),
353 rc = ev_render_context_new (EV_ORIENTATION_PORTRAIT, page, size/w);
354 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
355 g_object_unref (G_OBJECT (rc));
359 GdkPixbuf *tmp_pixbuf = pixbuf;
360 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
361 g_object_unref (tmp_pixbuf);
368 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
370 gint suggested_width,
377 tiff_document_get_page_size (EV_DOCUMENT (document),
380 g_return_if_fail (w > 0);
382 *width = suggested_width;
383 *height = (gint) (suggested_width * page_ratio);
387 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
389 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
390 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
393 /* postscript exporter implementation */
396 tiff_document_ps_export_begin (EvPSExporter *exporter, const char *filename,
397 int first_page, int last_page,
398 double width, double height, gboolean duplex)
400 TiffDocument *document = TIFF_DOCUMENT (exporter);
402 document->ps_export_ctx = tiff2ps_context_new(filename);
406 tiff_document_ps_export_do_page (EvPSExporter *exporter, EvRenderContext *rc)
408 TiffDocument *document = TIFF_DOCUMENT (exporter);
410 if (document->ps_export_ctx == NULL)
412 if (TIFFSetDirectory (document->tiff, rc->page) != 1)
414 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
419 tiff_document_ps_export_end (EvPSExporter *exporter)
421 TiffDocument *document = TIFF_DOCUMENT (exporter);
423 if (document->ps_export_ctx == NULL)
425 tiff2ps_context_finalize(document->ps_export_ctx);
429 tiff_document_document_ps_exporter_iface_init (EvPSExporterIface *iface)
431 iface->begin = tiff_document_ps_export_begin;
432 iface->do_page = tiff_document_ps_export_do_page;
433 iface->end = tiff_document_ps_export_end;
437 tiff_document_init (TiffDocument *tiff_document)
439 tiff_document->n_pages = -1;