Skip to content

Latest commit

 

History

History
306 lines (258 loc) · 7.34 KB

IMG_webp.c

File metadata and controls

306 lines (258 loc) · 7.34 KB
 
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
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
*/
/* This is a WEBP image file loading framework */
#include <stdlib.h>
#include <stdio.h>
#include "SDL_image.h"
#ifdef LOAD_WEBP
/*=============================================================================
File: SDL_webp.c
Purpose: A WEBP loader for the SDL library
Revision:
Created by: Michael Bonfils (Murlock) (26 November 2011)
murlock42@gmail.com
=============================================================================*/
#include "SDL_endian.h"
#ifdef macintosh
#define MACOS
#endif
#include <webp/decode.h>
static struct {
int loaded;
void *handle;
int/*VP8StatuCode*/ (*webp_get_features_internal) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version);
uint8_t* (*webp_decode_rgb_into) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* (*webp_decode_rgba_into) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
} lib;
#ifdef LOAD_WEBP_DYNAMIC
int IMG_InitWEBP()
{
if ( lib.loaded == 0 ) {
lib.handle = SDL_LoadObject(LOAD_WEBP_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
lib.webp_get_features_internal =
( int (*) (const uint8_t *, uint32_t, WebPBitstreamFeatures* const, int) )
SDL_LoadFunction(lib.handle, "WebPGetFeaturesInternal" );
if ( lib.webp_get_features_internal == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.webp_decode_rgb_into =
( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
SDL_LoadFunction(lib.handle, "WebPDecodeRGBInto" );
if ( lib.webp_decode_rgb_into == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
lib.webp_decode_rgba_into =
( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
Mar 10, 2013
Mar 10, 2013
81
SDL_LoadFunction(lib.handle, "WebPDecodeRGBAInto" );
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
if ( lib.webp_decode_rgba_into == NULL ) {
SDL_UnloadObject(lib.handle);
return -1;
}
}
++lib.loaded;
return 0;
}
void IMG_QuitWEBP()
{
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
SDL_UnloadObject(lib.handle);
}
--lib.loaded;
}
#else
int IMG_InitWEBP()
{
if ( lib.loaded == 0 ) {
lib.webp_get_features_internal = WebPGetFeaturesInternal;
lib.webp_decode_rgb_into = WebPDecodeRGBInto;
lib.webp_decode_rgba_into = WebPDecodeRGBAInto;
}
++lib.loaded;
return 0;
}
void IMG_QuitWEBP()
{
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
}
--lib.loaded;
}
#endif /* LOAD_WEBP_DYNAMIC */
static int webp_getinfo( SDL_RWops *src, int *datasize ) {
Feb 3, 2013
Feb 3, 2013
125
Sint64 start;
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
int is_WEBP;
Uint8 magic[20];
if ( !src )
return 0;
start = SDL_RWtell(src);
is_WEBP = 0;
if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
if ( magic[ 0] == 'R' &&
magic[ 1] == 'I' &&
magic[ 2] == 'F' &&
magic[ 3] == 'F' &&
magic[ 8] == 'W' &&
magic[ 9] == 'E' &&
magic[10] == 'B' &&
magic[11] == 'P' &&
magic[12] == 'V' &&
magic[13] == 'P' &&
magic[14] == '8' &&
Mar 10, 2013
Mar 10, 2013
145
146
147
148
149
#if WEBP_DECODER_ABI_VERSION < 0x0003 /* old versions don't support WEBPVP8X and WEBPVP8L */
magic[15] == ' ') {
#else
(magic[15] == ' ' || magic[15] == 'X' || magic[15] == 'L')) {
#endif
150
is_WEBP = 1;
Mar 10, 2013
Mar 10, 2013
151
152
153
if ( datasize ) {
*datasize = SDL_RWseek(src, 0, SEEK_END);
}
154
155
156
157
158
159
160
161
162
163
164
165
166
167
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_WEBP);
}
/* See if an image is contained in a data source */
int IMG_isWEBP(SDL_RWops *src)
{
return webp_getinfo( src, NULL );
}
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
{
Feb 3, 2013
Feb 3, 2013
168
Sint64 start;
169
170
171
172
173
174
const char *error = NULL;
SDL_Surface *volatile surface = NULL;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
Jan 2, 2012
Jan 2, 2012
175
176
WebPBitstreamFeatures features;
int raw_data_size;
Feb 3, 2013
Feb 3, 2013
177
uint8_t *raw_data = NULL;
Mar 10, 2013
Mar 10, 2013
178
int r, s;
Jan 2, 2012
Jan 2, 2012
179
uint8_t *ret;
180
181
182
183
184
185
186
187
188
189
190
191
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);
if ( !IMG_Init(IMG_INIT_WEBP) ) {
goto error;
}
Jan 2, 2012
Jan 2, 2012
192
raw_data_size = -1;
193
194
195
196
197
if ( !webp_getinfo( src, &raw_data_size ) ) {
error = "Invalid WEBP";
goto error;
}
Mar 10, 2013
Mar 10, 2013
198
199
// seek to start of file
SDL_RWseek(src, 0, RW_SEEK_SET );
200
Feb 3, 2013
Feb 3, 2013
201
raw_data = (uint8_t*) SDL_malloc( raw_data_size );
202
203
204
205
206
if ( raw_data == NULL ) {
error = "Failed to allocate enought buffer for WEBP";
goto error;
}
Jan 2, 2012
Jan 2, 2012
207
r = SDL_RWread(src, raw_data, 1, raw_data_size );
208
209
210
211
212
213
214
215
216
217
218
219
220
221
if ( r != raw_data_size ) {
error = "Failed to read WEBP";
goto error;
}
#if 0
// extract size of picture, not interesting since we don't know about alpha channel
int width = -1, height = -1;
if ( !WebPGetInfo( raw_data, raw_data_size, &width, &height ) ) {
printf("WebPGetInfo has failed\n" );
return NULL;
}
#endif
Jan 2, 2012
Jan 2, 2012
222
if ( lib.webp_get_features_internal( raw_data, raw_data_size, &features, WEBP_DECODER_ABI_VERSION ) != VP8_STATUS_OK ) {
223
error = "WebPGetFeatures has failed";
Feb 3, 2013
Feb 3, 2013
224
goto error;
225
226
227
}
/* Check if it's ok !*/
Mar 10, 2013
Mar 10, 2013
228
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
229
230
231
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Mar 10, 2013
Mar 10, 2013
232
233
234
235
236
237
238
Amask = (features.has_alpha) ? 0xFF000000 : 0;
#else
s = (features.has_alpha) ? 0 : 8;
Rmask = 0xFF000000 >> s;
Gmask = 0x00FF0000 >> s;
Bmask = 0x0000FF00 >> s;
Amask = 0x000000FF >> s;
Mar 2, 2013
Mar 2, 2013
239
#endif
240
Jan 23, 2012
Jan 23, 2012
241
surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
Mar 10, 2013
Mar 10, 2013
242
features.width, features.height,
243
244
245
246
247
248
249
250
features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
if ( surface == NULL ) {
error = "Failed to allocate SDL_Surface";
goto error;
}
if ( features.has_alpha ) {
Feb 3, 2013
Feb 3, 2013
251
ret = lib.webp_decode_rgba_into( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h, surface->pitch );
252
} else {
Feb 3, 2013
Feb 3, 2013
253
ret = lib.webp_decode_rgb_into( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h, surface->pitch );
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
}
if ( !ret ) {
error = "Failed to decode WEBP";
goto error;
}
return surface;
error:
if ( surface ) {
SDL_FreeSurface( surface );
}
if ( raw_data ) {
Feb 3, 2013
Feb 3, 2013
271
SDL_free( raw_data );
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
}
if ( error ) {
IMG_SetError( error );
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(NULL);
}
#else
int IMG_InitWEBP()
{
IMG_SetError("WEBP images are not supported");
return(-1);
}
void IMG_QuitWEBP()
{
}
/* See if an image is contained in a data source */
int IMG_isWEBP(SDL_RWops *src)
{
return(0);
}
/* Load a WEBP type image from an SDL datasource */
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_WEBP */