1 /* GdkPixbuf library - JPEG 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 */
33 #include "gdk-pixbuf-private.h"
34 #include "gdk-pixbuf-io.h"
38 #define O_RDWR _O_RDWR
42 typedef struct _TiffData TiffData;
45 ModulePreparedNotifyFunc prepare_func;
46 ModuleUpdatedNotifyFunc update_func;
57 gdk_pixbuf__tiff_image_load_real (FILE *f, TiffData *context)
60 guchar *pixels = NULL;
62 gint w, h, x, y, num_pixs, fd;
63 uint32 *rast, *tmp_rast;
67 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
72 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w);
73 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h);
75 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, w, h);
78 (* context->prepare_func) (pixbuf, context->user_data);
80 /* Yes, it needs to be _TIFFMalloc... */
81 rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
88 if (TIFFReadRGBAImage (tiff, w, h, rast, 0)) {
89 pixels = gdk_pixbuf_get_pixels (pixbuf);
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
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);
118 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
119 gdk_pixbuf_unref (pixbuf);
130 gdk_pixbuf__tiff_image_load (FILE *f)
132 return gdk_pixbuf__tiff_image_load_real (f, NULL);
137 /* Progressive loader */
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.
145 gdk_pixbuf__tiff_image_begin_load (ModulePreparedNotifyFunc prepare_func,
146 ModuleUpdatedNotifyFunc update_func,
147 ModuleFrameDoneNotifyFunc frame_done_func,
148 ModuleAnimationDoneNotifyFunc anim_done_func,
153 gchar *tmp = g_get_tmp_dir ();
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;
162 tmp[strlen (tmp)] == G_DIR_SEPARATOR ? G_DIR_SEPARATOR_S : "",
163 "gdkpixbuf-tif-tmp.XXXXXX",
166 fd = mkstemp (context->tempname);
168 mktemp (context->tempname);
169 fd = open (context->tempname, O_RDWR);
176 context->file = fdopen (fd, "w");
177 if (context->file == NULL) {
186 gdk_pixbuf__tiff_image_stop_load (gpointer data)
188 TiffData *context = (TiffData*) data;
190 g_return_if_fail (data != NULL);
192 fflush (context->file);
193 rewind (context->file);
194 if (context->all_okay)
195 gdk_pixbuf__tiff_image_load_real (context->file, context);
197 fclose (context->file);
198 unlink (context->tempname);
199 g_free (context->tempname);
200 g_free ((TiffData *) context);
204 gdk_pixbuf__tiff_image_load_increment (gpointer data, guchar *buf, guint size)
206 TiffData *context = (TiffData *) data;
208 g_return_val_if_fail (data != NULL, FALSE);
210 if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
211 context->all_okay = FALSE;