1 /* GdkPixbuf library - TIFF image loader
3 * Copyright (C) 1999 Mark Crichton
4 * Copyright (C) 1999 The Free Software Foundation
6 * Authors: Mark Crichton <crichton@gimp.org>
7 * Federico Mena-Quintero <federico@gimp.org>
8 * Jonathan Blandford <jrb@redhat.com>
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.
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.
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.
26 /* Following code (almost) blatantly ripped from Imlib */
36 #include "gdk-pixbuf-private.h"
37 #include "gdk-pixbuf-io.h"
41 #define O_RDWR _O_RDWR
45 typedef struct _TiffData TiffData;
48 ModulePreparedNotifyFunc prepare_func;
49 ModuleUpdatedNotifyFunc update_func;
60 gdk_pixbuf__tiff_image_load_real (FILE *f, TiffData *context, GError **error)
63 guchar *pixels = NULL;
65 gint w, h, x, y, num_pixs, fd;
66 uint32 *rast, *tmp_rast;
70 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
75 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
76 _("Failed to open TIFF image"));
80 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w);
81 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h);
83 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
88 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
89 _("Insufficient memory to open TIFF file"));
95 (* context->prepare_func) (pixbuf, NULL, context->user_data);
97 /* Yes, it needs to be _TIFFMalloc... */
98 rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
103 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
104 _("Insufficient memory to open TIFF file"));
109 if (TIFFReadRGBAImage (tiff, w, h, rast, 0)) {
110 pixels = gdk_pixbuf_get_pixels (pixbuf);
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
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);
137 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
138 gdk_pixbuf_unref (pixbuf);
149 gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
151 return gdk_pixbuf__tiff_image_load_real (f, NULL, error);
156 /* Progressive loader */
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.
164 gdk_pixbuf__tiff_image_begin_load (ModulePreparedNotifyFunc prepare_func,
165 ModuleUpdatedNotifyFunc update_func,
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,
184 context->file = fdopen (fd, "w");
185 if (context->file == NULL) {
186 g_free (context->tempname);
195 gdk_pixbuf__tiff_image_stop_load (gpointer data,
198 TiffData *context = (TiffData*) data;
199 gboolean retval = FALSE;
201 g_return_val_if_fail (data != NULL, TRUE);
203 fflush (context->file);
204 rewind (context->file);
205 if (context->all_okay) {
207 pixbuf = gdk_pixbuf__tiff_image_load_real (context->file, context, error);
212 fclose (context->file);
213 unlink (context->tempname);
214 g_free (context->tempname);
215 g_free ((TiffData *) context);
221 gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
222 guint size, GError **error)
224 TiffData *context = (TiffData *) data;
226 g_return_val_if_fail (data != NULL, FALSE);
228 if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
229 context->all_okay = FALSE;
232 g_file_error_from_errno (errno),
233 _("Failed to write to temporary file when loading TIFF image"));
241 gdk_pixbuf__tiff_fill_vtable (GdkPixbufModule *module)
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;