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);
97 wchar_t *wfilename = g_utf8_to_utf16 (filename, -1, NULL, NULL, error);
98 if (wfilename == NULL) {
102 tiff = TIFFOpenW (wfilename, "r");
107 tiff = TIFFOpen (filename, "r");
112 /* FIXME: unused data? why bother here */
113 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
114 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
120 g_set_error_literal (error,
122 EV_DOCUMENT_ERROR_INVALID,
123 _("Invalid document"));
129 tiff_document->tiff = tiff;
130 g_free (tiff_document->uri);
132 tiff_document->uri = g_strdup (uri);
139 tiff_document_save (EvDocument *document,
143 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
145 return ev_xfer_uri_simple (tiff_document->uri, uri, error);
149 tiff_document_get_n_pages (EvDocument *document)
151 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
153 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
154 g_return_val_if_fail (tiff_document->tiff != NULL, 0);
156 if (tiff_document->n_pages == -1) {
158 tiff_document->n_pages = 0;
161 tiff_document->n_pages ++;
163 while (TIFFReadDirectory (tiff_document->tiff));
167 return tiff_document->n_pages;
171 tiff_document_get_resolution (TiffDocument *tiff_document,
175 gfloat x = 72.0, y = 72.0;
178 if (TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x) &&
179 TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y)) {
180 if (TIFFGetFieldDefaulted (tiff_document->tiff, TIFFTAG_RESOLUTIONUNIT, &unit)) {
181 if (unit == RESUNIT_CENTIMETER) {
193 tiff_document_get_page_size (EvDocument *document,
200 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
202 g_return_if_fail (TIFF_IS_DOCUMENT (document));
203 g_return_if_fail (tiff_document->tiff != NULL);
206 if (TIFFSetDirectory (tiff_document->tiff, page->index) != 1) {
211 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
212 TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
213 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
214 h = h * (x_res / y_res);
222 static cairo_surface_t *
223 tiff_document_render (EvDocument *document,
226 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
229 gint rowstride, bytes;
230 guchar *pixels = NULL;
233 cairo_surface_t *surface;
234 cairo_surface_t *rotated_surface;
235 static const cairo_user_data_key_t key;
237 g_return_val_if_fail (TIFF_IS_DOCUMENT (document), NULL);
238 g_return_val_if_fail (tiff_document->tiff != NULL, NULL);
241 if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
246 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
251 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
256 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_ORIENTATION, &orientation)) {
257 orientation = ORIENTATION_TOPLEFT;
260 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
264 /* Sanity check the doc */
265 if (width <= 0 || height <= 0)
268 #ifdef HAVE_CAIRO_FORMAT_STRIDE_FOR_WIDTH
269 rowstride = cairo_format_stride_for_width (CAIRO_FORMAT_RGB24, width);
271 rowstride = width * 4;
273 if (rowstride / 4 != width)
274 /* overflow, or cairo was changed in an unsupported way */
277 bytes = height * rowstride;
278 if (bytes / rowstride != height)
282 pixels = g_try_malloc (bytes);
286 surface = cairo_image_surface_create_for_data (pixels,
290 cairo_surface_set_user_data (surface, &key,
291 pixels, (cairo_destroy_func_t)g_free);
293 TIFFReadRGBAImageOriented (tiff_document->tiff,
299 /* Convert the format returned by libtiff to
303 while (p < pixels + bytes) {
304 guint32 *pixel = (guint32*)p;
305 guint8 r = TIFFGetR(*pixel);
306 guint8 g = TIFFGetG(*pixel);
307 guint8 b = TIFFGetB(*pixel);
308 guint8 a = TIFFGetA(*pixel);
310 *pixel = (a << 24) | (r << 16) | (g << 8) | b;
315 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
316 (width * rc->scale) + 0.5,
317 (height * rc->scale * (x_res / y_res)) + 0.5,
319 cairo_surface_destroy (surface);
321 return rotated_surface;
325 tiff_document_get_thumbnail (EvDocument *document,
328 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
331 gint rowstride, bytes;
332 guchar *pixels = NULL;
334 GdkPixbuf *scaled_pixbuf;
335 GdkPixbuf *rotated_pixbuf;
338 if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
343 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
348 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
353 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
357 /* Sanity check the doc */
358 if (width <= 0 || height <= 0)
361 rowstride = width * 4;
362 if (rowstride / 4 != width)
366 bytes = height * rowstride;
367 if (bytes / rowstride != height)
371 pixels = g_try_malloc (bytes);
375 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
376 width, height, rowstride,
377 (GdkPixbufDestroyNotify) g_free, NULL);
378 TIFFReadRGBAImageOriented (tiff_document->tiff,
381 ORIENTATION_TOPLEFT, 0);
384 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
386 height * rc->scale * (x_res / y_res),
387 GDK_INTERP_BILINEAR);
388 g_object_unref (pixbuf);
390 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
391 g_object_unref (scaled_pixbuf);
393 return rotated_pixbuf;
397 tiff_document_get_page_label (EvDocument *document,
400 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
403 if (TIFFGetField (tiff_document->tiff, TIFFTAG_PAGENAME, &label) &&
404 g_utf8_validate (label, -1, NULL)) {
405 return g_strdup (label);
412 tiff_document_finalize (GObject *object)
414 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
416 if (tiff_document->tiff)
417 TIFFClose (tiff_document->tiff);
418 if (tiff_document->uri)
419 g_free (tiff_document->uri);
421 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
425 tiff_document_class_init (TiffDocumentClass *klass)
427 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
428 EvDocumentClass *ev_document_class = EV_DOCUMENT_CLASS (klass);
430 gobject_class->finalize = tiff_document_finalize;
432 ev_document_class->load = tiff_document_load;
433 ev_document_class->save = tiff_document_save;
434 ev_document_class->get_n_pages = tiff_document_get_n_pages;
435 ev_document_class->get_page_size = tiff_document_get_page_size;
436 ev_document_class->render = tiff_document_render;
437 ev_document_class->get_thumbnail = tiff_document_get_thumbnail;
438 ev_document_class->get_page_label = tiff_document_get_page_label;
441 /* postscript exporter implementation */
443 tiff_document_file_exporter_begin (EvFileExporter *exporter,
444 EvFileExporterContext *fc)
446 TiffDocument *document = TIFF_DOCUMENT (exporter);
448 document->ps_export_ctx = tiff2ps_context_new(fc->filename);
452 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
454 TiffDocument *document = TIFF_DOCUMENT (exporter);
456 if (document->ps_export_ctx == NULL)
458 if (TIFFSetDirectory (document->tiff, rc->page->index) != 1)
460 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
465 tiff_document_file_exporter_end (EvFileExporter *exporter)
467 TiffDocument *document = TIFF_DOCUMENT (exporter);
469 if (document->ps_export_ctx == NULL)
471 tiff2ps_context_finalize(document->ps_export_ctx);
474 static EvFileExporterCapabilities
475 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
477 return EV_FILE_EXPORTER_CAN_PAGE_SET |
478 EV_FILE_EXPORTER_CAN_COPIES |
479 EV_FILE_EXPORTER_CAN_COLLATE |
480 EV_FILE_EXPORTER_CAN_REVERSE |
481 EV_FILE_EXPORTER_CAN_GENERATE_PS;
485 tiff_document_document_file_exporter_iface_init (EvFileExporterInterface *iface)
487 iface->begin = tiff_document_file_exporter_begin;
488 iface->do_page = tiff_document_file_exporter_do_page;
489 iface->end = tiff_document_file_exporter_end;
490 iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
494 tiff_document_init (TiffDocument *tiff_document)
496 tiff_document->n_pages = -1;