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 Library 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 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library 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"
31 * gdk_pixbuf_animation_new_from_file:
32 * @filename: Name of file to load.
34 * Creates a new animation by loading it from a file. The file format is
35 * detected automatically. If the file's format does not support multi-frame
36 * images, then an animation with a single frame will be created.
38 * Return value: A newly created animation with a reference count of 1, or NULL
39 * if any of several error conditions ocurred: the file could not be opened,
40 * there was no loader for the file's format, there was not enough memory to
41 * allocate the image buffer, or the image file contained invalid data.
44 gdk_pixbuf_animation_new_from_file (const char *filename)
46 GdkPixbufAnimation *animation;
50 GdkPixbufModule *image_module;
52 g_return_val_if_fail (filename != NULL, NULL);
54 f = fopen (filename, "r");
58 size = fread (&buffer, 1, sizeof (buffer), f);
65 image_module = gdk_pixbuf_get_module (buffer, size);
67 g_warning ("Unable to find handler for file: %s", filename);
72 if (image_module->module == NULL)
73 gdk_pixbuf_load_module (image_module);
75 if (image_module->load_animation == NULL) {
77 GdkPixbufFrame *frame;
79 /* Keep this logic in sync with gdk_pixbuf_new_from_file() */
81 if (image_module->load == NULL) {
86 fseek (f, 0, SEEK_SET);
87 pixbuf = (* image_module->load) (f);
91 g_assert (pixbuf->ref_count > 0);
95 frame = g_new (GdkPixbufFrame, 1);
96 frame->pixbuf = pixbuf;
99 frame->delay_time = -1;
100 frame->action = GDK_PIXBUF_FRAME_RETAIN;
102 animation = g_new0 (GdkPixbufAnimation, 1);
103 animation->ref_count = 1;
104 animation->n_frames = 1;
105 animation->frames = g_list_prepend (NULL, frame);
106 animation->width = gdk_pixbuf_get_width (pixbuf);
107 animation->height = gdk_pixbuf_get_height (pixbuf);
109 fseek (f, 0, SEEK_SET);
110 animation = (* image_module->load_animation) (f);
118 * gdk_pixbuf_animation_ref:
119 * @animation: An animation.
121 * Adds a reference to an animation. It must be released afterwards using
122 * gdk_pixbuf_animation_unref().
124 * Return value: The same as the @animation argument.
127 gdk_pixbuf_animation_ref (GdkPixbufAnimation *animation)
129 g_return_val_if_fail (animation != NULL, NULL);
130 g_return_val_if_fail (animation->ref_count > 0, NULL);
132 animation->ref_count++;
137 * gdk_pixbuf_animation_unref:
138 * @animation: An animation.
140 * Removes a reference from an animation. It will be destroyed when the
141 * reference count drops to zero. At that point, all the frames in the
142 * animation will be freed and their corresponding pixbufs will be unreferenced.
145 gdk_pixbuf_animation_unref (GdkPixbufAnimation *animation)
147 g_return_if_fail (animation != NULL);
148 g_return_if_fail (animation->ref_count > 0);
150 animation->ref_count--;
152 if (animation->ref_count == 0) {
154 GdkPixbufFrame *frame;
156 for (l = animation->frames; l; l = l->next) {
158 gdk_pixbuf_unref (frame->pixbuf);
162 g_list_free (animation->frames);
168 * gdk_pixbuf_animation_get_width:
169 * @animation: An animation.
171 * Return the width of @animation.
174 gdk_pixbuf_animation_get_width (GdkPixbufAnimation *animation)
176 g_return_val_if_fail (animation != NULL, 0);
177 g_return_val_if_fail (animation->ref_count > 0, 0);
179 return animation->width;
183 * gdk_pixbuf_animation_get_height:
184 * @animation: An animation.
186 * Return the height of @animation.
189 gdk_pixbuf_animation_get_height (GdkPixbufAnimation *animation)
191 g_return_val_if_fail (animation != NULL, 0);
192 g_return_val_if_fail (animation->ref_count > 0, 0);
194 return animation->height;
198 * gdk_pixbuf_animation_get_num_frames:
199 * @animation: An animation.
201 * Return the number of frames in @animation.
204 gdk_pixbuf_animation_get_num_frames (GdkPixbufAnimation *animation)
206 g_return_val_if_fail (animation != NULL, 0);
207 g_return_val_if_fail (animation->ref_count > 0, 0);
209 return animation->n_frames;
213 * gdk_pixbuf_animation_get_frames:
214 * @animation: An animation.
216 * Return the frames of @animation as a list of
217 * GdkPixbufAnimationFrame objects.
220 gdk_pixbuf_animation_get_frames (GdkPixbufAnimation *animation)
222 g_return_val_if_fail (animation != NULL, 0);
223 g_return_val_if_fail (animation->ref_count > 0, 0);
225 return animation->frames;
231 * gdk_pixbuf_frame_get_pixbuf:
232 * @frame: A pixbuf animation frame.
234 * Queries the pixbuf of an animation frame.
236 * Return value: A pixbuf.
239 gdk_pixbuf_frame_get_pixbuf (GdkPixbufFrame *frame)
241 g_return_val_if_fail (frame != NULL, NULL);
243 return frame->pixbuf;
247 * gdk_pixbuf_frame_get_x_offset:
248 * @frame: A pixbuf animation frame.
250 * Queries the X offset of an animation frame.
252 * Return value: X offset from the top left corner of the animation.
255 gdk_pixbuf_frame_get_x_offset (GdkPixbufFrame *frame)
257 g_return_val_if_fail (frame != NULL, -1);
259 return frame->x_offset;
263 * gdk_pixbuf_frame_get_y_offset:
264 * @frame: A pixbuf animation frame.
266 * Queries the Y offset of an animation frame.
268 * Return value: Y offset from the top left corner of the animation.
271 gdk_pixbuf_frame_get_y_offset (GdkPixbufFrame *frame)
273 g_return_val_if_fail (frame != NULL, -1);
275 return frame->y_offset;
279 * gdk_pixbuf_frame_get_delay_time:
280 * @frame: A pixbuf animation frame.
282 * Queries the delay time in milliseconds of an animation frame.
284 * Return value: Delay time in milliseconds.
287 gdk_pixbuf_frame_get_delay_time (GdkPixbufFrame *frame)
289 g_return_val_if_fail (frame != NULL, -1);
291 return frame->delay_time;
295 * gdk_pixbuf_frame_get_action:
296 * @frame: A pixbuf animation frame.
298 * Queries the overlay action of an animation frame.
300 * Return value: Overlay action for this frame.
303 gdk_pixbuf_frame_get_action (GdkPixbufFrame *frame)
305 g_return_val_if_fail (frame != NULL, GDK_PIXBUF_FRAME_RETAIN);
307 return frame->action;