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>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
25 /* Following code (almost) blatantly ripped from Imlib */
31 #include "gdk-pixbuf.h"
32 #include "gdk-pixbuf-io.h"
37 typedef struct _TiffData TiffData;
40 ModulePreparedNotifyFunc prepare_func;
41 ModuleUpdatedNotifyFunc update_func;
52 image_load_real (FILE *f, TiffData *context)
55 guchar *pixels = NULL;
57 gint w, h, x, y, num_pixs, fd;
58 uint32 *rast, *tmp_rast;
62 tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
67 TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w);
68 TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h);
70 pixbuf = gdk_pixbuf_new (ART_PIX_RGB, TRUE, 8, w, h);
73 (* context->prepare_func) (pixbuf, context->user_data);
75 /* Yes, it needs to be _TIFFMalloc... */
76 rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
83 if (TIFFReadRGBAImage (tiff, w, h, rast, 0)) {
84 pixels = gdk_pixbuf_get_pixels (pixbuf);
92 for (y = 0; y < h; y++) {
93 /* Unexplainable...are tiffs backwards? */
94 /* Also looking at the GIMP plugin, this
95 * whole reading thing can be a bit more
98 tmp_rast = rast + ((h - y - 1) * w);
99 for (x = 0; x < w; x++) {
100 tmppix[0] = TIFFGetR (*tmp_rast);
101 tmppix[1] = TIFFGetG (*tmp_rast);
102 tmppix[2] = TIFFGetB (*tmp_rast);
103 tmppix[3] = TIFFGetA (*tmp_rast);
113 gdk_pixbuf_unref (pixbuf);
114 (* context->update_func) (pixbuf, context->user_data, 0, 0, w, h);
127 return image_load_real (f, NULL);
132 /* Progressive loader */
134 * Tiff loading progressively cannot be done. We write it to a file, then load
135 * the file when it's done. It's not pretty.
140 image_begin_load (ModulePreparedNotifyFunc prepare_func,
141 ModuleUpdatedNotifyFunc update_func,
147 context = g_new (TiffData, 1);
148 context->prepare_func = prepare_func;
149 context->update_func = update_func;
150 context->user_data = user_data;
151 context->all_okay = TRUE;
152 context->tempname = g_strdup ("/tmp/gdkpixbuf-tif-tmp.XXXXXX");
153 fd = mkstemp (context->tempname);
159 context->file = fdopen (fd, "w");
160 if (context->file == NULL) {
169 image_stop_load (gpointer data)
171 TiffData *context = (TiffData*) data;
173 g_return_if_fail (data != NULL);
175 fflush (context->file);
176 rewind (context->file);
177 if (context->all_okay)
178 image_load_real (context->file, context);
180 fclose (context->file);
181 unlink (context->tempname);
182 g_free ((TiffData *) context);
186 image_load_increment (gpointer data, guchar *buf, guint size)
188 TiffData *context = (TiffData *) data;
190 g_return_val_if_fail (data != NULL, FALSE);
192 if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
193 context->all_okay = FALSE;