IMG_webp.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Oct 2017 02:05:28 -0700
changeset 531 4491ac456363
parent 528 7f9e88f4b45e
child 537 8959a43f5590
permissions -rwxr-xr-x
Fixed bug 3425 - Issue with loading WEBP images with RWops

Sylvain

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