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;
219 GdkPixbuf *scaled_pixbuf;
220 GdkPixbuf *rotated_pixbuf;
221 cairo_surface_t *surface;
222 cairo_surface_t *rotated_surface;
223 static const cairo_user_data_key_t key;
225 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
226 g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
229 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
234 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
239 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
244 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
248 /* Sanity check the doc */
249 if (width <= 0 || height <= 0)
252 rowstride = width * 4;
253 if (rowstride / 4 != width)
257 bytes = height * rowstride;
258 if (bytes / rowstride != height)
262 pixels = g_try_malloc (bytes);
266 surface = cairo_image_surface_create_for_data (pixels,
270 cairo_surface_set_user_data (surface, &key,
271 pixels, (cairo_destroy_func_t)g_free);
273 TIFFReadRGBAImageOriented (tiff_document->tiff,
276 ORIENTATION_TOPLEFT, 1);
279 /* Convert the format returned by libtiff to
283 while (p < pixels + bytes) {
284 uint32 pixel = *(uint32 *)p;
285 int r = TIFFGetR(pixel);
286 int g = TIFFGetG(pixel);
287 int b = TIFFGetB(pixel);
288 int a = TIFFGetA(pixel);
296 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
297 (width * rc->scale) + 0.5,
298 (height * rc->scale * (x_res / y_res)) + 0.5,
300 cairo_surface_destroy (surface);
302 return rotated_surface;
306 tiff_document_render_pixbuf (EvDocument *document,
309 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
312 gint rowstride, bytes;
313 guchar *pixels = NULL;
315 GdkPixbuf *scaled_pixbuf;
316 GdkPixbuf *rotated_pixbuf;
319 if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1) {
324 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
329 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
334 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
338 /* Sanity check the doc */
339 if (width <= 0 || height <= 0)
342 rowstride = width * 4;
343 if (rowstride / 4 != width)
347 bytes = height * rowstride;
348 if (bytes / rowstride != height)
352 pixels = g_try_malloc (bytes);
356 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
357 width, height, rowstride,
358 (GdkPixbufDestroyNotify) g_free, NULL);
359 TIFFReadRGBAImageOriented (tiff_document->tiff,
362 ORIENTATION_TOPLEFT, 1);
365 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
367 height * rc->scale * (x_res / y_res),
368 GDK_INTERP_BILINEAR);
369 g_object_unref (pixbuf);
371 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
372 g_object_unref (scaled_pixbuf);
374 return rotated_pixbuf;
378 tiff_document_finalize (GObject *object)
380 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
382 if (tiff_document->tiff)
383 TIFFClose (tiff_document->tiff);
384 if (tiff_document->uri)
385 g_free (tiff_document->uri);
387 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
391 tiff_document_class_init (TiffDocumentClass *klass)
393 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
395 gobject_class->finalize = tiff_document_finalize;
398 static EvDocumentInfo *
399 tiff_document_get_info (EvDocument *document)
401 EvDocumentInfo *info;
403 info = g_new0 (EvDocumentInfo, 1);
404 info->fields_mask = 0;
410 tiff_document_document_iface_init (EvDocumentIface *iface)
412 iface->load = tiff_document_load;
413 iface->save = tiff_document_save;
414 iface->get_n_pages = tiff_document_get_n_pages;
415 iface->get_page_size = tiff_document_get_page_size;
416 iface->render = tiff_document_render;
417 iface->get_info = tiff_document_get_info;
421 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
427 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
430 GdkPixbuf *tmp_pixbuf = pixbuf;
432 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
433 g_object_unref (tmp_pixbuf);
440 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
445 gdouble page_width, page_height;
447 tiff_document_get_page_size (EV_DOCUMENT (document),
449 &page_width, &page_height);
451 if (rc->rotation == 90 || rc->rotation == 270) {
452 *width = (gint) (page_height * rc->scale);
453 *height = (gint) (page_width * rc->scale);
455 *width = (gint) (page_width * rc->scale);
456 *height = (gint) (page_height * rc->scale);
461 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
463 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
464 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
467 /* postscript exporter implementation */
469 tiff_document_file_exporter_begin (EvFileExporter *exporter,
470 EvFileExporterContext *fc)
472 TiffDocument *document = TIFF_DOCUMENT (exporter);
474 document->ps_export_ctx = tiff2ps_context_new(fc->filename);
478 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
480 TiffDocument *document = TIFF_DOCUMENT (exporter);
482 if (document->ps_export_ctx == NULL)
484 if (TIFFSetDirectory (document->tiff, rc->page) != 1)
486 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
491 tiff_document_file_exporter_end (EvFileExporter *exporter)
493 TiffDocument *document = TIFF_DOCUMENT (exporter);
495 if (document->ps_export_ctx == NULL)
497 tiff2ps_context_finalize(document->ps_export_ctx);
500 static EvFileExporterCapabilities
501 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
503 return EV_FILE_EXPORTER_CAN_PAGE_SET |
504 EV_FILE_EXPORTER_CAN_COPIES |
505 EV_FILE_EXPORTER_CAN_COLLATE |
506 EV_FILE_EXPORTER_CAN_REVERSE |
507 EV_FILE_EXPORTER_CAN_GENERATE_PS;
511 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
513 iface->begin = tiff_document_file_exporter_begin;
514 iface->do_page = tiff_document_file_exporter_do_page;
515 iface->end = tiff_document_file_exporter_end;
516 iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
520 tiff_document_init (TiffDocument *tiff_document)
522 tiff_document->n_pages = -1;