New loader, for .ANI animations.

* io-ani.c, io-ani-animation.h, io-ani-animation.c,
	pixbufloader_ani.def, Makefile.am, gdk-pixbuf-io.c: New loader,
	for .ANI animations.
This commit is contained in:
Matthias Clasen 2002-09-06 21:14:15 +00:00
parent ab97160067
commit 8a1a091f87
6 changed files with 1161 additions and 1 deletions

View File

@ -86,6 +86,14 @@ libpixbufloader_ico_la_SOURCES = io-ico.c
libpixbufloader_ico_la_LDFLAGS = -avoid-version -module $(no_undefined)
libpixbufloader_ico_la_LIBADD = $(module_libs)
#
# The ANI loader
#
libpixbufloader_static_ani_la_SOURCES = io-ani.c io-ani-animation.c io-ani-animation.h
libpixbufloader_ani_la_SOURCES = io-ani.c io-ani-animation.c io-ani-animation.h
libpixbufloader_ani_la_LDFLAGS = -avoid-version -module $(no_undefined)
libpixbufloader_ani_la_LIBADD = $(module_libs)
#
# The RAS loader
#
@ -158,6 +166,9 @@ STATIC_GIF_LIB = libpixbufloader-static-gif.la
ICO_LIB = libpixbufloader-ico.la
STATIC_ICO_LIB = libpixbufloader-static-ico.la
ANI_LIB = libpixbufloader-ani.la
STATIC_ANI_LIB = libpixbufloader-static-ani.la
RAS_LIB = libpixbufloader-ras.la
STATIC_RAS_LIB = libpixbufloader-static-ras.la
@ -191,6 +202,7 @@ loader_LTLIBRARIES = \
$(JPEG_LIB) \
$(GIF_LIB) \
$(ICO_LIB) \
$(ANI_LIB) \
$(RAS_LIB) \
$(XPM_LIB) \
$(TIFF_LIB) \
@ -211,6 +223,7 @@ noinst_LTLIBRARIES = \
$(STATIC_JPEG_LIB) \
$(STATIC_GIF_LIB) \
$(STATIC_ICO_LIB) \
$(STATIC_ANI_LIB) \
$(STATIC_RAS_LIB) \
$(STATIC_XPM_LIB) \
$(STATIC_TIFF_LIB) \
@ -379,6 +392,7 @@ EXTRA_DIST = \
gdk-pixbuf-marshal.c \
gdk-pixbuf-marshal.list \
pixbufloader_ico.def \
pixbufloader_ani.def \
pixbufloader_pnm.def \
pixbufloader_xpm.def \
pixbufloader_bmp.def \

View File

@ -163,6 +163,26 @@ pixbuf_check_ico (guchar *buffer, int size)
return TRUE;
}
static gboolean
pixbuf_check_ani (guchar *buffer, int size)
{
if (size < 12)
return FALSE;
if (buffer [0] != 'R' ||
buffer [1] != 'I' ||
buffer [2] != 'F' ||
buffer [3] != 'F' ||
buffer [8] != 'A' ||
buffer [9] != 'C' ||
buffer[10] != 'O' ||
buffer[11] != 'N')
return FALSE;
return TRUE;
}
static gboolean
pixbuf_check_bmp (guchar *buffer, int size)
{
@ -241,7 +261,9 @@ static GdkPixbufModule file_formats [] = {
{ "ras", pixbuf_check_sunras, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ "bmp", pixbuf_check_bmp, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ "xbm", pixbuf_check_xbm, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ "ico", pixbuf_check_ico, NULL, NULL, NULL, NULL, NULL, NULL, NULL }, { "tga", pixbuf_check_tga, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ "ico", pixbuf_check_ico, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ "ani", pixbuf_check_ani, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ "tga", pixbuf_check_tga, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ "wbmp", pixbuf_check_wbmp, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
{ NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
};
@ -392,6 +414,7 @@ m_fill_vtable (bmp);
m_fill_vtable (wbmp);
m_fill_vtable (gif);
m_fill_vtable (ico);
m_fill_vtable (ani);
m_fill_vtable (jpeg);
m_fill_vtable (pnm);
m_fill_vtable (ras);
@ -441,6 +464,12 @@ _gdk_pixbuf_load_module (GdkPixbufModule *image_module,
}
#endif
#ifdef INCLUDE_ani
else if (strcmp (image_module->module_name, "ani") == 0){
fill_vtable = mname (ani, fill_vtable);
}
#endif
#ifdef INCLUDE_jpeg
else if (strcmp (image_module->module_name, "jpeg") == 0){
fill_vtable = mname (jpeg, fill_vtable);

View File

@ -0,0 +1,347 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* GdkPixbuf library - ani support
*
* Copyright (C) 2002 The Free Software Foundation
*
* Author: Matthias Clasen <maclas@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* 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>
#include <errno.h>
#include "gdk-pixbuf-io.h"
#include "gdk-pixbuf-private.h"
#include "io-ani-animation.h"
static void gdk_pixbuf_ani_anim_class_init (GdkPixbufAniAnimClass *klass);
static void gdk_pixbuf_ani_anim_finalize (GObject *object);
static gboolean gdk_pixbuf_ani_anim_is_static_image (GdkPixbufAnimation *animation);
static GdkPixbuf* gdk_pixbuf_ani_anim_get_static_image (GdkPixbufAnimation *animation);
static void gdk_pixbuf_ani_anim_get_size (GdkPixbufAnimation *anim,
int *width,
int *height);
static GdkPixbufAnimationIter* gdk_pixbuf_ani_anim_get_iter (GdkPixbufAnimation *anim,
const GTimeVal *start_time);
static gpointer parent_class;
GType
gdk_pixbuf_ani_anim_get_type (void)
{
static GType object_type = 0;
if (!object_type) {
static const GTypeInfo object_info = {
sizeof (GdkPixbufAniAnimClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gdk_pixbuf_ani_anim_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GdkPixbufAniAnim),
0, /* n_preallocs */
(GInstanceInitFunc) NULL,
};
object_type = g_type_register_static (GDK_TYPE_PIXBUF_ANIMATION,
"GdkPixbufAniAnim",
&object_info, 0);
}
return object_type;
}
static void
gdk_pixbuf_ani_anim_class_init (GdkPixbufAniAnimClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GdkPixbufAnimationClass *anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gdk_pixbuf_ani_anim_finalize;
anim_class->is_static_image = gdk_pixbuf_ani_anim_is_static_image;
anim_class->get_static_image = gdk_pixbuf_ani_anim_get_static_image;
anim_class->get_size = gdk_pixbuf_ani_anim_get_size;
anim_class->get_iter = gdk_pixbuf_ani_anim_get_iter;
}
static void
gdk_pixbuf_ani_anim_finalize (GObject *object)
{
GdkPixbufAniAnim *ani_anim = GDK_PIXBUF_ANI_ANIM (object);
gint i;
for (i = 0; i < ani_anim->n_pixbufs; i++) {
if (ani_anim->pixbufs[i])
g_object_unref (ani_anim->pixbufs[i]);
}
g_free (ani_anim->pixbufs);
g_free (ani_anim->sequence);
g_free (ani_anim->delay);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static gboolean
gdk_pixbuf_ani_anim_is_static_image (GdkPixbufAnimation *animation)
{
GdkPixbufAniAnim *ani_anim;
ani_anim = GDK_PIXBUF_ANI_ANIM (animation);
return ani_anim->n_frames == 1;
}
static GdkPixbuf*
gdk_pixbuf_ani_anim_get_static_image (GdkPixbufAnimation *animation)
{
GdkPixbufAniAnim *ani_anim;
ani_anim = GDK_PIXBUF_ANI_ANIM (animation);
if (ani_anim->pixbufs == NULL)
return NULL;
else
return ani_anim->pixbufs[0];
}
static void
gdk_pixbuf_ani_anim_get_size (GdkPixbufAnimation *anim,
int *width,
int *height)
{
GdkPixbufAniAnim *ani_anim;
ani_anim = GDK_PIXBUF_ANI_ANIM (anim);
if (width)
*width = ani_anim->width;
if (height)
*height = ani_anim->height;
}
static void
iter_restart (GdkPixbufAniAnimIter *iter)
{
iter->current_frame = 0;
iter->position = 0;
iter->elapsed = 0;
}
static GdkPixbufAnimationIter*
gdk_pixbuf_ani_anim_get_iter (GdkPixbufAnimation *anim,
const GTimeVal *start_time)
{
GdkPixbufAniAnimIter *iter;
iter = g_object_new (GDK_TYPE_PIXBUF_ANI_ANIM_ITER, NULL);
iter->ani_anim = GDK_PIXBUF_ANI_ANIM (anim);
g_object_ref (iter->ani_anim);
iter_restart (iter);
iter->start_time = *start_time;
iter->current_time = *start_time;
return GDK_PIXBUF_ANIMATION_ITER (iter);
}
static void gdk_pixbuf_ani_anim_iter_class_init (GdkPixbufAniAnimIterClass *klass);
static void gdk_pixbuf_ani_anim_iter_finalize (GObject *object);
static int gdk_pixbuf_ani_anim_iter_get_delay_time (GdkPixbufAnimationIter *iter);
static GdkPixbuf* gdk_pixbuf_ani_anim_iter_get_pixbuf (GdkPixbufAnimationIter *iter);
static gboolean gdk_pixbuf_ani_anim_iter_on_currently_loading_frame (GdkPixbufAnimationIter *iter);
static gboolean gdk_pixbuf_ani_anim_iter_advance (GdkPixbufAnimationIter *iter,
const GTimeVal *current_time);
static gpointer iter_parent_class;
GType
gdk_pixbuf_ani_anim_iter_get_type (void)
{
static GType object_type = 0;
if (!object_type) {
static const GTypeInfo object_info = {
sizeof (GdkPixbufAniAnimIterClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gdk_pixbuf_ani_anim_iter_class_init,
NULL, /* class_finalize */
NULL, /* class_data */
sizeof (GdkPixbufAniAnimIter),
0, /* n_preallocs */
(GInstanceInitFunc) NULL,
};
object_type = g_type_register_static (GDK_TYPE_PIXBUF_ANIMATION_ITER,
"GdkPixbufAniAnimIter",
&object_info, 0);
}
return object_type;
}
static void
gdk_pixbuf_ani_anim_iter_class_init (GdkPixbufAniAnimIterClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GdkPixbufAnimationIterClass *anim_iter_class =
GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
iter_parent_class = g_type_class_peek_parent (klass);
object_class->finalize = gdk_pixbuf_ani_anim_iter_finalize;
anim_iter_class->get_delay_time = gdk_pixbuf_ani_anim_iter_get_delay_time;
anim_iter_class->get_pixbuf = gdk_pixbuf_ani_anim_iter_get_pixbuf;
anim_iter_class->on_currently_loading_frame = gdk_pixbuf_ani_anim_iter_on_currently_loading_frame;
anim_iter_class->advance = gdk_pixbuf_ani_anim_iter_advance;
}
static void
gdk_pixbuf_ani_anim_iter_finalize (GObject *object)
{
GdkPixbufAniAnimIter *iter = GDK_PIXBUF_ANI_ANIM_ITER (object);
g_object_unref (iter->ani_anim);
G_OBJECT_CLASS (iter_parent_class)->finalize (object);
}
static gboolean
gdk_pixbuf_ani_anim_iter_advance (GdkPixbufAnimationIter *anim_iter,
const GTimeVal *current_time)
{
GdkPixbufAniAnimIter *iter;
gint elapsed;
gint tmp;
gint old;
iter = GDK_PIXBUF_ANI_ANIM_ITER (anim_iter);
iter->current_time = *current_time;
/* We use milliseconds for all times */
elapsed =
(((iter->current_time.tv_sec - iter->start_time.tv_sec) * G_USEC_PER_SEC +
iter->current_time.tv_usec - iter->start_time.tv_usec)) / 1000;
if (elapsed < 0) {
/* Try to compensate; probably the system clock
* was set backwards
*/
iter->start_time = iter->current_time;
elapsed = 0;
}
g_assert (iter->ani_anim->total_time > 0);
/* See how many times we've already played the full animation,
* and subtract time for that.
*/
elapsed = elapsed % iter->ani_anim->total_time;
iter->position = elapsed;
/* Now move to the proper frame */
iter->elapsed = 0;
for (tmp = 0; tmp < iter->ani_anim->n_frames; tmp++) {
if (iter->position >= iter->elapsed &&
iter->position < (iter->elapsed + iter->ani_anim->delay[tmp]))
break;
iter->elapsed += iter->ani_anim->delay[tmp];
}
old = iter->current_frame;
iter->current_frame = tmp;
return iter->current_frame != old;
}
int
gdk_pixbuf_ani_anim_iter_get_delay_time (GdkPixbufAnimationIter *anim_iter)
{
GdkPixbufAniAnimIter *iter;
iter = GDK_PIXBUF_ANI_ANIM_ITER (anim_iter);
return iter->ani_anim->delay[iter->current_frame] - (iter->position - iter->elapsed);
}
GdkPixbuf*
gdk_pixbuf_ani_anim_iter_get_pixbuf (GdkPixbufAnimationIter *anim_iter)
{
GdkPixbufAniAnimIter *iter;
gint frame;
iter = GDK_PIXBUF_ANI_ANIM_ITER (anim_iter);
frame = iter->ani_anim->sequence[iter->current_frame];
/* this is necessary if the animation is displayed while loading */
while (frame > 0 && !iter->ani_anim->pixbufs[frame])
frame--;
return iter->ani_anim->pixbufs[frame];
}
static gboolean
gdk_pixbuf_ani_anim_iter_on_currently_loading_frame (GdkPixbufAnimationIter *anim_iter)
{
GdkPixbufAniAnimIter *iter;
gint frame;
iter = GDK_PIXBUF_ANI_ANIM_ITER (anim_iter);
if (iter->current_frame >= iter->ani_anim->n_frames - 1)
return TRUE;
frame = iter->ani_anim->sequence[iter->current_frame + 1];
if (!iter->ani_anim->pixbufs[frame])
return TRUE;
return FALSE;
}

View File

@ -0,0 +1,112 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* GdkPixbuf library - ANI loader declarations
*
* Copyright (C) 2002 The Free Software Foundation
*
* Author: Matthias Clasen <maclas@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef GDK_PIXBUF_ANI_ANIMATION_H
#define GDK_PIXBUF_ANI_ANIMATION_H
#include "gdk-pixbuf-private.h"
typedef struct _GdkPixbufAniAnim GdkPixbufAniAnim;
typedef struct _GdkPixbufAniAnimClass GdkPixbufAniAnimClass;
#define GDK_TYPE_PIXBUF_ANI_ANIM (gdk_pixbuf_ani_anim_get_type ())
#define GDK_PIXBUF_ANI_ANIM(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF_ANI_ANIM, GdkPixbufAniAnim))
#define GDK_IS_PIXBUF_ANI_ANIM(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF_ANI_ANIM))
#define GDK_PIXBUF_ANI_ANIM_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF_ANI_ANIM, GdkPixbufAniAnimClass))
#define GDK_IS_PIXBUF_ANI_ANIM_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF_ANI_ANIM))
#define GDK_PIXBUF_ANI_ANIM_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF_ANI_ANIM, GdkPixbufAniAnimClass))
/* Private part of the GdkPixbufAniAnim structure */
struct _GdkPixbufAniAnim {
GdkPixbufAnimation parent_instance;
/* Total length of animation */
int total_time;
/* Number of frames */
int n_frames;
/* Number of pixbufs */
int n_pixbufs;
GdkPixbuf **pixbufs;
/* Maps frame number to pixbuf */
int *sequence;
/* The duration of each frame, in milliseconds */
int *delay;
/* bounding box size */
int width, height;
};
struct _GdkPixbufAniAnimClass {
GdkPixbufAnimationClass parent_class;
};
GType gdk_pixbuf_ani_anim_get_type (void) G_GNUC_CONST;
typedef struct _GdkPixbufAniAnimIter GdkPixbufAniAnimIter;
typedef struct _GdkPixbufAniAnimIterClass GdkPixbufAniAnimIterClass;
#define GDK_TYPE_PIXBUF_ANI_ANIM_ITER (gdk_pixbuf_ani_anim_iter_get_type ())
#define GDK_PIXBUF_ANI_ANIM_ITER(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GDK_TYPE_PIXBUF_ANI_ANIM_ITER, GdkPixbufAniAnimIter))
#define GDK_IS_PIXBUF_ANI_ANIM_ITER(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GDK_TYPE_PIXBUF_ANI_ANIM_ITER))
#define GDK_PIXBUF_ANI_ANIM_ITER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GDK_TYPE_PIXBUF_ANI_ANIM_ITER, GdkPixbufAniAnimIterClass))
#define GDK_IS_PIXBUF_ANI_ANIM_ITER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GDK_TYPE_PIXBUF_ANI_ANIM_ITER))
#define GDK_PIXBUF_ANI_ANIM_ITER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GDK_TYPE_PIXBUF_ANI_ANIM_ITER, GdkPixbufAniAnimIterClass))
struct _GdkPixbufAniAnimIter {
GdkPixbufAnimationIter parent_instance;
GdkPixbufAniAnim *ani_anim;
GTimeVal start_time;
GTimeVal current_time;
/* Time in milliseconds into this run of the animation */
gint position;
/* Index of the current frame */
gint current_frame;
/* Time in milliseconds from the start of the animation till the
begin of the current frame */
gint elapsed;
};
struct _GdkPixbufAniAnimIterClass {
GdkPixbufAnimationIterClass parent_class;
};
GType gdk_pixbuf_ani_anim_iter_get_type (void) G_GNUC_CONST;
#endif

656
gdk-pixbuf/io-ani.c Normal file
View File

@ -0,0 +1,656 @@
/* -*- mode: C; c-file-style: "linux" -*- */
/* GdkPixbuf library - ANI image loader
*
* Copyright (C) 2002 The Free Software Foundation
*
* Authors: Matthias Clasen <maclas@gmx.de>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#undef DEBUG_ANI
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include "gdk-pixbuf-private.h"
#include "gdk-pixbuf-io.h"
#include "io-ani-animation.h"
static int
lsb_32 (guchar *src)
{
return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
}
#define MAKE_TAG(a,b,c,d) ( (guint32)d << 24 | \
(guint32)c << 16 | \
(guint32)b << 8 | \
(guint32)a )
#define TAG_RIFF MAKE_TAG('R','I','F','F')
#define TAG_ACON MAKE_TAG('A','C','O','N')
#define TAG_LIST MAKE_TAG('L','I','S','T')
#define TAG_INAM MAKE_TAG('I','N','A','M')
#define TAG_IART MAKE_TAG('I','A','R','T')
#define TAG_anih MAKE_TAG('a','n','i','h')
#define TAG_seq MAKE_TAG('s','e','q',' ')
#define TAG_rate MAKE_TAG('r','a','t','e')
#define TAG_icon MAKE_TAG('i','c','o','n')
typedef struct _AniLoaderContext
{
guint32 cp;
guchar *buffer;
guchar *byte;
guint n_bytes;
guint buffer_size;
ModulePreparedNotifyFunc prepared_func;
ModuleUpdatedNotifyFunc updated_func;
gpointer user_data;
guint32 data_size;
guint32 HeaderSize;
guint32 NumFrames;
guint32 NumSteps;
guint32 Width;
guint32 Height;
guint32 BitCount;
guint32 NumPlanes;
guint32 DisplayRate;
guint32 Flags;
guint32 chunk_id;
guint32 chunk_size;
gchar *title;
gchar *author;
GdkPixbufAniAnim *animation;
GdkPixbufLoader *loader;
int pos;
} AniLoaderContext;
#define BYTES_LEFT(context) \
((context)->n_bytes - ((context)->byte - (context)->buffer))
static void
read_int8 (AniLoaderContext *context,
guchar *data,
int count)
{
int total = MIN (count, BYTES_LEFT (context));
memcpy (data, context->byte, total);
context->byte += total;
context->cp += total;
}
static guint32
read_int32 (AniLoaderContext *context)
{
guint32 result;
read_int8 (context, (guchar*) &result, 4);
return lsb_32 ((guchar *) &result);
}
static void
context_free (AniLoaderContext *context)
{
if (!context)
return;
if (context->loader)
{
gdk_pixbuf_loader_close (context->loader, NULL);
g_object_unref (context->loader);
}
if (context->animation)
g_object_unref (context->animation);
g_free (context->buffer);
g_free (context->title);
g_free (context->author);
g_free (context);
}
static void
prepared_callback (GdkPixbufLoader *loader,
gpointer data)
{
AniLoaderContext *context = (AniLoaderContext*)data;
#ifdef DEBUG_ANI
g_print ("%d pixbuf prepared\n", context->pos);
#endif
GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (!pixbuf)
return;
if (gdk_pixbuf_get_width (pixbuf) > context->animation->width)
context->animation->width = gdk_pixbuf_get_width (pixbuf);
if (gdk_pixbuf_get_height (pixbuf) > context->animation->height)
context->animation->height = gdk_pixbuf_get_height (pixbuf);
if (context->title != NULL)
gdk_pixbuf_set_option (pixbuf, "Title", context->title);
if (context->author != NULL)
gdk_pixbuf_set_option (pixbuf, "Author", context->author);
g_object_ref (pixbuf);
context->animation->pixbufs[context->pos] = pixbuf;
if (context->pos == 0)
{
if (context->prepared_func)
(* context->prepared_func) (pixbuf,
GDK_PIXBUF_ANIMATION (context->animation),
context->user_data);
}
else {
/* FIXME - this is necessary for nice display of loading
animations because GtkImage ignores
gdk_pixbuf_animation_iter_on_currently_loading_frame()
and always exposes the full frame */
GdkPixbuf *last = context->animation->pixbufs[context->pos - 1];
gint width = MIN (gdk_pixbuf_get_width (last), gdk_pixbuf_get_width (pixbuf));
gint height = MIN (gdk_pixbuf_get_height (last), gdk_pixbuf_get_height (pixbuf));
gdk_pixbuf_copy_area (last, 0, 0, width, height, pixbuf, 0, 0);
}
context->pos++;
}
static void
updated_callback (GdkPixbufLoader* loader,
gint x, gint y, gint width, gint height,
gpointer data)
{
AniLoaderContext *context = (AniLoaderContext*)data;
GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);
if (context->updated_func)
(* context->updated_func) (pixbuf,
x, y, width, height,
context->user_data);
}
static gboolean
ani_load_chunk (AniLoaderContext *context, GError **error)
{
int i;
if (context->chunk_id == 0x0) {
if (BYTES_LEFT (context) < 8)
return FALSE;
context->chunk_id = read_int32 (context);
context->chunk_size = read_int32 (context);
/* Pad it up to word length */
if (context->chunk_size % 2)
context->chunk_size += (2 - (context->chunk_size % 2));
}
while (context->chunk_id == TAG_LIST)
{
if (BYTES_LEFT (context) < 12)
return FALSE;
read_int32 (context);
context->chunk_id = read_int32 (context);
context->chunk_size = read_int32 (context);
/* Pad it up to word length */
if (context->chunk_size % 2)
context->chunk_size += (2 - (context->chunk_size % 2));
}
if (context->chunk_id == TAG_icon)
{
GError *loader_error = NULL;
guchar *data;
guint32 towrite;
if (context->loader == NULL)
{
if (context->pos >= context->NumFrames)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Unexpected icon chunk in animation"));
return FALSE;
}
#ifdef DEBUG_ANI
g_print ("opening loader\n");
#endif
context->loader = gdk_pixbuf_loader_new_with_type ("ico", &loader_error);
if (loader_error)
{
g_propagate_error (error, loader_error);
return FALSE;
}
g_signal_connect (context->loader, "area_prepared",
G_CALLBACK (prepared_callback), context);
g_signal_connect (context->loader, "area_updated",
G_CALLBACK (updated_callback), context);
}
towrite = MIN (context->chunk_size, BYTES_LEFT (context));
data = context->byte;
context->byte += towrite;
context->cp += towrite;
#ifdef DEBUG_ANI
g_print ("miss %d, get %d, leftover %d\n", context->chunk_size, towrite, BYTES_LEFT (context));
#endif
context->chunk_size -= towrite;
if (!gdk_pixbuf_loader_write (context->loader, data, towrite, &loader_error))
{
g_propagate_error (error, loader_error);
gdk_pixbuf_loader_close (context->loader, NULL);
g_object_unref (context->loader);
context->loader = NULL;
return FALSE;
}
if (context->chunk_size == 0)
{
#ifdef DEBUG_ANI
g_print ("closing loader\n");
#endif
if (!gdk_pixbuf_loader_close (context->loader, &loader_error))
{
g_propagate_error (error, loader_error);
g_object_unref (context->loader);
context->loader = NULL;
return FALSE;
}
g_object_unref (context->loader);
context->loader = NULL;
context->chunk_id = 0x0;
}
return BYTES_LEFT (context) > 0;
}
if (BYTES_LEFT (context) < context->chunk_size)
return FALSE;
if (context->chunk_id == TAG_anih)
{
context->HeaderSize = read_int32 (context);
context->NumFrames = read_int32 (context);
context->NumSteps = read_int32 (context);
context->Width = read_int32 (context);
context->Height = read_int32 (context);
context->BitCount = read_int32 (context);
context->NumPlanes = read_int32 (context);
context->DisplayRate = read_int32 (context);
context->Flags = read_int32 (context);
#ifdef DEBUG_ANI
g_print ("HeaderSize \t%" G_GUINT32_FORMAT
"\nNumFrames \t%" G_GUINT32_FORMAT
"\nNumSteps \t%" G_GUINT32_FORMAT
"\nWidth \t%" G_GUINT32_FORMAT
"\nHeight \t%" G_GUINT32_FORMAT
"\nBitCount \t%" G_GUINT32_FORMAT
"\nNumPlanes \t%" G_GUINT32_FORMAT
"\nDisplayRate \t%" G_GUINT32_FORMAT
"\nSequenceFlag \t%d"
"\nIconFlag \t%d"
"\n",
context->HeaderSize, context->NumFrames,
context->NumSteps, context->Width,
context->Height, context->BitCount,
context->NumPlanes, context->DisplayRate,
(context->Flags & 0x2) != 0,
(context->Flags & 0x1) != 0);
#endif
if (!context->Flags & 0x2)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Unsupported animation type"));
return FALSE;
}
if (context->NumFrames == 0 ||
context->NumFrames >= 1024 ||
context->NumSteps == 0 ||
context->NumSteps >= 1024)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Invalid header in animation"));
return FALSE;
}
context->animation = g_object_new (GDK_TYPE_PIXBUF_ANI_ANIM, NULL);
if (!context->animation)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Not enough memory to load animation"));
return FALSE;
}
context->animation->n_pixbufs = context->NumFrames;
context->animation->n_frames = context->NumSteps;
context->animation->total_time = context->NumSteps * (context->DisplayRate * 1000 / 60);
context->animation->width = 0;
context->animation->height = 0;
context->animation->pixbufs = (GdkPixbuf**) g_try_malloc (sizeof (GdkPixbuf*) * context->NumFrames);
if (context->animation->pixbufs)
memset (context->animation->pixbufs, 0, sizeof (GdkPixbuf*) * context->NumFrames);
context->animation->delay = (guint32*) g_try_malloc (sizeof (guint32) * context->NumSteps);
context->animation->sequence = (guint32*) g_try_malloc (sizeof (guint32) * context->NumSteps);
if (!context->animation->pixbufs ||
!context->animation->delay ||
!context->animation->sequence)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Not enough memory to load animation"));
return FALSE;
}
for (i = 0; i < context->NumSteps; i++)
{
/* default values if the corresponding chunks are absent */
context->animation->delay[i] = context->DisplayRate * 1000 / 60;
context->animation->sequence[i] = MIN (i, context->NumFrames - 1);
}
}
else if (context->chunk_id == TAG_rate)
{
if (context->chunk_size != 4 * context->NumSteps)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Malformed chunk in animation"));
return FALSE;
}
context->animation->total_time = 0;
for (i = 0; i < context->NumSteps; i++)
{
context->animation->delay[i] = read_int32 (context) * 1000 / 60;
context->animation->total_time += context->animation->delay[i];
}
}
else if (context->chunk_id == TAG_seq)
{
if (context->chunk_size != 4 * context->NumSteps)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Malformed chunk in animation"));
return FALSE;
}
for (i = 0; i < context->NumSteps; i++)
{
context->animation->sequence[i] = read_int32 (context);
if (context->animation->sequence[i] >= context->NumFrames)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Malformed chunk in animation"));
return FALSE;
}
}
}
else if (context->chunk_id == TAG_INAM)
{
context->title = g_try_malloc (context->chunk_size + 1);
if (!context->title)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Not enough memory to load animation"));
return FALSE;
}
context->title[context->chunk_size] = 0;
read_int8 (context, context->title, context->chunk_size);
#ifdef DEBUG_ANI
g_print ("INAM %s\n", context->title);
#endif
for (i = 0; i < context->pos; i++)
gdk_pixbuf_set_option (context->animation->pixbufs[i], "Title", context->title);
}
else if (context->chunk_id == TAG_IART)
{
context->author = g_try_malloc (context->chunk_size + 1);
if (!context->author)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Not enough memory to load animation"));
return FALSE;
}
context->author[context->chunk_size] = 0;
read_int8 (context, context->author, context->chunk_size);
#ifdef DEBUG_ANI
g_print ("IART %s\n", context->author);
#endif
for (i = 0; i < context->pos; i++)
gdk_pixbuf_set_option (context->animation->pixbufs[i], "Author", context->author);
}
#ifdef DEBUG_ANI
{
gint32 dummy = lsb_32 ((guchar *)&context->chunk_id);
g_print ("Loaded chunk with ID '%c%c%c%c' and length %" G_GUINT32_FORMAT "\n",
((char*)&dummy)[0], ((char*)&dummy)[1],
((char*)&dummy)[2], ((char*)&dummy)[3],
context->chunk_size);
}
#endif
context->chunk_id = 0x0;
return TRUE;
}
static gboolean
gdk_pixbuf__ani_image_load_increment (gpointer data,
const guchar *buf, guint size,
GError **error)
{
AniLoaderContext *context = (AniLoaderContext *)data;
if (context->n_bytes + size >= context->buffer_size) {
int drop = context->byte - context->buffer;
memmove (context->buffer, context->byte, context->n_bytes - drop);
context->n_bytes -= drop;
context->byte = context->buffer;
if (context->n_bytes + size >= context->buffer_size) {
guchar *tmp;
context->buffer_size = MAX (context->n_bytes + size, context->buffer_size + 4096);
#ifdef DEBUG_ANI
g_print ("growing buffer to %" G_GUINT32_FORMAT "\n", context->buffer_size);
#endif
tmp = g_try_realloc (context->buffer, context->buffer_size);
if (!tmp)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Not enough memory to load animation"));
return FALSE;
}
context->byte = context->buffer = tmp;
}
}
memcpy (context->buffer + context->n_bytes, buf, size);
context->n_bytes += size;
if (context->data_size == 0)
{
guint32 riff_id, chunk_id;
if (BYTES_LEFT (context) < 12)
return TRUE;
riff_id = read_int32 (context);
context->data_size = read_int32 (context);
chunk_id = read_int32 (context);
if (riff_id != TAG_RIFF ||
context->data_size == 0 ||
chunk_id != TAG_ACON)
{
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
_("Invalid header in animation"));
return FALSE;
}
}
if (context->cp < context->data_size + 8)
{
GError *chunk_error = NULL;
while (ani_load_chunk (context, &chunk_error)) ;
if (chunk_error)
{
g_propagate_error (error, chunk_error);
return FALSE;
}
}
return TRUE;
}
static gpointer
gdk_pixbuf__ani_image_begin_load (ModuleSizeFunc size_func,
ModulePreparedNotifyFunc prepared_func,
ModuleUpdatedNotifyFunc updated_func,
gpointer user_data,
GError **error)
{
AniLoaderContext *context;
context = g_new0 (AniLoaderContext, 1);
context->prepared_func = prepared_func;
context->updated_func = updated_func;
context->user_data = user_data;
context->pos = 0;
context->buffer_size = 4096;
context->buffer = g_try_malloc (context->buffer_size);
if (!context->buffer)
{
context_free (context);
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
_("Not enough memory to load animation"));
return NULL;
}
context->byte = context->buffer;
context->n_bytes = 0;
return (gpointer) context;
}
static gboolean
gdk_pixbuf__ani_image_stop_load (gpointer data,
GError **error)
{
AniLoaderContext *context = (AniLoaderContext *) data;
g_return_val_if_fail (context != NULL, TRUE);
context_free (context);
return TRUE;
}
static void
prepared_notify (GdkPixbuf *pixbuf,
GdkPixbufAnimation *anim,
gpointer user_data)
{
if (anim != NULL)
g_object_ref (anim);
*((GdkPixbufAnimation **)user_data) = anim;
}
static GdkPixbufAnimation *
gdk_pixbuf__ani_image_load_animation (FILE *f, GError **error)
{
guchar buffer[4096];
size_t length;
GdkPixbufAnimation *anim = NULL;
gpointer context;
context = gdk_pixbuf__ani_image_begin_load (NULL, prepared_notify,
NULL, &anim, error);
if (!context)
return NULL;
while (!feof (f)) {
length = fread (buffer, 1, sizeof (buffer), f);
if (length > 0)
if (!gdk_pixbuf__ani_image_load_increment (context, buffer, length, error)) {
gdk_pixbuf__ani_image_stop_load (context, NULL);
if (anim != NULL)
g_object_unref (anim);
return NULL;
}
}
if (!gdk_pixbuf__ani_image_stop_load (context, error)) {
if (anim != NULL)
g_object_unref (anim);
return NULL;
}
return anim;
}
void
gdk_pixbuf__ani_fill_vtable (GdkPixbufModule *module)
{
module->load_animation = gdk_pixbuf__ani_image_load_animation;
module->begin_load = gdk_pixbuf__ani_image_begin_load;
module->stop_load = gdk_pixbuf__ani_image_stop_load;
module->load_increment = gdk_pixbuf__ani_image_load_increment;
}

View File

@ -0,0 +1,2 @@
EXPORTS
gdk_pixbuf__ani_fill_vtable