/
IMG_ppm.c
187 lines (160 loc) · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
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
*/
25
26
27
28
29
30
31
32
33
/*
* PPM (portable pixmap) image loader:
*
* Supports: ASCII (P3) and binary (P6) formats
* Does not support: maximum component value > 255
*
* TODO: add PGM (greyscale) and PBM (monochrome bitmap) support.
* Should be easy since almost all code is already in place
*/
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#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)
{
char magic[2];
48
49
50
/* "P3" is the ASCII PPM format, "P6" the binary PPM format */
return (SDL_RWread(src, magic, 2, 1)
&& magic[0] == 'P' && (magic[1] == '3' || magic[1] == '6'));
51
52
}
53
/* read a non-negative integer from the source. return -1 upon error */
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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) ) {
71
return -1;
72
73
74
75
76
77
78
79
80
81
}
} while ( (ch != '\r') && (ch != '\n') );
}
} while ( isspace(ch) );
/* Add up the number */
do {
number *= 10;
number += ch-'0';
82
83
if ( !SDL_RWread(src, &ch, 1, 1) ) {
return -1;
84
85
86
87
88
}
} while ( isdigit(ch) );
return(number);
}
89
90
91
SDL_Surface *IMG_LoadPPM_RW(SDL_RWops *src)
{
92
SDL_Surface *surface = NULL;
93
int width, height;
94
int maxval, y, bpl;
95
Uint8 *row;
96
97
98
char *error = NULL;
Uint8 magic[2];
int ascii;
99
100
101
102
103
if ( ! src ) {
goto done;
}
104
105
SDL_RWread(src, magic, 2, 1);
ascii = (magic[1] == '3');
106
107
108
width = ReadNumber(src);
height = ReadNumber(src);
109
110
if(width <= 0 || height <= 0) {
error = "Unable to read image width and height";
111
112
113
114
goto done;
}
maxval = ReadNumber(src);
115
116
117
118
119
120
if(maxval <= 0 || maxval > 255) {
error = "unsupported ppm format";
goto done;
}
/* binary PPM allows just a single character of whitespace after
the maxval, and we've already consumed it */
121
122
/* 24-bit surface in R,G,B byte order */
123
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24,
124
125
126
127
128
129
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000ff, 0x0000ff00, 0x00ff0000,
#else
0x00ff0000, 0x0000ff00, 0x000000ff,
#endif
0);
130
if ( surface == NULL ) {
131
error = "Out of memory";
132
133
134
135
goto done;
}
/* Read the image into the surface */
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
row = surface->pixels;
bpl = width * 3;
for(y = 0; y < height; y++) {
if(ascii) {
int i;
for(i = 0; i < bpl; i++) {
int c;
c = ReadNumber(src);
if(c < 0) {
error = "file truncated";
goto done;
}
row[i] = c;
}
} else {
if(!SDL_RWread(src, row, bpl, 1)) {
error = "file truncated";
goto done;
}
}
if(maxval < 255) {
/* scale up to full dynamic range (slow) */
int i;
for(i = 0; i < bpl; i++)
row[i] = row[i] * 255 / maxval;
}
row += surface->pitch;
}
done:
if(error) {
166
SDL_FreeSurface(surface);
167
IMG_SetError(error);
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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 */