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-file-exporter.h"
33 struct _TiffDocumentClass
35 GObjectClass parent_class;
40 GObject parent_instance;
44 TIFF2PSContext *ps_export_ctx;
49 typedef struct _TiffDocumentClass TiffDocumentClass;
51 static void tiff_document_document_iface_init (EvDocumentIface *iface);
52 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
53 static void tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface);
55 G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
56 { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
57 tiff_document_document_iface_init);
58 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
59 tiff_document_document_thumbnails_iface_init);
60 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
61 tiff_document_document_file_exporter_iface_init);
64 static TIFFErrorHandler orig_error_handler = NULL;
65 static TIFFErrorHandler orig_warning_handler = NULL;
70 orig_error_handler = TIFFSetErrorHandler (NULL);
71 orig_warning_handler = TIFFSetWarningHandler (NULL);
77 TIFFSetErrorHandler (orig_error_handler);
78 TIFFSetWarningHandler (orig_warning_handler);
82 tiff_document_load (EvDocument *document,
86 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
91 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);
111 tiff_document->tiff = tiff;
112 g_free (tiff_document->uri);
114 tiff_document->uri = g_strdup (uri);
121 tiff_document_save (EvDocument *document,
125 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
127 return ev_xfer_uri_simple (tiff_document->uri, uri, error);
131 tiff_document_get_n_pages (EvDocument *document)
133 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
135 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
136 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
138 if (tiff_document->n_pages == -1) {
140 tiff_document->n_pages = 0;
143 tiff_document->n_pages ++;
145 while (TIFFReadDirectory (tiff_document->tiff));
149 return tiff_document->n_pages;
153 tiff_document_get_resolution (TiffDocument *tiff_document,
157 gfloat x = 72.0, y = 72.0;
160 if (TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x) &&
161 TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y)) {
162 if (TIFFGetFieldDefaulted (tiff_document->tiff, TIFFTAG_RESOLUTIONUNIT, &unit)) {
163 if (unit == RESUNIT_CENTIMETER) {
175 tiff_document_get_page_size (EvDocument *document,
182 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
184 g_return_if_fail (TIFF_IS_DOCUMENT (document));
185 g_return_if_fail (tiff_document->tiff != NULL);
188 if (TIFFSetDirectory (tiff_document->tiff, page) != 1) {
193 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
194 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
195 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
196 h = h * (x_res / y_res);
204 static cairo_surface_t *
205 tiff_document_render (EvDocument *document,
208 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
211 gint rowstride, bytes;
212 guchar *pixels = NULL;
214 GdkPixbuf *scaled_pixbuf;
215 GdkPixbuf *rotated_pixbuf;
216 cairo_surface_t *surface;
217 cairo_surface_t *rotated_surface;
218 static const cairo_user_data_key_t key;
220 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
221 g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
224 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
229 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
234 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
239 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
243 /* Sanity check the doc */
244 if (width <= 0 || height <= 0)
247 rowstride = width * 4;
248 if (rowstride / 4 != width)
252 bytes = height * rowstride;
253 if (bytes / rowstride != height)
257 pixels = g_try_malloc (bytes);
261 surface = cairo_image_surface_create_for_data (pixels,
265 cairo_surface_set_user_data (surface, &key,
266 pixels, (cairo_destroy_func_t)g_free);
268 TIFFReadRGBAImageOriented (tiff_document->tiff,
271 ORIENTATION_TOPLEFT, 1);
274 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
275 (width * rc->scale) + 0.5,
276 (height * rc->scale * (x_res / y_res)) + 0.5,
278 cairo_surface_destroy (surface);
280 return rotated_surface;
284 tiff_document_render_pixbuf (EvDocument *document,
287 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
290 gint rowstride, bytes;
291 guchar *pixels = NULL;
293 GdkPixbuf *scaled_pixbuf;
294 GdkPixbuf *rotated_pixbuf;
297 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
302 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
307 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
312 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
316 /* Sanity check the doc */
317 if (width <= 0 || height <= 0)
320 rowstride = width * 4;
321 if (rowstride / 4 != width)
325 bytes = height * rowstride;
326 if (bytes / rowstride != height)
330 pixels = g_try_malloc (bytes);
334 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
335 width, height, rowstride,
336 (GdkPixbufDestroyNotify) g_free, NULL);
337 TIFFReadRGBAImageOriented (tiff_document->tiff,
340 ORIENTATION_TOPLEFT, 1);
343 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
345 height * rc->scale * (x_res / y_res),
346 GDK_INTERP_BILINEAR);
347 g_object_unref (pixbuf);
349 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
350 g_object_unref (scaled_pixbuf);
352 return rotated_pixbuf;
356 tiff_document_finalize (GObject *object)
358 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
360 if (tiff_document->tiff)
361 TIFFClose (tiff_document->tiff);
362 if (tiff_document->uri)
363 g_free (tiff_document->uri);
365 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
369 tiff_document_class_init (TiffDocumentClass *klass)
371 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
373 gobject_class->finalize = tiff_document_finalize;
377 tiff_document_can_get_text (EvDocument *document)
382 static EvDocumentInfo *
383 tiff_document_get_info (EvDocument *document)
385 EvDocumentInfo *info;
387 info = g_new0 (EvDocumentInfo, 1);
388 info->fields_mask = 0;
394 tiff_document_document_iface_init (EvDocumentIface *iface)
396 iface->load = tiff_document_load;
397 iface->save = tiff_document_save;
398 iface->can_get_text = tiff_document_can_get_text;
399 iface->get_n_pages = tiff_document_get_n_pages;
400 iface->get_page_size = tiff_document_get_page_size;
401 iface->render = tiff_document_render;
402 iface->get_info = tiff_document_get_info;
406 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
412 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
415 GdkPixbuf *tmp_pixbuf = pixbuf;
417 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
418 g_object_unref (tmp_pixbuf);
425 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
430 gdouble page_width, page_height;
432 tiff_document_get_page_size (EV_DOCUMENT (document),
434 &page_width, &page_height);
436 if (rc->rotation == 90 || rc->rotation == 270) {
437 *width = (gint) (page_height * rc->scale);
438 *height = (gint) (page_width * rc->scale);
440 *width = (gint) (page_width * rc->scale);
441 *height = (gint) (page_height * rc->scale);
446 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
448 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
449 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
452 /* postscript exporter implementation */
455 tiff_document_file_exporter_format_supported (EvFileExporter *exporter,
456 EvFileExporterFormat format)
458 return (format == EV_FILE_FORMAT_PS);
462 tiff_document_file_exporter_begin (EvFileExporter *exporter,
463 EvFileExporterFormat format,
464 const char *filename,
471 TiffDocument *document = TIFF_DOCUMENT (exporter);
473 document->ps_export_ctx = tiff2ps_context_new(filename);
477 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
479 TiffDocument *document = TIFF_DOCUMENT (exporter);
481 if (document->ps_export_ctx == NULL)
483 if (TIFFSetDirectory (document->tiff, rc->page) != 1)
485 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
490 tiff_document_file_exporter_end (EvFileExporter *exporter)
492 TiffDocument *document = TIFF_DOCUMENT (exporter);
494 if (document->ps_export_ctx == NULL)
496 tiff2ps_context_finalize(document->ps_export_ctx);
500 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
502 iface->format_supported = tiff_document_file_exporter_format_supported;
503 iface->begin = tiff_document_file_exporter_begin;
504 iface->do_page = tiff_document_file_exporter_do_page;
505 iface->end = tiff_document_file_exporter_end;
509 tiff_document_init (TiffDocument *tiff_document)
511 tiff_document->n_pages = -1;