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 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.
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.
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.
26 /* Following code (almost) blatantly ripped from Imlib */
33 #include "gdk-pixbuf.h"
34 #include "gdk-pixbuf-io.h"
38 typedef struct _TiffData TiffData;
41 ModulePreparedNotifyFunc prepare_func;
42 ModuleUpdatedNotifyFunc update_func;
53 image_load_real (FILE *f, TiffData *context)
56 guchar *pixels = NULL;
58 gint w, h, x, y, num_pixs, fd;
59 uint32 *rast, *tmp_rast;
63 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
68 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w);
69 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h);
71 pixbuf = gdk_pixbuf_new (ART_PIX_RGB, TRUE, 8, w, h);
74 (* context->prepare_func) (pixbuf, context->user_data);
76 /* Yes, it needs to be _TIFFMalloc... */
77 rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
84 if (TIFFReadRGBAImage (tiff, w, h, rast, 0)) {
85 pixels = gdk_pixbuf_get_pixels (pixbuf);
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
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);
114 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
115 gdk_pixbuf_unref (pixbuf);
128 return image_load_real (f, NULL);
133 /* Progressive loader */
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.
141 image_begin_load (ModulePreparedNotifyFunc prepare_func,
142 ModuleUpdatedNotifyFunc update_func,
143 ModuleFrameDoneNotifyFunc frame_done_func,
144 ModuleAnimationDoneNotifyFunc anim_done_func,
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);
162 context->file = fdopen (fd, "w");
163 if (context->file == NULL) {
172 image_stop_load (gpointer data)
174 TiffData *context = (TiffData*) data;
176 g_return_if_fail (data != NULL);
178 fflush (context->file);
179 rewind (context->file);
180 if (context->all_okay)
181 image_load_real (context->file, context);
183 fclose (context->file);
184 unlink (context->tempname);
185 g_free ((TiffData *) context);
189 image_load_increment (gpointer data, guchar *buf, guint size)
191 TiffData *context = (TiffData *) data;
193 g_return_val_if_fail (data != NULL, FALSE);
195 if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
196 context->all_okay = FALSE;