-
Notifications
You must be signed in to change notification settings - Fork 25
/
io-webp-anim.c
160 lines (135 loc) · 4.27 KB
/
io-webp-anim.c
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
/* GdkPixbuf library - WebP Image Loader
*
* SPDX-License-Identifier: LGPL-2.0-or-later
* Copyright (C) 2021 Alan Hawrelak
* Copyright (C) 2022 Alberto Ruiz
*
* Authors: Alan Hawrelak <[email protected]>
* Alberto Ruiz <[email protected]>
*/
#include "io-webp-anim.h"
#include <webp/decode.h>
#include <webp/encode.h>
typedef struct
{
gint w;
gint h;
} FrameSize;
typedef struct
{
GByteArray *anim_data;
FrameSize frame_size;
gboolean is_static;
GdkPixbuf *cached_static_image;
} GdkWebpAnimationPrivate;
struct _GdkWebpAnimation
{
GdkPixbufAnimation parent_instance;
};
G_DEFINE_TYPE_WITH_PRIVATE (GdkWebpAnimation, gdk_webp_animation, GDK_TYPE_PIXBUF_ANIMATION)
static void
anim_finalize (GObject *self)
{
G_OBJECT_CLASS (gdk_webp_animation_parent_class)->finalize (self);
}
static void
anim_dispose (GObject *self)
{
GdkWebpAnimationPrivate *priv = gdk_webp_animation_get_instance_private (
GDK_WEBP_ANIMATION (self));
if (priv->anim_data)
{
g_byte_array_free (priv->anim_data, TRUE);
priv->anim_data = NULL;
}
g_clear_object (&priv->cached_static_image);
G_OBJECT_CLASS (gdk_webp_animation_parent_class)->dispose (self);
}
static gboolean
is_static_image (GdkPixbufAnimation *self)
{
GdkWebpAnimationPrivate *priv = gdk_webp_animation_get_instance_private (
GDK_WEBP_ANIMATION (self));
return priv->is_static;
}
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
static GdkPixbufAnimationIter *
get_iter (GdkPixbufAnimation *self, const GTimeVal *start_time)
{
GdkWebpAnimationPrivate *priv = gdk_webp_animation_get_instance_private (
GDK_WEBP_ANIMATION (self));
GError *error = NULL;
GdkWebpAnimationIter *iter
= gdk_webp_animation_new_from_buffer_and_time (priv->anim_data, start_time, &error);
if (error)
{
g_warning ("Could not instantiate WebP implementation of "
"GdkPixbufAnimationIter: %s",
error->message);
g_error_free (error);
return NULL;
}
return GDK_PIXBUF_ANIMATION_ITER (iter);
}
G_GNUC_END_IGNORE_DEPRECATIONS
static GdkPixbuf *
get_static_image (GdkPixbufAnimation *self)
{
GdkWebpAnimationPrivate *priv = gdk_webp_animation_get_instance_private (
GDK_WEBP_ANIMATION (self));
if (! priv->cached_static_image)
{
GdkPixbufAnimationIter *iter = get_iter (self, NULL);
priv->cached_static_image = gdk_pixbuf_animation_iter_get_pixbuf (
GDK_PIXBUF_ANIMATION_ITER (iter));
g_object_ref (priv->cached_static_image);
g_object_unref (iter);
}
return priv->cached_static_image;
}
static void
get_size (GdkPixbufAnimation *self, gint *width, gint *height)
{
GdkWebpAnimationPrivate *priv = gdk_webp_animation_get_instance_private (
GDK_WEBP_ANIMATION (self));
if (width)
*width = priv->frame_size.w;
if (height)
*height = priv->frame_size.h;
}
static void
gdk_webp_animation_class_init (GdkWebpAnimationClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GdkPixbufAnimationClass *anim_class = GDK_PIXBUF_ANIMATION_CLASS (klass);
object_class->finalize = anim_finalize;
object_class->dispose = anim_dispose;
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;
}
static void
gdk_webp_animation_init (GdkWebpAnimation *self)
{
GdkWebpAnimationPrivate *priv = gdk_webp_animation_get_instance_private (self);
*priv = (GdkWebpAnimationPrivate){ 0 };
}
GdkWebpAnimation *
gdk_webp_animation_new_from_bytes (GByteArray *data, GError **error)
{
WebPBitstreamFeatures features = (WebPBitstreamFeatures){ 0 };
if (WebPGetFeatures (data->data, data->len, &features) != VP8_STATUS_OK)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
"Could not get WebP feature information from file data");
return NULL;
}
GdkWebpAnimation *anim = GDK_WEBP_ANIMATION (g_object_new (GDK_WEBP_ANIMATION_TYPE, NULL));
GdkWebpAnimationPrivate *priv = gdk_webp_animation_get_instance_private (
GDK_WEBP_ANIMATION (anim));
priv->is_static = features.has_animation == FALSE;
priv->frame_size = (FrameSize){ features.width, features.height };
priv->anim_data = data;
return anim;
}