Skip to content

Latest commit

 

History

History
187 lines (160 loc) · 4.1 KB

IMG_ppm.c

File metadata and controls

187 lines (160 loc) · 4.1 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
/*
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
*/
Mar 4, 2001
Mar 4, 2001
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
*/
Aug 10, 2000
Aug 10, 2000
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];
Mar 4, 2001
Mar 4, 2001
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'));
Aug 10, 2000
Aug 10, 2000
51
52
}
Mar 4, 2001
Mar 4, 2001
53
/* read a non-negative integer from the source. return -1 upon error */
Aug 10, 2000
Aug 10, 2000
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) ) {
Mar 4, 2001
Mar 4, 2001
71
return -1;
Aug 10, 2000
Aug 10, 2000
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';
Mar 4, 2001
Mar 4, 2001
82
83
if ( !SDL_RWread(src, &ch, 1, 1) ) {
return -1;
Aug 10, 2000
Aug 10, 2000
84
85
86
87
88
}
} while ( isdigit(ch) );
return(number);
}
Nov 29, 2000
Nov 29, 2000
89
Aug 10, 2000
Aug 10, 2000
90
91
SDL_Surface *IMG_LoadPPM_RW(SDL_RWops *src)
{
Mar 4, 2001
Mar 4, 2001
92
SDL_Surface *surface = NULL;
Aug 10, 2000
Aug 10, 2000
93
int width, height;
Mar 4, 2001
Mar 4, 2001
94
int maxval, y, bpl;
Feb 27, 2001
Feb 27, 2001
95
Uint8 *row;
Mar 4, 2001
Mar 4, 2001
96
97
98
char *error = NULL;
Uint8 magic[2];
int ascii;
Aug 10, 2000
Aug 10, 2000
99
100
101
102
103
if ( ! src ) {
goto done;
}
Mar 4, 2001
Mar 4, 2001
104
105
SDL_RWread(src, magic, 2, 1);
ascii = (magic[1] == '3');
Aug 10, 2000
Aug 10, 2000
106
107
108
width = ReadNumber(src);
height = ReadNumber(src);
Mar 4, 2001
Mar 4, 2001
109
110
if(width <= 0 || height <= 0) {
error = "Unable to read image width and height";
Aug 10, 2000
Aug 10, 2000
111
112
113
114
goto done;
}
maxval = ReadNumber(src);
Mar 4, 2001
Mar 4, 2001
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 */
Aug 10, 2000
Aug 10, 2000
121
Mar 4, 2001
Mar 4, 2001
122
/* 24-bit surface in R,G,B byte order */
Feb 27, 2001
Feb 27, 2001
123
surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24,
Mar 4, 2001
Mar 4, 2001
124
125
126
127
128
129
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000ff, 0x0000ff00, 0x00ff0000,
#else
0x00ff0000, 0x0000ff00, 0x000000ff,
#endif
0);
Aug 10, 2000
Aug 10, 2000
130
if ( surface == NULL ) {
Mar 4, 2001
Mar 4, 2001
131
error = "Out of memory";
Aug 10, 2000
Aug 10, 2000
132
133
134
135
goto done;
}
/* Read the image into the surface */
Mar 4, 2001
Mar 4, 2001
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) {
Aug 10, 2000
Aug 10, 2000
166
SDL_FreeSurface(surface);
Mar 4, 2001
Mar 4, 2001
167
IMG_SetError(error);
Aug 10, 2000
Aug 10, 2000
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 */