summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/gdk-pixbuf-scaled-anim.c
blob: 462fac606305dfa7cd54e90d2c8b7ce087860379 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* GdkPixbuf library - Simple transformations of animations
 *
 * Copyright (C) Red Hat, Inc
 *
 * Authors: Matthias Clasen <mclasen@redhat.com>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include <glib.h>

#include "gdk-pixbuf.h"
#include "gdk-pixbuf-io.h"
#include "gdk-pixbuf-scaled-anim.h"


struct _GdkPixbufScaledAnimClass
{
        GdkPixbufAnimationClass parent_class;
};

struct _GdkPixbufScaledAnim
{
 	GdkPixbufAnimation parent_instance;

	GdkPixbufAnimation *anim;
	gdouble xscale;
	gdouble yscale;
	gdouble tscale;

	GdkPixbuf *current;
};

struct _GdkPixbufScaledAnimIterClass
{
        GdkPixbufAnimationClass parent_class;
};

struct _GdkPixbufScaledAnimIter
{
 	GdkPixbufAnimationIter parent_instance;

	GdkPixbufScaledAnim *scaled;
        GdkPixbufAnimationIter *iter;
};

typedef struct _GdkPixbufScaledAnimIter GdkPixbufScaledAnimIter;
typedef struct _GdkPixbufScaledAnimIterClass GdkPixbufScaledAnimIterClass;

GdkPixbufScaledAnim *
_gdk_pixbuf_scaled_anim_new (GdkPixbufAnimation *anim,
                             gdouble             xscale,
                             gdouble             yscale,
                             gdouble             tscale)
{
	GdkPixbufScaledAnim *scaled;

	scaled = g_object_new (GDK_TYPE_PIXBUF_SCALED_ANIM, NULL);

	scaled->anim = g_object_ref (anim);
	scaled->xscale = xscale;
	scaled->yscale = yscale;
	scaled->tscale = tscale;

	return scaled;
}

G_DEFINE_TYPE (GdkPixbufScaledAnim, gdk_pixbuf_scaled_anim, GDK_TYPE_PIXBUF_ANIMATION);

static void
gdk_pixbuf_scaled_anim_init (GdkPixbufScaledAnim *scaled)
{
	scaled->xscale = 1.0;
	scaled->yscale = 1.0;
	scaled->tscale = 1.0;
}

static void
gdk_pixbuf_scaled_anim_finalize (GObject *object)
{
	GdkPixbufScaledAnim *scaled = (GdkPixbufScaledAnim *)object;

	if (scaled->anim) {
		g_object_unref (scaled->anim);
		scaled->anim = NULL;
	}

	if (scaled->current) {
		g_object_unref (scaled->current);
		scaled->current = NULL;
	}

	G_OBJECT_CLASS (gdk_pixbuf_scaled_anim_parent_class)->finalize (object);
}

static gboolean
is_static_image (GdkPixbufAnimation *anim)
{
	GdkPixbufScaledAnim *scaled = (GdkPixbufScaledAnim *)anim;

	return gdk_pixbuf_animation_is_static_image (scaled->anim);
}	

static GdkPixbuf *
get_scaled_pixbuf (GdkPixbufScaledAnim *scaled, 
                   GdkPixbuf           *pixbuf)
{
	GQuark  quark;
	gchar **options;

	if (scaled->current) 
		g_object_unref (scaled->current);

	/* Preserve the options associated with the original pixbuf 
	   (if present), mostly so that client programs can use the
	   "orientation" option (if present) to rotate the image 
	   appropriately. gdk_pixbuf_scale_simple (and most other
           gdk transform operations) does not preserve the attached
           options when returning a new pixbuf. */

	quark = g_quark_from_static_string ("gdk_pixbuf_options");
	options = g_object_get_qdata (G_OBJECT (pixbuf), quark);

	/* Get a new scaled pixbuf */
	scaled->current  = gdk_pixbuf_scale_simple (pixbuf, 
                        MAX((int) ((gdouble) gdk_pixbuf_get_width (pixbuf) * scaled->xscale + .5), 1),
                        MAX((int) ((gdouble) gdk_pixbuf_get_height (pixbuf) * scaled->yscale + .5), 1),
			GDK_INTERP_BILINEAR);

	/* Copy the original pixbuf options to the scaled pixbuf */
        if (options && scaled->current)
	          g_object_set_qdata_full (G_OBJECT (scaled->current), quark, 
                                           g_strdupv (options), (GDestroyNotify) g_strfreev);

	return scaled->current;
}

static GdkPixbuf *
get_static_image (GdkPixbufAnimation *anim)
{
	GdkPixbufScaledAnim *scaled = (GdkPixbufScaledAnim *)anim;
	GdkPixbuf *pixbuf;
	
	pixbuf = gdk_pixbuf_animation_get_static_image (scaled->anim);
	return get_scaled_pixbuf (scaled, pixbuf);
}

static void
get_size (GdkPixbufAnimation *anim,
	  int                *width,
	  int 		     *height)
{
	GdkPixbufScaledAnim *scaled = (GdkPixbufScaledAnim *)anim;

        GDK_PIXBUF_ANIMATION_GET_CLASS (scaled->anim)->get_size (scaled->anim, width, height);
	if (width) 
		*width = (int)(*width * scaled->xscale + .5);
	if (height)
		*height = (int)(*height * scaled->yscale + .5);
}

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static GdkPixbufAnimationIter *
get_iter (GdkPixbufAnimation *anim,
          const GTimeVal     *start_time)
{
	GdkPixbufScaledAnim *scaled = (GdkPixbufScaledAnim *)anim;
	GdkPixbufScaledAnimIter *iter;

	iter = g_object_new (GDK_TYPE_PIXBUF_SCALED_ANIM_ITER, NULL);

	iter->scaled = g_object_ref (scaled);
	iter->iter = gdk_pixbuf_animation_get_iter (scaled->anim, start_time);
	
	return (GdkPixbufAnimationIter*)iter;
}
G_GNUC_END_IGNORE_DEPRECATIONS

static void
gdk_pixbuf_scaled_anim_class_init (GdkPixbufScaledAnimClass *klass)
{
        GObjectClass *object_class;
        GdkPixbufAnimationClass *anim_class;

        object_class = G_OBJECT_CLASS (klass);
        anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
        
        object_class->finalize = gdk_pixbuf_scaled_anim_finalize;
        
        anim_class->is_static_image = is_static_image;
        anim_class->get_static_image = get_static_image;
        anim_class->get_size = get_size;
        anim_class->get_iter = get_iter;
}


G_DEFINE_TYPE (GdkPixbufScaledAnimIter, gdk_pixbuf_scaled_anim_iter, GDK_TYPE_PIXBUF_ANIMATION_ITER);

static void
gdk_pixbuf_scaled_anim_iter_init (GdkPixbufScaledAnimIter *iter)
{
}

static int
get_delay_time (GdkPixbufAnimationIter *iter)
{
	GdkPixbufScaledAnimIter *scaled = (GdkPixbufScaledAnimIter *)iter;
	int delay;

	delay = gdk_pixbuf_animation_iter_get_delay_time (scaled->iter);
	delay = (int)(delay * scaled->scaled->tscale);

	return delay;
}

static GdkPixbuf *
get_pixbuf (GdkPixbufAnimationIter *iter)
{
	GdkPixbufScaledAnimIter *scaled = (GdkPixbufScaledAnimIter *)iter;
	GdkPixbuf *pixbuf;

	pixbuf = gdk_pixbuf_animation_iter_get_pixbuf (scaled->iter);
	return get_scaled_pixbuf (scaled->scaled, pixbuf);
}

static gboolean 
on_currently_loading_frame (GdkPixbufAnimationIter *iter)
{
	GdkPixbufScaledAnimIter *scaled = (GdkPixbufScaledAnimIter *)iter;

	return gdk_pixbuf_animation_iter_on_currently_loading_frame (scaled->iter);
}

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static gboolean
advance (GdkPixbufAnimationIter *iter,
	 const GTimeVal         *current_time)
{
	GdkPixbufScaledAnimIter *scaled = (GdkPixbufScaledAnimIter *)iter;

	return gdk_pixbuf_animation_iter_advance (scaled->iter, current_time);
}
G_GNUC_END_IGNORE_DEPRECATIONS

static void
gdk_pixbuf_scaled_anim_iter_finalize (GObject *object)
{
        GdkPixbufScaledAnimIter *iter = (GdkPixbufScaledAnimIter *)object;
        
	g_object_unref (iter->iter);
   	g_object_unref (iter->scaled);

	G_OBJECT_CLASS (gdk_pixbuf_scaled_anim_iter_parent_class)->finalize (object);
}

static void
gdk_pixbuf_scaled_anim_iter_class_init (GdkPixbufScaledAnimIterClass *klass)
{
        GObjectClass *object_class;
        GdkPixbufAnimationIterClass *anim_iter_class;

        object_class = G_OBJECT_CLASS (klass);
        anim_iter_class = GDK_PIXBUF_ANIMATION_ITER_CLASS (klass);
        
        object_class->finalize = gdk_pixbuf_scaled_anim_iter_finalize;
        
        anim_iter_class->get_delay_time = get_delay_time;
        anim_iter_class->get_pixbuf = get_pixbuf;
        anim_iter_class->on_currently_loading_frame = on_currently_loading_frame;
        anim_iter_class->advance = advance;
}