IMG_webp.c
author Thomas Bernard <miniupnp@free.fr>
Fri, 30 Nov 2018 11:04:15 +0100
branchSDL-1.2
changeset 634 68f958f43339
parent 609 e14e00a2754d
permissions -rw-r--r--
IMG_xcf.c: Avoid infinite loop in read_xcf_header()
slouken@278
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@280
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@278
     4
slouken@280
     5
  This software is provided 'as-is', without any express or implied
slouken@280
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@280
     7
  arising from the use of this software.
slouken@278
     8
slouken@280
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@280
    10
  including commercial applications, and to alter it and redistribute it
slouken@280
    11
  freely, subject to the following restrictions:
slouken@278
    12
slouken@280
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@280
    14
     claim that you wrote the original software. If you use this software
slouken@280
    15
     in a product, an acknowledgment in the product documentation would be
slouken@280
    16
     appreciated but is not required.
slouken@280
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@280
    18
     misrepresented as being the original software.
slouken@280
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@278
    20
*/
slouken@278
    21
slouken@278
    22
/* This is a WEBP image file loading framework */
slouken@278
    23
slouken@278
    24
#include <stdlib.h>
slouken@278
    25
#include <stdio.h>
slouken@278
    26
slouken@278
    27
#include "SDL_image.h"
slouken@278
    28
slouken@278
    29
#ifdef LOAD_WEBP
slouken@278
    30
slouken@278
    31
/*=============================================================================
slouken@278
    32
        File: SDL_webp.c
slouken@278
    33
     Purpose: A WEBP loader for the SDL library      
slouken@278
    34
    Revision: 
slouken@278
    35
  Created by: Michael Bonfils (Murlock) (26 November 2011)
slouken@278
    36
              murlock42@gmail.com
slouken@278
    37
slouken@278
    38
=============================================================================*/
slouken@278
    39
slouken@278
    40
#include "SDL_endian.h"
slouken@278
    41
slouken@278
    42
#ifdef macintosh
slouken@278
    43
#define MACOS
slouken@278
    44
#endif
slouken@278
    45
#include <webp/decode.h>
slouken@278
    46
slouken@278
    47
static struct {
slouken@278
    48
	int loaded;
slouken@278
    49
	void *handle;
sezeroz@609
    50
#if WEBP_DECODER_ABI_VERSION < 0x0100
slouken@278
    51
	int/*VP8StatuCode*/ (*webp_get_features_internal) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version);
slouken@278
    52
	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);
slouken@278
    53
	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);
sezeroz@609
    54
#else
sezeroz@609
    55
	VP8StatusCode (*webp_get_features_internal) (const uint8_t *data, size_t data_size, WebPBitstreamFeatures* features, int decoder_abi_version);
sezeroz@609
    56
	uint8_t*	(*webp_decode_rgb_into) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
sezeroz@609
    57
	uint8_t*	(*webp_decode_rgba_into) (const uint8_t* data, size_t data_size, uint8_t* output_buffer, size_t output_buffer_size, int output_stride);
sezeroz@609
    58
#endif
slouken@278
    59
} lib;
slouken@278
    60
slouken@278
    61
#ifdef LOAD_WEBP_DYNAMIC
slouken@278
    62
int IMG_InitWEBP()
slouken@278
    63
{
slouken@278
    64
	if ( lib.loaded == 0 ) {
slouken@278
    65
		lib.handle = SDL_LoadObject(LOAD_WEBP_DYNAMIC);
slouken@278
    66
		if ( lib.handle == NULL ) {
slouken@278
    67
			return -1;
slouken@278
    68
		}
sezeroz@609
    69
slouken@278
    70
		lib.webp_get_features_internal = 
sezeroz@609
    71
			#if WEBP_DECODER_ABI_VERSION < 0x0100
slouken@278
    72
			( int (*) (const uint8_t *, uint32_t, WebPBitstreamFeatures* const, int) )
sezeroz@609
    73
			#else
sezeroz@609
    74
			( VP8StatusCode (*) (const uint8_t *, size_t, WebPBitstreamFeatures*, int) )
sezeroz@609
    75
			#endif
slouken@278
    76
			SDL_LoadFunction(lib.handle, "WebPGetFeaturesInternal" );
slouken@278
    77
		if ( lib.webp_get_features_internal == NULL ) {
slouken@278
    78
			SDL_UnloadObject(lib.handle);
slouken@278
    79
			return -1;
slouken@278
    80
		}
slouken@278
    81
slouken@278
    82
		lib.webp_decode_rgb_into = 
sezeroz@609
    83
			#if WEBP_DECODER_ABI_VERSION < 0x0100
slouken@278
    84
			( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
sezeroz@609
    85
			#else
sezeroz@609
    86
			( uint8_t* (*) (const uint8_t*, size_t, uint8_t*, size_t, int ) )
sezeroz@609
    87
			#endif
slouken@278
    88
			SDL_LoadFunction(lib.handle, "WebPDecodeRGBInto" );
slouken@278
    89
		if ( lib.webp_decode_rgb_into == NULL ) {
slouken@278
    90
			SDL_UnloadObject(lib.handle);
slouken@278
    91
			return -1;
slouken@278
    92
		}
slouken@278
    93
slouken@278
    94
		lib.webp_decode_rgba_into = 
sezeroz@609
    95
			#if WEBP_DECODER_ABI_VERSION < 0x0100
slouken@278
    96
			( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
sezeroz@609
    97
			#else
sezeroz@609
    98
			( uint8_t* (*) (const uint8_t*, size_t, uint8_t*, size_t, int ) )
sezeroz@609
    99
			#endif
sezeroz@609
   100
			SDL_LoadFunction(lib.handle, "WebPDecodeRGBAInto" );
slouken@278
   101
		if ( lib.webp_decode_rgba_into == NULL ) {
slouken@278
   102
			SDL_UnloadObject(lib.handle);
slouken@278
   103
			return -1;
slouken@278
   104
		}
slouken@278
   105
	}
slouken@278
   106
	++lib.loaded;
slouken@278
   107
slouken@278
   108
	return 0;
slouken@278
   109
}
slouken@278
   110
void IMG_QuitWEBP()
slouken@278
   111
{
slouken@278
   112
	if ( lib.loaded == 0 ) {
slouken@278
   113
		return;
slouken@278
   114
	}
slouken@278
   115
	if ( lib.loaded == 1 ) {
slouken@278
   116
		SDL_UnloadObject(lib.handle);
slouken@278
   117
	}
slouken@278
   118
	--lib.loaded;
slouken@278
   119
}
slouken@278
   120
#else
slouken@278
   121
int IMG_InitWEBP()
slouken@278
   122
{
slouken@278
   123
	if ( lib.loaded == 0 ) {
slouken@278
   124
		lib.webp_get_features_internal = WebPGetFeaturesInternal;
slouken@278
   125
		lib.webp_decode_rgb_into = WebPDecodeRGBInto;
slouken@278
   126
		lib.webp_decode_rgba_into = WebPDecodeRGBAInto;
slouken@278
   127
	}
slouken@278
   128
	++lib.loaded;
slouken@278
   129
slouken@278
   130
	return 0;
slouken@278
   131
}
slouken@278
   132
void IMG_QuitWEBP()
slouken@278
   133
{
slouken@278
   134
	if ( lib.loaded == 0 ) {
slouken@278
   135
		return;
slouken@278
   136
	}
slouken@278
   137
	if ( lib.loaded == 1 ) {
slouken@278
   138
	}
slouken@278
   139
	--lib.loaded;
slouken@278
   140
}
slouken@278
   141
#endif /* LOAD_WEBP_DYNAMIC */
slouken@278
   142
slouken@278
   143
static int webp_getinfo( SDL_RWops *src, int *datasize ) {
slouken@278
   144
	int start;
slouken@278
   145
	int is_WEBP;
slouken@278
   146
	Uint8 magic[20];
slouken@278
   147
sezeroz@609
   148
	if ( !src ) {
slouken@278
   149
		return 0;
sezeroz@609
   150
	}
slouken@278
   151
	start = SDL_RWtell(src);
slouken@278
   152
	is_WEBP = 0;
slouken@278
   153
	if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
slouken@278
   154
		if ( magic[ 0] == 'R' &&
sezeroz@609
   155
		     magic[ 1] == 'I' &&
sezeroz@609
   156
		     magic[ 2] == 'F' &&
sezeroz@609
   157
		     magic[ 3] == 'F' &&
sezeroz@609
   158
		     magic[ 8] == 'W' &&
sezeroz@609
   159
		     magic[ 9] == 'E' &&
sezeroz@609
   160
		     magic[10] == 'B' &&
sezeroz@609
   161
		     magic[11] == 'P' &&
sezeroz@609
   162
		     magic[12] == 'V' &&
sezeroz@609
   163
		     magic[13] == 'P' &&
sezeroz@609
   164
		     magic[14] == '8' &&
sezeroz@609
   165
		/* old versions don't support VP8X and VP8L */
sezeroz@609
   166
		#if (WEBP_DECODER_ABI_VERSION < 0x0003)
sezeroz@609
   167
		     magic[15] == ' '
sezeroz@609
   168
		#else
sezeroz@609
   169
		    (magic[15] == ' ' || magic[15] == 'X' || magic[15] == 'L')
sezeroz@609
   170
		#endif
sezeroz@609
   171
		 ) {
slouken@278
   172
			is_WEBP = 1;
sezeroz@609
   173
			if ( datasize ) {
sezeroz@609
   174
				*datasize = SDL_RWseek(src, 0, RW_SEEK_END) - start;
sezeroz@609
   175
			}
slouken@278
   176
		}
slouken@278
   177
	}
slouken@278
   178
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@278
   179
	return(is_WEBP);
slouken@278
   180
}
slouken@278
   181
slouken@278
   182
/* See if an image is contained in a data source */
slouken@278
   183
int IMG_isWEBP(SDL_RWops *src)
slouken@278
   184
{
slouken@278
   185
	return webp_getinfo( src, NULL );
slouken@278
   186
}
slouken@278
   187
slouken@278
   188
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
slouken@278
   189
{
slouken@278
   190
	int start;
slouken@278
   191
	const char *error = NULL;
slouken@278
   192
	SDL_Surface *volatile surface = NULL;
slouken@278
   193
	Uint32 Rmask;
slouken@278
   194
	Uint32 Gmask;
slouken@278
   195
	Uint32 Bmask;
slouken@278
   196
	Uint32 Amask;
slouken@288
   197
	WebPBitstreamFeatures features;
slouken@288
   198
	int raw_data_size;
sezeroz@607
   199
	uint8_t *raw_data = NULL;
slouken@288
   200
	int r;
slouken@288
   201
	uint8_t *ret;
slouken@278
   202
slouken@278
   203
	if ( !src ) {
slouken@278
   204
		/* The error message has been set in SDL_RWFromFile */
slouken@278
   205
		return NULL;
slouken@278
   206
	}
slouken@278
   207
slouken@278
   208
	start = SDL_RWtell(src);
slouken@278
   209
sezeroz@607
   210
	if ( (IMG_Init(IMG_INIT_WEBP) & IMG_INIT_WEBP) == 0 ) {
slouken@278
   211
		goto error;
slouken@278
   212
	}
slouken@278
   213
slouken@288
   214
	raw_data_size = -1;
slouken@278
   215
	if ( !webp_getinfo( src, &raw_data_size ) ) {
slouken@278
   216
		error = "Invalid WEBP";
slouken@278
   217
		goto error;
slouken@278
   218
	}
slouken@278
   219
slouken@288
   220
	raw_data = (uint8_t*) malloc( raw_data_size );
slouken@278
   221
	if ( raw_data == NULL ) {
sezeroz@609
   222
		error = "Failed to allocate enough buffer for WEBP";
slouken@278
   223
		goto error;
slouken@278
   224
	}
slouken@278
   225
slouken@288
   226
	r = SDL_RWread(src, raw_data, 1, raw_data_size );
slouken@278
   227
	if ( r != raw_data_size ) {
slouken@278
   228
		error = "Failed to read WEBP";
slouken@278
   229
		goto error;
slouken@278
   230
	}
slouken@278
   231
	
slouken@278
   232
#if 0
slouken@278
   233
	// extract size of picture, not interesting since we don't know about alpha channel
slouken@278
   234
	int width = -1, height = -1;
slouken@278
   235
	if ( !WebPGetInfo( raw_data, raw_data_size, &width, &height ) ) {
slouken@278
   236
		printf("WebPGetInfo has failed\n" );
slouken@278
   237
		return NULL;
slouken@278
   238
	}
slouken@278
   239
#endif
slouken@278
   240
slouken@288
   241
	if ( lib.webp_get_features_internal( raw_data, raw_data_size, &features, WEBP_DECODER_ABI_VERSION ) != VP8_STATUS_OK ) {
slouken@278
   242
		error = "WebPGetFeatures has failed";
sezeroz@607
   243
		goto error;
slouken@278
   244
	}
slouken@278
   245
slouken@278
   246
	/* Check if it's ok !*/
sezeroz@607
   247
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@278
   248
	Rmask = 0x000000FF;
slouken@278
   249
	Gmask = 0x0000FF00;
slouken@278
   250
	Bmask = 0x00FF0000;
sezeroz@607
   251
	Amask = (features.has_alpha) ? 0xFF000000 : 0;
sezeroz@607
   252
#else
sezeroz@607
   253
	{ int s = (features.has_alpha) ? 0 : 8;
sezeroz@607
   254
	Rmask = 0xFF000000 >> s;
sezeroz@607
   255
	Gmask = 0x00FF0000 >> s;
sezeroz@607
   256
	Bmask = 0x0000FF00 >> s;
sezeroz@607
   257
	Amask = 0x000000FF >> s;
sezeroz@607
   258
	}
sezeroz@607
   259
#endif
slouken@278
   260
slouken@278
   261
	surface = SDL_AllocSurface(SDL_SWSURFACE, features.width, features.height,
slouken@278
   262
			features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
slouken@278
   263
slouken@278
   264
	if ( surface == NULL ) {
slouken@278
   265
		error = "Failed to allocate SDL_Surface";
slouken@278
   266
		goto error;
slouken@278
   267
	}
slouken@278
   268
slouken@278
   269
	if ( features.has_alpha ) {
sezeroz@607
   270
		ret = lib.webp_decode_rgba_into( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h,  surface->pitch );
slouken@278
   271
	} else {
sezeroz@607
   272
		ret = lib.webp_decode_rgb_into( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h,  surface->pitch );
slouken@278
   273
	}
slouken@278
   274
slouken@278
   275
	if ( !ret ) {
slouken@278
   276
		error = "Failed to decode WEBP";
slouken@278
   277
		goto error;
slouken@278
   278
	}
slouken@278
   279
sezeroz@607
   280
	if ( raw_data ) {
sezeroz@607
   281
		free( raw_data );
sezeroz@607
   282
	}
sezeroz@607
   283
slouken@278
   284
	return surface;
slouken@278
   285
slouken@278
   286
slouken@278
   287
error:
slouken@278
   288
sezeroz@607
   289
	if ( raw_data ) {
sezeroz@607
   290
		free( raw_data );
sezeroz@607
   291
	}
sezeroz@607
   292
slouken@278
   293
	if ( surface ) {
slouken@278
   294
		SDL_FreeSurface( surface );
slouken@278
   295
	}
slouken@278
   296
slouken@278
   297
	if ( error ) {
slouken@278
   298
		IMG_SetError( error );
slouken@278
   299
	}
slouken@278
   300
slouken@278
   301
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@278
   302
	return(NULL);
slouken@278
   303
}
slouken@278
   304
slouken@278
   305
#else
slouken@278
   306
slouken@278
   307
int IMG_InitWEBP()
slouken@278
   308
{
slouken@278
   309
	IMG_SetError("WEBP images are not supported");
slouken@278
   310
	return(-1);
slouken@278
   311
}
slouken@278
   312
slouken@278
   313
void IMG_QuitWEBP()
slouken@278
   314
{
slouken@278
   315
}
slouken@278
   316
slouken@278
   317
/* See if an image is contained in a data source */
slouken@278
   318
int IMG_isWEBP(SDL_RWops *src)
slouken@278
   319
{
slouken@278
   320
	return(0);
slouken@278
   321
}
slouken@278
   322
slouken@278
   323
/* Load a WEBP type image from an SDL datasource */
slouken@278
   324
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
slouken@278
   325
{
slouken@278
   326
	return(NULL);
slouken@278
   327
}
slouken@278
   328
slouken@278
   329
#endif /* LOAD_WEBP */