2 SDL_image: An example image loading library for use with SDL
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 /* This is a XV thumbnail image file loading framework */
28 #include "SDL_image.h"
32 static int get_line(SDL_RWops *src, char *line, int size)
35 if ( SDL_RWread(src, line, 1, 1) <= 0 ) {
38 if ( *line == '\r' ) {
41 if ( *line == '\n' ) {
48 /* Out of space for the line */
52 static int get_header(SDL_RWops *src, int *w, int *h)
59 /* Check the header magic */
60 if ( (get_line(src, line, sizeof(line)) < 0) ||
61 (memcmp(line, "P7 332", 6) != 0) ) {
66 while ( get_line(src, line, sizeof(line)) == 0 ) {
67 if ( memcmp(line, "#BUILTIN:", 9) == 0 ) {
68 /* Builtin image, no data */
71 if ( memcmp(line, "#END_OF_COMMENTS", 16) == 0 ) {
72 if ( get_line(src, line, sizeof(line)) == 0 ) {
73 sscanf(line, "%d %d", w, h);
74 if ( *w >= 0 && *h >= 0 ) {
85 /* See if an image is contained in a data source */
86 int IMG_isXV(SDL_RWops *src)
94 start = SDL_RWtell(src);
96 if ( get_header(src, &w, &h) == 0 ) {
99 SDL_RWseek(src, start, RW_SEEK_SET);
103 /* Load a XV thumbnail image from an SDL datasource */
104 SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
107 const char *error = NULL;
108 SDL_Surface *surface = NULL;
113 /* The error message has been set in SDL_RWFromFile */
116 start = SDL_RWtell(src);
118 /* Read the header */
119 if ( get_header(src, &w, &h) < 0 ) {
120 error = "Unsupported image format";
124 /* Create the 3-3-2 indexed palette surface */
125 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8, 0xe0, 0x1c, 0x03, 0);
126 if ( surface == NULL ) {
127 error = "Out of memory";
131 /* Load the image data */
132 for ( pixels = (Uint8 *)surface->pixels; h > 0; --h ) {
133 if ( SDL_RWread(src, pixels, w, 1) <= 0 ) {
134 error = "Couldn't read image data";
137 pixels += surface->pitch;
142 SDL_RWseek(src, start, RW_SEEK_SET);
144 SDL_FreeSurface(surface);
154 /* See if an image is contained in a data source */
155 int IMG_isXV(SDL_RWops *src)
160 /* Load a XXX type image from an SDL datasource */
161 SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)