~ubuntu-branches/ubuntu/utopic/bumprace/utopic

« back to all changes in this revision

Viewing changes to src/img.c

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess, Joey Hess, root
  • Date: 2006-10-14 23:00:39 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20061014230039-navrufb60e95o381
Tags: 1.5.1.dfsg-2.1
[ Joey Hess ]
* NMU with patch from tbm to fix FTBFS with current gcc. Closes: #392318

[ root ]
* GNU config automated update: config.sub     (20060223 to 20060920),
  config.guess     (20060223 to 20060702)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    IMGLIB:  An example image loading library for use with SDL
3
 
    Copyright (C) 1999  Sam Lantinga
4
 
 
5
 
    This library is free software; you can redistribute it and/or
6
 
    modify it under the terms of the GNU Library General Public
7
 
    License as published by the Free Software Foundation; either
8
 
    version 2 of the License, or (at your option) any later version.
9
 
 
10
 
    This library is distributed in the hope that it will be useful,
11
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
    Library General Public License for more details.
14
 
 
15
 
    You should have received a copy of the GNU Library General Public
16
 
    License along with this library; if not, write to the Free
17
 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
 
19
 
    Sam Lantinga
20
 
    5635-34 Springhouse Dr.
21
 
    Pleasanton, CA 94588 (USA)
22
 
    slouken@devolution.com
23
 
*/
24
 
 
25
 
/* A simple library to load images of various formats as SDL surfaces */
26
 
 
27
 
#include <stdio.h>
28
 
#include <string.h>
29
 
#include <ctype.h>
30
 
 
31
 
#include "IMG.h"
32
 
 
33
 
/* Table of image detection and loading functions */
34
 
static struct {
35
 
        char *type;
36
 
        int (*is)(SDL_RWops *src);
37
 
        SDL_Surface *(*load)(SDL_RWops *src);
38
 
} supported[] = {
39
 
        /* keep magicless formats first (denoted by is==NULL) */
40
 
        { "GIF", IMG_isGIF, IMG_LoadGIF_RW },
41
 
        { "JPG", IMG_isJPG, IMG_LoadJPG_RW },
42
 
        { "PNG", IMG_isPNG, IMG_LoadPNG_RW },
43
 
};
44
 
 
45
 
/* Load an image from a file */
46
 
SDL_Surface *IMG_Load(const char *file)
47
 
{
48
 
    SDL_RWops *src = SDL_RWFromFile(file, "rb");
49
 
    char *ext = strrchr(file, '.');
50
 
    if(ext)
51
 
        ext++;
52
 
    return IMG_LoadTyped_RW(src, 1, ext);
53
 
}
54
 
 
55
 
/* Load an image from an SDL datasource (for compatibility) */
56
 
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
57
 
{
58
 
    return IMG_LoadTyped_RW(src, freesrc, NULL);
59
 
}
60
 
 
61
 
/* Portable case-insensitive string compare function */
62
 
static int string_equals(const char *str1, const char *str2)
63
 
{
64
 
        while ( *str1 && *str2 ) {
65
 
                if ( toupper((unsigned char)*str1) !=
66
 
                     toupper((unsigned char)*str2) )
67
 
                        break;
68
 
                ++str1;
69
 
                ++str2;
70
 
        }
71
 
        return (!*str1 && !*str2);
72
 
}
73
 
 
74
 
/* Load an image from an SDL datasource, optionally specifying the type */
75
 
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
76
 
{
77
 
        int i, start;
78
 
        SDL_Surface *image;
79
 
 
80
 
        /* Make sure there is something to do.. */
81
 
        if ( src == NULL ) {
82
 
                return(NULL);
83
 
        }
84
 
 
85
 
        /* See whether or not this data source can handle seeking */
86
 
        if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
87
 
                IMG_SetError("Can't seek in this data source");
88
 
                return(NULL);
89
 
        }
90
 
 
91
 
        /* Detect the type of image being loaded */
92
 
        start = SDL_RWtell(src);
93
 
        image = NULL;
94
 
        for ( i=0; supported[i].type && !image; ++i ) {
95
 
                if( (supported[i].is
96
 
                     && (SDL_RWseek(src, start, SEEK_SET),
97
 
                         supported[i].is(src)))
98
 
                    || (type && string_equals(type, supported[i].type))) {
99
 
#ifdef DEBUG_IMGLIB
100
 
                        fprintf(stderr, "IMGLIB: Loading image as %s\n",
101
 
                                                        supported[i].type);
102
 
#endif
103
 
                        SDL_RWseek(src, start, SEEK_SET);
104
 
                        image = supported[i].load(src);
105
 
                        break;
106
 
                }
107
 
        }
108
 
 
109
 
        /* Clean up, check for errors, and return */
110
 
        if ( freesrc ) {
111
 
                SDL_RWclose(src);
112
 
        }
113
 
        if ( image == NULL ) {
114
 
                IMG_SetError("Unsupported image format");
115
 
        }
116
 
        return(image);
117
 
}
118
 
 
119
 
/* Invert the alpha of a surface for use with OpenGL
120
 
   This function is a no-op and only kept for backwards compatibility.
121
 
 */
122
 
int IMG_InvertAlpha(int on)
123
 
{
124
 
    return 1;
125
 
}