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