forked from aruiz/webp-pixbuf-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio-webp.c
437 lines (398 loc) · 16.4 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
/* GdkPixbuf library - WebP Image Loader
*
* Copyright (C) 2011 Alberto Ruiz
* Copyright (C) 2011 David Mazary
*
* Authors: Alberto Ruiz <[email protected]>
* David Mazary <[email protected]>
*
* 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 <webp/decode.h>
#include <webp/encode.h>
#include <string.h>
#define GDK_PIXBUF_ENABLE_BACKEND
#include <gdk-pixbuf/gdk-pixbuf.h>
#undef GDK_PIXBUF_ENABLE_BACKEND
/* Progressive loader context */
typedef struct {
GdkPixbufModuleSizeFunc size_func;
GdkPixbufModuleUpdatedFunc update_func;
GdkPixbufModulePreparedFunc prepare_func;
gpointer user_data;
GdkPixbuf *pixbuf;
gboolean got_header;
WebPIDecoder *idec;
guchar *decbuf;
gint last_y;
GError **error;
} WebPContext;
static void
destroy_data (guchar *pixels, gpointer data)
{
g_free (pixels);
}
/* Shared library entry point */
static GdkPixbuf *
gdk_pixbuf__webp_image_load (FILE *f, GError **error)
{
GdkPixbuf * volatile pixbuf = NULL;
guint32 data_size;
guint8 *out;
gint w, h, ok;
gpointer data;
/* Get data size */
fseek (f, 0, SEEK_END);
data_size = ftell(f);
fseek (f, 0, SEEK_SET);
/* Get data */
data = g_malloc (data_size);
ok = (fread (data, data_size, 1, f) == 1);
if (!ok) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Failed to read file");
return FALSE;
}
out = WebPDecodeRGB (data, data_size, &w, &h);
g_free (data);
if (!out) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Cannot create WebP decoder.");
return FALSE;
}
/*FIXME: libwebp does not support alpha channel detection, once supported
* we need to make the alpha channel conditional to save memory if possible */
pixbuf = gdk_pixbuf_new_from_data ((const guchar *)out,
GDK_COLORSPACE_RGB,
FALSE ,
8,
w, h,
3 * w,
destroy_data,
NULL);
if (!pixbuf) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Failed to decode image");
return FALSE;
}
return pixbuf;
}
static gpointer
gdk_pixbuf__webp_image_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;
return context;
}
static gboolean
gdk_pixbuf__webp_image_stop_load (gpointer context, GError **error)
{
WebPContext *data = (WebPContext *) context;
g_return_val_if_fail(data != NULL, TRUE);
if (data->pixbuf) {
g_object_unref (data->pixbuf);
}
if (data->idec) {
WebPIDelete (data->idec);
}
if (data->decbuf) {
g_free (data->decbuf);
}
return TRUE;
}
static gboolean
gdk_pixbuf__webp_image_load_increment (gpointer context,
const guchar *buf, guint size,
GError **error)
{
gint w, h, stride;
WebPContext *data = (WebPContext *) context;
g_return_val_if_fail(data != NULL, FALSE);
if (!data->got_header) {
gint rc;
rc = WebPGetInfo (buf, size, &w, &h);
if (rc == 0) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
"Cannot read WebP image header.");
return FALSE;
}
stride = w * 3; /* TODO Update when alpha support released */
data->got_header = TRUE;
if (data->size_func) {
(* data->size_func) (&w, &h,
data->user_data);
}
data->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
FALSE,
8,
w,
h);
data->decbuf = g_try_malloc (h * stride);
if (!data->decbuf) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
"Cannot allocate memory for decoded image data.");
return FALSE;
}
data->idec = WebPINewRGB (MODE_RGB,
data->decbuf,
h * stride,
stride);
if (!data->idec) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Cannot create WebP decoder.");
return FALSE;
}
if (data->prepare_func) {
(* data->prepare_func) (data->pixbuf,
NULL,
data->user_data);
}
}
/* Append size bytes to decoder's buffer */
const VP8StatusCode status = WebPIAppend (data->idec, buf, size);
if (status != VP8_STATUS_SUSPENDED && status != VP8_STATUS_OK) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
"WebP decoder failed with status code %d.",
status);
return FALSE;
}
/* Decode decoder's updated buffer */
guint8 *dec_output;
dec_output = WebPIDecGetRGB (data->idec, &data->last_y, &w, &h, &stride);
if (dec_output == NULL && status != VP8_STATUS_SUSPENDED) {
g_set_error(error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Bad inputs to WebP decoder.");
return FALSE;
}
/* Copy decoder output to pixbuf */
gint y, row;
guchar *dptr;
dptr = gdk_pixbuf_get_pixels (data->pixbuf);
const guint8 offset = w % 4; /* decoded width will be divisible by 4 */
for (y = 0; y < data->last_y; ++y, dptr += offset) {
row = y * stride;
g_memmove (dptr + row, dec_output + row, stride);
}
if (data->update_func) {
(* data->update_func) (data->pixbuf, 0, 0,
w,
data->last_y,
data->user_data);
}
return TRUE;
}
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, data_size, 1, out) == 1) : 1;
}
typedef struct {
GdkPixbufSaveFunc func;
gpointer data;
} save_context;
static int
save_callback (const uint8_t* data, size_t data_size, const WebPPicture* const pic)
{
save_context *env = (save_context *) pic->custom_ptr;
return (* env->func) (env->data, data_size, NULL, (gpointer) data);
}
static gboolean
real_save_webp (GdkPixbuf *pixbuf,
gchar **keys,
gchar **values,
GError **error,
gboolean to_callback,
FILE *f,
save_context *context)
{
WebPPicture picture;
WebPConfig config;
gint w, h, rowstride, has_alpha, rc;
guchar *pixels;
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) {
gchar **kiter = keys;
gchar **viter = values;
while (*kiter) {
if (strncmp (*kiter, "quality", 7) == 0) {
float quality = (float) g_ascii_strtod (*viter, NULL);
if (quality < 0 || quality > 100) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_BAD_OPTION,
"WebP quality must be a value between 0 and 100.");
return FALSE;
}
config.quality = quality;
} else if (strncmp (*kiter, "preset", 6) == 0) {
WebPPreset preset;
if (strncmp (*viter, "default", 7) == 0) {
preset = WEBP_PRESET_DEFAULT;
} else if (strncmp (*viter, "photo", 5) == 0) {
preset = WEBP_PRESET_PHOTO;
} else if (strncmp (*viter, "picture", 7) == 0) {
preset = WEBP_PRESET_PICTURE;
} else if (strncmp (*viter, "drawing", 7) == 0) {
preset = WEBP_PRESET_DRAWING;
} else if (strncmp (*viter, "icon", 4) == 0) {
preset = WEBP_PRESET_ICON;
} else if (strncmp (*viter, "text", 4) == 0) {
preset = WEBP_PRESET_TEXT;
} else {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_BAD_OPTION,
"WebP encoder invalid preset.");
return FALSE;
}
if (WebPConfigPreset (&config, preset, config.quality) == 0) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_FAILED,
"Could not initialize decoder with preset.");
return FALSE;
}
}
++kiter;
++viter;
}
}
if (WebPValidateConfig (&config) != 1) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_BAD_OPTION,
"Invalid encoding configuration");
return FALSE;
}
w = gdk_pixbuf_get_width (pixbuf);
h = gdk_pixbuf_get_height (pixbuf);
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
pixels = gdk_pixbuf_get_pixels (pixbuf);
picture.width = w;
picture.height = h;
if (has_alpha) {
rc = WebPPictureImportRGBA (&picture, pixels, rowstride);
} else {
rc = WebPPictureImportRGB (&picture, pixels, rowstride);
}
if (rc == 0) {
g_set_error (error,
GDK_PIXBUF_ERROR,
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
"Failed to allocate picture");
return FALSE;
}
if (to_callback) {
picture.writer = save_callback;
picture.custom_ptr = (void*) context;
} else {
picture.writer = write_file;
picture.custom_ptr = (void*) f;
}
if (WebPEncode(&config, &picture) == 0) {
fprintf(stderr, "Error! Cannot encode picture as WebP\n");
}
return TRUE;
}
static gboolean
gdk_pixbuf__webp_image_save (FILE *f,
GdkPixbuf *pixbuf,
gchar **keys,
gchar **values,
GError **error)
{
return real_save_webp (pixbuf, keys, values, error,
FALSE, f, NULL);
}
static gboolean
gdk_pixbuf__webp_image_save_to_callback (GdkPixbufSaveFunc save_func,
gpointer user_data,
GdkPixbuf *pixbuf,
gchar **keys,
gchar **values,
GError **error)
{
save_context *context = g_new0 (save_context, 1);
context->func = save_func;
context->data = user_data;
return real_save_webp (pixbuf, keys, values, error,
TRUE, NULL, context);
}
void
fill_vtable (GdkPixbufModule *module)
{
module->load = gdk_pixbuf__webp_image_load;
module->begin_load = gdk_pixbuf__webp_image_begin_load;
module->stop_load = gdk_pixbuf__webp_image_stop_load;
module->load_increment = gdk_pixbuf__webp_image_load_increment;
module->save = gdk_pixbuf__webp_image_save;
module->save_to_callback = gdk_pixbuf__webp_image_save_to_callback;
}
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";
}