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
|
/* GdkPixdata loader
*
* Copyright (c) 2012 Alexander Larsson <alexl@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 "config.h"
#include "gdk-pixbuf-private.h"
#include "gdk-pixdata.h"
G_MODULE_EXPORT void fill_vtable (GdkPixbufModule * module);
G_MODULE_EXPORT void fill_info (GdkPixbufFormat * info);
struct pixdata_context {
GdkPixbufModuleSizeFunc size_func;
GdkPixbufModuleUpdatedFunc updated_func;
GdkPixbufModulePreparedFunc prepared_func;
gpointer user_data;
GString *data;
GdkPixdata pixdata;
gboolean got_header;
gboolean got_pixbuf;
};
static void
free_pixdata_context (struct pixdata_context *context)
{
if (!context)
return;
g_free (context);
}
static gpointer
pixdata_image_begin_load (GdkPixbufModuleSizeFunc size_func,
GdkPixbufModulePreparedFunc prepared_func,
GdkPixbufModuleUpdatedFunc updated_func,
gpointer user_data, GError **error)
{
struct pixdata_context *context;
context = g_new0 (struct pixdata_context, 1);
if (!context)
return NULL;
context->size_func = size_func;
context->updated_func = updated_func;
context->prepared_func = prepared_func;
context->user_data = user_data;
context->data = g_string_new ("");
return context;
}
static gboolean try_load (struct pixdata_context *context, GError **error)
{
GdkPixbuf *pixbuf;
if (context->got_pixbuf)
return TRUE;
if (!gdk_pixdata_deserialize (&context->pixdata,
context->data->len,
(guchar *)context->data->str,
error))
return FALSE;
pixbuf = gdk_pixbuf_from_pixdata (&context->pixdata,
TRUE, error);
if (pixbuf == NULL)
return FALSE;
context->got_pixbuf = TRUE;
if (context->prepared_func)
(* context->prepared_func) (pixbuf,
NULL,
context->user_data);
if (context->updated_func)
(* context->updated_func) (pixbuf, 0, 0, pixbuf->width, pixbuf->height, context->user_data);
return TRUE;
}
static gboolean
pixdata_image_stop_load (gpointer data, GError **error)
{
struct pixdata_context *context = (struct pixdata_context *) data;
gboolean res;
res = try_load (context, error);
g_string_free (context->data, TRUE);
free_pixdata_context (context);
return res;
}
static gboolean
pixdata_image_load_increment (gpointer data, const guchar *buf, guint size, GError **error)
{
struct pixdata_context *context = (struct pixdata_context *) data;
g_string_append_len (context->data, (char *)buf, size);
if (!context->got_header && context->data->len >= GDK_PIXDATA_HEADER_LENGTH)
{
/* This never reads past the header anyway, and we know we have at least
the header size, so we pass it a really large size to avoid any error reporting
due to missing data */
if (!gdk_pixdata_deserialize (&context->pixdata,
G_MAXUINT,
(guchar *)context->data->str,
error))
return FALSE;
context->got_header = TRUE;
if (context->size_func)
{
gint w = context->pixdata.width;
gint h = context->pixdata.height;
(* context->size_func) (&w, &h, context->user_data);
if (w == 0 || h == 0)
{
g_set_error_literal (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
_("Transformed pixbuf has zero width or height."));
return FALSE;
}
}
}
try_load (context, NULL);
return TRUE;
}
/* Always included */
#define MODULE_ENTRY(function) void _gdk_pixbuf__pixdata_ ## function
MODULE_ENTRY (fill_vtable) (GdkPixbufModule * module)
{
module->begin_load = pixdata_image_begin_load;
module->stop_load = pixdata_image_stop_load;
module->load_increment = pixdata_image_load_increment;
}
MODULE_ENTRY (fill_info) (GdkPixbufFormat * info)
{
static const GdkPixbufModulePattern signature[] = {
{ "GdkP", NULL, 100 }, /* file begins with 'GdkP' at offset 0 */
{ NULL, NULL, 0 }
};
static const gchar *mime_types[] = {
"image/x-gdkpixdata",
NULL
};
static const gchar *extensions[] = {
"gdkp",
NULL
};
info->name = "GdkPixdata";
info->signature = (GdkPixbufModulePattern *) signature;
info->description = N_("The GdkPixdata format");
info->mime_types = (gchar **) mime_types;
info->extensions = (gchar **) extensions;
info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
info->license = "LGPL";
info->disabled = FALSE;
}
|