gdk-pixbuf-animation.c 13.2 KB
Newer Older
1 2 3 4 5 6 7 8
/* GdkPixbuf library - Simple animation support
 *
 * Copyright (C) 1999 The Free Software Foundation
 *
 * Authors: Jonathan Blandford <jrb@redhat.com>
 *          Havoc Pennington <hp@redhat.com>
 *
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10 11 12 13 14 15
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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 20 21 22 23 24
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
Havoc Pennington's avatar
Havoc Pennington committed
25
#include <errno.h>
26
#include "gdk-pixbuf-io.h"
27
#include "gdk-pixbuf-private.h"
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static void gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass);
static void gdk_pixbuf_animation_finalize   (GObject        *object);



static gpointer parent_class;

GType
gdk_pixbuf_animation_get_type (void)
{
        static GType object_type = 0;

        if (!object_type) {
                static const GTypeInfo object_info = {
                        sizeof (GdkPixbufAnimationClass),
                        (GBaseInitFunc) NULL,
                        (GBaseFinalizeFunc) NULL,
                        (GClassInitFunc) gdk_pixbuf_animation_class_init,
                        NULL,           /* class_finalize */
                        NULL,           /* class_data */
                        sizeof (GdkPixbufAnimation),
                        0,              /* n_preallocs */
                        (GInstanceInitFunc) NULL,
                };
53 54 55

                g_type_init ();
                
56 57
                object_type = g_type_register_static (G_TYPE_OBJECT,
                                                      "GdkPixbufAnimation",
58
                                                      &object_info, 0);
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        }
  
        return object_type;
}

static void
gdk_pixbuf_animation_class_init (GdkPixbufAnimationClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        
        parent_class = g_type_class_peek_parent (klass);
        
        object_class->finalize = gdk_pixbuf_animation_finalize;
}

static void
gdk_pixbuf_animation_finalize (GObject *object)
{
        GdkPixbufAnimation *animation = GDK_PIXBUF_ANIMATION (object);

        GList *l;
        GdkPixbufFrame *frame;
        
        for (l = animation->frames; l; l = l->next) {
                frame = l->data;
                gdk_pixbuf_unref (frame->pixbuf);
                g_free (frame);
        }
        
        g_list_free (animation->frames);
        
        G_OBJECT_CLASS (parent_class)->finalize (object);
}

93 94 95 96 97


/**
 * gdk_pixbuf_animation_new_from_file:
 * @filename: Name of file to load.
Havoc Pennington's avatar
Havoc Pennington committed
98
 * @error: return location for error
99 100 101
 *
 * Creates a new animation by loading it from a file.  The file format is
 * detected automatically.  If the file's format does not support multi-frame
Havoc Pennington's avatar
Havoc Pennington committed
102 103
 * images, then an animation with a single frame will be created. Possible errors
 * are in the #GDK_PIXBUF_ERROR and #G_FILE_ERROR domains.
104 105 106 107 108 109 110
 *
 * Return value: A newly created animation with a reference count of 1, or NULL
 * if any of several error conditions ocurred:  the file could not be opened,
 * there was no loader for the file's format, there was not enough memory to
 * allocate the image buffer, or the image file contained invalid data.
 **/
GdkPixbufAnimation *
Havoc Pennington's avatar
Havoc Pennington committed
111 112
gdk_pixbuf_animation_new_from_file (const char *filename,
                                    GError    **error)
113 114 115 116 117 118 119 120 121
{
	GdkPixbufAnimation *animation;
	int size;
	FILE *f;
	guchar buffer [128];
	GdkPixbufModule *image_module;

	g_return_val_if_fail (filename != NULL, NULL);

Tor Lillqvist's avatar
Tor Lillqvist committed
122
	f = fopen (filename, "rb");
Havoc Pennington's avatar
Havoc Pennington committed
123 124 125 126 127 128
	if (!f) {
                g_set_error (error,
                             G_FILE_ERROR,
                             g_file_error_from_errno (errno),
                             _("Failed to open file '%s': %s"),
                             filename, g_strerror (errno));
129
		return NULL;
Havoc Pennington's avatar
Havoc Pennington committed
130
        }
131 132 133 134

	size = fread (&buffer, 1, sizeof (buffer), f);

	if (size == 0) {
Havoc Pennington's avatar
Havoc Pennington committed
135 136 137 138 139 140
                g_set_error (error,
                             GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                             _("Image file '%s' contains no data"),
                             filename);
                
141 142 143 144
		fclose (f);
		return NULL;
	}

Havoc Pennington's avatar
Havoc Pennington committed
145
	image_module = gdk_pixbuf_get_module (buffer, size, filename, error);
146 147 148 149 150 151
	if (!image_module) {
		fclose (f);
		return NULL;
	}

	if (image_module->module == NULL)
Havoc Pennington's avatar
Havoc Pennington committed
152 153 154 155
                if (!gdk_pixbuf_load_module (image_module, error)) {
                        fclose (f);
                        return NULL;
                }
156 157 158 159 160 161 162 163

	if (image_module->load_animation == NULL) {
		GdkPixbuf *pixbuf;
		GdkPixbufFrame *frame;

		/* Keep this logic in sync with gdk_pixbuf_new_from_file() */

		if (image_module->load == NULL) {
Havoc Pennington's avatar
Havoc Pennington committed
164 165 166 167 168
                        g_set_error (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_UNSUPPORTED_OPERATION,
                                     _("Don't know how to load the animation in file '%s'"),
                                     filename);                        
169 170 171 172 173
			fclose (f);
			return NULL;
		}

		fseek (f, 0, SEEK_SET);
Havoc Pennington's avatar
Havoc Pennington committed
174
		pixbuf = (* image_module->load) (f, error);
175 176
		fclose (f);

Havoc Pennington's avatar
Havoc Pennington committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
                if (pixbuf == NULL && error != NULL && *error == NULL) {
                        /* I don't trust these crufty longjmp()'ing image libs
                         * to maintain proper error invariants, and I don't
                         * want user code to segfault as a result. We need to maintain
                         * the invariant that error gets set if NULL is returned.
                         */
                        
                        g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.",
                                   image_module->module_name);
                        g_set_error (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_FAILED,
                                     _("Failed to load image '%s': reason not known, probably a corrupt image file"),
                                     filename);
                }
                
193 194
		if (pixbuf == NULL)
                        return NULL;
195 196 197

		frame = g_new (GdkPixbufFrame, 1);
		frame->pixbuf = pixbuf;
198
                g_object_ref (G_OBJECT (frame->pixbuf));
199 200 201 202 203
		frame->x_offset = 0;
		frame->y_offset = 0;
		frame->delay_time = -1;
		frame->action = GDK_PIXBUF_FRAME_RETAIN;

204
		animation = g_object_new (GDK_TYPE_PIXBUF_ANIMATION, NULL);
205

206 207
		animation->n_frames = 1;
		animation->frames = g_list_prepend (NULL, frame);
Radek Doulik's avatar
Radek Doulik committed
208 209
		animation->width = gdk_pixbuf_get_width (pixbuf);
		animation->height = gdk_pixbuf_get_height (pixbuf);
210 211
	} else {
		fseek (f, 0, SEEK_SET);
Havoc Pennington's avatar
Havoc Pennington committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		animation = (* image_module->load_animation) (f, error);

                if (animation == NULL && error != NULL && *error == NULL) {
                        /* I don't trust these crufty longjmp()'ing
                         * image libs to maintain proper error
                         * invariants, and I don't want user code to
                         * segfault as a result. We need to maintain
                         * the invariant that error gets set if NULL
                         * is returned.
                         */
                        
                        g_warning ("Bug! gdk-pixbuf loader '%s' didn't set an error on failure.",
                                   image_module->module_name);
                        g_set_error (error,
                                     GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_FAILED,
                                     _("Failed to load animation '%s': reason not known, probably a corrupt animation file"),
                                     filename);
                }
                
232 233 234 235 236 237 238 239 240 241
		fclose (f);
	}

	return animation;
}

/**
 * gdk_pixbuf_animation_ref:
 * @animation: An animation.
 *
242 243 244
 * Adds a reference to an animation. Deprecated; use
 * g_object_ref(). The reference must be released afterwards using
 * g_object_unref().
245 246
 *
 * Return value: The same as the @animation argument.
247
 **/
248
GdkPixbufAnimation *
249 250
gdk_pixbuf_animation_ref (GdkPixbufAnimation *animation)
{
251
        return (GdkPixbufAnimation*) g_object_ref (G_OBJECT (animation));
252 253 254 255 256 257
}

/**
 * gdk_pixbuf_animation_unref:
 * @animation: An animation.
 * 
258
 * Removes a reference from an animation. Deprecated; use g_object_unref().
259 260 261 262
 **/
void
gdk_pixbuf_animation_unref (GdkPixbufAnimation *animation)
{
263
        g_object_unref (G_OBJECT (animation));
264
}
265 266 267 268

/**
 * gdk_pixbuf_animation_get_width:
 * @animation: An animation.
269 270
 *
 * Queries the width of the bounding box of a pixbuf animation.
271
 * 
272
 * Return value: Width of the bounding box of the animation.
273 274 275 276
 **/
int
gdk_pixbuf_animation_get_width (GdkPixbufAnimation *animation)
{
277
	g_return_val_if_fail (animation != NULL, -1);
278 279 280 281 282 283 284

	return animation->width;
}

/**
 * gdk_pixbuf_animation_get_height:
 * @animation: An animation.
285 286
 *
 * Queries the height of the bounding box of a pixbuf animation.
287
 * 
288
 * Return value: Height of the bounding box of the animation.
289 290 291 292
 **/
int
gdk_pixbuf_animation_get_height (GdkPixbufAnimation *animation)
{
293
	g_return_val_if_fail (animation != NULL, -1);
294 295 296 297 298 299 300

	return animation->height;
}

/**
 * gdk_pixbuf_animation_get_num_frames:
 * @animation: An animation.
301 302
 *
 * Queries the number of frames in a pixbuf animation.
303
 * 
304
 * Return value: Number of frames in the animation.
305 306 307 308
 **/
int
gdk_pixbuf_animation_get_num_frames (GdkPixbufAnimation *animation)
{
309
	g_return_val_if_fail (animation != NULL, -1);
310 311 312 313 314 315 316

	return animation->n_frames;
}

/**
 * gdk_pixbuf_animation_get_frames:
 * @animation: An animation.
317 318
 *
 * Queries the list of frames of an animation.
319
 * 
320 321
 * Return value: List of frames in the animation; this is a #GList of
 * #GdkPixbufFrame structures.
322 323 324 325
 **/
GList *
gdk_pixbuf_animation_get_frames (GdkPixbufAnimation *animation)
{
326
	g_return_val_if_fail (animation != NULL, NULL);
327 328 329

	return animation->frames;
}
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411



/**
 * gdk_pixbuf_frame_get_pixbuf:
 * @frame: A pixbuf animation frame.
 * 
 * Queries the pixbuf of an animation frame.
 * 
 * Return value: A pixbuf.
 **/
GdkPixbuf *
gdk_pixbuf_frame_get_pixbuf (GdkPixbufFrame *frame)
{
	g_return_val_if_fail (frame != NULL, NULL);

	return frame->pixbuf;
}

/**
 * gdk_pixbuf_frame_get_x_offset:
 * @frame: A pixbuf animation frame.
 * 
 * Queries the X offset of an animation frame.
 * 
 * Return value: X offset from the top left corner of the animation.
 **/
int
gdk_pixbuf_frame_get_x_offset (GdkPixbufFrame *frame)
{
	g_return_val_if_fail (frame != NULL, -1);

	return frame->x_offset;
}

/**
 * gdk_pixbuf_frame_get_y_offset:
 * @frame: A pixbuf animation frame.
 * 
 * Queries the Y offset of an animation frame.
 * 
 * Return value: Y offset from the top left corner of the animation.
 **/
int
gdk_pixbuf_frame_get_y_offset (GdkPixbufFrame *frame)
{
	g_return_val_if_fail (frame != NULL, -1);

	return frame->y_offset;
}

/**
 * gdk_pixbuf_frame_get_delay_time:
 * @frame: A pixbuf animation frame.
 * 
 * Queries the delay time in milliseconds of an animation frame.
 * 
 * Return value: Delay time in milliseconds.
 **/
int
gdk_pixbuf_frame_get_delay_time (GdkPixbufFrame *frame)
{
	g_return_val_if_fail (frame != NULL, -1);

	return frame->delay_time;
}

/**
 * gdk_pixbuf_frame_get_action:
 * @frame: A pixbuf animation frame.
 * 
 * Queries the overlay action of an animation frame.
 * 
 * Return value: Overlay action for this frame.
 **/
GdkPixbufFrameAction
gdk_pixbuf_frame_get_action (GdkPixbufFrame *frame)
{
	g_return_val_if_fail (frame != NULL, GDK_PIXBUF_FRAME_RETAIN);

	return frame->action;
}
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

/**
 * gdk_pixbuf_frame_copy:
 * @src: a #GdkPixbufFrame to copy
 * 
 * Copies a #GdkPixbufFrame. Free the result
 * with gdk_pixbuf_frame_free().
 * 
 * Return value: a new #GdkPixbufFrame
 **/
GdkPixbufFrame*
gdk_pixbuf_frame_copy (GdkPixbufFrame *src)
{
  GdkPixbufFrame *frame;

  frame = g_new (GdkPixbufFrame, 1);
  frame->pixbuf = src->pixbuf;
  g_object_ref (G_OBJECT (frame->pixbuf));
  frame->x_offset = src->x_offset;
  frame->y_offset = src->y_offset;
  frame->delay_time = src->delay_time;
  frame->action = src->action;
  
  return frame;
}

/**
 * gdk_pixbuf_frame_free:
 * @frame: a #GdkPixbufFrame
 * 
 * Frees a #GdkPixbufFrame. Don't do this with frames you got from
 * #GdkPixbufAnimation, usually the animation owns those (it doesn't
 * make a copy before returning the frame).
 **/
void
gdk_pixbuf_frame_free (GdkPixbufFrame *frame)
{
  g_return_if_fail (frame != NULL);

  g_object_unref (G_OBJECT (frame->pixbuf));
  g_free (frame);
}

GType
gdk_pixbuf_frame_get_type (void)
{
  static GType our_type = 0;

  if (our_type == 0)
    {
462 463
      g_type_init ();
      
464 465 466 467 468 469 470
      our_type = g_boxed_type_register_static ("GdkPixbufFrame",
                                               gdk_pixbuf_frame_copy,
                                               gdk_pixbuf_frame_free);
    }

  return our_type;
}