Skip to content

Latest commit

 

History

History
259 lines (230 loc) · 5.8 KB

IMG_pnm.c

File metadata and controls

259 lines (230 loc) · 5.8 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 14, 2001
Dec 14, 2001
2
SDL_image: An example image loading library for use with SDL
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
Aug 10, 2000
Aug 10, 2000
4
5
This library is free software; you can redistribute it and/or
Feb 4, 2006
Feb 4, 2006
6
modify it under the terms of the GNU Lesser General Public
Aug 10, 2000
Aug 10, 2000
7
License as published by the Free Software Foundation; either
Feb 4, 2006
Feb 4, 2006
8
version 2.1 of the License, or (at your option) any later version.
Aug 10, 2000
Aug 10, 2000
9
10
11
12
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
Feb 4, 2006
Feb 4, 2006
13
Lesser General Public License for more details.
Aug 10, 2000
Aug 10, 2000
14
Feb 4, 2006
Feb 4, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Aug 10, 2000
Aug 10, 2000
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Aug 10, 2000
Aug 10, 2000
21
22
*/
Mar 4, 2001
Mar 4, 2001
23
/*
Mar 7, 2001
Mar 7, 2001
24
* PNM (portable anymap) image loader:
Mar 4, 2001
Mar 4, 2001
25
*
Mar 7, 2001
Mar 7, 2001
26
27
* Supports: PBM, PGM and PPM, ASCII and binary formats
* (PBM and PGM are loaded as 8bpp surfaces)
Mar 4, 2001
Mar 4, 2001
28
29
* Does not support: maximum component value > 255
*/
Aug 10, 2000
Aug 10, 2000
30
31
#include <stdio.h>
Mar 7, 2001
Mar 7, 2001
32
#include <stdlib.h>
Aug 10, 2000
Aug 10, 2000
33
34
35
36
37
#include <ctype.h>
#include <string.h>
#include "SDL_image.h"
Mar 7, 2001
Mar 7, 2001
38
#ifdef LOAD_PNM
Aug 10, 2000
Aug 10, 2000
39
40
/* See if an image is contained in a data source */
Mar 7, 2001
Mar 7, 2001
41
int IMG_isPNM(SDL_RWops *src)
Aug 10, 2000
Aug 10, 2000
42
{
Feb 4, 2006
Feb 4, 2006
43
44
int start;
int is_PNM;
Aug 10, 2000
Aug 10, 2000
45
46
char magic[2];
Feb 13, 2007
Feb 13, 2007
47
48
if ( !src )
return 0;
Feb 4, 2006
Feb 4, 2006
49
50
51
52
53
54
55
56
57
58
59
start = SDL_RWtell(src);
is_PNM = 0;
if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
/*
* PNM magic signatures:
* P1 PBM, ascii format
* P2 PGM, ascii format
* P3 PPM, ascii format
* P4 PBM, binary format
* P5 PGM, binary format
* P6 PPM, binary format
Feb 4, 2006
Feb 4, 2006
60
* P7 PAM, a general wrapper for PNM data
Feb 4, 2006
Feb 4, 2006
61
62
63
64
65
*/
if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
is_PNM = 1;
}
}
Nov 8, 2009
Nov 8, 2009
66
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
67
return(is_PNM);
Aug 10, 2000
Aug 10, 2000
68
69
}
Mar 4, 2001
Mar 4, 2001
70
/* read a non-negative integer from the source. return -1 upon error */
Aug 10, 2000
Aug 10, 2000
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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) ) {
Mar 4, 2001
Mar 4, 2001
88
return -1;
Aug 10, 2000
Aug 10, 2000
89
90
91
92
93
94
95
96
97
98
}
} while ( (ch != '\r') && (ch != '\n') );
}
} while ( isspace(ch) );
/* Add up the number */
do {
number *= 10;
number += ch-'0';
Mar 4, 2001
Mar 4, 2001
99
100
if ( !SDL_RWread(src, &ch, 1, 1) ) {
return -1;
Aug 10, 2000
Aug 10, 2000
101
102
103
104
105
}
} while ( isdigit(ch) );
return(number);
}
Nov 29, 2000
Nov 29, 2000
106
Mar 7, 2001
Mar 7, 2001
107
SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
Aug 10, 2000
Aug 10, 2000
108
{
Feb 4, 2006
Feb 4, 2006
109
int start;
Mar 4, 2001
Mar 4, 2001
110
SDL_Surface *surface = NULL;
Aug 10, 2000
Aug 10, 2000
111
int width, height;
Mar 4, 2001
Mar 4, 2001
112
int maxval, y, bpl;
Feb 27, 2001
Feb 27, 2001
113
Uint8 *row;
Mar 7, 2001
Mar 7, 2001
114
Uint8 *buf = NULL;
Mar 4, 2001
Mar 4, 2001
115
116
117
char *error = NULL;
Uint8 magic[2];
int ascii;
Feb 4, 2006
Feb 4, 2006
118
enum { PBM, PGM, PPM, PAM } kind;
Aug 10, 2000
Aug 10, 2000
119
Mar 7, 2001
Mar 7, 2001
120
121
#define ERROR(s) do { error = (s); goto done; } while(0)
Jan 4, 2004
Jan 4, 2004
122
123
124
125
if ( !src ) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Feb 4, 2006
Feb 4, 2006
126
start = SDL_RWtell(src);
Jan 4, 2004
Jan 4, 2004
127
Mar 4, 2001
Mar 4, 2001
128
SDL_RWread(src, magic, 2, 1);
Mar 7, 2001
Mar 7, 2001
129
130
131
132
133
134
kind = magic[1] - '1';
ascii = 1;
if(kind >= 3) {
ascii = 0;
kind -= 3;
}
Aug 10, 2000
Aug 10, 2000
135
136
137
width = ReadNumber(src);
height = ReadNumber(src);
Mar 7, 2001
Mar 7, 2001
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
if(width <= 0 || height <= 0)
ERROR("Unable to read image width and height");
if(kind != PBM) {
maxval = ReadNumber(src);
if(maxval <= 0 || maxval > 255)
ERROR("unsupported PNM format");
} else
maxval = 255; /* never scale PBMs */
/* binary PNM allows just a single character of whitespace after
the last parameter, and we've already consumed it */
if(kind == PPM) {
/* 24-bit surface in R,G,B byte order */
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24,
Mar 4, 2001
Mar 4, 2001
154
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
Mar 7, 2001
Mar 7, 2001
155
0x000000ff, 0x0000ff00, 0x00ff0000,
Mar 4, 2001
Mar 4, 2001
156
#else
Mar 7, 2001
Mar 7, 2001
157
0x00ff0000, 0x0000ff00, 0x000000ff,
Mar 4, 2001
Mar 4, 2001
158
#endif
Mar 7, 2001
Mar 7, 2001
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
0);
} else {
/* load PBM/PGM as 8-bit indexed images */
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 8,
0, 0, 0, 0);
}
if ( surface == NULL )
ERROR("Out of memory");
bpl = width * surface->format->BytesPerPixel;
if(kind == PGM) {
SDL_Color *c = surface->format->palette->colors;
int i;
for(i = 0; i < 256; i++)
c[i].r = c[i].g = c[i].b = i;
surface->format->palette->ncolors = 256;
} else if(kind == PBM) {
/* for some reason PBM has 1=black, 0=white */
SDL_Color *c = surface->format->palette->colors;
c[0].r = c[0].g = c[0].b = 255;
c[1].r = c[1].g = c[1].b = 0;
surface->format->palette->ncolors = 2;
bpl = (width + 7) >> 3;
buf = malloc(bpl);
if(buf == NULL)
ERROR("Out of memory");
Aug 10, 2000
Aug 10, 2000
184
185
186
}
/* Read the image into the surface */
Mar 4, 2001
Mar 4, 2001
187
188
189
190
row = surface->pixels;
for(y = 0; y < height; y++) {
if(ascii) {
int i;
Mar 7, 2001
Mar 7, 2001
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
if(kind == PBM) {
for(i = 0; i < width; i++) {
Uint8 ch;
do {
if(!SDL_RWread(src, &ch,
1, 1))
ERROR("file truncated");
ch -= '0';
} while(ch > 1);
row[i] = ch;
}
} else {
for(i = 0; i < bpl; i++) {
int c;
c = ReadNumber(src);
if(c < 0)
ERROR("file truncated");
row[i] = c;
Mar 4, 2001
Mar 4, 2001
209
210
211
}
}
} else {
Mar 7, 2001
Mar 7, 2001
212
213
214
215
216
217
218
219
220
221
Uint8 *dst = (kind == PBM) ? buf : row;
if(!SDL_RWread(src, dst, bpl, 1))
ERROR("file truncated");
if(kind == PBM) {
/* expand bitmap to 8bpp */
int i;
for(i = 0; i < width; i++) {
int bit = 7 - (i & 7);
row[i] = (buf[i >> 3] >> bit) & 1;
}
Mar 4, 2001
Mar 4, 2001
222
223
224
225
226
227
228
229
230
231
232
}
}
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:
Mar 7, 2001
Mar 7, 2001
233
free(buf);
Mar 4, 2001
Mar 4, 2001
234
if(error) {
Nov 8, 2009
Nov 8, 2009
235
SDL_RWseek(src, start, RW_SEEK_SET);
Feb 4, 2006
Feb 4, 2006
236
237
238
239
if ( surface ) {
SDL_FreeSurface(surface);
surface = NULL;
}
Mar 4, 2001
Mar 4, 2001
240
IMG_SetError(error);
Aug 10, 2000
Aug 10, 2000
241
242
243
244
245
246
247
}
return(surface);
}
#else
/* See if an image is contained in a data source */
Mar 7, 2001
Mar 7, 2001
248
int IMG_isPNM(SDL_RWops *src)
Aug 10, 2000
Aug 10, 2000
249
250
251
252
{
return(0);
}
Mar 7, 2001
Mar 7, 2001
253
254
/* Load a PNM type image from an SDL datasource */
SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
Aug 10, 2000
Aug 10, 2000
255
256
257
258
{
return(NULL);
}
Mar 7, 2001
Mar 7, 2001
259
#endif /* LOAD_PNM */