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
27 #include <glib/gi18n.h>
31 #include "tiff-document.h"
32 #include "ev-document-misc.h"
33 #include "ev-document-thumbnails.h"
34 #include "ev-file-exporter.h"
35 #include "ev-file-helpers.h"
37 struct _TiffDocumentClass
39 GObjectClass parent_class;
44 GObject parent_instance;
48 TIFF2PSContext *ps_export_ctx;
53 typedef struct _TiffDocumentClass TiffDocumentClass;
55 static void tiff_document_document_iface_init (EvDocumentIface *iface);
56 static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
57 static void tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface);
59 EV_BACKEND_REGISTER_WITH_CODE (TiffDocument, tiff_document,
61 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
62 tiff_document_document_thumbnails_iface_init);
63 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
64 tiff_document_document_file_exporter_iface_init);
67 static TIFFErrorHandler orig_error_handler = NULL;
68 static TIFFErrorHandler orig_warning_handler = NULL;
73 orig_error_handler = TIFFSetErrorHandler (NULL);
74 orig_warning_handler = TIFFSetWarningHandler (NULL);
80 TIFFSetErrorHandler (orig_error_handler);
81 TIFFSetWarningHandler (orig_warning_handler);
85 tiff_document_load (EvDocument *document,
89 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
94 filename = g_filename_from_uri (uri, NULL, error);
100 tiff = TIFFOpen (filename, "r");
104 /* FIXME: unused data? why bother here */
105 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
106 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
114 tiff_document->tiff = tiff;
115 g_free (tiff_document->uri);
117 tiff_document->uri = g_strdup (uri);
124 tiff_document_save (EvDocument *document,
128 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
130 return ev_xfer_uri_simple (tiff_document->uri, uri, error);
134 tiff_document_get_n_pages (EvDocument *document)
136 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
138 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
139 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
141 if (tiff_document->n_pages == -1) {
143 tiff_document->n_pages = 0;
146 tiff_document->n_pages ++;
148 while (TIFFReadDirectory (tiff_document->tiff));
152 return tiff_document->n_pages;
156 tiff_document_get_resolution (TiffDocument *tiff_document,
160 gfloat x = 72.0, y = 72.0;
163 if (TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x) &&
164 TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y)) {
165 if (TIFFGetFieldDefaulted (tiff_document->tiff, TIFFTAG_RESOLUTIONUNIT, &unit)) {
166 if (unit == RESUNIT_CENTIMETER) {
178 tiff_document_get_page_size (EvDocument *document,
185 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
187 g_return_if_fail (TIFF_IS_DOCUMENT (document));
188 g_return_if_fail (tiff_document->tiff != NULL);
191 if (TIFFSetDirectory (tiff_document->tiff, page) != 1) {
196 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
197 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
198 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
199 h = h * (x_res / y_res);
207 static cairo_surface_t *
208 tiff_document_render (EvDocument *document,
211 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
214 gint rowstride, bytes;
215 guchar *pixels = NULL;
218 GdkPixbuf *scaled_pixbuf;
219 GdkPixbuf *rotated_pixbuf;
220 cairo_surface_t *surface;
221 cairo_surface_t *rotated_surface;
222 static const cairo_user_data_key_t key;
224 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
225 g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
228 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
233 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
238 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
243 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
247 /* Sanity check the doc */
248 if (width <= 0 || height <= 0)
251 rowstride = width * 4;
252 if (rowstride / 4 != width)
256 bytes = height * rowstride;
257 if (bytes / rowstride != height)
261 pixels = g_try_malloc (bytes);
265 surface = cairo_image_surface_create_for_data (pixels,
269 cairo_surface_set_user_data (surface, &key,
270 pixels, (cairo_destroy_func_t)g_free);
272 TIFFReadRGBAImageOriented (tiff_document->tiff,
275 ORIENTATION_TOPLEFT, 1);
278 /* Convert the format returned by libtiff to
282 while (p < pixels + bytes) {
283 uint32 pixel = *(uint32 *)p;
284 int r = TIFFGetR(pixel);
285 int g = TIFFGetG(pixel);
286 int b = TIFFGetB(pixel);
287 int a = TIFFGetA(pixel);
295 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
296 (width * rc->scale) + 0.5,
297 (height * rc->scale * (x_res / y_res)) + 0.5,
299 cairo_surface_destroy (surface);
301 return rotated_surface;
305 tiff_document_render_pixbuf (EvDocument *document,
308 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
311 gint rowstride, bytes;
312 guchar *pixels = NULL;
314 GdkPixbuf *scaled_pixbuf;
315 GdkPixbuf *rotated_pixbuf;
318 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
323 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
328 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
333 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
337 /* Sanity check the doc */
338 if (width <= 0 || height <= 0)
341 rowstride = width * 4;
342 if (rowstride / 4 != width)
346 bytes = height * rowstride;
347 if (bytes / rowstride != height)
351 pixels = g_try_malloc (bytes);
355 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
356 width, height, rowstride,
357 (GdkPixbufDestroyNotify) g_free, NULL);
358 TIFFReadRGBAImageOriented (tiff_document->tiff,
361 ORIENTATION_TOPLEFT, 1);
364 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
366 height * rc->scale * (x_res / y_res),
367 GDK_INTERP_BILINEAR);
368 g_object_unref (pixbuf);
370 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
371 g_object_unref (scaled_pixbuf);
373 return rotated_pixbuf;
377 tiff_document_finalize (GObject *object)
379 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
381 if (tiff_document->tiff)
382 TIFFClose (tiff_document->tiff);
383 if (tiff_document->uri)
384 g_free (tiff_document->uri);
386 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
390 tiff_document_class_init (TiffDocumentClass *klass)
392 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
394 gobject_class->finalize = tiff_document_finalize;
397 static EvDocumentInfo *
398 tiff_document_get_info (EvDocument *document)
400 EvDocumentInfo *info;
402 info = g_new0 (EvDocumentInfo, 1);
403 info->fields_mask = 0;
409 tiff_document_document_iface_init (EvDocumentIface *iface)
411 iface->load = tiff_document_load;
412 iface->save = tiff_document_save;
413 iface->get_n_pages = tiff_document_get_n_pages;
414 iface->get_page_size = tiff_document_get_page_size;
415 iface->render = tiff_document_render;
416 iface->get_info = tiff_document_get_info;
420 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
426 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
429 GdkPixbuf *tmp_pixbuf = pixbuf;
431 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
432 g_object_unref (tmp_pixbuf);
439 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
444 gdouble page_width, page_height;
446 tiff_document_get_page_size (EV_DOCUMENT (document),
448 &page_width, &page_height);
450 if (rc->rotation == 90 || rc->rotation == 270) {
451 *width = (gint) (page_height * rc->scale);
452 *height = (gint) (page_width * rc->scale);
454 *width = (gint) (page_width * rc->scale);
455 *height = (gint) (page_height * rc->scale);
460 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
462 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
463 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
466 /* postscript exporter implementation */
468 tiff_document_file_exporter_begin (EvFileExporter *exporter,
469 EvFileExporterContext *fc)
471 TiffDocument *document = TIFF_DOCUMENT (exporter);
473 document->ps_export_ctx = tiff2ps_context_new(fc->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);
499 static EvFileExporterCapabilities
500 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
502 return EV_FILE_EXPORTER_CAN_PAGE_SET |
503 EV_FILE_EXPORTER_CAN_COPIES |
504 EV_FILE_EXPORTER_CAN_COLLATE |
505 EV_FILE_EXPORTER_CAN_REVERSE |
506 EV_FILE_EXPORTER_CAN_GENERATE_PS;
510 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
512 iface->begin = tiff_document_file_exporter_begin;
513 iface->do_page = tiff_document_file_exporter_do_page;
514 iface->end = tiff_document_file_exporter_end;
515 iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
519 tiff_document_init (TiffDocument *tiff_document)
521 tiff_document->n_pages = -1;