Skip to content

Latest commit

 

History

History
294 lines (249 loc) · 8.69 KB

IMG_webp.c

File metadata and controls

294 lines (249 loc) · 8.69 KB
 
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 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
*/
/* This is a WEBP image file loading framework */
#include "SDL_image.h"
#ifdef LOAD_WEBP
/*=============================================================================
File: SDL_webp.c
May 22, 2013
May 22, 2013
30
31
Purpose: A WEBP loader for the SDL library
Revision:
32
33
34
35
36
37
38
39
40
41
42
43
44
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 {
May 22, 2013
May 22, 2013
45
46
int loaded;
void *handle;
Oct 16, 2018
Oct 16, 2018
47
48
49
50
51
#if WEBP_DECODER_ABI_VERSION < 0x0100
VP8StatusCode (*WebPGetFeaturesInternal) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version);
uint8_t* (*WebPDecodeRGBInto) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
uint8_t* (*WebPDecodeRGBAInto) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride);
#else
Oct 22, 2017
Oct 22, 2017
52
53
54
VP8StatusCode (*WebPGetFeaturesInternal) (const uint8_t *data, size_t data_size, WebPBitstreamFeatures* features, int decoder_abi_version);
uint8_t* (*WebPDecodeRGBInto) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
uint8_t* (*WebPDecodeRGBAInto) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
Oct 16, 2018
Oct 16, 2018
55
#endif
56
57
58
} lib;
#ifdef LOAD_WEBP_DYNAMIC
Oct 22, 2017
Oct 22, 2017
59
60
61
62
63
64
65
66
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = (SIG) SDL_LoadFunction(lib.handle, #FUNC); \
if (lib.FUNC == NULL) { SDL_UnloadObject(lib.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
lib.FUNC = FUNC;
#endif
67
68
int IMG_InitWEBP()
{
May 22, 2013
May 22, 2013
69
if ( lib.loaded == 0 ) {
Oct 22, 2017
Oct 22, 2017
70
#ifdef LOAD_WEBP_DYNAMIC
May 22, 2013
May 22, 2013
71
72
73
74
lib.handle = SDL_LoadObject(LOAD_WEBP_DYNAMIC);
if ( lib.handle == NULL ) {
return -1;
}
Oct 22, 2017
Oct 22, 2017
75
#endif
Oct 16, 2018
Oct 16, 2018
76
77
78
79
80
#if WEBP_DECODER_ABI_VERSION < 0x0100
FUNCTION_LOADER(WebPGetFeaturesInternal, VP8StatusCode (*) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version))
FUNCTION_LOADER(WebPDecodeRGBInto, uint8_t * (*) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride))
FUNCTION_LOADER(WebPDecodeRGBAInto, uint8_t * (*) (const uint8_t* data, uint32_t data_size, uint8_t* output_buffer, int output_buffer_size, int output_stride))
#else
Oct 22, 2017
Oct 22, 2017
81
82
83
FUNCTION_LOADER(WebPGetFeaturesInternal, VP8StatusCode (*) (const uint8_t *data, size_t data_size, WebPBitstreamFeatures* features, int decoder_abi_version))
FUNCTION_LOADER(WebPDecodeRGBInto, uint8_t * (*) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride))
FUNCTION_LOADER(WebPDecodeRGBAInto, uint8_t * (*) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride))
Oct 16, 2018
Oct 16, 2018
84
#endif
May 22, 2013
May 22, 2013
85
86
87
88
}
++lib.loaded;
return 0;
89
90
91
}
void IMG_QuitWEBP()
{
May 22, 2013
May 22, 2013
92
93
94
95
if ( lib.loaded == 0 ) {
return;
}
if ( lib.loaded == 1 ) {
Oct 22, 2017
Oct 22, 2017
96
#ifdef LOAD_WEBP_DYNAMIC
May 22, 2013
May 22, 2013
97
SDL_UnloadObject(lib.handle);
Oct 22, 2017
Oct 22, 2017
98
#endif
May 22, 2013
May 22, 2013
99
100
}
--lib.loaded;
101
102
103
}
static int webp_getinfo( SDL_RWops *src, int *datasize ) {
May 22, 2013
May 22, 2013
104
105
106
107
Sint64 start;
int is_WEBP;
Uint8 magic[20];
Oct 16, 2018
Oct 16, 2018
108
if ( !src ) {
May 22, 2013
May 22, 2013
109
return 0;
Oct 16, 2018
Oct 16, 2018
110
}
May 22, 2013
May 22, 2013
111
112
113
114
start = SDL_RWtell(src);
is_WEBP = 0;
if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
if ( magic[ 0] == 'R' &&
Oct 22, 2017
Oct 22, 2017
115
116
117
118
119
120
121
122
123
124
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' &&
Oct 16, 2018
Oct 16, 2018
125
126
127
128
129
130
131
/* old versions don't support VP8X and VP8L */
#if (WEBP_DECODER_ABI_VERSION < 0x0003)
magic[15] == ' '
#else
(magic[15] == ' ' || magic[15] == 'X' || magic[15] == 'L')
#endif
) {
May 22, 2013
May 22, 2013
132
133
is_WEBP = 1;
if ( datasize ) {
Oct 22, 2017
Oct 22, 2017
134
*datasize = (int)(SDL_RWseek(src, 0, RW_SEEK_END) - start);
May 22, 2013
May 22, 2013
135
136
137
138
139
}
}
}
SDL_RWseek(src, start, RW_SEEK_SET);
return(is_WEBP);
140
141
142
143
144
}
/* See if an image is contained in a data source */
int IMG_isWEBP(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
145
return webp_getinfo( src, NULL );
146
147
148
149
}
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
150
151
152
153
154
155
156
157
158
159
Sint64 start;
const char *error = NULL;
SDL_Surface *volatile surface = NULL;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
Uint32 Amask;
WebPBitstreamFeatures features;
int raw_data_size;
uint8_t *raw_data = NULL;
Jun 8, 2013
Jun 8, 2013
160
int r;
May 22, 2013
May 22, 2013
161
162
163
164
165
166
167
168
169
uint8_t *ret;
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
start = SDL_RWtell(src);
Feb 15, 2016
Feb 15, 2016
170
if ( (IMG_Init(IMG_INIT_WEBP) & IMG_INIT_WEBP) == 0 ) {
May 22, 2013
May 22, 2013
171
172
173
174
175
176
177
178
179
180
181
goto error;
}
raw_data_size = -1;
if ( !webp_getinfo( src, &raw_data_size ) ) {
error = "Invalid WEBP";
goto error;
}
raw_data = (uint8_t*) SDL_malloc( raw_data_size );
if ( raw_data == NULL ) {
Oct 22, 2017
Oct 22, 2017
182
error = "Failed to allocate enough buffer for WEBP";
May 22, 2013
May 22, 2013
183
184
185
goto error;
}
Oct 22, 2017
Oct 22, 2017
186
r = (int)SDL_RWread(src, raw_data, 1, raw_data_size );
May 22, 2013
May 22, 2013
187
188
189
190
191
if ( r != raw_data_size ) {
error = "Failed to read WEBP";
goto error;
}
192
#if 0
May 22, 2013
May 22, 2013
193
194
195
196
197
198
// 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;
}
199
200
#endif
Oct 22, 2017
Oct 22, 2017
201
if ( lib.WebPGetFeaturesInternal( raw_data, raw_data_size, &features, WEBP_DECODER_ABI_VERSION ) != VP8_STATUS_OK ) {
May 22, 2013
May 22, 2013
202
203
204
error = "WebPGetFeatures has failed";
goto error;
}
205
May 22, 2013
May 22, 2013
206
/* Check if it's ok !*/
Mar 10, 2013
Mar 10, 2013
207
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
May 22, 2013
May 22, 2013
208
209
210
211
Rmask = 0x000000FF;
Gmask = 0x0000FF00;
Bmask = 0x00FF0000;
Amask = (features.has_alpha) ? 0xFF000000 : 0;
Mar 10, 2013
Mar 10, 2013
212
#else
Sep 14, 2013
Sep 14, 2013
213
214
215
216
217
218
219
{
int s = (features.has_alpha) ? 0 : 8;
Rmask = 0xFF000000 >> s;
Gmask = 0x00FF0000 >> s;
Bmask = 0x0000FF00 >> s;
Amask = 0x000000FF >> s;
}
Mar 2, 2013
Mar 2, 2013
220
#endif
221
May 22, 2013
May 22, 2013
222
223
224
surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
features.width, features.height,
features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
225
May 22, 2013
May 22, 2013
226
227
228
229
if ( surface == NULL ) {
error = "Failed to allocate SDL_Surface";
goto error;
}
230
May 22, 2013
May 22, 2013
231
if ( features.has_alpha ) {
Oct 22, 2017
Oct 22, 2017
232
ret = lib.WebPDecodeRGBAInto( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h, surface->pitch );
May 22, 2013
May 22, 2013
233
} else {
Oct 22, 2017
Oct 22, 2017
234
ret = lib.WebPDecodeRGBInto( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h, surface->pitch );
May 22, 2013
May 22, 2013
235
}
236
May 22, 2013
May 22, 2013
237
238
239
240
if ( !ret ) {
error = "Failed to decode WEBP";
goto error;
}
241
Dec 12, 2013
Dec 12, 2013
242
243
244
245
if ( raw_data ) {
SDL_free( raw_data );
}
May 22, 2013
May 22, 2013
246
return surface;
247
248
249
250
error:
May 22, 2013
May 22, 2013
251
252
253
if ( raw_data ) {
SDL_free( raw_data );
}
254
Dec 12, 2013
Dec 12, 2013
255
256
257
258
if ( surface ) {
SDL_FreeSurface( surface );
}
May 22, 2013
May 22, 2013
259
if ( error ) {
Jun 16, 2014
Jun 16, 2014
260
IMG_SetError( "%s", error );
May 22, 2013
May 22, 2013
261
}
262
May 22, 2013
May 22, 2013
263
264
SDL_RWseek(src, start, RW_SEEK_SET);
return(NULL);
265
266
267
}
#else
Oct 9, 2019
Oct 9, 2019
268
269
270
#if _MSC_VER >= 1300
#pragma warning(disable : 4100) /* warning C4100: 'op' : unreferenced formal parameter */
#endif
271
272
273
int IMG_InitWEBP()
{
May 22, 2013
May 22, 2013
274
275
IMG_SetError("WEBP images are not supported");
return(-1);
276
277
278
279
280
281
282
283
284
}
void IMG_QuitWEBP()
{
}
/* See if an image is contained in a data source */
int IMG_isWEBP(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
285
return(0);
286
287
288
289
290
}
/* Load a WEBP type image from an SDL datasource */
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
291
return(NULL);
292
293
294
}
#endif /* LOAD_WEBP */