IMG_webp.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 30 Dec 2011 15:57:44 -0500
changeset 278 2fd74c55f313
child 280 ec4ae96c100c
permissions -rw-r--r--
Michael Bonfils added WEBP image support
slouken@278
     1
/*
slouken@278
     2
    SDL_image:  An example image loading library for use with SDL
slouken@278
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@278
     4
slouken@278
     5
    This library is free software; you can redistribute it and/or
slouken@278
     6
    modify it under the terms of the GNU Lesser General Public
slouken@278
     7
    License as published by the Free Software Foundation; either
slouken@278
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@278
     9
slouken@278
    10
    This library is distributed in the hope that it will be useful,
slouken@278
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@278
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@278
    13
    Lesser General Public License for more details.
slouken@278
    14
slouken@278
    15
    You should have received a copy of the GNU Lesser General Public
slouken@278
    16
    License along with this library; if not, write to the Free Software
slouken@278
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@278
    18
slouken@278
    19
    Sam Lantinga
slouken@278
    20
    slouken@libsdl.org
slouken@278
    21
*/
slouken@278
    22
slouken@278
    23
/* This is a WEBP image file loading framework */
slouken@278
    24
slouken@278
    25
#include <stdlib.h>
slouken@278
    26
#include <stdio.h>
slouken@278
    27
slouken@278
    28
#include "SDL_image.h"
slouken@278
    29
slouken@278
    30
#ifdef LOAD_WEBP
slouken@278
    31
slouken@278
    32
/*=============================================================================
slouken@278
    33
        File: SDL_webp.c
slouken@278
    34
     Purpose: A WEBP loader for the SDL library      
slouken@278
    35
    Revision: 
slouken@278
    36
  Created by: Michael Bonfils (Murlock) (26 November 2011)
slouken@278
    37
              murlock42@gmail.com
slouken@278
    38
 Modified by: 
slouken@278
    39
slouken@278
    40
 Copyright notice:
slouken@278
    41
          This library is free software; you can redistribute it and/or
slouken@278
    42
          modify it under the terms of the GNU Library General Public
slouken@278
    43
          License as published by the Free Software Foundation; either
slouken@278
    44
          version 2 of the License, or (at your option) any later version.
slouken@278
    45
 
slouken@278
    46
          This library is distributed in the hope that it will be useful,
slouken@278
    47
          but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@278
    48
          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@278
    49
          Library General Public License for more details.
slouken@278
    50
 
slouken@278
    51
          You should have received a copy of the GNU Library General Public
slouken@278
    52
          License along with this library; if not, write to the Free
slouken@278
    53
          Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
slouken@278
    54
slouken@278
    55
    Comments:
slouken@278
    56
            
slouken@278
    57
slouken@278
    58
  Changes:
slouken@278
    59
slouken@278
    60
=============================================================================*/
slouken@278
    61
slouken@278
    62
#include "SDL_endian.h"
slouken@278
    63
slouken@278
    64
#ifdef macintosh
slouken@278
    65
#define MACOS
slouken@278
    66
#endif
slouken@278
    67
#include <webp/decode.h>
slouken@278
    68
slouken@278
    69
static struct {
slouken@278
    70
	int loaded;
slouken@278
    71
	void *handle;
slouken@278
    72
	int/*VP8StatuCode*/ (*webp_get_features_internal) (const uint8_t *data, uint32_t data_size, WebPBitstreamFeatures* const features, int decoder_abi_version);
slouken@278
    73
	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
    74
	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
    75
} lib;
slouken@278
    76
slouken@278
    77
#ifdef LOAD_WEBP_DYNAMIC
slouken@278
    78
int IMG_InitWEBP()
slouken@278
    79
{
slouken@278
    80
	if ( lib.loaded == 0 ) {
slouken@278
    81
		lib.handle = SDL_LoadObject(LOAD_WEBP_DYNAMIC);
slouken@278
    82
		if ( lib.handle == NULL ) {
slouken@278
    83
			return -1;
slouken@278
    84
		}
slouken@278
    85
		lib.webp_get_features_internal = 
slouken@278
    86
			( int (*) (const uint8_t *, uint32_t, WebPBitstreamFeatures* const, int) )
slouken@278
    87
			SDL_LoadFunction(lib.handle, "WebPGetFeaturesInternal" );
slouken@278
    88
		if ( lib.webp_get_features_internal == NULL ) {
slouken@278
    89
			SDL_UnloadObject(lib.handle);
slouken@278
    90
			return -1;
slouken@278
    91
		}
slouken@278
    92
slouken@278
    93
		lib.webp_decode_rgb_into = 
slouken@278
    94
			( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
slouken@278
    95
			SDL_LoadFunction(lib.handle, "WebPDecodeRGBInto" );
slouken@278
    96
		if ( lib.webp_decode_rgb_into == NULL ) {
slouken@278
    97
			SDL_UnloadObject(lib.handle);
slouken@278
    98
			return -1;
slouken@278
    99
		}
slouken@278
   100
slouken@278
   101
		lib.webp_decode_rgba_into = 
slouken@278
   102
			( uint8_t* (*) (const uint8_t*, uint32_t, uint8_t*, int, int ) )
slouken@278
   103
			SDL_LoadFunction(lib.handle, "WebPDecodeRGBInto" );
slouken@278
   104
		if ( lib.webp_decode_rgba_into == NULL ) {
slouken@278
   105
			SDL_UnloadObject(lib.handle);
slouken@278
   106
			return -1;
slouken@278
   107
		}
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
		SDL_UnloadObject(lib.handle);
slouken@278
   120
	}
slouken@278
   121
	--lib.loaded;
slouken@278
   122
}
slouken@278
   123
#else
slouken@278
   124
int IMG_InitWEBP()
slouken@278
   125
{
slouken@278
   126
	if ( lib.loaded == 0 ) {
slouken@278
   127
		lib.webp_get_features_internal = WebPGetFeaturesInternal;
slouken@278
   128
		lib.webp_decode_rgb_into = WebPDecodeRGBInto;
slouken@278
   129
		lib.webp_decode_rgba_into = WebPDecodeRGBAInto;
slouken@278
   130
	}
slouken@278
   131
	++lib.loaded;
slouken@278
   132
slouken@278
   133
	return 0;
slouken@278
   134
}
slouken@278
   135
void IMG_QuitWEBP()
slouken@278
   136
{
slouken@278
   137
	if ( lib.loaded == 0 ) {
slouken@278
   138
		return;
slouken@278
   139
	}
slouken@278
   140
	if ( lib.loaded == 1 ) {
slouken@278
   141
	}
slouken@278
   142
	--lib.loaded;
slouken@278
   143
}
slouken@278
   144
#endif /* LOAD_WEBP_DYNAMIC */
slouken@278
   145
slouken@278
   146
static int webp_getinfo( SDL_RWops *src, int *datasize ) {
slouken@278
   147
	int start;
slouken@278
   148
	int is_WEBP;
slouken@278
   149
	Uint8 magic[20];
slouken@278
   150
slouken@278
   151
	if ( !src )
slouken@278
   152
		return 0;
slouken@278
   153
	start = SDL_RWtell(src);
slouken@278
   154
	is_WEBP = 0;
slouken@278
   155
	if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
slouken@278
   156
		if ( magic[ 0] == 'R' &&
slouken@278
   157
                     magic[ 1] == 'I' &&
slouken@278
   158
                     magic[ 2] == 'F' &&
slouken@278
   159
                     magic[ 3] == 'F' &&
slouken@278
   160
										 magic[ 8] == 'W' &&
slouken@278
   161
										 magic[ 9] == 'E' &&
slouken@278
   162
										 magic[10] == 'B' &&
slouken@278
   163
										 magic[11] == 'P' &&
slouken@278
   164
										 magic[12] == 'V' &&
slouken@278
   165
										 magic[13] == 'P' &&
slouken@278
   166
										 magic[14] == '8' &&
slouken@278
   167
										 magic[15] == ' '  ) {
slouken@278
   168
			is_WEBP = 1;
slouken@278
   169
			int data = magic[16] | magic[17]<<8 | magic[18]<<16 | magic[19]<<24;
slouken@278
   170
			if ( datasize )
slouken@278
   171
				*datasize = data;
slouken@278
   172
		}
slouken@278
   173
	}
slouken@278
   174
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@278
   175
	return(is_WEBP);
slouken@278
   176
}
slouken@278
   177
slouken@278
   178
/* See if an image is contained in a data source */
slouken@278
   179
int IMG_isWEBP(SDL_RWops *src)
slouken@278
   180
{
slouken@278
   181
	return webp_getinfo( src, NULL );
slouken@278
   182
}
slouken@278
   183
slouken@278
   184
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
slouken@278
   185
{
slouken@278
   186
	int start;
slouken@278
   187
	const char *error = NULL;
slouken@278
   188
	SDL_Surface *volatile surface = NULL;
slouken@278
   189
	Uint32 Rmask;
slouken@278
   190
	Uint32 Gmask;
slouken@278
   191
	Uint32 Bmask;
slouken@278
   192
	Uint32 Amask;
slouken@278
   193
slouken@278
   194
	if ( !src ) {
slouken@278
   195
		/* The error message has been set in SDL_RWFromFile */
slouken@278
   196
		return NULL;
slouken@278
   197
	}
slouken@278
   198
slouken@278
   199
	start = SDL_RWtell(src);
slouken@278
   200
slouken@278
   201
	if ( !IMG_Init(IMG_INIT_WEBP) ) {
slouken@278
   202
		goto error;
slouken@278
   203
	}
slouken@278
   204
slouken@278
   205
slouken@278
   206
	int raw_data_size = -1;
slouken@278
   207
	if ( !webp_getinfo( src, &raw_data_size ) ) {
slouken@278
   208
		error = "Invalid WEBP";
slouken@278
   209
		goto error;
slouken@278
   210
	}
slouken@278
   211
slouken@278
   212
	// skip header
slouken@278
   213
	SDL_RWseek(src, start+20, RW_SEEK_SET );
slouken@278
   214
slouken@278
   215
	uint8_t *raw_data = (uint8_t*) malloc( raw_data_size );
slouken@278
   216
	if ( raw_data == NULL ) {
slouken@278
   217
		error = "Failed to allocate enought buffer for WEBP";
slouken@278
   218
		goto error;
slouken@278
   219
	}
slouken@278
   220
slouken@278
   221
	int r = SDL_RWread(src, raw_data, 1, raw_data_size );
slouken@278
   222
	if ( r != raw_data_size ) {
slouken@278
   223
		error = "Failed to read WEBP";
slouken@278
   224
		goto error;
slouken@278
   225
	}
slouken@278
   226
	
slouken@278
   227
#if 0
slouken@278
   228
	// extract size of picture, not interesting since we don't know about alpha channel
slouken@278
   229
	int width = -1, height = -1;
slouken@278
   230
	if ( !WebPGetInfo( raw_data, raw_data_size, &width, &height ) ) {
slouken@278
   231
		printf("WebPGetInfo has failed\n" );
slouken@278
   232
		return NULL;
slouken@278
   233
	}
slouken@278
   234
#endif
slouken@278
   235
slouken@278
   236
	WebPBitstreamFeatures features;
slouken@278
   237
  if ( lib.webp_get_features_internal( raw_data, raw_data_size, &features, WEBP_DECODER_ABI_VERSION ) != VP8_STATUS_OK ) {
slouken@278
   238
		error = "WebPGetFeatures has failed";
slouken@278
   239
		return NULL;
slouken@278
   240
	}
slouken@278
   241
slouken@278
   242
	/* Check if it's ok !*/
slouken@278
   243
	Rmask = 0x000000FF;
slouken@278
   244
	Gmask = 0x0000FF00;
slouken@278
   245
	Bmask = 0x00FF0000;
slouken@278
   246
	Amask = features.has_alpha?0xFF000001:0;
slouken@278
   247
slouken@278
   248
	surface = SDL_AllocSurface(SDL_SWSURFACE, features.width, features.height,
slouken@278
   249
			features.has_alpha?32:24, Rmask,Gmask,Bmask,Amask);
slouken@278
   250
slouken@278
   251
	if ( surface == NULL ) {
slouken@278
   252
		error = "Failed to allocate SDL_Surface";
slouken@278
   253
		goto error;
slouken@278
   254
	}
slouken@278
   255
slouken@278
   256
slouken@278
   257
	uint8_t *ret = NULL;
slouken@278
   258
	if ( features.has_alpha ) {
slouken@278
   259
		ret = lib.webp_decode_rgba_into( raw_data, raw_data_size, surface->pixels, surface->pitch * surface->h,  surface->pitch );
slouken@278
   260
	} else {
slouken@278
   261
		ret = lib.webp_decode_rgb_into( raw_data, raw_data_size, surface->pixels, surface->pitch * surface->h,  surface->pitch );
slouken@278
   262
	}
slouken@278
   263
slouken@278
   264
	if ( !ret ) {
slouken@278
   265
		error = "Failed to decode WEBP";
slouken@278
   266
		goto error;
slouken@278
   267
	}
slouken@278
   268
slouken@278
   269
	return surface;
slouken@278
   270
slouken@278
   271
slouken@278
   272
error:
slouken@278
   273
slouken@278
   274
	if ( surface ) {
slouken@278
   275
		SDL_FreeSurface( surface );
slouken@278
   276
	}
slouken@278
   277
slouken@278
   278
	if ( raw_data ) {
slouken@278
   279
		free( raw_data );
slouken@278
   280
	}
slouken@278
   281
slouken@278
   282
	if ( error ) {
slouken@278
   283
		IMG_SetError( error );
slouken@278
   284
	}
slouken@278
   285
slouken@278
   286
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@278
   287
	return(NULL);
slouken@278
   288
}
slouken@278
   289
slouken@278
   290
#else
slouken@278
   291
slouken@278
   292
int IMG_InitWEBP()
slouken@278
   293
{
slouken@278
   294
	IMG_SetError("WEBP images are not supported");
slouken@278
   295
	return(-1);
slouken@278
   296
}
slouken@278
   297
slouken@278
   298
void IMG_QuitWEBP()
slouken@278
   299
{
slouken@278
   300
}
slouken@278
   301
slouken@278
   302
/* See if an image is contained in a data source */
slouken@278
   303
int IMG_isWEBP(SDL_RWops *src)
slouken@278
   304
{
slouken@278
   305
	return(0);
slouken@278
   306
}
slouken@278
   307
slouken@278
   308
/* Load a WEBP type image from an SDL datasource */
slouken@278
   309
SDL_Surface *IMG_LoadWEBP_RW(SDL_RWops *src)
slouken@278
   310
{
slouken@278
   311
	return(NULL);
slouken@278
   312
}
slouken@278
   313
slouken@278
   314
#endif /* LOAD_WEBP */