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.
22 /* This is a XV thumbnail image file loading framework */
27 #include "SDL_image.h"
31 static int get_line(SDL_RWops *src, char *line, int size)
34 if ( SDL_RWread(src, line, 1, 1) <= 0 ) {
37 if ( *line == '\r' ) {
40 if ( *line == '\n' ) {
47 /* Out of space for the line */
51 static int get_header(SDL_RWops *src, int *w, int *h)
58 /* Check the header magic */
59 if ( (get_line(src, line, sizeof(line)) < 0) ||
60 (SDL_memcmp(line, "P7 332", 6) != 0) ) {
65 while ( get_line(src, line, sizeof(line)) == 0 ) {
66 if ( SDL_memcmp(line, "#BUILTIN:", 9) == 0 ) {
67 /* Builtin image, no data */
70 if ( SDL_memcmp(line, "#END_OF_COMMENTS", 16) == 0 ) {
71 if ( get_line(src, line, sizeof(line)) == 0 ) {
72 SDL_sscanf(line, "%d %d", w, h);
73 if ( *w >= 0 && *h >= 0 ) {
84 /* See if an image is contained in a data source */
85 int IMG_isXV(SDL_RWops *src)
93 start = SDL_RWtell(src);
95 if ( get_header(src, &w, &h) == 0 ) {
98 SDL_RWseek(src, start, RW_SEEK_SET);
102 /* Load a XV thumbnail image from an SDL datasource */
103 SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
106 const char *error = NULL;
107 SDL_Surface *surface = NULL;
112 /* The error message has been set in SDL_RWFromFile */
115 start = SDL_RWtell(src);
117 /* Read the header */
118 if ( get_header(src, &w, &h) < 0 ) {
119 error = "Unsupported image format";
123 /* Create the 3-3-2 indexed palette surface */
124 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8, 0xe0, 0x1c, 0x03, 0);
125 if ( surface == NULL ) {
126 error = "Out of memory";
130 /* Load the image data */
131 for ( pixels = (Uint8 *)surface->pixels; h > 0; --h ) {
132 if ( SDL_RWread(src, pixels, w, 1) <= 0 ) {
133 error = "Couldn't read image data";
136 pixels += surface->pitch;
141 SDL_RWseek(src, start, RW_SEEK_SET);
143 SDL_FreeSurface(surface);
153 /* See if an image is contained in a data source */
154 int IMG_isXV(SDL_RWops *src)
159 /* Load a XXX type image from an SDL datasource */
160 SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)