]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/gdk-pixbuf-animation.c
applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[~andy/gtk] / gdk-pixbuf / gdk-pixbuf-animation.c
1 /* GdkPixbuf library - Simple animation support
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Jonathan Blandford <jrb@redhat.com>
6  *          Havoc Pennington <hp@redhat.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include <config.h>
25 #include "gdk-pixbuf-io.h"
26 #include "gdk-pixbuf-private.h"
27
28 static void gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass);
29 static void gdk_pixbuf_animation_finalize   (GObject        *object);
30
31 \f
32
33 static gpointer parent_class;
34
35 GType
36 gdk_pixbuf_animation_get_type (void)
37 {
38         static GType object_type = 0;
39
40         if (!object_type) {
41                 static const GTypeInfo object_info = {
42                         sizeof (GdkPixbufAnimationClass),
43                         (GBaseInitFunc) NULL,
44                         (GBaseFinalizeFunc) NULL,
45                         (GClassInitFunc) gdk_pixbuf_animation_class_init,
46                         NULL,           /* class_finalize */
47                         NULL,           /* class_data */
48                         sizeof (GdkPixbufAnimation),
49                         0,              /* n_preallocs */
50                         (GInstanceInitFunc) NULL,
51                 };
52       
53                 object_type = g_type_register_static (G_TYPE_OBJECT,
54                                                       "GdkPixbufAnimation",
55                                                       &object_info);
56         }
57   
58         return object_type;
59 }
60
61 static void
62 gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass)
63 {
64         GObjectClass *object_class = G_OBJECT_CLASS (klass);
65         
66         parent_class = g_type_class_peek_parent (klass);
67         
68         object_class->finalize = gdk_pixbuf_animation_finalize;
69 }
70
71 static void
72 gdk_pixbuf_animation_finalize (GObject *object)
73 {
74         GdkPixbufAnimation *animation = GDK_PIXBUF_ANIMATION (object);
75
76         GList *l;
77         GdkPixbufFrame *frame;
78         
79         for (l = animation->frames; l; l = l->next) {
80                 frame = l->data;
81                 gdk_pixbuf_unref (frame->pixbuf);
82                 g_free (frame);
83         }
84         
85         g_list_free (animation->frames);
86         
87         G_OBJECT_CLASS (parent_class)->finalize (object);
88 }
89
90 \f
91
92 /**
93  * gdk_pixbuf_animation_new_from_file:
94  * @filename: Name of file to load.
95  *
96  * Creates a new animation by loading it from a file.  The file format is
97  * detected automatically.  If the file's format does not support multi-frame
98  * images, then an animation with a single frame will be created.
99  *
100  * Return value: A newly created animation with a reference count of 1, or NULL
101  * if any of several error conditions ocurred:  the file could not be opened,
102  * there was no loader for the file's format, there was not enough memory to
103  * allocate the image buffer, or the image file contained invalid data.
104  **/
105 GdkPixbufAnimation *
106 gdk_pixbuf_animation_new_from_file (const char *filename)
107 {
108         GdkPixbufAnimation *animation;
109         int size;
110         FILE *f;
111         guchar buffer [128];
112         GdkPixbufModule *image_module;
113
114         g_return_val_if_fail (filename != NULL, NULL);
115
116         f = fopen (filename, "rb");
117         if (!f)
118                 return NULL;
119
120         size = fread (&buffer, 1, sizeof (buffer), f);
121
122         if (size == 0) {
123                 fclose (f);
124                 return NULL;
125         }
126
127         image_module = gdk_pixbuf_get_module (buffer, size);
128         if (!image_module) {
129                 g_warning ("Unable to find handler for file: %s", filename);
130                 fclose (f);
131                 return NULL;
132         }
133
134         if (image_module->module == NULL)
135                 gdk_pixbuf_load_module (image_module);
136
137         if (image_module->load_animation == NULL) {
138                 GdkPixbuf *pixbuf;
139                 GdkPixbufFrame *frame;
140
141                 /* Keep this logic in sync with gdk_pixbuf_new_from_file() */
142
143                 if (image_module->load == NULL) {
144                         fclose (f);
145                         return NULL;
146                 }
147
148                 fseek (f, 0, SEEK_SET);
149                 pixbuf = (* image_module->load) (f);
150                 fclose (f);
151
152                 if (pixbuf == NULL)
153                         return NULL;
154
155                 frame = g_new (GdkPixbufFrame, 1);
156                 frame->pixbuf = pixbuf;
157                 frame->x_offset = 0;
158                 frame->y_offset = 0;
159                 frame->delay_time = -1;
160                 frame->action = GDK_PIXBUF_FRAME_RETAIN;
161
162                 animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
163
164                 animation->n_frames = 1;
165                 animation->frames = g_list_prepend (NULL, frame);
166                 animation->width = gdk_pixbuf_get_width (pixbuf);
167                 animation->height = gdk_pixbuf_get_height (pixbuf);
168         } else {
169                 fseek (f, 0, SEEK_SET);
170                 animation = (* image_module->load_animation) (f);
171                 fclose (f);
172         }
173
174         return animation;
175 }
176
177 /**
178  * gdk_pixbuf_animation_ref:
179  * @animation: An animation.
180  *
181  * Adds a reference to an animation. Deprecated; use
182  * g_object_ref(). The reference must be released afterwards using
183  * g_object_unref().
184  *
185  * Return value: The same as the @animation argument.
186  **/
187 GdkPixbufAnimation *
188 gdk_pixbuf_animation_ref (GdkPixbufAnimation *animation)
189 {
190         return (GdkPixbufAnimation*) g_object_ref (G_OBJECT (animation));
191 }
192
193 /**
194  * gdk_pixbuf_animation_unref:
195  * @animation: An animation.
196  * 
197  * Removes a reference from an animation. Deprecated; use g_object_unref().
198  **/
199 void
200 gdk_pixbuf_animation_unref (GdkPixbufAnimation *animation)
201 {
202         g_object_unref (G_OBJECT (animation));
203 }
204
205 /**
206  * gdk_pixbuf_animation_get_width:
207  * @animation: An animation.
208  *
209  * Queries the width of the bounding box of a pixbuf animation.
210  * 
211  * Return value: Width of the bounding box of the animation.
212  **/
213 int
214 gdk_pixbuf_animation_get_width (GdkPixbufAnimation *animation)
215 {
216         g_return_val_if_fail (animation != NULL, -1);
217
218         return animation->width;
219 }
220
221 /**
222  * gdk_pixbuf_animation_get_height:
223  * @animation: An animation.
224  *
225  * Queries the height of the bounding box of a pixbuf animation.
226  * 
227  * Return value: Height of the bounding box of the animation.
228  **/
229 int
230 gdk_pixbuf_animation_get_height (GdkPixbufAnimation *animation)
231 {
232         g_return_val_if_fail (animation != NULL, -1);
233
234         return animation->height;
235 }
236
237 /**
238  * gdk_pixbuf_animation_get_num_frames:
239  * @animation: An animation.
240  *
241  * Queries the number of frames in a pixbuf animation.
242  * 
243  * Return value: Number of frames in the animation.
244  **/
245 int
246 gdk_pixbuf_animation_get_num_frames (GdkPixbufAnimation *animation)
247 {
248         g_return_val_if_fail (animation != NULL, -1);
249
250         return animation->n_frames;
251 }
252
253 /**
254  * gdk_pixbuf_animation_get_frames:
255  * @animation: An animation.
256  *
257  * Queries the list of frames of an animation.
258  * 
259  * Return value: List of frames in the animation; this is a #GList of
260  * #GdkPixbufFrame structures.
261  **/
262 GList *
263 gdk_pixbuf_animation_get_frames (GdkPixbufAnimation *animation)
264 {
265         g_return_val_if_fail (animation != NULL, NULL);
266
267         return animation->frames;
268 }
269
270 \f
271
272 /**
273  * gdk_pixbuf_frame_get_pixbuf:
274  * @frame: A pixbuf animation frame.
275  * 
276  * Queries the pixbuf of an animation frame.
277  * 
278  * Return value: A pixbuf.
279  **/
280 GdkPixbuf *
281 gdk_pixbuf_frame_get_pixbuf (GdkPixbufFrame *frame)
282 {
283         g_return_val_if_fail (frame != NULL, NULL);
284
285         return frame->pixbuf;
286 }
287
288 /**
289  * gdk_pixbuf_frame_get_x_offset:
290  * @frame: A pixbuf animation frame.
291  * 
292  * Queries the X offset of an animation frame.
293  * 
294  * Return value: X offset from the top left corner of the animation.
295  **/
296 int
297 gdk_pixbuf_frame_get_x_offset (GdkPixbufFrame *frame)
298 {
299         g_return_val_if_fail (frame != NULL, -1);
300
301         return frame->x_offset;
302 }
303
304 /**
305  * gdk_pixbuf_frame_get_y_offset:
306  * @frame: A pixbuf animation frame.
307  * 
308  * Queries the Y offset of an animation frame.
309  * 
310  * Return value: Y offset from the top left corner of the animation.
311  **/
312 int
313 gdk_pixbuf_frame_get_y_offset (GdkPixbufFrame *frame)
314 {
315         g_return_val_if_fail (frame != NULL, -1);
316
317         return frame->y_offset;
318 }
319
320 /**
321  * gdk_pixbuf_frame_get_delay_time:
322  * @frame: A pixbuf animation frame.
323  * 
324  * Queries the delay time in milliseconds of an animation frame.
325  * 
326  * Return value: Delay time in milliseconds.
327  **/
328 int
329 gdk_pixbuf_frame_get_delay_time (GdkPixbufFrame *frame)
330 {
331         g_return_val_if_fail (frame != NULL, -1);
332
333         return frame->delay_time;
334 }
335
336 /**
337  * gdk_pixbuf_frame_get_action:
338  * @frame: A pixbuf animation frame.
339  * 
340  * Queries the overlay action of an animation frame.
341  * 
342  * Return value: Overlay action for this frame.
343  **/
344 GdkPixbufFrameAction
345 gdk_pixbuf_frame_get_action (GdkPixbufFrame *frame)
346 {
347         g_return_val_if_fail (frame != NULL, GDK_PIXBUF_FRAME_RETAIN);
348
349         return frame->action;
350 }