Skip to content

Latest commit

 

History

History
175 lines (147 loc) · 3.94 KB

IMG_ppm.c

File metadata and controls

175 lines (147 loc) · 3.94 KB
 
Aug 10, 2000
Aug 10, 2000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
IMGLIB: An example image loading library for use with SDL
Copyright (C) 1999 Sam Lantinga
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
5635-34 Springhouse Dr.
Pleasanton, CA 94588 (USA)
slouken@devolution.com
*/
/* This is a PPM image file loading framework */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "SDL_image.h"
#ifdef LOAD_PPM
/* See if an image is contained in a data source */
int IMG_isPPM(SDL_RWops *src)
{
int is_PPM;
char magic[2];
is_PPM = 0;
if ( SDL_RWread(src, magic, 2, 1) ) {
if ( (strncmp(magic, "P6", 2) == 0) ||
(strncmp(magic, "P3", 2) == 0) ) {
is_PPM = 1;
}
/* P3 is the ASCII PPM format, which is not yet supported */
if ( strncmp(magic, "P3", 2) == 0 ) {
is_PPM = 0;
}
}
return(is_PPM);
}
/* Load a PPM type image from an SDL datasource */
static int ReadNumber(SDL_RWops *src)
{
int number;
unsigned char ch;
/* Initialize return value */
number = 0;
/* Skip leading whitespace */
do {
if ( ! SDL_RWread(src, &ch, 1, 1) ) {
return(0);
}
/* Eat comments as whitespace */
if ( ch == '#' ) { /* Comment is '#' to end of line */
do {
if ( ! SDL_RWread(src, &ch, 1, 1) ) {
return(0);
}
} while ( (ch != '\r') && (ch != '\n') );
}
} while ( isspace(ch) );
/* Add up the number */
do {
number *= 10;
number += ch-'0';
if ( ! SDL_RWread(src, &ch, 1, 1) ) {
return(0);
}
} while ( isdigit(ch) );
return(number);
}
SDL_Surface *IMG_LoadPPM_RW(SDL_RWops *src)
{
SDL_Surface *surface;
int width, height;
int maxval, x, y;
Uint32 *row;
Uint8 rgb[3];
int read_error;
/* Initialize the data we will clean up when we're done */
surface = NULL;
read_error = 0;
/* Check to make sure we have something to do */
if ( ! src ) {
goto done;
}
/* Read the magic header */
SDL_RWread(src, rgb, 2, 1);
/* Read the width and height */
width = ReadNumber(src);
height = ReadNumber(src);
if ( ! width || ! height ) {
IMG_SetError("Unable to read image width and height");
goto done;
}
/* Read the maximum color component value, and skip to image data */
maxval = ReadNumber(src);
do {
if ( ! SDL_RWread(src, rgb, 1, 1) ) {
IMG_SetError("Unable to read max color component");
goto done;
}
} while ( isspace(rgb[0]) );
SDL_RWseek(src, -1, SEEK_CUR);
/* Create the surface of the appropriate type */
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 32,
0x00FF0000,0x0000FF00,0x000000FF,0);
if ( surface == NULL ) {
IMG_SetError("Out of memory");
goto done;
}
/* Read the image into the surface */
for ( y=0; y<surface->h; ++y ) {
row = (Uint32 *)((Uint8 *)surface->pixels + y*surface->pitch);
for ( x=0; x<surface->w; ++x ) {
if ( ! SDL_RWread(src, rgb, 3, 1) ) {
read_error = 1;
goto done;
}
*row++ = ((((Uint32)rgb[0])<<16)|
(((Uint32)rgb[1])<< 8)|
(((Uint32)rgb[2]) ));
}
}
done:
if ( read_error ) {
SDL_FreeSurface(surface);
IMG_SetError("Error reading PPM data");
surface = NULL;
}
return(surface);
}
#else
/* See if an image is contained in a data source */
int IMG_isPPM(SDL_RWops *src)
{
return(0);
}
/* Load a PPM type image from an SDL datasource */
SDL_Surface *IMG_LoadPPM_RW(SDL_RWops *src)
{
return(NULL);
}
#endif /* LOAD_PPM */