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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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-file-exporter.h"
35 #include "ev-file-helpers.h"
37 struct _TiffDocumentClass
39 EvDocumentClass parent_class;
44 EvDocument parent_instance;
48 TIFF2PSContext *ps_export_ctx;
53 typedef struct _TiffDocumentClass TiffDocumentClass;
55 static void tiff_document_document_file_exporter_iface_init (EvFileExporterInterface *iface);
57 EV_BACKEND_REGISTER_WITH_CODE (TiffDocument, tiff_document,
59 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
60 tiff_document_document_file_exporter_iface_init);
63 static TIFFErrorHandler orig_error_handler = NULL;
64 static TIFFErrorHandler orig_warning_handler = NULL;
69 orig_error_handler = TIFFSetErrorHandler (NULL);
70 orig_warning_handler = TIFFSetWarningHandler (NULL);
76 TIFFSetErrorHandler (orig_error_handler);
77 TIFFSetWarningHandler (orig_warning_handler);
81 tiff_document_load (EvDocument *document,
85 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
89 filename = g_filename_from_uri (uri, NULL, error);
94 tiff = TIFFOpen (filename, "r");
98 /* FIXME: unused data? why bother here */
99 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
100 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
106 g_set_error_literal (error,
108 EV_DOCUMENT_ERROR_INVALID,
109 _("Invalid document"));
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->index) != 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 cairo_surface_t *surface;
220 cairo_surface_t *rotated_surface;
221 static const cairo_user_data_key_t key;
223 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
224 g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
227 if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
232 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
237 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
242 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_ORIENTATION, &orientation)) {
243 orientation = ORIENTATION_TOPLEFT;
246 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
250 /* Sanity check the doc */
251 if (width <= 0 || height <= 0)
254 #ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
255 rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
257 rowstride = width * 4;
259 if (rowstride / 4 != width)
260 /* overflow, or cairo was changed in an unsupported way */
263 bytes = height * rowstride;
264 if (bytes / rowstride != height)
268 pixels = g_try_malloc (bytes);
272 surface = cairo_image_surface_create_for_data (pixels,
276 cairo_surface_set_user_data (surface, &key,
277 pixels, (cairo_destroy_func_t)g_free);
279 TIFFReadRGBAImageOriented (tiff_document->tiff,
285 /* Convert the format returned by libtiff to
289 while (p < pixels + bytes) {
290 guint32 *pixel = (guint32*)p;
291 guint8 r = TIFFGetR(*pixel);
292 guint8 g = TIFFGetG(*pixel);
293 guint8 b = TIFFGetB(*pixel);
294 guint8 a = TIFFGetA(*pixel);
296 *pixel = (a << 24) | (r << 16) | (g << 8) | b;
301 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
302 (width * rc->scale) + 0.5,
303 (height * rc->scale * (x_res / y_res)) + 0.5,
305 cairo_surface_destroy (surface);
307 return rotated_surface;
311 tiff_document_get_thumbnail (EvDocument *document,
314 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
317 gint rowstride, bytes;
318 guchar *pixels = NULL;
320 GdkPixbuf *scaled_pixbuf;
321 GdkPixbuf *rotated_pixbuf;
324 if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
329 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
334 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
339 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
343 /* Sanity check the doc */
344 if (width <= 0 || height <= 0)
347 rowstride = width * 4;
348 if (rowstride / 4 != width)
352 bytes = height * rowstride;
353 if (bytes / rowstride != height)
357 pixels = g_try_malloc (bytes);
361 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
362 width, height, rowstride,
363 (GdkPixbufDestroyNotify) g_free, NULL);
364 TIFFReadRGBAImageOriented (tiff_document->tiff,
367 ORIENTATION_TOPLEFT, 1);
370 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
372 height * rc->scale * (x_res / y_res),
373 GDK_INTERP_BILINEAR);
374 g_object_unref (pixbuf);
376 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
377 g_object_unref (scaled_pixbuf);
379 return rotated_pixbuf;
383 tiff_document_get_page_label (EvDocument *document,
386 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
389 if (TIFFGetField (tiff_document->tiff, TIFFTAG_PAGENAME, &label) &&
390 g_utf8_validate (label, -1, NULL)) {
391 return g_strdup (label);
398 tiff_document_finalize (GObject *object)
400 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
402 if (tiff_document->tiff)
403 TIFFClose (tiff_document->tiff);
404 if (tiff_document->uri)
405 g_free (tiff_document->uri);
407 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
411 tiff_document_class_init (TiffDocumentClass *klass)
413 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
414 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
416 gobject_class->finalize = tiff_document_finalize;
418 ev_document_class->load = tiff_document_load;
419 ev_document_class->save = tiff_document_save;
420 ev_document_class->get_n_pages = tiff_document_get_n_pages;
421 ev_document_class->get_page_size = tiff_document_get_page_size;
422 ev_document_class->render = tiff_document_render;
423 ev_document_class->get_thumbnail = tiff_document_get_thumbnail;
424 ev_document_class->get_page_label = tiff_document_get_page_label;
427 /* postscript exporter implementation */
429 tiff_document_file_exporter_begin (EvFileExporter *exporter,
430 EvFileExporterContext *fc)
432 TiffDocument *document = TIFF_DOCUMENT (exporter);
434 document->ps_export_ctx = tiff2ps_context_new(fc->filename);
438 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
440 TiffDocument *document = TIFF_DOCUMENT (exporter);
442 if (document->ps_export_ctx == NULL)
444 if (TIFFSetDirectory (document->tiff, rc->page->index) != 1)
446 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
451 tiff_document_file_exporter_end (EvFileExporter *exporter)
453 TiffDocument *document = TIFF_DOCUMENT (exporter);
455 if (document->ps_export_ctx == NULL)
457 tiff2ps_context_finalize(document->ps_export_ctx);
460 static EvFileExporterCapabilities
461 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
463 return EV_FILE_EXPORTER_CAN_PAGE_SET |
464 EV_FILE_EXPORTER_CAN_COPIES |
465 EV_FILE_EXPORTER_CAN_COLLATE |
466 EV_FILE_EXPORTER_CAN_REVERSE |
467 EV_FILE_EXPORTER_CAN_GENERATE_PS;
471 tiff_document_document_file_exporter_iface_init (EvFileExporterInterface *iface)
473 iface->begin = tiff_document_file_exporter_begin;
474 iface->do_page = tiff_document_file_exporter_do_page;
475 iface->end = tiff_document_file_exporter_end;
476 iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
480 tiff_document_init (TiffDocument *tiff_document)
482 tiff_document->n_pages = -1;