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 rowstride = width * 4;
250 if (rowstride / 4 != width)
254 bytes = height * rowstride;
255 if (bytes / rowstride != height)
259 pixels = g_try_malloc (bytes);
263 surface = cairo_image_surface_create_for_data (pixels,
267 cairo_surface_set_user_data (surface, &key,
268 pixels, (cairo_destroy_func_t)g_free);
270 TIFFReadRGBAImageOriented (tiff_document->tiff,
273 ORIENTATION_TOPLEFT, 1);
276 /* Convert the format returned by libtiff to
280 while (p < pixels + bytes) {
281 uint32 pixel = *(uint32 *)p;
282 int r = TIFFGetR(pixel);
283 int g = TIFFGetG(pixel);
284 int b = TIFFGetB(pixel);
285 int a = TIFFGetA(pixel);
293 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
294 (width * rc->scale) + 0.5,
295 (height * rc->scale * (x_res / y_res)) + 0.5,
297 cairo_surface_destroy (surface);
299 return rotated_surface;
303 tiff_document_render_pixbuf (EvDocument *document,
306 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
309 gint rowstride, bytes;
310 guchar *pixels = NULL;
312 GdkPixbuf *scaled_pixbuf;
313 GdkPixbuf *rotated_pixbuf;
316 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
321 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
326 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
331 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
335 /* Sanity check the doc */
336 if (width <= 0 || height <= 0)
339 rowstride = width * 4;
340 if (rowstride / 4 != width)
344 bytes = height * rowstride;
345 if (bytes / rowstride != height)
349 pixels = g_try_malloc (bytes);
353 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
354 width, height, rowstride,
355 (GdkPixbufDestroyNotify) g_free, NULL);
356 TIFFReadRGBAImageOriented (tiff_document->tiff,
359 ORIENTATION_TOPLEFT, 1);
362 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
364 height * rc->scale * (x_res / y_res),
365 GDK_INTERP_BILINEAR);
366 g_object_unref (pixbuf);
368 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
369 g_object_unref (scaled_pixbuf);
371 return rotated_pixbuf;
375 tiff_document_finalize (GObject *object)
377 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
379 if (tiff_document->tiff)
380 TIFFClose (tiff_document->tiff);
381 if (tiff_document->uri)
382 g_free (tiff_document->uri);
384 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
388 tiff_document_class_init (TiffDocumentClass *klass)
390 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
392 gobject_class->finalize = tiff_document_finalize;
395 static EvDocumentInfo *
396 tiff_document_get_info (EvDocument *document)
398 EvDocumentInfo *info;
400 info = g_new0 (EvDocumentInfo, 1);
401 info->fields_mask = 0;
407 tiff_document_document_iface_init (EvDocumentIface *iface)
409 iface->load = tiff_document_load;
410 iface->save = tiff_document_save;
411 iface->get_n_pages = tiff_document_get_n_pages;
412 iface->get_page_size = tiff_document_get_page_size;
413 iface->render = tiff_document_render;
414 iface->get_info = tiff_document_get_info;
418 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
424 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
427 GdkPixbuf *tmp_pixbuf = pixbuf;
429 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
430 g_object_unref (tmp_pixbuf);
437 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
442 gdouble page_width, page_height;
444 tiff_document_get_page_size (EV_DOCUMENT (document),
446 &page_width, &page_height);
448 if (rc->rotation == 90 || rc->rotation == 270) {
449 *width = (gint) (page_height * rc->scale);
450 *height = (gint) (page_width * rc->scale);
452 *width = (gint) (page_width * rc->scale);
453 *height = (gint) (page_height * rc->scale);
458 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
460 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
461 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
464 /* postscript exporter implementation */
466 tiff_document_file_exporter_begin (EvFileExporter *exporter,
467 EvFileExporterContext *fc)
469 TiffDocument *document = TIFF_DOCUMENT (exporter);
471 document->ps_export_ctx = tiff2ps_context_new(fc->filename);
475 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
477 TiffDocument *document = TIFF_DOCUMENT (exporter);
479 if (document->ps_export_ctx == NULL)
481 if (TIFFSetDirectory (document->tiff, rc->page) != 1)
483 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
488 tiff_document_file_exporter_end (EvFileExporter *exporter)
490 TiffDocument *document = TIFF_DOCUMENT (exporter);
492 if (document->ps_export_ctx == NULL)
494 tiff2ps_context_finalize(document->ps_export_ctx);
497 static EvFileExporterCapabilities
498 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
500 return EV_FILE_EXPORTER_CAN_PAGE_SET |
501 EV_FILE_EXPORTER_CAN_COPIES |
502 EV_FILE_EXPORTER_CAN_COLLATE |
503 EV_FILE_EXPORTER_CAN_REVERSE |
504 EV_FILE_EXPORTER_CAN_GENERATE_PS;
508 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
510 iface->begin = tiff_document_file_exporter_begin;
511 iface->do_page = tiff_document_file_exporter_do_page;
512 iface->end = tiff_document_file_exporter_end;
513 iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
517 tiff_document_init (TiffDocument *tiff_document)
519 tiff_document->n_pages = -1;