-
Notifications
You must be signed in to change notification settings - Fork 25
/
io-webp.c
510 lines (436 loc) · 15.5 KB
/
io-webp.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* GdkPixbuf library - WebP Image Loader
*
* SPDX-License-Identifier: LGPL-2.0-or-later
* Copyright (C) 2022 Alberto Ruiz
* Copyright (C) 2011 David Mazary
* Copyright (C) 2014 Přemysl Janouch
*
* Authors: Alberto Ruiz <[email protected]>
* David Mazary <[email protected]>
* Přemysl Janouch <[email protected]>
*/
#include <string.h>
#include <webp/decode.h>
#include <webp/encode.h>
#include <webp/mux.h>
#define GDK_PIXBUF_ENABLE_BACKEND
#include <gdk-pixbuf/gdk-pixbuf.h>
#undef GDK_PIXBUF_ENABLE_BACKEND
#include "io-webp-anim.h"
typedef struct
{
GdkPixbufModuleSizeFunc size_func;
GdkPixbufModuleUpdatedFunc update_func;
GdkPixbufModulePreparedFunc prepare_func;
gpointer user_data;
gboolean got_header;
gboolean is_animation;
gboolean has_alpha;
GByteArray *buffer;
gint width;
gint height;
} WebPContext;
static gpointer
begin_load (GdkPixbufModuleSizeFunc size_func,
GdkPixbufModulePreparedFunc prepare_func,
GdkPixbufModuleUpdatedFunc update_func,
gpointer user_data,
GError **error)
{
WebPContext *context = g_new0 (WebPContext, 1);
context->size_func = size_func;
context->prepare_func = prepare_func;
context->update_func = update_func;
context->user_data = user_data;
context->got_header = FALSE;
context->is_animation = FALSE;
context->has_alpha = FALSE;
context->buffer = NULL;
context->width = 0;
context->height = 0;
return context;
}
// Create an empty pixbuf from the size function and target canvas size
static void
init_dec_config (WebPDecoderConfig *config, GdkPixbuf *pixbuf)
{
guint pblen = 0;
WebPInitDecoderConfig (config);
config->options.use_scaling = TRUE;
config->options.scaled_width = gdk_pixbuf_get_width (pixbuf);
config->options.scaled_height = gdk_pixbuf_get_height (pixbuf);
config->output.is_external_memory = TRUE;
config->output.colorspace = gdk_pixbuf_get_has_alpha (pixbuf) ? MODE_RGBA : MODE_RGB;
config->output.u.RGBA.rgba = gdk_pixbuf_get_pixels_with_length (pixbuf, &pblen);
config->output.u.RGBA.size = (gsize) pblen;
config->output.u.RGBA.stride = gdk_pixbuf_get_rowstride (pixbuf);
}
static gboolean
load_increment (gpointer data, const guchar *buf, guint size, GError **error)
{
WebPContext *context = (WebPContext *) data;
if (context->got_header == FALSE)
{
if (WebPGetInfo (buf, size, &context->width, &context->height) == FALSE)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
"Could not get WebP header information");
return FALSE;
}
if (context->size_func)
{
context->size_func (&context->width, &context->height, context->user_data);
// Check if this is a gdk_pixbuf_get_info call
if (context->width == 0 || context->height == 0)
{
return TRUE;
}
}
WebPBitstreamFeatures features;
if (WebPGetFeatures (buf, size, &features) != VP8_STATUS_OK)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
"Could not get WebP image feature information");
return FALSE;
}
context->got_header = TRUE;
context->has_alpha = features.has_alpha;
context->is_animation = features.has_animation;
context->buffer = g_byte_array_new ();
}
if (context->buffer)
g_byte_array_append (context->buffer, buf, size);
return TRUE;
}
static gboolean
stop_load (gpointer data, GError **error)
{
WebPContext *context = (WebPContext *) data;
gboolean ret = FALSE;
if (context->got_header && context->is_animation)
{
GdkWebpAnimation *anim = gdk_webp_animation_new_from_bytes (context->buffer, error);
context->buffer = NULL;
GdkPixbufAnimationIter *iter
= gdk_pixbuf_animation_get_iter (GDK_PIXBUF_ANIMATION (anim), NULL);
GdkPixbuf *pb = gdk_pixbuf_animation_iter_get_pixbuf (iter);
if (pb == NULL)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
"Could not get Pixbuf from WebP animation iter");
}
else
{
if (context->prepare_func)
context->prepare_func (pb, GDK_PIXBUF_ANIMATION (anim), context->user_data);
if (context->update_func)
context->update_func (pb, 0, 0, context->width, context->height,
context->user_data);
ret = TRUE;
}
g_clear_object (&iter);
g_clear_object (&anim);
}
else if (context->got_header && context->buffer)
{
gchar *icc_data = NULL;
WebPData wp_data = { .bytes = context->buffer->data,
.size = context->buffer->len };
WebPMux *mux = WebPMuxCreate (&wp_data, FALSE);
if (mux)
{
WebPData icc_profile = { 0 };
if (WebPMuxGetChunk (mux, "ICCP", &icc_profile) == WEBP_MUX_OK
&& icc_profile.bytes)
icc_data = g_base64_encode (icc_profile.bytes, icc_profile.size);
g_clear_pointer (&mux, WebPMuxDelete);
}
GdkPixbuf *pb = gdk_pixbuf_new (GDK_COLORSPACE_RGB, context->has_alpha, 8,
context->width, context->height);
if (! pb)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
"Could not allocate GdkPixbuf");
return FALSE;
}
if (context->prepare_func)
context->prepare_func (pb, NULL, context->user_data);
if (icc_data)
{
gdk_pixbuf_set_option (pb, "icc-profile", icc_data);
g_clear_pointer (&icc_data, g_free);
}
WebPDecoderConfig config;
init_dec_config (&config, pb);
VP8StatusCode status = WebPDecode (context->buffer->data,
context->buffer->len, &config);
if (status == VP8_STATUS_OK)
{
if (context->update_func)
context->update_func (pb, 0, 0, context->width, context->height,
context->user_data);
ret = TRUE;
}
else
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
"WebP decoder failed with VP8 status code: %d", status);
}
g_clear_object (&pb);
}
if (context->buffer)
{
g_byte_array_free (context->buffer, TRUE);
context->buffer = NULL;
}
g_clear_pointer (&context, g_free);
return ret;
}
/* pixbuf save logic */
/* Encoder write callback to accumulate output data in a FILE stream */
static int
write_file (const uint8_t *data, size_t data_size, const WebPPicture *const pic)
{
FILE *const out = (FILE *) pic->custom_ptr;
return data_size == fwrite (data, sizeof (guchar), data_size, out) ? TRUE : FALSE;
}
/* Encoder write callback to accumulate output data in a GByteArray */
static int
write_array (const uint8_t *data, size_t data_size, const WebPPicture *const pic)
{
GByteArray *arr = pic->custom_ptr;
g_byte_array_append (arr, data, data_size);
return TRUE;
}
static gboolean
is_save_option_supported (const gchar *option_key)
{
char *options[4] = { "quality", "preset", "icc-profile", NULL };
for (char **o = options; *o; o++)
{
if (g_strcmp0 (*o, option_key) == 0)
return TRUE;
}
return FALSE;
}
/* Creates a new image data buffer with the ICC profile data in it */
WebPData
add_icc_data (WebPData *image_data, WebPData *icc_data, GError **error)
{
WebPMux *mux = WebPMuxCreate (image_data, FALSE);
if (mux == NULL)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
"Could not create WebPMux instance");
return (WebPData){ 0 };
}
if (WebPMuxSetChunk (mux, "ICCP", icc_data, FALSE) != WEBP_MUX_OK)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
"Could not set ICC profile data WebP using Muxer");
WebPMuxDelete (mux);
return (WebPData){ 0 };
}
WebPData output = { 0 };
if (WebPMuxAssemble (mux, &output) != WEBP_MUX_OK)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
"Could not assemble WebP data using Muxer");
WebPMuxDelete (mux);
return (WebPData){ 0 };
}
WebPMuxDelete (mux);
return output;
}
static gboolean
save_webp (GdkPixbuf *pixbuf,
gchar **keys,
gchar **values,
GError **error,
GdkPixbufSaveFunc save_func,
FILE *f,
gpointer *user_data)
{
WebPPicture picture;
WebPConfig config;
uint8_t *icc_data = NULL;
gsize icc_data_len = 0;
g_clear_error (error);
if (! WebPPictureInit (&picture) || ! WebPConfigInit (&config))
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_BAD_OPTION,
"WebP encoder version mismatch.");
return FALSE;
}
if (keys && *keys && values && *values)
{
gchar **kiter = keys;
gchar **viter = values;
while (*kiter)
{
if (g_strcmp0 (*kiter, "quality") == 0)
{
guint64 quality;
if (! g_ascii_string_to_unsigned (*viter, 10, 0, 100, &quality, error))
return FALSE;
config.quality = (float) quality;
}
else if (g_strcmp0 (*kiter, "icc-profile") == 0)
{
icc_data = g_base64_decode (*viter, &icc_data_len);
}
else if (g_strcmp0 (*kiter, "preset") == 0)
{
gchar *PRESET_KEYS[7] = { "default", "picture", "photo",
"drawing", "icon", "text",
NULL };
WebPPreset PRESET_VALS[7] = { WEBP_PRESET_DEFAULT,
WEBP_PRESET_PICTURE,
WEBP_PRESET_PHOTO,
WEBP_PRESET_DRAWING,
WEBP_PRESET_ICON,
WEBP_PRESET_TEXT,
0 };
gboolean preset_set = FALSE;
for (gchar **key = PRESET_KEYS; *key; key++)
{
if (g_strcmp0 (*viter, *key) != 0)
continue;
if (WebPConfigPreset (&config, PRESET_VALS[key - PRESET_KEYS],
config.quality)
== 0)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED,
"Could not initialize decoder with "
"preset.");
return FALSE;
}
preset_set = TRUE;
break;
}
if (! preset_set)
{
g_warning ("Invalid WebP preset '%s', ignoring.", *viter);
}
}
++kiter;
++viter;
}
}
if (WebPValidateConfig (&config) != 1)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_BAD_OPTION,
"Invalid WebP encoding configuration");
return FALSE;
}
picture.width = gdk_pixbuf_get_width (pixbuf);
picture.height = gdk_pixbuf_get_height (pixbuf);
gint rowstride = gdk_pixbuf_get_rowstride (pixbuf);
if ((gdk_pixbuf_get_has_alpha (pixbuf)
? WebPPictureImportRGBA (&picture, gdk_pixbuf_get_pixels (pixbuf), rowstride)
: WebPPictureImportRGB (&picture, gdk_pixbuf_get_pixels (pixbuf), rowstride))
== FALSE)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
"Failed to allocate picture");
WebPPictureFree (&picture);
return FALSE;
}
gboolean uses_array = save_func || icc_data;
if (uses_array)
{
picture.writer = write_array;
picture.custom_ptr = (void *) g_byte_array_new ();
}
else if (f && ! icc_data)
{
picture.writer = write_file;
picture.custom_ptr = (void *) f;
}
else
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_BAD_OPTION,
"Save webp called without callback nor FILE stream value");
WebPPictureFree (&picture);
return FALSE;
}
if (WebPEncode (&config, &picture) == FALSE)
{
g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_BAD_OPTION,
"Could not encode WebP data");
if ((save_func || icc_data) && picture.custom_ptr)
g_byte_array_free ((GByteArray *) picture.custom_ptr, TRUE);
WebPPictureFree (&picture);
return FALSE;
}
gpointer custom_ptr = picture.custom_ptr;
WebPPictureFree (&picture);
if (uses_array)
{
WebPData data; // NOTE We can't do field initialization since we can't get length after free
data.size = ((GByteArray *) custom_ptr)->len;
data.bytes = g_byte_array_free ((GByteArray *) custom_ptr, FALSE);
if (icc_data)
{
WebPData icc_wpdata = { .bytes = icc_data, .size = icc_data_len };
WebPData output = add_icc_data (&data, &icc_wpdata, error);
g_clear_pointer (&icc_data, g_free);
g_free ((gpointer) data.bytes);
if (output.bytes == NULL)
return FALSE;
data = output;
}
gboolean ret = FALSE;
if (save_func)
ret = save_func ((const gchar *) data.bytes, data.size, error, user_data);
else if (f)
ret = fwrite (data.bytes, sizeof (guchar), data.size, f) == data.size ? TRUE : FALSE;
WebPDataClear (&data);
return ret;
}
return TRUE;
}
static gboolean
save (FILE *f, GdkPixbuf *pixbuf, gchar **keys, gchar **values, GError **error)
{
return save_webp (pixbuf, keys, values, error, NULL, f, NULL);
}
static gboolean
save_to_callback (GdkPixbufSaveFunc save_func,
gpointer user_data,
GdkPixbuf *pixbuf,
gchar **keys,
gchar **values,
GError **error)
{
return save_webp (pixbuf, keys, values, error, save_func, NULL, user_data);
}
/* module entry points */
G_MODULE_EXPORT void
fill_vtable (GdkPixbufModule *module)
{
module->begin_load = begin_load;
module->stop_load = stop_load;
module->load_increment = load_increment;
module->save = save;
module->save_to_callback = save_to_callback;
module->is_save_option_supported = is_save_option_supported;
}
G_MODULE_EXPORT void
fill_info (GdkPixbufFormat *info)
{
static GdkPixbufModulePattern signature[] = {
{"RIFFsizeWEBP", " xxxx ", 100},
{ NULL, NULL, 0 }
};
static gchar *mime_types[] = { "image/webp", "audio/x-riff", /* FIXME hack around systems missing mime type */
NULL };
static gchar *extensions[] = { "webp", NULL };
info->name = "webp";
info->signature = signature;
info->description = "The WebP image format";
info->mime_types = mime_types;
info->extensions = extensions;
info->flags = GDK_PIXBUF_FORMAT_WRITABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
info->license = "LGPL";
}