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