]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-tiff.c
Added progressive animation loading. Added progressive animation loading.
[~andy/gtk] / gdk-pixbuf / io-tiff.c
1 /* GdkPixbuf library - JPEG 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 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.
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  * Library General Public License for more details.
19  *
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.
24  */
25
26 /* Following code (almost) blatantly ripped from Imlib */
27
28 #include <config.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <tiffio.h>
33 #include "gdk-pixbuf.h"
34 #include "gdk-pixbuf-io.h"
35
36 \f
37
38 typedef struct _TiffData TiffData;
39 struct _TiffData
40 {
41         ModulePreparedNotifyFunc prepare_func;
42         ModuleUpdatedNotifyFunc update_func;
43         gpointer user_data;
44
45         gchar *tempname;
46         FILE *file;
47         gboolean all_okay;
48 };
49
50 \f
51
52 GdkPixbuf *
53 image_load_real (FILE *f, TiffData *context)
54 {
55         TIFF *tiff;
56         guchar *pixels = NULL;
57         guchar *tmppix;
58         gint w, h, x, y, num_pixs, fd;
59         uint32 *rast, *tmp_rast;
60         GdkPixbuf *pixbuf;
61         
62         fd = fileno (f);
63         tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
64
65         if (!tiff)
66                 return NULL;
67
68         TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &w);
69         TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &h);
70         num_pixs = w * h;
71         pixbuf = gdk_pixbuf_new (ART_PIX_RGB, TRUE, 8, w, h);
72
73         if (context)
74                 (* context->prepare_func) (pixbuf, context->user_data);
75
76         /* Yes, it needs to be _TIFFMalloc... */
77         rast = (uint32 *) _TIFFmalloc (num_pixs * sizeof (uint32));
78
79         if (!rast) {
80                 TIFFClose (tiff);
81                 return NULL;
82         }
83
84         if (TIFFReadRGBAImage (tiff, w, h, rast, 0)) {
85                 pixels = gdk_pixbuf_get_pixels (pixbuf);
86                 if (!pixels) {
87                         _TIFFfree (rast);
88                         TIFFClose (tiff);
89                         return NULL;
90                 }
91                 tmppix = pixels;
92
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
97                          * robust.
98                          */
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);
105                                 tmp_rast++;
106                                 tmppix += 4;
107                         }
108                 }
109         }
110         _TIFFfree (rast);
111         TIFFClose (tiff);
112
113         if (context) {
114                 (* context->update_func) (pixbuf, 0, 0, w, h, context->user_data);
115                 gdk_pixbuf_unref (pixbuf);
116         }
117
118         return pixbuf;
119 }
120
121 \f
122
123 /* Static loader */
124
125 GdkPixbuf *
126 image_load (FILE *f)
127 {
128         return image_load_real (f, NULL);
129 }
130
131 \f
132
133 /* Progressive loader */
134 /*
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.
137  */
138
139
140 gpointer
141 image_begin_load (ModulePreparedNotifyFunc prepare_func,
142                   ModuleUpdatedNotifyFunc update_func,
143                   ModuleFrameDoneNotifyFunc frame_done_func,
144                   ModuleAnimationDoneNotifyFunc anim_done_func,
145                   gpointer user_data)
146 {
147         TiffData *context;
148         gint fd;
149
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);
157         if (fd < 0) {
158                 g_free (context);
159                 return NULL;
160         }
161
162         context->file = fdopen (fd, "w");
163         if (context->file == NULL) {
164                 g_free (context);
165                 return NULL;
166         }
167
168         return context;
169 }
170
171 void
172 image_stop_load (gpointer data)
173 {
174         TiffData *context = (TiffData*) data;
175
176         g_return_if_fail (data != NULL);
177
178         fflush (context->file);
179         rewind (context->file);
180         if (context->all_okay)
181                 image_load_real (context->file, context);
182
183         fclose (context->file);
184         unlink (context->tempname);
185         g_free ((TiffData *) context);
186 }
187
188 gboolean
189 image_load_increment (gpointer data, guchar *buf, guint size)
190 {
191         TiffData *context = (TiffData *) data;
192
193         g_return_val_if_fail (data != NULL, FALSE);
194
195         if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
196                 context->all_okay = FALSE;
197                 return FALSE;
198         }
199
200         return TRUE;
201 }
202