summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/io-ani-animation.c
blob: b3a236cb85096c821650b98d9d65e2a08ffcb0cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
53
54
55
56
57
58
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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,
                                                      g_intern_static_string ("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,
                                                      g_intern_static_string ("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;
}