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-lib.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 EvDocumentClass parent_class;
45 EvDocument parent_instance;
49 TIFF2PSContext *ps_export_ctx;
54 typedef struct _TiffDocumentClass TiffDocumentClass;
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 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
62 tiff_document_document_thumbnails_iface_init);
63 EV_BACKEND_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);
93 filename = g_filename_from_uri (uri, NULL, error);
98 tiff = TIFFOpen (filename, "r");
102 /* FIXME: unused data? why bother here */
103 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
104 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
110 g_set_error_literal (error,
112 EV_DOCUMENT_ERROR_INVALID,
113 _("Invalid document"));
119 tiff_document->tiff = tiff;
120 g_free (tiff_document->uri);
122 tiff_document->uri = g_strdup (uri);
129 tiff_document_save (EvDocument *document,
133 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
135 return ev_xfer_uri_simple (tiff_document->uri, uri, error);
139 tiff_document_get_n_pages (EvDocument *document)
141 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
143 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
144 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
146 if (tiff_document->n_pages == -1) {
148 tiff_document->n_pages = 0;
151 tiff_document->n_pages ++;
153 while (TIFFReadDirectory (tiff_document->tiff));
157 return tiff_document->n_pages;
161 tiff_document_get_resolution (TiffDocument *tiff_document,
165 gfloat x = 72.0, y = 72.0;
168 if (TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x) &&
169 TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y)) {
170 if (TIFFGetFieldDefaulted (tiff_document->tiff, TIFFTAG_RESOLUTIONUNIT, &unit)) {
171 if (unit == RESUNIT_CENTIMETER) {
183 tiff_document_get_page_size (EvDocument *document,
190 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
192 g_return_if_fail (TIFF_IS_DOCUMENT (document));
193 g_return_if_fail (tiff_document->tiff != NULL);
196 if (TIFFSetDirectory (tiff_document->tiff, page->index) != 1) {
201 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
202 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
203 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
204 h = h * (x_res / y_res);
212 static cairo_surface_t *
213 tiff_document_render (EvDocument *document,
216 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
219 gint rowstride, bytes;
220 guchar *pixels = NULL;
223 cairo_surface_t *surface;
224 cairo_surface_t *rotated_surface;
225 static const cairo_user_data_key_t key;
227 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
228 g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
231 if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
236 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
241 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
246 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_ORIENTATION, &orientation)) {
247 orientation = ORIENTATION_TOPLEFT;
250 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
254 /* Sanity check the doc */
255 if (width <= 0 || height <= 0)
258 #ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
259 rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
261 rowstride = width * 4;
263 if (rowstride / 4 != width)
264 /* overflow, or cairo was changed in an unsupported way */
267 bytes = height * rowstride;
268 if (bytes / rowstride != height)
272 pixels = g_try_malloc (bytes);
276 surface = cairo_image_surface_create_for_data (pixels,
280 cairo_surface_set_user_data (surface, &key,
281 pixels, (cairo_destroy_func_t)g_free);
283 TIFFReadRGBAImageOriented (tiff_document->tiff,
289 /* Convert the format returned by libtiff to
293 while (p < pixels + bytes) {
294 guint32 *pixel = (guint32*)p;
295 guint8 r = TIFFGetR(*pixel);
296 guint8 g = TIFFGetG(*pixel);
297 guint8 b = TIFFGetB(*pixel);
298 guint8 a = TIFFGetA(*pixel);
300 *pixel = (a << 24) | (r << 16) | (g << 8) | b;
305 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
306 (width * rc->scale) + 0.5,
307 (height * rc->scale * (x_res / y_res)) + 0.5,
309 cairo_surface_destroy (surface);
311 return rotated_surface;
315 tiff_document_render_pixbuf (EvDocument *document,
318 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
321 gint rowstride, bytes;
322 guchar *pixels = NULL;
324 GdkPixbuf *scaled_pixbuf;
325 GdkPixbuf *rotated_pixbuf;
328 if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
333 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
338 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
343 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
347 /* Sanity check the doc */
348 if (width <= 0 || height <= 0)
351 rowstride = width * 4;
352 if (rowstride / 4 != width)
356 bytes = height * rowstride;
357 if (bytes / rowstride != height)
361 pixels = g_try_malloc (bytes);
365 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
366 width, height, rowstride,
367 (GdkPixbufDestroyNotify) g_free, NULL);
368 TIFFReadRGBAImageOriented (tiff_document->tiff,
371 ORIENTATION_TOPLEFT, 1);
374 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
376 height * rc->scale * (x_res / y_res),
377 GDK_INTERP_BILINEAR);
378 g_object_unref (pixbuf);
380 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
381 g_object_unref (scaled_pixbuf);
383 return rotated_pixbuf;
387 tiff_document_get_page_label (EvDocument *document,
390 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
393 if (TIFFGetField (tiff_document->tiff, TIFFTAG_PAGENAME, &label) &&
394 g_utf8_validate (label, -1, NULL)) {
395 return g_strdup (label);
402 tiff_document_finalize (GObject *object)
404 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
406 if (tiff_document->tiff)
407 TIFFClose (tiff_document->tiff);
408 if (tiff_document->uri)
409 g_free (tiff_document->uri);
411 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
415 tiff_document_class_init (TiffDocumentClass *klass)
417 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
418 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
420 gobject_class->finalize = tiff_document_finalize;
422 ev_document_class->load = tiff_document_load;
423 ev_document_class->save = tiff_document_save;
424 ev_document_class->get_n_pages = tiff_document_get_n_pages;
425 ev_document_class->get_page_size = tiff_document_get_page_size;
426 ev_document_class->render = tiff_document_render;
427 ev_document_class->get_page_label = tiff_document_get_page_label;
431 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
437 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
440 GdkPixbuf *tmp_pixbuf = pixbuf;
442 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
443 g_object_unref (tmp_pixbuf);
450 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
455 gdouble page_width, page_height;
457 tiff_document_get_page_size (EV_DOCUMENT (document),
459 &page_width, &page_height);
461 if (rc->rotation == 90 || rc->rotation == 270) {
462 *width = (gint) (page_height * rc->scale);
463 *height = (gint) (page_width * rc->scale);
465 *width = (gint) (page_width * rc->scale);
466 *height = (gint) (page_height * rc->scale);
471 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
473 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
474 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
477 /* postscript exporter implementation */
479 tiff_document_file_exporter_begin (EvFileExporter *exporter,
480 EvFileExporterContext *fc)
482 TiffDocument *document = TIFF_DOCUMENT (exporter);
484 document->ps_export_ctx = tiff2ps_context_new(fc->filename);
488 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
490 TiffDocument *document = TIFF_DOCUMENT (exporter);
492 if (document->ps_export_ctx == NULL)
494 if (TIFFSetDirectory (document->tiff, rc->page->index) != 1)
496 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
501 tiff_document_file_exporter_end (EvFileExporter *exporter)
503 TiffDocument *document = TIFF_DOCUMENT (exporter);
505 if (document->ps_export_ctx == NULL)
507 tiff2ps_context_finalize(document->ps_export_ctx);
510 static EvFileExporterCapabilities
511 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
513 return EV_FILE_EXPORTER_CAN_PAGE_SET |
514 EV_FILE_EXPORTER_CAN_COPIES |
515 EV_FILE_EXPORTER_CAN_COLLATE |
516 EV_FILE_EXPORTER_CAN_REVERSE |
517 EV_FILE_EXPORTER_CAN_GENERATE_PS;
521 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
523 iface->begin = tiff_document_file_exporter_begin;
524 iface->do_page = tiff_document_file_exporter_do_page;
525 iface->end = tiff_document_file_exporter_end;
526 iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
530 tiff_document_init (TiffDocument *tiff_document)
532 tiff_document->n_pages = -1;