IMG_webp.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 10 Mar 2013 09:59:30 -0700
changeset 358 dd40be56d64e
parent 356 1e7a55d77b3c
child 360 3d002acf103d
permissions -rw-r--r--
Better endian surface mask code (taken from IMG_png.c)
slouken@278
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@347
     3
  Copyright (C) 1997-2013 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;
slouken@278
    50
	int/*VP8StatuCode*/ (*webp_get_features_internal) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version);
slouken@278
    51
	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
    52
	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);
slouken@278
    53
} lib;
slouken@278
    54
slouken@278
    55
#ifdef LOAD_WEBP_DYNAMIC
slouken@278
    56
int IMG_InitWEBP()
slouken@278
    57
{
slouken@278
    58
	if ( lib.loaded == 0 ) {
slouken@278
    59
		lib.handle = SDL_LoadObject(LOAD_WEBP_DYNAMIC);
slouken@278
    60
		if ( lib.handle == NULL ) {
slouken@278
    61
			return -1;
slouken@278
    62
		}
slouken@278
    63
		lib.webp_get_features_internal = 
slouken@278
    64
			( int (*) (const uint8_t *, uint32_t, WebPBitstreamFeatures* const, int) )
slouken@278
    65
			SDL_LoadFunction(lib.handle, "WebPGetFeaturesInternal" );
slouken@278
    66
		if ( lib.webp_get_features_internal == NULL ) {
slouken@278
    67
			SDL_UnloadObject(lib.handle);
slouken@278
    68
			return -1;
slouken@278
    69
		}
slouken@278
    70
slouken@278
    71
		lib.webp_decode_rgb_into = 
slouken@278
    72
			( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
slouken@278
    73
			SDL_LoadFunction(lib.handle, "WebPDecodeRGBInto" );
slouken@278
    74
		if ( lib.webp_decode_rgb_into == NULL ) {
slouken@278
    75
			SDL_UnloadObject(lib.handle);
slouken@278
    76
			return -1;
slouken@278
    77
		}
slouken@278
    78
slouken@278
    79
		lib.webp_decode_rgba_into = 
slouken@278
    80
			( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
slouken@278
    81
			SDL_LoadFunction(lib.handle, "WebPDecodeRGBInto" );
slouken@278
    82
		if ( lib.webp_decode_rgba_into == NULL ) {
slouken@278
    83
			SDL_UnloadObject(lib.handle);
slouken@278
    84
			return -1;
slouken@278
    85
		}
slouken@278
    86
	}
slouken@278
    87
	++lib.loaded;
slouken@278
    88
slouken@278
    89
	return 0;
slouken@278
    90
}
slouken@278
    91
void IMG_QuitWEBP()
slouken@278
    92
{
slouken@278
    93
	if ( lib.loaded == 0 ) {
slouken@278
    94
		return;
slouken@278
    95
	}
slouken@278
    96
	if ( lib.loaded == 1 ) {
slouken@278
    97
		SDL_UnloadObject(lib.handle);
slouken@278
    98
	}
slouken@278
    99
	--lib.loaded;
slouken@278
   100
}
slouken@278
   101
#else
slouken@278
   102
int IMG_InitWEBP()
slouken@278
   103
{
slouken@278
   104
	if ( lib.loaded == 0 ) {
slouken@278
   105
		lib.webp_get_features_internal = WebPGetFeaturesInternal;
slouken@278
   106
		lib.webp_decode_rgb_into = WebPDecodeRGBInto;
slouken@278
   107
		lib.webp_decode_rgba_into = WebPDecodeRGBAInto;
slouken@278
   108
	}
slouken@278
   109
	++lib.loaded;
slouken@278
   110
slouken@278
   111
	return 0;
slouken@278
   112
}
slouken@278
   113
void IMG_QuitWEBP()
slouken@278
   114
{
slouken@278
   115
	if ( lib.loaded == 0 ) {
slouken@278
   116
		return;
slouken@278
   117
	}
slouken@278
   118
	if ( lib.loaded == 1 ) {
slouken@278
   119
	}
slouken@278
   120
	--lib.loaded;
slouken@278
   121
}
slouken@278
   122
#endif /* LOAD_WEBP_DYNAMIC */
slouken@278
   123
slouken@278
   124
static int webp_getinfo( SDL_RWops *src, int *datasize ) {
aschiffler@343
   125
	Sint64 start;
slouken@278
   126
	int is_WEBP;
slouken@288
   127
	int data;
slouken@278
   128
	Uint8 magic[20];
slouken@278
   129
slouken@278
   130
	if ( !src )
slouken@278
   131
		return 0;
slouken@278
   132
	start = SDL_RWtell(src);
slouken@278
   133
	is_WEBP = 0;
slouken@278
   134
	if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
slouken@278
   135
		if ( magic[ 0] == 'R' &&
slouken@278
   136
                     magic[ 1] == 'I' &&
slouken@278
   137
                     magic[ 2] == 'F' &&
slouken@278
   138
                     magic[ 3] == 'F' &&
slouken@278
   139
										 magic[ 8] == 'W' &&
slouken@278
   140
										 magic[ 9] == 'E' &&
slouken@278
   141
										 magic[10] == 'B' &&
slouken@278
   142
										 magic[11] == 'P' &&
slouken@278
   143
										 magic[12] == 'V' &&
slouken@278
   144
										 magic[13] == 'P' &&
slouken@278
   145
										 magic[14] == '8' &&
slouken@278
   146
										 magic[15] == ' '  ) {
slouken@278
   147
			is_WEBP = 1;
slouken@288
   148
			data = magic[16] | magic[17]<<8 | magic[18]<<16 | magic[19]<<24;
slouken@278
   149
			if ( datasize )
slouken@278
   150
				*datasize = data;
slouken@278
   151
		}
slouken@278
   152
	}
slouken@278
   153
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@278
   154
	return(is_WEBP);
slouken@278
   155
}
slouken@278
   156
slouken@278
   157
/* See if an image is contained in a data source */
slouken@278
   158
int IMG_isWEBP(SDL_RWops *src)
slouken@278
   159
{
slouken@278
   160
	return webp_getinfo( src, NULL );
slouken@278
   161
}
slouken@278
   162
slouken@278
   163
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
slouken@278
   164
{
aschiffler@343
   165
	Sint64 start;
slouken@278
   166
	const char *error = NULL;
slouken@278
   167
	SDL_Surface *volatile surface = NULL;
slouken@278
   168
	Uint32 Rmask;
slouken@278
   169
	Uint32 Gmask;
slouken@278
   170
	Uint32 Bmask;
slouken@278
   171
	Uint32 Amask;
slouken@288
   172
	WebPBitstreamFeatures features;
slouken@288
   173
	int raw_data_size;
aschiffler@343
   174
	uint8_t *raw_data = NULL;
slouken@358
   175
	int r, s;
slouken@288
   176
	uint8_t *ret;
slouken@278
   177
slouken@278
   178
	if ( !src ) {
slouken@278
   179
		/* The error message has been set in SDL_RWFromFile */
slouken@278
   180
		return NULL;
slouken@278
   181
	}
slouken@278
   182
slouken@278
   183
	start = SDL_RWtell(src);
slouken@278
   184
slouken@278
   185
	if ( !IMG_Init(IMG_INIT_WEBP) ) {
slouken@278
   186
		goto error;
slouken@278
   187
	}
slouken@278
   188
slouken@288
   189
	raw_data_size = -1;
slouken@278
   190
	if ( !webp_getinfo( src, &raw_data_size ) ) {
slouken@278
   191
		error = "Invalid WEBP";
slouken@278
   192
		goto error;
slouken@278
   193
	}
slouken@278
   194
slouken@278
   195
	// skip header
slouken@278
   196
	SDL_RWseek(src, start+20, RW_SEEK_SET );
slouken@278
   197
aschiffler@343
   198
	raw_data = (uint8_t*) SDL_malloc( raw_data_size );
slouken@278
   199
	if ( raw_data == NULL ) {
slouken@278
   200
		error = "Failed to allocate enought buffer for WEBP";
slouken@278
   201
		goto error;
slouken@278
   202
	}
slouken@278
   203
slouken@288
   204
	r = SDL_RWread(src, raw_data, 1, raw_data_size );
slouken@278
   205
	if ( r != raw_data_size ) {
slouken@278
   206
		error = "Failed to read WEBP";
slouken@278
   207
		goto error;
slouken@278
   208
	}
slouken@278
   209
	
slouken@278
   210
#if 0
slouken@278
   211
	// extract size of picture, not interesting since we don't know about alpha channel
slouken@278
   212
	int width = -1, height = -1;
slouken@278
   213
	if ( !WebPGetInfo( raw_data, raw_data_size, &width, &height ) ) {
slouken@278
   214
		printf("WebPGetInfo has failed\n" );
slouken@278
   215
		return NULL;
slouken@278
   216
	}
slouken@278
   217
#endif
slouken@278
   218
slouken@288
   219
	if ( lib.webp_get_features_internal( raw_data, raw_data_size, &features, WEBP_DECODER_ABI_VERSION ) != VP8_STATUS_OK ) {
slouken@278
   220
		error = "WebPGetFeatures has failed";
aschiffler@343
   221
		goto error;
slouken@278
   222
	}
slouken@278
   223
slouken@278
   224
	/* Check if it's ok !*/
slouken@358
   225
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@278
   226
	Rmask = 0x000000FF;
slouken@278
   227
	Gmask = 0x0000FF00;
slouken@278
   228
	Bmask = 0x00FF0000;
slouken@358
   229
	Amask = (features.has_alpha) ? 0xFF000000 : 0;
slouken@358
   230
#else
slouken@358
   231
	s = (features.has_alpha) ? 0 : 8;
slouken@358
   232
	Rmask = 0xFF000000 >> s;
slouken@358
   233
	Gmask = 0x00FF0000 >> s;
slouken@358
   234
	Bmask = 0x0000FF00 >> s;
slouken@358
   235
	Amask = 0x000000FF >> s;
slouken@355
   236
#endif
slouken@278
   237
slouken@320
   238
	surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
slouken@358
   239
			features.width, features.height,
slouken@278
   240
			features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
slouken@278
   241
slouken@278
   242
	if ( surface == NULL ) {
slouken@278
   243
		error = "Failed to allocate SDL_Surface";
slouken@278
   244
		goto error;
slouken@278
   245
	}
slouken@278
   246
slouken@278
   247
	if ( features.has_alpha ) {
aschiffler@343
   248
		ret = lib.webp_decode_rgba_into( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h,  surface->pitch );
slouken@278
   249
	} else {
aschiffler@343
   250
		ret = lib.webp_decode_rgb_into( raw_data, raw_data_size, (uint8_t *)surface->pixels, surface->pitch * surface->h,  surface->pitch );
slouken@278
   251
	}
slouken@278
   252
slouken@278
   253
	if ( !ret ) {
slouken@278
   254
		error = "Failed to decode WEBP";
slouken@278
   255
		goto error;
slouken@278
   256
	}
slouken@278
   257
slouken@278
   258
	return surface;
slouken@278
   259
slouken@278
   260
slouken@278
   261
error:
slouken@278
   262
slouken@278
   263
	if ( surface ) {
slouken@278
   264
		SDL_FreeSurface( surface );
slouken@278
   265
	}
slouken@278
   266
slouken@278
   267
	if ( raw_data ) {
aschiffler@343
   268
		SDL_free( raw_data );
slouken@278
   269
	}
slouken@278
   270
slouken@278
   271
	if ( error ) {
slouken@278
   272
		IMG_SetError( error );
slouken@278
   273
	}
slouken@278
   274
slouken@278
   275
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@278
   276
	return(NULL);
slouken@278
   277
}
slouken@278
   278
slouken@278
   279
#else
slouken@278
   280
slouken@278
   281
int IMG_InitWEBP()
slouken@278
   282
{
slouken@278
   283
	IMG_SetError("WEBP images are not supported");
slouken@278
   284
	return(-1);
slouken@278
   285
}
slouken@278
   286
slouken@278
   287
void IMG_QuitWEBP()
slouken@278
   288
{
slouken@278
   289
}
slouken@278
   290
slouken@278
   291
/* See if an image is contained in a data source */
slouken@278
   292
int IMG_isWEBP(SDL_RWops *src)
slouken@278
   293
{
slouken@278
   294
	return(0);
slouken@278
   295
}
slouken@278
   296
slouken@278
   297
/* Load a WEBP type image from an SDL datasource */
slouken@278
   298
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
slouken@278
   299
{
slouken@278
   300
	return(NULL);
slouken@278
   301
}
slouken@278
   302
slouken@278
   303
#endif /* LOAD_WEBP */