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 <glib/gi18n.h>
32 #include "tiff-document.h"
33 #include "ev-document-misc.h"
34 #include "ev-document-thumbnails.h"
35 #include "ev-file-exporter.h"
36 #include "ev-file-helpers.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_file_exporter_iface_init (EvFileExporterIface *iface);
60 EV_BACKEND_REGISTER_WITH_CODE (TiffDocument, tiff_document,
62 G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
63 tiff_document_document_thumbnails_iface_init);
64 G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
65 tiff_document_document_file_exporter_iface_init);
68 static TIFFErrorHandler orig_error_handler = NULL;
69 static TIFFErrorHandler orig_warning_handler = NULL;
74 orig_error_handler = TIFFSetErrorHandler (NULL);
75 orig_warning_handler = TIFFSetWarningHandler (NULL);
81 TIFFSetErrorHandler (orig_error_handler);
82 TIFFSetWarningHandler (orig_warning_handler);
86 tiff_document_load (EvDocument *document,
90 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
95 filename = g_filename_from_uri (uri, NULL, error);
101 tiff = TIFFOpen (filename, "r");
105 /* FIXME: unused data? why bother here */
106 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
107 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
115 tiff_document->tiff = tiff;
116 g_free (tiff_document->uri);
118 tiff_document->uri = g_strdup (uri);
125 tiff_document_save (EvDocument *document,
129 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
131 return ev_xfer_uri_simple (tiff_document->uri, uri, error);
135 tiff_document_get_n_pages (EvDocument *document)
137 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
139 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
140 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
142 if (tiff_document->n_pages == -1) {
144 tiff_document->n_pages = 0;
147 tiff_document->n_pages ++;
149 while (TIFFReadDirectory (tiff_document->tiff));
153 return tiff_document->n_pages;
157 tiff_document_get_resolution (TiffDocument *tiff_document,
161 gfloat x = 72.0, y = 72.0;
164 if (TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x) &&
165 TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y)) {
166 if (TIFFGetFieldDefaulted (tiff_document->tiff, TIFFTAG_RESOLUTIONUNIT, &unit)) {
167 if (unit == RESUNIT_CENTIMETER) {
179 tiff_document_get_page_size (EvDocument *document,
186 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
188 g_return_if_fail (TIFF_IS_DOCUMENT (document));
189 g_return_if_fail (tiff_document->tiff != NULL);
192 if (TIFFSetDirectory (tiff_document->tiff, page) != 1) {
197 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
198 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
199 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
200 h = h * (x_res / y_res);
208 static cairo_surface_t *
209 tiff_document_render (EvDocument *document,
212 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
215 gint rowstride, bytes;
216 guchar *pixels = NULL;
218 cairo_surface_t *surface;
219 cairo_surface_t *rotated_surface;
220 static const cairo_user_data_key_t key;
222 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
223 g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
226 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
231 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
236 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
241 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
245 /* Sanity check the doc */
246 if (width <= 0 || height <= 0)
249 #ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
250 rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
252 rowstride = width * 4;
253 if (rowstride / 4 != width)
258 bytes = height * rowstride;
259 if (bytes / rowstride != height)
263 pixels = g_try_malloc (bytes);
267 surface = cairo_image_surface_create_for_data (pixels,
271 cairo_surface_set_user_data (surface, &key,
272 pixels, (cairo_destroy_func_t)g_free);
274 TIFFReadRGBAImageOriented (tiff_document->tiff,
277 ORIENTATION_TOPLEFT, 1);
280 /* Convert the format returned by libtiff to
284 while (p < pixels + bytes) {
285 uint32 pixel = *(uint32 *)p;
286 int r = TIFFGetR(pixel);
287 int g = TIFFGetG(pixel);
288 int b = TIFFGetB(pixel);
289 int a = TIFFGetA(pixel);
297 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
298 (width * rc->scale) + 0.5,
299 (height * rc->scale * (x_res / y_res)) + 0.5,
301 cairo_surface_destroy (surface);
303 return rotated_surface;
307 tiff_document_render_pixbuf (EvDocument *document,
310 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
313 gint rowstride, bytes;
314 guchar *pixels = NULL;
316 GdkPixbuf *scaled_pixbuf;
317 GdkPixbuf *rotated_pixbuf;
320 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
325 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
330 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
335 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
339 /* Sanity check the doc */
340 if (width <= 0 || height <= 0)
343 rowstride = width * 4;
344 if (rowstride / 4 != width)
348 bytes = height * rowstride;
349 if (bytes / rowstride != height)
353 pixels = g_try_malloc (bytes);
357 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
358 width, height, rowstride,
359 (GdkPixbufDestroyNotify) g_free, NULL);
360 TIFFReadRGBAImageOriented (tiff_document->tiff,
363 ORIENTATION_TOPLEFT, 1);
366 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
368 height * rc->scale * (x_res / y_res),
369 GDK_INTERP_BILINEAR);
370 g_object_unref (pixbuf);
372 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
373 g_object_unref (scaled_pixbuf);
375 return rotated_pixbuf;
379 tiff_document_finalize (GObject *object)
381 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
383 if (tiff_document->tiff)
384 TIFFClose (tiff_document->tiff);
385 if (tiff_document->uri)
386 g_free (tiff_document->uri);
388 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
392 tiff_document_class_init (TiffDocumentClass *klass)
394 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
396 gobject_class->finalize = tiff_document_finalize;
399 static EvDocumentInfo *
400 tiff_document_get_info (EvDocument *document)
402 EvDocumentInfo *info;
404 info = g_new0 (EvDocumentInfo, 1);
405 info->fields_mask = 0;
411 tiff_document_document_iface_init (EvDocumentIface *iface)
413 iface->load = tiff_document_load;
414 iface->save = tiff_document_save;
415 iface->get_n_pages = tiff_document_get_n_pages;
416 iface->get_page_size = tiff_document_get_page_size;
417 iface->render = tiff_document_render;
418 iface->get_info = tiff_document_get_info;
422 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
428 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
431 GdkPixbuf *tmp_pixbuf = pixbuf;
433 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
434 g_object_unref (tmp_pixbuf);
441 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
446 gdouble page_width, page_height;
448 tiff_document_get_page_size (EV_DOCUMENT (document),
450 &page_width, &page_height);
452 if (rc->rotation == 90 || rc->rotation == 270) {
453 *width = (gint) (page_height * rc->scale);
454 *height = (gint) (page_width * rc->scale);
456 *width = (gint) (page_width * rc->scale);
457 *height = (gint) (page_height * rc->scale);
462 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
464 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
465 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
468 /* postscript exporter implementation */
470 tiff_document_file_exporter_begin (EvFileExporter *exporter,
471 EvFileExporterContext *fc)
473 TiffDocument *document = TIFF_DOCUMENT (exporter);
475 document->ps_export_ctx = tiff2ps_context_new(fc->filename);
479 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
481 TiffDocument *document = TIFF_DOCUMENT (exporter);
483 if (document->ps_export_ctx == NULL)
485 if (TIFFSetDirectory (document->tiff, rc->page) != 1)
487 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
492 tiff_document_file_exporter_end (EvFileExporter *exporter)
494 TiffDocument *document = TIFF_DOCUMENT (exporter);
496 if (document->ps_export_ctx == NULL)
498 tiff2ps_context_finalize(document->ps_export_ctx);
501 static EvFileExporterCapabilities
502 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
504 return EV_FILE_EXPORTER_CAN_PAGE_SET |
505 EV_FILE_EXPORTER_CAN_COPIES |
506 EV_FILE_EXPORTER_CAN_COLLATE |
507 EV_FILE_EXPORTER_CAN_REVERSE |
508 EV_FILE_EXPORTER_CAN_GENERATE_PS;
512 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
514 iface->begin = tiff_document_file_exporter_begin;
515 iface->do_page = tiff_document_file_exporter_do_page;
516 iface->end = tiff_document_file_exporter_end;
517 iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
521 tiff_document_init (TiffDocument *tiff_document)
523 tiff_document->n_pages = -1;