1 /* GdkPixbuf library - Simple animation support
3 * Copyright (C) 1999 The Free Software Foundation
5 * Authors: Jonathan Blandford <jrb@redhat.com>
6 * Havoc Pennington <hp@redhat.com>
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.
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.
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.
25 #include "gdk-pixbuf-io.h"
26 #include "gdk-pixbuf-private.h"
28 static void gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass);
29 static void gdk_pixbuf_animation_finalize (GObject *object);
33 static gpointer parent_class;
36 gdk_pixbuf_animation_get_type (void)
38 static GType object_type = 0;
41 static const GTypeInfo object_info = {
42 sizeof (GdkPixbufAnimationClass),
44 (GBaseFinalizeFunc) NULL,
45 (GClassInitFunc) gdk_pixbuf_animation_class_init,
46 NULL, /* class_finalize */
47 NULL, /* class_data */
48 sizeof (GdkPixbufAnimation),
50 (GInstanceInitFunc) NULL,
53 object_type = g_type_register_static (G_TYPE_OBJECT,
62 gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass)
64 GObjectClass *object_class = G_OBJECT_CLASS (klass);
66 parent_class = g_type_class_peek_parent (klass);
68 object_class->finalize = gdk_pixbuf_animation_finalize;
72 gdk_pixbuf_animation_finalize (GObject *object)
74 GdkPixbufAnimation *animation = GDK_PIXBUF_ANIMATION (object);
77 GdkPixbufFrame *frame;
79 for (l = animation->frames; l; l = l->next) {
81 gdk_pixbuf_unref (frame->pixbuf);
85 g_list_free (animation->frames);
87 G_OBJECT_CLASS (parent_class)->finalize (object);
93 * gdk_pixbuf_animation_new_from_file:
94 * @filename: Name of file to load.
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.
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.
106 gdk_pixbuf_animation_new_from_file (const char *filename)
108 GdkPixbufAnimation *animation;
112 GdkPixbufModule *image_module;
114 g_return_val_if_fail (filename != NULL, NULL);
116 f = fopen (filename, "rb");
120 size = fread (&buffer, 1, sizeof (buffer), f);
127 image_module = gdk_pixbuf_get_module (buffer, size);
129 g_warning ("Unable to find handler for file: %s", filename);
134 if (image_module->module == NULL)
135 gdk_pixbuf_load_module (image_module);
137 if (image_module->load_animation == NULL) {
139 GdkPixbufFrame *frame;
141 /* Keep this logic in sync with gdk_pixbuf_new_from_file() */
143 if (image_module->load == NULL) {
148 fseek (f, 0, SEEK_SET);
149 pixbuf = (* image_module->load) (f);
155 frame = g_new (GdkPixbufFrame, 1);
156 frame->pixbuf = pixbuf;
159 frame->delay_time = -1;
160 frame->action = GDK_PIXBUF_FRAME_RETAIN;
162 animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
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);
169 fseek (f, 0, SEEK_SET);
170 animation = (* image_module->load_animation) (f);
178 * gdk_pixbuf_animation_ref:
179 * @animation: An animation.
181 * Adds a reference to an animation. Deprecated; use
182 * g_object_ref(). The reference must be released afterwards using
185 * Return value: The same as the @animation argument.
188 gdk_pixbuf_animation_ref (GdkPixbufAnimation *animation)
190 return (GdkPixbufAnimation*) g_object_ref (G_OBJECT (animation));
194 * gdk_pixbuf_animation_unref:
195 * @animation: An animation.
197 * Removes a reference from an animation. Deprecated; use g_object_unref().
200 gdk_pixbuf_animation_unref (GdkPixbufAnimation *animation)
202 g_object_unref (G_OBJECT (animation));
206 * gdk_pixbuf_animation_get_width:
207 * @animation: An animation.
209 * Queries the width of the bounding box of a pixbuf animation.
211 * Return value: Width of the bounding box of the animation.
214 gdk_pixbuf_animation_get_width (GdkPixbufAnimation *animation)
216 g_return_val_if_fail (animation != NULL, -1);
218 return animation->width;
222 * gdk_pixbuf_animation_get_height:
223 * @animation: An animation.
225 * Queries the height of the bounding box of a pixbuf animation.
227 * Return value: Height of the bounding box of the animation.
230 gdk_pixbuf_animation_get_height (GdkPixbufAnimation *animation)
232 g_return_val_if_fail (animation != NULL, -1);
234 return animation->height;
238 * gdk_pixbuf_animation_get_num_frames:
239 * @animation: An animation.
241 * Queries the number of frames in a pixbuf animation.
243 * Return value: Number of frames in the animation.
246 gdk_pixbuf_animation_get_num_frames (GdkPixbufAnimation *animation)
248 g_return_val_if_fail (animation != NULL, -1);
250 return animation->n_frames;
254 * gdk_pixbuf_animation_get_frames:
255 * @animation: An animation.
257 * Queries the list of frames of an animation.
259 * Return value: List of frames in the animation; this is a #GList of
260 * #GdkPixbufFrame structures.
263 gdk_pixbuf_animation_get_frames (GdkPixbufAnimation *animation)
265 g_return_val_if_fail (animation != NULL, NULL);
267 return animation->frames;
273 * gdk_pixbuf_frame_get_pixbuf:
274 * @frame: A pixbuf animation frame.
276 * Queries the pixbuf of an animation frame.
278 * Return value: A pixbuf.
281 gdk_pixbuf_frame_get_pixbuf (GdkPixbufFrame *frame)
283 g_return_val_if_fail (frame != NULL, NULL);
285 return frame->pixbuf;
289 * gdk_pixbuf_frame_get_x_offset:
290 * @frame: A pixbuf animation frame.
292 * Queries the X offset of an animation frame.
294 * Return value: X offset from the top left corner of the animation.
297 gdk_pixbuf_frame_get_x_offset (GdkPixbufFrame *frame)
299 g_return_val_if_fail (frame != NULL, -1);
301 return frame->x_offset;
305 * gdk_pixbuf_frame_get_y_offset:
306 * @frame: A pixbuf animation frame.
308 * Queries the Y offset of an animation frame.
310 * Return value: Y offset from the top left corner of the animation.
313 gdk_pixbuf_frame_get_y_offset (GdkPixbufFrame *frame)
315 g_return_val_if_fail (frame != NULL, -1);
317 return frame->y_offset;
321 * gdk_pixbuf_frame_get_delay_time:
322 * @frame: A pixbuf animation frame.
324 * Queries the delay time in milliseconds of an animation frame.
326 * Return value: Delay time in milliseconds.
329 gdk_pixbuf_frame_get_delay_time (GdkPixbufFrame *frame)
331 g_return_val_if_fail (frame != NULL, -1);
333 return frame->delay_time;
337 * gdk_pixbuf_frame_get_action:
338 * @frame: A pixbuf animation frame.
340 * Queries the overlay action of an animation frame.
342 * Return value: Overlay action for this frame.
345 gdk_pixbuf_frame_get_action (GdkPixbufFrame *frame)
347 g_return_val_if_fail (frame != NULL, GDK_PIXBUF_FRAME_RETAIN);
349 return frame->action;