~ubuntu-branches/ubuntu/vivid/gdk-pixbuf/vivid

« back to all changes in this revision

Viewing changes to gdk-pixbuf/io-tiff.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2004-10-06 22:10:04 UTC
  • Revision ID: james.westby@ubuntu.com-20041006221004-rma9deknj8qctu67
Tags: upstream-0.22.0
ImportĀ upstreamĀ versionĀ 0.22.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GdkPixbuf library - TIFF image loader
 
2
 *
 
3
 * Copyright (C) 1999 Mark Crichton
 
4
 * Copyright (C) 1999 The Free Software Foundation
 
5
 *
 
6
 * Authors: Mark Crichton <crichton@gimp.org>
 
7
 *          Federico Mena-Quintero <federico@gimp.org>
 
8
 *          Jonathan Blandford <jrb@redhat.com>
 
9
 *
 
10
 * This library is free software; you can redistribute it and/or
 
11
 * modify it under the terms of the GNU Library General Public
 
12
 * License as published by the Free Software Foundation; either
 
13
 * version 2 of the License, or (at your option) any later version.
 
14
 *
 
15
 * This library is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 * Library General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU Library General Public
 
21
 * License along with this library; if not, write to the
 
22
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
23
 * Boston, MA 02111-1307, USA.
 
24
 */
 
25
 
 
26
/* Following code (almost) blatantly ripped from Imlib */
 
27
 
 
28
#include <config.h>
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include <unistd.h>
 
32
#include <tiffio.h>
 
33
#include "gdk-pixbuf-private.h"
 
34
#include "gdk-pixbuf-io.h"
 
35
 
 
36
 
 
37
 
 
38
typedef struct _TiffData TiffData;
 
39
struct _TiffData
 
40
{
 
41
        ModulePreparedNotifyFunc prepare_func;
 
42
        ModuleUpdatedNotifyFunc update_func;
 
43
        gpointer user_data;
 
44
 
 
45
        gchar *tempname;
 
46
        FILE *file;
 
47
        gboolean all_okay;
 
48
};
 
49
 
 
50
 
 
51
 
 
52
GdkPixbuf *
 
53
gdk_pixbuf__tiff_image_load_real (FILE *f, TiffData *context)
 
54
{
 
55
        TIFF *tiff;
 
56
        guchar *pixels = NULL;
 
57
        guchar *tmppix;
 
58
        gint w, h, x, y, num_pixs, fd;
 
59
        uint32 *rast, *tmp_rast;
 
60
        GdkPixbuf *pixbuf;
 
61
        
 
62
        fd = fileno (f);
 
63
        tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
 
64
 
 
65
        if (!tiff)
 
66
                return NULL;
 
67
 
 
68
        TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w);
 
69
        TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h);
 
70
        num_pixs = w * h;
 
71
        pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
 
72
 
 
73
        if (context)
 
74
                (* context->prepare_func) (pixbuf, context->user_data);
 
75
 
 
76
        /* Yes, it needs to be _TIFFMalloc... */
 
77
        rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
 
78
 
 
79
        if (!rast) {
 
80
                TIFFClose (tiff);
 
81
                return NULL;
 
82
        }
 
83
 
 
84
        if (TIFFReadRGBAImage (tiff, w, h, rast, 0)) {
 
85
                pixels = gdk_pixbuf_get_pixels (pixbuf);
 
86
                if (!pixels) {
 
87
                        _TIFFfree (rast);
 
88
                        TIFFClose (tiff);
 
89
                        return NULL;
 
90
                }
 
91
                tmppix = pixels;
 
92
 
 
93
                for (y = 0; y < h; y++) {
 
94
                        /* Unexplainable...are tiffs backwards? */
 
95
                        /* Also looking at the GIMP plugin, this
 
96
                         * whole reading thing can be a bit more
 
97
                         * robust.
 
98
                         */
 
99
                        tmp_rast = rast + ((h - y - 1) * w);
 
100
                        for (x = 0; x < w; x++) {
 
101
                                tmppix[0] = TIFFGetR (*tmp_rast);
 
102
                                tmppix[1] = TIFFGetG (*tmp_rast);
 
103
                                tmppix[2] = TIFFGetB (*tmp_rast);
 
104
                                tmppix[3] = TIFFGetA (*tmp_rast);
 
105
                                tmp_rast++;
 
106
                                tmppix += 4;
 
107
                        }
 
108
                }
 
109
        }
 
110
        _TIFFfree (rast);
 
111
        TIFFClose (tiff);
 
112
 
 
113
        if (context) {
 
114
                (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
 
115
                gdk_pixbuf_unref (pixbuf);
 
116
        }
 
117
 
 
118
        return pixbuf;
 
119
}
 
120
 
 
121
 
 
122
 
 
123
/* Static loader */
 
124
 
 
125
GdkPixbuf *
 
126
gdk_pixbuf__tiff_image_load (FILE *f)
 
127
{
 
128
        return gdk_pixbuf__tiff_image_load_real (f, NULL);
 
129
}
 
130
 
 
131
 
 
132
 
 
133
/* Progressive loader */
 
134
/*
 
135
 * Tiff loading progressively cannot be done.  We write it to a file, then load
 
136
 * the file when it's done.  It's not pretty.
 
137
 */
 
138
 
 
139
 
 
140
gpointer
 
141
gdk_pixbuf__tiff_image_begin_load (ModulePreparedNotifyFunc prepare_func,
 
142
                                   ModuleUpdatedNotifyFunc update_func,
 
143
                                   ModuleFrameDoneNotifyFunc frame_done_func,
 
144
                                   ModuleAnimationDoneNotifyFunc anim_done_func,
 
145
                                   gpointer user_data)
 
146
{
 
147
        TiffData *context;
 
148
        gint fd;
 
149
 
 
150
        context = g_new (TiffData, 1);
 
151
        context->prepare_func = prepare_func;
 
152
        context->update_func = update_func;
 
153
        context->user_data = user_data;
 
154
        context->all_okay = TRUE;
 
155
        context->tempname = g_strdup ("/tmp/gdkpixbuf-tif-tmp.XXXXXX");
 
156
        fd = mkstemp (context->tempname);
 
157
        if (fd < 0) {
 
158
                g_free (context->tempname);
 
159
                g_free (context);
 
160
                return NULL;
 
161
        }
 
162
 
 
163
        context->file = fdopen (fd, "w");
 
164
        if (context->file == NULL) {
 
165
                g_free (context->tempname);
 
166
                g_free (context);
 
167
                return NULL;
 
168
        }
 
169
 
 
170
        return context;
 
171
}
 
172
 
 
173
void
 
174
gdk_pixbuf__tiff_image_stop_load (gpointer data)
 
175
{
 
176
        TiffData *context = (TiffData*) data;
 
177
 
 
178
        g_return_if_fail (data != NULL);
 
179
 
 
180
        fflush (context->file);
 
181
        rewind (context->file);
 
182
        if (context->all_okay)
 
183
                gdk_pixbuf__tiff_image_load_real (context->file, context);
 
184
 
 
185
        fclose (context->file);
 
186
        unlink (context->tempname);
 
187
        g_free (context->tempname);
 
188
        g_free ((TiffData *) context);
 
189
}
 
190
 
 
191
gboolean
 
192
gdk_pixbuf__tiff_image_load_increment (gpointer data, guchar *buf, guint size)
 
193
{
 
194
        TiffData *context = (TiffData *) data;
 
195
 
 
196
        g_return_val_if_fail (data != NULL, FALSE);
 
197
 
 
198
        if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
 
199
                context->all_okay = FALSE;
 
200
                return FALSE;
 
201
        }
 
202
 
 
203
        return TRUE;
 
204
}
 
205
 
 
206
 
 
207
 
 
208
 
 
209
 
 
210