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 EV_BACKEND_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
63 tiff_document_document_thumbnails_iface_init);
64 EV_BACKEND_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->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;
258 if (rowstride / 4 != width)
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 uint32 pixel = *(uint32 *)p;
291 int r = TIFFGetR(pixel);
292 int g = TIFFGetG(pixel);
293 int b = TIFFGetB(pixel);
294 int a = TIFFGetA(pixel);
302 rotated_surface = ev_document_misc_surface_rotate_and_scale (surface,
303 (width * rc->scale) + 0.5,
304 (height * rc->scale * (x_res / y_res)) + 0.5,
306 cairo_surface_destroy (surface);
308 return rotated_surface;
312 tiff_document_render_pixbuf (EvDocument *document,
315 TiffDocument *tiff_document = TIFF_DOCUMENT (document);
318 gint rowstride, bytes;
319 guchar *pixels = NULL;
321 GdkPixbuf *scaled_pixbuf;
322 GdkPixbuf *rotated_pixbuf;
325 if (TIFFSetDirectory (tiff_document->tiff, rc->page->index) != 1) {
330 if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width)) {
335 if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height)) {
340 tiff_document_get_resolution (tiff_document, &x_res, &y_res);
344 /* Sanity check the doc */
345 if (width <= 0 || height <= 0)
348 rowstride = width * 4;
349 if (rowstride / 4 != width)
353 bytes = height * rowstride;
354 if (bytes / rowstride != height)
358 pixels = g_try_malloc (bytes);
362 pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
363 width, height, rowstride,
364 (GdkPixbufDestroyNotify) g_free, NULL);
365 TIFFReadRGBAImageOriented (tiff_document->tiff,
368 ORIENTATION_TOPLEFT, 1);
371 scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
373 height * rc->scale * (x_res / y_res),
374 GDK_INTERP_BILINEAR);
375 g_object_unref (pixbuf);
377 rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
378 g_object_unref (scaled_pixbuf);
380 return rotated_pixbuf;
384 tiff_document_finalize (GObject *object)
386 TiffDocument *tiff_document = TIFF_DOCUMENT (object);
388 if (tiff_document->tiff)
389 TIFFClose (tiff_document->tiff);
390 if (tiff_document->uri)
391 g_free (tiff_document->uri);
393 G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
397 tiff_document_class_init (TiffDocumentClass *klass)
399 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
401 gobject_class->finalize = tiff_document_finalize;
404 static EvDocumentInfo *
405 tiff_document_get_info (EvDocument *document)
407 EvDocumentInfo *info;
409 info = g_new0 (EvDocumentInfo, 1);
410 info->fields_mask = 0;
416 tiff_document_document_iface_init (EvDocumentIface *iface)
418 iface->load = tiff_document_load;
419 iface->save = tiff_document_save;
420 iface->get_n_pages = tiff_document_get_n_pages;
421 iface->get_page_size = tiff_document_get_page_size;
422 iface->render = tiff_document_render;
423 iface->get_info = tiff_document_get_info;
427 tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
433 pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
436 GdkPixbuf *tmp_pixbuf = pixbuf;
438 pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, tmp_pixbuf);
439 g_object_unref (tmp_pixbuf);
446 tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
451 gdouble page_width, page_height;
453 tiff_document_get_page_size (EV_DOCUMENT (document),
455 &page_width, &page_height);
457 if (rc->rotation == 90 || rc->rotation == 270) {
458 *width = (gint) (page_height * rc->scale);
459 *height = (gint) (page_width * rc->scale);
461 *width = (gint) (page_width * rc->scale);
462 *height = (gint) (page_height * rc->scale);
467 tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
469 iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
470 iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
473 /* postscript exporter implementation */
475 tiff_document_file_exporter_begin (EvFileExporter *exporter,
476 EvFileExporterContext *fc)
478 TiffDocument *document = TIFF_DOCUMENT (exporter);
480 document->ps_export_ctx = tiff2ps_context_new(fc->filename);
484 tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
486 TiffDocument *document = TIFF_DOCUMENT (exporter);
488 if (document->ps_export_ctx == NULL)
490 if (TIFFSetDirectory (document->tiff, rc->page->index) != 1)
492 tiff2ps_process_page (document->ps_export_ctx, document->tiff,
497 tiff_document_file_exporter_end (EvFileExporter *exporter)
499 TiffDocument *document = TIFF_DOCUMENT (exporter);
501 if (document->ps_export_ctx == NULL)
503 tiff2ps_context_finalize(document->ps_export_ctx);
506 static EvFileExporterCapabilities
507 tiff_document_file_exporter_get_capabilities (EvFileExporter *exporter)
509 return EV_FILE_EXPORTER_CAN_PAGE_SET |
510 EV_FILE_EXPORTER_CAN_COPIES |
511 EV_FILE_EXPORTER_CAN_COLLATE |
512 EV_FILE_EXPORTER_CAN_REVERSE |
513 EV_FILE_EXPORTER_CAN_GENERATE_PS;
517 tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
519 iface->begin = tiff_document_file_exporter_begin;
520 iface->do_page = tiff_document_file_exporter_do_page;
521 iface->end = tiff_document_file_exporter_end;
522 iface->get_capabilities = tiff_document_file_exporter_get_capabilities;
526 tiff_document_init (TiffDocument *tiff_document)
528 tiff_document->n_pages = -1;