~ubuntu-branches/ubuntu/oneiric/evince/oneiric-updates

« back to all changes in this revision

Viewing changes to tiff/tiff-document.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2007-01-10 19:35:11 UTC
  • mto: (1.3.1 experimental) (52.1.1 hardy-proposed)
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: james.westby@ubuntu.com-20070110193511-yjrnndv8e8wv03yy
Tags: upstream-0.7.1
ImportĀ upstreamĀ versionĀ 0.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; c-indent-level: 8 -*- */
2
 
/*
3
 
 * Copyright (C) 2005, Jonathan Blandford <jrb@gnome.org>
4
 
 *
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)
8
 
 * any later version.
9
 
 *
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.
14
 
 *
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.
18
 
 */
19
 
 
20
 
/* FIXME: Should probably buffer calls to libtiff with TIFFSetWarningHandler
21
 
 */
22
 
 
23
 
#include <stdio.h>
24
 
#include <glib.h>
25
 
 
26
 
#include "tiffio.h"
27
 
#include "tiff2ps.h"
28
 
#include "tiff-document.h"
29
 
#include "ev-document-misc.h"
30
 
#include "ev-document-thumbnails.h"
31
 
#include "ev-file-exporter.h"
32
 
 
33
 
struct _TiffDocumentClass
34
 
{
35
 
  GObjectClass parent_class;
36
 
};
37
 
 
38
 
struct _TiffDocument
39
 
{
40
 
  GObject parent_instance;
41
 
 
42
 
  TIFF *tiff;
43
 
  gint n_pages;
44
 
  TIFF2PSContext *ps_export_ctx;
45
 
  
46
 
  gchar *uri;
47
 
};
48
 
 
49
 
typedef struct _TiffDocumentClass TiffDocumentClass;
50
 
 
51
 
static void tiff_document_document_iface_init (EvDocumentIface *iface);
52
 
static void tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface);
53
 
static void tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface);
54
 
 
55
 
G_DEFINE_TYPE_WITH_CODE (TiffDocument, tiff_document, G_TYPE_OBJECT,
56
 
                         { G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT,
57
 
                                                  tiff_document_document_iface_init);
58
 
                           G_IMPLEMENT_INTERFACE (EV_TYPE_DOCUMENT_THUMBNAILS,
59
 
                                                  tiff_document_document_thumbnails_iface_init);
60
 
                           G_IMPLEMENT_INTERFACE (EV_TYPE_FILE_EXPORTER,
61
 
                                                  tiff_document_document_file_exporter_iface_init);
62
 
                         });
63
 
 
64
 
static TIFFErrorHandler orig_error_handler = NULL;
65
 
static TIFFErrorHandler orig_warning_handler = NULL;
66
 
 
67
 
static void
68
 
push_handlers (void)
69
 
{
70
 
  orig_error_handler = TIFFSetErrorHandler (NULL);
71
 
  orig_warning_handler = TIFFSetWarningHandler (NULL);
72
 
}
73
 
 
74
 
static void
75
 
pop_handlers (void)
76
 
{
77
 
  TIFFSetErrorHandler (orig_error_handler);
78
 
  TIFFSetWarningHandler (orig_warning_handler);
79
 
}
80
 
 
81
 
static gboolean
82
 
tiff_document_load (EvDocument  *document,
83
 
                    const char  *uri,
84
 
                    GError     **error)
85
 
{
86
 
  TiffDocument *tiff_document = TIFF_DOCUMENT (document);
87
 
  gchar *filename;
88
 
  TIFF *tiff;
89
 
 
90
 
  push_handlers ();
91
 
  filename = g_filename_from_uri (uri, NULL, error);
92
 
  if (!filename)
93
 
    {
94
 
      pop_handlers ();
95
 
      return FALSE;
96
 
    }
97
 
 
98
 
  tiff = TIFFOpen (filename, "r");
99
 
  if (tiff)
100
 
    {
101
 
      guint32 w, h;
102
 
      /* FIXME: unused data? why bother here */
103
 
      TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &w);
104
 
      TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &h);
105
 
    }
106
 
  if (!tiff)
107
 
    {
108
 
      pop_handlers ();
109
 
      return FALSE;
110
 
    }
111
 
  tiff_document->tiff = tiff;
112
 
  g_free (tiff_document->uri);
113
 
  g_free (filename);
114
 
  tiff_document->uri = g_strdup (uri);
115
 
 
116
 
  pop_handlers ();
117
 
  return TRUE;
118
 
}
119
 
 
120
 
static gboolean
121
 
tiff_document_save (EvDocument  *document,
122
 
                      const char  *uri,
123
 
                      GError     **error)
124
 
{               
125
 
        TiffDocument *tiff_document = TIFF_DOCUMENT (document);
126
 
 
127
 
        return ev_xfer_uri_simple (tiff_document->uri, uri, error); 
128
 
}
129
 
 
130
 
static int
131
 
tiff_document_get_n_pages (EvDocument  *document)
132
 
{
133
 
  TiffDocument *tiff_document = TIFF_DOCUMENT (document);
134
 
 
135
 
  g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
136
 
  g_return_val_if_fail (tiff_document->tiff != NULL, 0);
137
 
 
138
 
  if (tiff_document->n_pages == -1)
139
 
    {
140
 
      push_handlers ();
141
 
      tiff_document->n_pages = 0;
142
 
      do
143
 
        {
144
 
          tiff_document->n_pages ++;
145
 
        }
146
 
      while (TIFFReadDirectory (tiff_document->tiff));
147
 
      pop_handlers ();
148
 
    }
149
 
 
150
 
  return tiff_document->n_pages;
151
 
}
152
 
 
153
 
static void
154
 
tiff_document_get_page_size (EvDocument   *document,
155
 
                             int           page,
156
 
                             double       *width,
157
 
                             double       *height)
158
 
{
159
 
  guint32 w, h;
160
 
  gfloat x_res, y_res;
161
 
  TiffDocument *tiff_document = TIFF_DOCUMENT (document);
162
 
 
163
 
  g_return_if_fail (TIFF_IS_DOCUMENT (document));
164
 
  g_return_if_fail (tiff_document->tiff != NULL);
165
 
 
166
 
  push_handlers ();
167
 
  if (TIFFSetDirectory (tiff_document->tiff, page) != 1)
168
 
    {
169
 
      pop_handlers ();
170
 
      return;
171
 
    }
172
 
 
173
 
  TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &w);
174
 
  TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &h);
175
 
  TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res);
176
 
  TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res);
177
 
  h = h * (x_res / y_res);
178
 
 
179
 
  *width = w;
180
 
  *height = h;
181
 
 
182
 
  pop_handlers ();
183
 
}
184
 
 
185
 
static GdkPixbuf *
186
 
tiff_document_render_pixbuf (EvDocument      *document,
187
 
                             EvRenderContext *rc)
188
 
{
189
 
  TiffDocument *tiff_document = TIFF_DOCUMENT (document);
190
 
  int width, height;
191
 
  float x_res, y_res;
192
 
  gint rowstride, bytes;
193
 
  guchar *pixels = NULL;
194
 
  GdkPixbuf *pixbuf;
195
 
  GdkPixbuf *scaled_pixbuf;
196
 
  GdkPixbuf *rotated_pixbuf;
197
 
 
198
 
  g_return_val_if_fail (TIFF_IS_DOCUMENT (document), 0);
199
 
  g_return_val_if_fail (tiff_document->tiff != NULL, 0);
200
 
 
201
 
  push_handlers ();
202
 
  if (TIFFSetDirectory (tiff_document->tiff, rc->page) != 1)
203
 
    {
204
 
      pop_handlers ();
205
 
      return NULL;
206
 
    }
207
 
 
208
 
  if (!TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGEWIDTH, &width))
209
 
    {
210
 
      pop_handlers ();
211
 
      return NULL;
212
 
    }
213
 
 
214
 
  if (! TIFFGetField (tiff_document->tiff, TIFFTAG_IMAGELENGTH, &height))
215
 
    {
216
 
      pop_handlers ();
217
 
      return NULL;
218
 
    }
219
 
 
220
 
  if (!TIFFGetField (tiff_document->tiff, TIFFTAG_XRESOLUTION, &x_res))
221
 
    {
222
 
      pop_handlers ();
223
 
      return NULL;
224
 
    }
225
 
 
226
 
  if (! TIFFGetField (tiff_document->tiff, TIFFTAG_YRESOLUTION, &y_res))
227
 
    {
228
 
      pop_handlers ();
229
 
      return NULL;
230
 
    }
231
 
 
232
 
  pop_handlers ();
233
 
 
234
 
  /* Sanity check the doc */
235
 
  if (width <= 0 || height <= 0)
236
 
    return NULL;                
237
 
        
238
 
  rowstride = width * 4;
239
 
  if (rowstride / 4 != width)
240
 
    /* overflow */
241
 
    return NULL;                
242
 
        
243
 
  bytes = height * rowstride;
244
 
  if (bytes / rowstride != height)
245
 
    /* overflow */
246
 
    return NULL;                
247
 
 
248
 
  pixels = g_try_malloc (bytes);
249
 
  if (!pixels)
250
 
    return NULL;
251
 
 
252
 
  pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8, 
253
 
                                     width, height, rowstride,
254
 
                                     (GdkPixbufDestroyNotify) g_free, NULL);
255
 
 
256
 
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
257
 
  TIFFReadRGBAImageOriented (tiff_document->tiff, width, height, (uint32 *)gdk_pixbuf_get_pixels (pixbuf), ORIENTATION_TOPLEFT, 1);
258
 
  pop_handlers ();
259
 
 
260
 
  scaled_pixbuf = gdk_pixbuf_scale_simple (pixbuf,
261
 
                                           width * rc->scale,
262
 
                                           height * rc->scale * (x_res/y_res),
263
 
                                           GDK_INTERP_BILINEAR);
264
 
  g_object_unref (pixbuf);
265
 
 
266
 
  rotated_pixbuf = gdk_pixbuf_rotate_simple (scaled_pixbuf, 360 - rc->rotation);
267
 
  g_object_unref (scaled_pixbuf);
268
 
 
269
 
  return rotated_pixbuf;
270
 
}
271
 
 
272
 
static void
273
 
tiff_document_finalize (GObject *object)
274
 
{
275
 
        TiffDocument *tiff_document = TIFF_DOCUMENT (object);
276
 
 
277
 
        if (tiff_document->tiff)
278
 
                TIFFClose (tiff_document->tiff);
279
 
        if (tiff_document->uri)
280
 
                g_free (tiff_document->uri);
281
 
 
282
 
        G_OBJECT_CLASS (tiff_document_parent_class)->finalize (object);
283
 
}
284
 
 
285
 
static void
286
 
tiff_document_class_init (TiffDocumentClass *klass)
287
 
{
288
 
        GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
289
 
 
290
 
        gobject_class->finalize = tiff_document_finalize;
291
 
}
292
 
 
293
 
static gboolean
294
 
tiff_document_can_get_text (EvDocument *document)
295
 
{
296
 
        return FALSE;
297
 
}
298
 
 
299
 
static EvDocumentInfo *
300
 
tiff_document_get_info (EvDocument *document)
301
 
{
302
 
        EvDocumentInfo *info;
303
 
 
304
 
        info = g_new0 (EvDocumentInfo, 1);
305
 
        info->fields_mask = 0;
306
 
 
307
 
        return info;
308
 
}
309
 
 
310
 
static void
311
 
tiff_document_document_iface_init (EvDocumentIface *iface)
312
 
{
313
 
        iface->load = tiff_document_load;
314
 
        iface->save = tiff_document_save;
315
 
        iface->can_get_text = tiff_document_can_get_text;
316
 
        iface->get_n_pages = tiff_document_get_n_pages;
317
 
        iface->get_page_size = tiff_document_get_page_size;
318
 
        iface->render_pixbuf = tiff_document_render_pixbuf;
319
 
        iface->get_info = tiff_document_get_info;
320
 
}
321
 
 
322
 
static GdkPixbuf *
323
 
tiff_document_thumbnails_get_thumbnail (EvDocumentThumbnails *document,
324
 
                                        gint                  page,
325
 
                                        gint                  rotation,
326
 
                                        gint                  size,
327
 
                                        gboolean              border)
328
 
{
329
 
  EvRenderContext *rc;
330
 
  GdkPixbuf *pixbuf;
331
 
  gdouble w, h;
332
 
 
333
 
  tiff_document_get_page_size (EV_DOCUMENT (document),
334
 
                               page,
335
 
                               &w, &h);
336
 
 
337
 
  rc = ev_render_context_new (rotation, page, size/w);
338
 
  pixbuf = tiff_document_render_pixbuf (EV_DOCUMENT (document), rc);
339
 
  g_object_unref (G_OBJECT (rc));
340
 
 
341
 
  if (border)
342
 
    {
343
 
      GdkPixbuf *tmp_pixbuf = pixbuf;
344
 
      pixbuf = ev_document_misc_get_thumbnail_frame (-1, -1, 0, tmp_pixbuf);
345
 
      g_object_unref (tmp_pixbuf);
346
 
    }
347
 
 
348
 
  return pixbuf;
349
 
}
350
 
 
351
 
static void
352
 
tiff_document_thumbnails_get_dimensions (EvDocumentThumbnails *document,
353
 
                                         gint                  page,
354
 
                                         gint                  suggested_width,
355
 
                                         gint                 *width,
356
 
                                         gint                 *height)
357
 
{
358
 
  gdouble page_ratio;
359
 
  gdouble w, h;
360
 
 
361
 
  tiff_document_get_page_size (EV_DOCUMENT (document),
362
 
                               page,
363
 
                               &w, &h);
364
 
  g_return_if_fail (w > 0);
365
 
  page_ratio = h/w;
366
 
  *width = suggested_width;
367
 
  *height = (gint) (suggested_width * page_ratio);
368
 
}
369
 
 
370
 
static void
371
 
tiff_document_document_thumbnails_iface_init (EvDocumentThumbnailsIface *iface)
372
 
{
373
 
  iface->get_thumbnail = tiff_document_thumbnails_get_thumbnail;
374
 
  iface->get_dimensions = tiff_document_thumbnails_get_dimensions;
375
 
}
376
 
 
377
 
/* postscript exporter implementation */
378
 
 
379
 
static gboolean
380
 
tiff_document_file_exporter_format_supported (EvFileExporter      *exporter,
381
 
                                              EvFileExporterFormat format)
382
 
{
383
 
        return (format == EV_FILE_FORMAT_PS);
384
 
}
385
 
 
386
 
static void
387
 
tiff_document_file_exporter_begin (EvFileExporter      *exporter,
388
 
                                   EvFileExporterFormat format,
389
 
                                   const char          *filename,
390
 
                                   int                  first_page,
391
 
                                   int                  last_page,
392
 
                                   double               width,
393
 
                                   double               height,
394
 
                                   gboolean             duplex)
395
 
{
396
 
        TiffDocument *document = TIFF_DOCUMENT (exporter);
397
 
 
398
 
        document->ps_export_ctx = tiff2ps_context_new(filename);
399
 
}
400
 
 
401
 
static void
402
 
tiff_document_file_exporter_do_page (EvFileExporter *exporter, EvRenderContext *rc)
403
 
{
404
 
        TiffDocument *document = TIFF_DOCUMENT (exporter);
405
 
 
406
 
        if (document->ps_export_ctx == NULL)
407
 
                return;
408
 
        if (TIFFSetDirectory (document->tiff, rc->page) != 1)
409
 
                return;
410
 
        tiff2ps_process_page (document->ps_export_ctx, document->tiff,
411
 
                              0, 0, 0, 0, 0);
412
 
}
413
 
 
414
 
static void
415
 
tiff_document_file_exporter_end (EvFileExporter *exporter)
416
 
{
417
 
        TiffDocument *document = TIFF_DOCUMENT (exporter);
418
 
 
419
 
        if (document->ps_export_ctx == NULL)
420
 
                return;
421
 
        tiff2ps_context_finalize(document->ps_export_ctx);
422
 
}
423
 
 
424
 
static void
425
 
tiff_document_document_file_exporter_iface_init (EvFileExporterIface *iface)
426
 
{
427
 
        iface->format_supported = tiff_document_file_exporter_format_supported;
428
 
        iface->begin = tiff_document_file_exporter_begin;
429
 
        iface->do_page = tiff_document_file_exporter_do_page;
430
 
        iface->end = tiff_document_file_exporter_end;
431
 
}
432
 
 
433
 
static void
434
 
tiff_document_init (TiffDocument *tiff_document)
435
 
{
436
 
  tiff_document->n_pages = -1;
437
 
}