]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-tiff.c
applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[~andy/gtk] / gdk-pixbuf / io-tiff.c
1 /* GdkPixbuf library - JPEG 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 Lesser 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  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser 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 #ifdef G_OS_WIN32
37 #include <fcntl.h>
38 #define O_RDWR _O_RDWR
39 #endif
40 \f
41
42 typedef struct _TiffData TiffData;
43 struct _TiffData
44 {
45         ModulePreparedNotifyFunc prepare_func;
46         ModuleUpdatedNotifyFunc update_func;
47         gpointer user_data;
48
49         gchar *tempname;
50         FILE *file;
51         gboolean all_okay;
52 };
53
54 \f
55
56 GdkPixbuf *
57 gdk_pixbuf__tiff_image_load_real (FILE *f, TiffData *context)
58 {
59         TIFF *tiff;
60         guchar *pixels = NULL;
61         guchar *tmppix;
62         gint w, h, x, y, num_pixs, fd;
63         uint32 *rast, *tmp_rast;
64         GdkPixbuf *pixbuf;
65         
66         fd = fileno (f);
67         tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
68
69         if (!tiff)
70                 return NULL;
71
72         TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w);
73         TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h);
74         num_pixs = w * h;
75         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
76
77         if (context)
78                 (* context->prepare_func) (pixbuf, context->user_data);
79
80         /* Yes, it needs to be _TIFFMalloc... */
81         rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
82
83         if (!rast) {
84                 TIFFClose (tiff);
85                 return NULL;
86         }
87
88         if (TIFFReadRGBAImage (tiff, w, h, rast, 0)) {
89                 pixels = gdk_pixbuf_get_pixels (pixbuf);
90                 if (!pixels) {
91                         _TIFFfree (rast);
92                         TIFFClose (tiff);
93                         return NULL;
94                 }
95                 tmppix = pixels;
96
97                 for (y = 0; y < h; y++) {
98                         /* Unexplainable...are tiffs backwards? */
99                         /* Also looking at the GIMP plugin, this
100                          * whole reading thing can be a bit more
101                          * robust.
102                          */
103                         tmp_rast = rast + ((h - y - 1) * w);
104                         for (x = 0; x < w; x++) {
105                                 tmppix[0] = TIFFGetR (*tmp_rast);
106                                 tmppix[1] = TIFFGetG (*tmp_rast);
107                                 tmppix[2] = TIFFGetB (*tmp_rast);
108                                 tmppix[3] = TIFFGetA (*tmp_rast);
109                                 tmp_rast++;
110                                 tmppix += 4;
111                         }
112                 }
113         }
114         _TIFFfree (rast);
115         TIFFClose (tiff);
116
117         if (context) {
118                 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
119                 gdk_pixbuf_unref (pixbuf);
120         }
121
122         return pixbuf;
123 }
124
125 \f
126
127 /* Static loader */
128
129 GdkPixbuf *
130 gdk_pixbuf__tiff_image_load (FILE *f)
131 {
132         return gdk_pixbuf__tiff_image_load_real (f, NULL);
133 }
134
135 \f
136
137 /* Progressive loader */
138 /*
139  * Tiff loading progressively cannot be done.  We write it to a file, then load
140  * the file when it's done.  It's not pretty.
141  */
142
143
144 gpointer
145 gdk_pixbuf__tiff_image_begin_load (ModulePreparedNotifyFunc prepare_func,
146                                    ModuleUpdatedNotifyFunc update_func,
147                                    ModuleFrameDoneNotifyFunc frame_done_func,
148                                    ModuleAnimationDoneNotifyFunc anim_done_func,
149                                    gpointer user_data)
150 {
151         TiffData *context;
152         gint fd;
153         gchar *tmp = g_get_tmp_dir ();
154
155         context = g_new (TiffData, 1);
156         context->prepare_func = prepare_func;
157         context->update_func = update_func;
158         context->user_data = user_data;
159         context->all_okay = TRUE;
160         context->tempname =
161           g_strconcat (tmp,
162                        tmp[strlen (tmp)] == G_DIR_SEPARATOR ? G_DIR_SEPARATOR_S : "",
163                        "gdkpixbuf-tif-tmp.XXXXXX",
164                        NULL);
165 #ifdef HAVE_MKSTEMP
166         fd = mkstemp (context->tempname);
167 #else
168         mktemp (context->tempname);
169         fd = open (context->tempname, O_RDWR);
170 #endif
171         if (fd < 0) {
172                 g_free (context);
173                 return NULL;
174         }
175
176         context->file = fdopen (fd, "w");
177         if (context->file == NULL) {
178                 g_free (context);
179                 return NULL;
180         }
181
182         return context;
183 }
184
185 void
186 gdk_pixbuf__tiff_image_stop_load (gpointer data)
187 {
188         TiffData *context = (TiffData*) data;
189
190         g_return_if_fail (data != NULL);
191
192         fflush (context->file);
193         rewind (context->file);
194         if (context->all_okay)
195                 gdk_pixbuf__tiff_image_load_real (context->file, context);
196
197         fclose (context->file);
198         unlink (context->tempname);
199         g_free (context->tempname);
200         g_free ((TiffData *) context);
201 }
202
203 gboolean
204 gdk_pixbuf__tiff_image_load_increment (gpointer data, guchar *buf, guint size)
205 {
206         TiffData *context = (TiffData *) data;
207
208         g_return_val_if_fail (data != NULL, FALSE);
209
210         if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
211                 context->all_okay = FALSE;
212                 return FALSE;
213         }
214
215         return TRUE;
216 }
217