~ubuntu-branches/ubuntu/karmic/sdl-image1.2/karmic

« back to all changes in this revision

Viewing changes to IMG_pnm.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Koch
  • Date: 2006-06-09 07:21:47 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060609072147-31buc05gjjhttun0
Tags: 1.2.5-1
* New upstream release
* Updated Build-Depends and Depends to SDL 1.2.10
* Updated minimum shlibs version to 1.2.5
* Updated Standards-Version to 3.7.2
* Simplified watch file
* Fixed address of FSF in debian/copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
    SDL_image:  An example image loading library for use with SDL
3
 
    Copyright (C) 1999-2004 Sam Lantinga
 
3
    Copyright (C) 1997-2006 Sam Lantinga
4
4
 
5
5
    This library is free software; you can redistribute it and/or
6
 
    modify it under the terms of the GNU Library General Public
 
6
    modify it under the terms of the GNU Lesser General Public
7
7
    License as published by the Free Software Foundation; either
8
 
    version 2 of the License, or (at your option) any later version.
 
8
    version 2.1 of the License, or (at your option) any later version.
9
9
 
10
10
    This library is distributed in the hope that it will be useful,
11
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
    Library General Public License for more details.
 
13
    Lesser General Public License for more details.
14
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
 
15
    You should have received a copy of the GNU Lesser General Public
 
16
    License along with this library; if not, write to the Free Software
 
17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 
19
19
    Sam Lantinga
20
20
    slouken@libsdl.org
21
21
*/
22
22
 
23
 
/* $Id: IMG_pnm.c,v 1.5 2004/01/04 22:04:38 slouken Exp $ */
24
 
 
25
23
/*
26
24
 * PNM (portable anymap) image loader:
27
25
 *
42
40
/* See if an image is contained in a data source */
43
41
int IMG_isPNM(SDL_RWops *src)
44
42
{
 
43
        int start;
 
44
        int is_PNM;
45
45
        char magic[2];
46
46
 
47
 
        /*
48
 
         * PNM magic signatures:
49
 
         * P1   PBM, ascii format
50
 
         * P2   PGM, ascii format
51
 
         * P3   PPM, ascii format
52
 
         * P4   PBM, binary format
53
 
         * P5   PGM, binary format
54
 
         * P6   PPM, binary format
55
 
         */
56
 
        return (SDL_RWread(src, magic, 2, 1)
57
 
                && magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6');
 
47
        start = SDL_RWtell(src);
 
48
        is_PNM = 0;
 
49
        if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
 
50
                /*
 
51
                 * PNM magic signatures:
 
52
                 * P1   PBM, ascii format
 
53
                 * P2   PGM, ascii format
 
54
                 * P3   PPM, ascii format
 
55
                 * P4   PBM, binary format
 
56
                 * P5   PGM, binary format
 
57
                 * P6   PPM, binary format
 
58
                 * P7   PAM, a general wrapper for PNM data
 
59
                 */
 
60
                if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
 
61
                        is_PNM = 1;
 
62
                }
 
63
        }
 
64
        SDL_RWseek(src, start, SEEK_SET);
 
65
        return(is_PNM);
58
66
}
59
67
 
60
68
/* read a non-negative integer from the source. return -1 upon error */
96
104
 
97
105
SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
98
106
{
 
107
        int start;
99
108
        SDL_Surface *surface = NULL;
100
109
        int width, height;
101
110
        int maxval, y, bpl;
104
113
        char *error = NULL;
105
114
        Uint8 magic[2];
106
115
        int ascii;
107
 
        enum { PBM, PGM, PPM } kind;
 
116
        enum { PBM, PGM, PPM, PAM } kind;
108
117
 
109
118
#define ERROR(s) do { error = (s); goto done; } while(0)
110
119
 
112
121
                /* The error message has been set in SDL_RWFromFile */
113
122
                return NULL;
114
123
        }
 
124
        start = SDL_RWtell(src);
115
125
 
116
126
        SDL_RWread(src, magic, 2, 1);
117
127
        kind = magic[1] - '1';
220
230
done:
221
231
        free(buf);
222
232
        if(error) {
223
 
                SDL_FreeSurface(surface);
 
233
                SDL_RWseek(src, start, SEEK_SET);
 
234
                if ( surface ) {
 
235
                        SDL_FreeSurface(surface);
 
236
                        surface = NULL;
 
237
                }
224
238
                IMG_SetError(error);
225
 
                surface = NULL;
226
239
        }
227
240
        return(surface);
228
241
}