IMG.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 12 Dec 2017 22:18:18 -0800
changeset 550 b5bcb9cb9f6d
parent 528 7f9e88f4b45e
child 575 36e9e2255178
permissions -rwxr-xr-x
Fixed iOS build warnings
slouken@0
     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@0
     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@0
     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@0
    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@0
    20
*/
slouken@0
    21
slouken@0
    22
/* A simple library to load images of various formats as SDL surfaces */
slouken@0
    23
slouken@0
    24
#include "SDL_image.h"
slouken@0
    25
slouken@7
    26
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
slouken@7
    27
slouken@0
    28
/* Table of image detection and loading functions */
slouken@0
    29
static struct {
slouken@503
    30
    const char *type;
slouken@368
    31
    int (SDLCALL *is)(SDL_RWops *src);
slouken@368
    32
    SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
slouken@0
    33
} supported[] = {
slouken@368
    34
    /* keep magicless formats first */
slouken@368
    35
    { "TGA", NULL,      IMG_LoadTGA_RW },
slouken@368
    36
    { "CUR", IMG_isCUR, IMG_LoadCUR_RW },
slouken@368
    37
    { "ICO", IMG_isICO, IMG_LoadICO_RW },
slouken@368
    38
    { "BMP", IMG_isBMP, IMG_LoadBMP_RW },
slouken@368
    39
    { "GIF", IMG_isGIF, IMG_LoadGIF_RW },
slouken@368
    40
    { "JPG", IMG_isJPG, IMG_LoadJPG_RW },
slouken@368
    41
    { "LBM", IMG_isLBM, IMG_LoadLBM_RW },
slouken@368
    42
    { "PCX", IMG_isPCX, IMG_LoadPCX_RW },
slouken@368
    43
    { "PNG", IMG_isPNG, IMG_LoadPNG_RW },
slouken@368
    44
    { "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
slouken@524
    45
    { "SVG", IMG_isSVG, IMG_LoadSVG_RW },
slouken@368
    46
    { "TIF", IMG_isTIF, IMG_LoadTIF_RW },
slouken@368
    47
    { "XCF", IMG_isXCF, IMG_LoadXCF_RW },
slouken@368
    48
    { "XPM", IMG_isXPM, IMG_LoadXPM_RW },
slouken@368
    49
    { "XV",  IMG_isXV,  IMG_LoadXV_RW  },
slouken@368
    50
    { "WEBP", IMG_isWEBP, IMG_LoadWEBP_RW },
slouken@0
    51
};
slouken@0
    52
slouken@89
    53
const SDL_version *IMG_Linked_Version(void)
slouken@89
    54
{
slouken@368
    55
    static SDL_version linked_version;
slouken@368
    56
    SDL_IMAGE_VERSION(&linked_version);
slouken@368
    57
    return(&linked_version);
slouken@89
    58
}
slouken@89
    59
slouken@550
    60
extern int IMG_InitJPG(void);
slouken@550
    61
extern void IMG_QuitJPG(void);
slouken@550
    62
extern int IMG_InitPNG(void);
slouken@550
    63
extern void IMG_QuitPNG(void);
slouken@550
    64
extern int IMG_InitTIF(void);
slouken@550
    65
extern void IMG_QuitTIF(void);
slouken@205
    66
slouken@550
    67
extern int IMG_InitWEBP(void);
slouken@550
    68
extern void IMG_QuitWEBP(void);
slouken@278
    69
slouken@205
    70
static int initialized = 0;
slouken@205
    71
slouken@205
    72
int IMG_Init(int flags)
slouken@205
    73
{
slouken@368
    74
    int result = 0;
slouken@205
    75
slouken@464
    76
    /* Passing 0 returns the currently initialized loaders */
slouken@464
    77
    if (!flags) {
slouken@464
    78
        return initialized;
slouken@464
    79
    }
slouken@464
    80
slouken@368
    81
    if (flags & IMG_INIT_JPG) {
slouken@368
    82
        if ((initialized & IMG_INIT_JPG) || IMG_InitJPG() == 0) {
slouken@368
    83
            result |= IMG_INIT_JPG;
slouken@368
    84
        }
slouken@368
    85
    }
slouken@368
    86
    if (flags & IMG_INIT_PNG) {
slouken@368
    87
        if ((initialized & IMG_INIT_PNG) || IMG_InitPNG() == 0) {
slouken@368
    88
            result |= IMG_INIT_PNG;
slouken@368
    89
        }
slouken@368
    90
    }
slouken@368
    91
    if (flags & IMG_INIT_TIF) {
slouken@368
    92
        if ((initialized & IMG_INIT_TIF) || IMG_InitTIF() == 0) {
slouken@368
    93
            result |= IMG_INIT_TIF;
slouken@368
    94
        }
slouken@368
    95
    }
slouken@368
    96
    if (flags & IMG_INIT_WEBP) {
slouken@368
    97
        if ((initialized & IMG_INIT_WEBP) || IMG_InitWEBP() == 0) {
slouken@368
    98
            result |= IMG_INIT_WEBP;
slouken@368
    99
        }
slouken@368
   100
    }
slouken@368
   101
    initialized |= result;
slouken@205
   102
slouken@464
   103
    return result;
slouken@205
   104
}
slouken@205
   105
slouken@205
   106
void IMG_Quit()
slouken@205
   107
{
slouken@368
   108
    if (initialized & IMG_INIT_JPG) {
slouken@368
   109
        IMG_QuitJPG();
slouken@368
   110
    }
slouken@368
   111
    if (initialized & IMG_INIT_PNG) {
slouken@368
   112
        IMG_QuitPNG();
slouken@368
   113
    }
slouken@368
   114
    if (initialized & IMG_INIT_TIF) {
slouken@368
   115
        IMG_QuitTIF();
slouken@368
   116
    }
slouken@368
   117
    if (initialized & IMG_INIT_WEBP) {
slouken@368
   118
        IMG_QuitWEBP();
slouken@368
   119
    }
slouken@368
   120
    initialized = 0;
slouken@205
   121
}
slouken@205
   122
slouken@187
   123
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
slouken@0
   124
/* Load an image from a file */
slouken@0
   125
SDL_Surface *IMG_Load(const char *file)
slouken@0
   126
{
slouken@0
   127
    SDL_RWops *src = SDL_RWFromFile(file, "rb");
slouken@481
   128
    const char *ext = SDL_strrchr(file, '.');
slouken@95
   129
    if(ext) {
slouken@95
   130
        ext++;
slouken@95
   131
    }
slouken@95
   132
    if(!src) {
slouken@95
   133
        /* The error message has been set in SDL_RWFromFile */
slouken@95
   134
        return NULL;
slouken@95
   135
    }
slouken@0
   136
    return IMG_LoadTyped_RW(src, 1, ext);
slouken@0
   137
}
slouken@187
   138
#endif
slouken@0
   139
slouken@0
   140
/* Load an image from an SDL datasource (for compatibility) */
slouken@0
   141
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
slouken@0
   142
{
slouken@0
   143
    return IMG_LoadTyped_RW(src, freesrc, NULL);
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
/* Portable case-insensitive string compare function */
slouken@50
   147
static int IMG_string_equals(const char *str1, const char *str2)
slouken@0
   148
{
slouken@368
   149
    while ( *str1 && *str2 ) {
slouken@481
   150
        if ( SDL_toupper((unsigned char)*str1) !=
slouken@481
   151
             SDL_toupper((unsigned char)*str2) )
slouken@368
   152
            break;
slouken@368
   153
        ++str1;
slouken@368
   154
        ++str2;
slouken@368
   155
    }
slouken@368
   156
    return (!*str1 && !*str2);
slouken@0
   157
}
slouken@0
   158
slouken@0
   159
/* Load an image from an SDL datasource, optionally specifying the type */
slouken@320
   160
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type)
slouken@0
   161
{
slouken@368
   162
    int i;
slouken@368
   163
    SDL_Surface *image;
slouken@0
   164
slouken@368
   165
    /* Make sure there is something to do.. */
slouken@368
   166
    if ( src == NULL ) {
slouken@368
   167
        IMG_SetError("Passed a NULL data source");
slouken@368
   168
        return(NULL);
slouken@368
   169
    }
slouken@0
   170
slouken@368
   171
    /* See whether or not this data source can handle seeking */
slouken@368
   172
    if ( SDL_RWseek(src, 0, RW_SEEK_CUR) < 0 ) {
slouken@368
   173
        IMG_SetError("Can't seek in this data source");
slouken@368
   174
        if(freesrc)
slouken@368
   175
            SDL_RWclose(src);
slouken@368
   176
        return(NULL);
slouken@368
   177
    }
slouken@0
   178
slouken@368
   179
    /* Detect the type of image being loaded */
slouken@368
   180
    image = NULL;
slouken@368
   181
    for ( i=0; i < ARRAYSIZE(supported); ++i ) {
slouken@368
   182
        if(supported[i].is) {
slouken@368
   183
            if(!supported[i].is(src))
slouken@368
   184
                continue;
slouken@368
   185
        } else {
slouken@368
   186
            /* magicless format */
slouken@368
   187
            if(!type
slouken@368
   188
               || !IMG_string_equals(type, supported[i].type))
slouken@368
   189
                continue;
slouken@368
   190
        }
slouken@0
   191
#ifdef DEBUG_IMGLIB
slouken@368
   192
        fprintf(stderr, "IMGLIB: Loading image as %s\n",
slouken@368
   193
            supported[i].type);
slouken@0
   194
#endif
slouken@368
   195
        image = supported[i].load(src);
slouken@368
   196
        if(freesrc)
slouken@368
   197
            SDL_RWclose(src);
slouken@368
   198
        return image;
slouken@368
   199
    }
slouken@0
   200
slouken@368
   201
    if ( freesrc ) {
slouken@368
   202
        SDL_RWclose(src);
slouken@368
   203
    }
slouken@368
   204
    IMG_SetError("Unsupported image format");
slouken@368
   205
    return NULL;
slouken@0
   206
}
slouken@0
   207
slouken@393
   208
#if SDL_VERSION_ATLEAST(2,0,0)
slouken@320
   209
SDL_Texture *IMG_LoadTexture(SDL_Renderer *renderer, const char *file)
slouken@320
   210
{
slouken@320
   211
    SDL_Texture *texture = NULL;
slouken@320
   212
    SDL_Surface *surface = IMG_Load(file);
slouken@320
   213
    if (surface) {
slouken@320
   214
        texture = SDL_CreateTextureFromSurface(renderer, surface);
slouken@320
   215
        SDL_FreeSurface(surface);
slouken@320
   216
    }
slouken@320
   217
    return texture;
slouken@320
   218
}
slouken@320
   219
slouken@320
   220
SDL_Texture *IMG_LoadTexture_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc)
slouken@320
   221
{
slouken@320
   222
    SDL_Texture *texture = NULL;
slouken@320
   223
    SDL_Surface *surface = IMG_Load_RW(src, freesrc);
slouken@320
   224
    if (surface) {
slouken@320
   225
        texture = SDL_CreateTextureFromSurface(renderer, surface);
slouken@320
   226
        SDL_FreeSurface(surface);
slouken@320
   227
    }
slouken@320
   228
    return texture;
slouken@320
   229
}
slouken@320
   230
slouken@320
   231
SDL_Texture *IMG_LoadTextureTyped_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc, const char *type)
slouken@320
   232
{
slouken@320
   233
    SDL_Texture *texture = NULL;
slouken@320
   234
    SDL_Surface *surface = IMG_LoadTyped_RW(src, freesrc, type);
slouken@320
   235
    if (surface) {
slouken@320
   236
        texture = SDL_CreateTextureFromSurface(renderer, surface);
slouken@320
   237
        SDL_FreeSurface(surface);
slouken@320
   238
    }
slouken@320
   239
    return texture;
slouken@320
   240
}
slouken@393
   241
#endif /* SDL 2.0 */