2 SDL_image: An example image loading library for use with SDL
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 * PNM (portable anymap) image loader:
25 * Supports: PBM, PGM and PPM, ASCII and binary formats
26 * (PBM and PGM are loaded as 8bpp surfaces)
27 * Does not support: maximum component value > 255
35 #include "SDL_image.h"
39 /* See if an image is contained in a data source */
40 int IMG_isPNM(SDL_RWops *src)
48 start = SDL_RWtell(src);
50 if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
52 * PNM magic signatures:
53 * P1 PBM, ascii format
54 * P2 PGM, ascii format
55 * P3 PPM, ascii format
56 * P4 PBM, binary format
57 * P5 PGM, binary format
58 * P6 PPM, binary format
59 * P7 PAM, a general wrapper for PNM data
61 if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
65 SDL_RWseek(src, start, RW_SEEK_SET);
69 /* read a non-negative integer from the source. return -1 upon error */
70 static int ReadNumber(SDL_RWops *src)
75 /* Initialize return value */
78 /* Skip leading whitespace */
80 if ( ! SDL_RWread(src, &ch, 1, 1) ) {
83 /* Eat comments as whitespace */
84 if ( ch == '#' ) { /* Comment is '#' to end of line */
86 if ( ! SDL_RWread(src, &ch, 1, 1) ) {
89 } while ( (ch != '\r') && (ch != '\n') );
91 } while ( isspace(ch) );
93 /* Add up the number */
98 if ( !SDL_RWread(src, &ch, 1, 1) ) {
101 } while ( isdigit(ch) );
106 SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
109 SDL_Surface *surface = NULL;
117 enum { PBM, PGM, PPM, PAM } kind;
119 #define ERROR(s) do { error = (s); goto done; } while(0)
122 /* The error message has been set in SDL_RWFromFile */
125 start = SDL_RWtell(src);
127 SDL_RWread(src, magic, 2, 1);
128 kind = magic[1] - '1';
135 width = ReadNumber(src);
136 height = ReadNumber(src);
137 if(width <= 0 || height <= 0)
138 ERROR("Unable to read image width and height");
141 maxval = ReadNumber(src);
142 if(maxval <= 0 || maxval > 255)
143 ERROR("unsupported PNM format");
145 maxval = 255; /* never scale PBMs */
147 /* binary PNM allows just a single character of whitespace after
148 the last parameter, and we've already consumed it */
151 /* 24-bit surface in R,G,B byte order */
152 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24,
153 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
154 0x000000ff, 0x0000ff00, 0x00ff0000,
156 0x00ff0000, 0x0000ff00, 0x000000ff,
160 /* load PBM/PGM as 8-bit indexed images */
161 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8,
164 if ( surface == NULL )
165 ERROR("Out of memory");
166 bpl = width * surface->format->BytesPerPixel;
168 SDL_Color *c = surface->format->palette->colors;
170 for(i = 0; i < 256; i++)
171 c[i].r = c[i].g = c[i].b = i;
172 surface->format->palette->ncolors = 256;
173 } else if(kind == PBM) {
174 /* for some reason PBM has 1=black, 0=white */
175 SDL_Color *c = surface->format->palette->colors;
176 c[0].r = c[0].g = c[0].b = 255;
177 c[1].r = c[1].g = c[1].b = 0;
178 surface->format->palette->ncolors = 2;
179 bpl = (width + 7) >> 3;
180 buf = (Uint8 *)SDL_malloc(bpl);
182 ERROR("Out of memory");
185 /* Read the image into the surface */
186 row = (Uint8 *)surface->pixels;
187 for(y = 0; y < height; y++) {
191 for(i = 0; i < width; i++) {
194 if(!SDL_RWread(src, &ch,
196 ERROR("file truncated");
202 for(i = 0; i < bpl; i++) {
206 ERROR("file truncated");
211 Uint8 *dst = (kind == PBM) ? buf : row;
212 if(!SDL_RWread(src, dst, bpl, 1))
213 ERROR("file truncated");
215 /* expand bitmap to 8bpp */
217 for(i = 0; i < width; i++) {
218 int bit = 7 - (i & 7);
219 row[i] = (buf[i >> 3] >> bit) & 1;
224 /* scale up to full dynamic range (slow) */
226 for(i = 0; i < bpl; i++)
227 row[i] = row[i] * 255 / maxval;
229 row += surface->pitch;
234 SDL_RWseek(src, start, RW_SEEK_SET);
236 SDL_FreeSurface(surface);
246 /* See if an image is contained in a data source */
247 int IMG_isPNM(SDL_RWops *src)
252 /* Load a PNM type image from an SDL datasource */
253 SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
258 #endif /* LOAD_PNM */