]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-tiff.c
fix some shell typos
[~andy/gtk] / gdk-pixbuf / io-tiff.c
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 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 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34 #include <tiffio.h>
35 #include <errno.h>
36 #include "gdk-pixbuf-private.h"
37 #include "gdk-pixbuf-io.h"
38
39 #ifdef G_OS_WIN32
40 #include <fcntl.h>
41 #define O_RDWR _O_RDWR
42 #endif
43 \f
44
45 typedef struct _TiffData TiffData;
46 struct _TiffData
47 {
48         ModulePreparedNotifyFunc prepare_func;
49         ModuleUpdatedNotifyFunc update_func;
50         gpointer user_data;
51
52         gchar *tempname;
53         FILE *file;
54         gboolean all_okay;
55 };
56
57 \f
58
59 static GdkPixbuf *
60 gdk_pixbuf__tiff_image_load_real (FILE *f, TiffData *context, GError **error)
61 {
62         TIFF *tiff;
63         guchar *pixels = NULL;
64         guchar *tmppix;
65         gint w, h, x, y, num_pixs, fd;
66         uint32 *rast, *tmp_rast;
67         GdkPixbuf *pixbuf;
68         
69         fd = fileno (f);
70         tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
71
72         if (!tiff) {
73                 g_set_error (error,
74                              GDK_PIXBUF_ERROR,
75                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
76                              _("Failed to open TIFF image"));
77                 return NULL;
78         }
79                 
80         TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w);
81         TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h);
82         num_pixs = w * h;
83         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
84
85         if (!pixbuf) {
86                 g_set_error (error,
87                              GDK_PIXBUF_ERROR,
88                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
89                              _("Insufficient memory to open TIFF file"));
90                 TIFFClose (tiff);
91                 return NULL;
92         }
93         
94         if (context)
95                 (* context->prepare_func) (pixbuf, NULL, context->user_data);
96
97         /* Yes, it needs to be _TIFFMalloc... */
98         rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
99
100         if (!rast) {
101                 g_set_error (error,
102                              GDK_PIXBUF_ERROR,
103                              GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
104                              _("Insufficient memory to open TIFF file"));
105                 TIFFClose (tiff);
106                 return NULL;
107         }
108
109         if (TIFFReadRGBAImage (tiff, w, h, rast, 0)) {
110                 pixels = gdk_pixbuf_get_pixels (pixbuf);
111
112                 g_assert (pixels);
113                 
114                 tmppix = pixels;
115
116                 for (y = 0; y < h; y++) {
117                         /* Unexplainable...are tiffs backwards? */
118                         /* Also looking at the GIMP plugin, this
119                          * whole reading thing can be a bit more
120                          * robust.
121                          */
122                         tmp_rast = rast + ((h - y - 1) * w);
123                         for (x = 0; x < w; x++) {
124                                 tmppix[0] = TIFFGetR (*tmp_rast);
125                                 tmppix[1] = TIFFGetG (*tmp_rast);
126                                 tmppix[2] = TIFFGetB (*tmp_rast);
127                                 tmppix[3] = TIFFGetA (*tmp_rast);
128                                 tmp_rast++;
129                                 tmppix += 4;
130                         }
131                 }
132         }
133         _TIFFfree (rast);
134         TIFFClose (tiff);
135
136         if (context) {
137                 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
138                 gdk_pixbuf_unref (pixbuf);
139         }
140
141         return pixbuf;
142 }
143
144 \f
145
146 /* Static loader */
147
148 static GdkPixbuf *
149 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
150 {
151         return gdk_pixbuf__tiff_image_load_real (f, NULL, error);
152 }
153
154 \f
155
156 /* Progressive loader */
157 /*
158  * Tiff loading progressively cannot be done.  We write it to a file, then load
159  * the file when it's done.  It's not pretty.
160  */
161
162
163 static gpointer
164 gdk_pixbuf__tiff_image_begin_load (ModulePreparedNotifyFunc prepare_func,
165                                    ModuleUpdatedNotifyFunc update_func,
166                                    gpointer user_data,
167                                    GError **error)
168 {
169         TiffData *context;
170         gint fd;
171
172         context = g_new (TiffData, 1);
173         context->prepare_func = prepare_func;
174         context->update_func = update_func;
175         context->user_data = user_data;
176         context->all_okay = TRUE;
177         fd = g_file_open_tmp ("gdkpixbuf-tif-tmp.XXXXXX", &context->tempname,
178                               NULL);
179         if (fd < 0) {
180                 g_free (context);
181                 return NULL;
182         }
183
184         context->file = fdopen (fd, "w");
185         if (context->file == NULL) {
186                 g_free (context->tempname);
187                 g_free (context);
188                 return NULL;
189         }
190
191         return context;
192 }
193
194 static gboolean
195 gdk_pixbuf__tiff_image_stop_load (gpointer data,
196                                   GError **error)
197 {
198         TiffData *context = (TiffData*) data;
199         gboolean retval = FALSE;
200         
201         g_return_val_if_fail (data != NULL, TRUE);
202
203         fflush (context->file);
204         rewind (context->file);
205         if (context->all_okay) {
206                 GdkPixbuf *pixbuf;
207                 pixbuf = gdk_pixbuf__tiff_image_load_real (context->file, context, error);
208                 if (pixbuf != NULL)
209                         retval = TRUE;
210         }
211
212         fclose (context->file);
213         unlink (context->tempname);
214         g_free (context->tempname);
215         g_free ((TiffData *) context);
216
217         return TRUE;
218 }
219
220 static gboolean
221 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
222                                        guint size, GError **error)
223 {
224         TiffData *context = (TiffData *) data;
225
226         g_return_val_if_fail (data != NULL, FALSE);
227
228         if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
229                 context->all_okay = FALSE;
230                 g_set_error (error,
231                              G_FILE_ERROR,
232                              g_file_error_from_errno (errno),
233                              _("Failed to write to temporary file when loading TIFF image"));
234                 return FALSE;
235         }
236
237         return TRUE;
238 }
239
240 void
241 gdk_pixbuf__tiff_fill_vtable (GdkPixbufModule *module)
242 {
243   module->load = gdk_pixbuf__tiff_image_load;
244   module->begin_load = gdk_pixbuf__tiff_image_begin_load;
245   module->stop_load = gdk_pixbuf__tiff_image_stop_load;
246   module->load_increment = gdk_pixbuf__tiff_image_load_increment;
247 }
248