IMG_xv.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 28 Jan 2012 10:51:24 -0500
changeset 326 fa6129ca8503
parent 280 ec4ae96c100c
child 343 5bf0f0d6a74e
permissions -rw-r--r--
Fixed SDL_image header location
     1 /*
     2   SDL_image:  An example image loading library for use with SDL
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     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.
     8 
     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:
    12 
    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.
    20 */
    21 
    22 /* This is a XV thumbnail image file loading framework */
    23 
    24 #include <stdio.h>
    25 #include <string.h>
    26 
    27 #include "SDL_image.h"
    28 
    29 #ifdef LOAD_XV
    30 
    31 static int get_line(SDL_RWops *src, char *line, int size)
    32 {
    33 	while ( size > 0 ) {
    34 		if ( SDL_RWread(src, line, 1, 1) <= 0 ) {
    35 			return -1;
    36 		}
    37 		if ( *line == '\r' ) {
    38 			continue;
    39 		}
    40 		if ( *line == '\n' ) {
    41 			*line = '\0';
    42 			return 0;
    43 		}
    44 		++line;
    45 		--size;
    46 	}
    47 	/* Out of space for the line */
    48 	return -1;
    49 }
    50 
    51 static int get_header(SDL_RWops *src, int *w, int *h)
    52 {
    53 	char line[1024];
    54 
    55 	*w = 0;
    56 	*h = 0;
    57 
    58 	/* Check the header magic */
    59 	if ( (get_line(src, line, sizeof(line)) < 0) ||
    60 	     (memcmp(line, "P7 332", 6) != 0) ) {
    61 		return -1;
    62 	}
    63 
    64 	/* Read the header */
    65 	while ( get_line(src, line, sizeof(line)) == 0 ) {
    66 		if ( memcmp(line, "#BUILTIN:", 9) == 0 ) {
    67 			/* Builtin image, no data */
    68 			break;
    69 		}
    70 		if ( memcmp(line, "#END_OF_COMMENTS", 16) == 0 ) {
    71 			if ( get_line(src, line, sizeof(line)) == 0 ) {
    72 				sscanf(line, "%d %d", w, h);
    73 				if ( *w >= 0 && *h >= 0 ) {
    74 					return 0;
    75 				}
    76 			}
    77 			break;
    78 		}
    79 	}
    80 	/* No image data */
    81 	return -1;
    82 }
    83 
    84 /* See if an image is contained in a data source */
    85 int IMG_isXV(SDL_RWops *src)
    86 {
    87 	int start;
    88 	int is_XV;
    89 	int w, h;
    90 
    91 	if ( !src )
    92 		return 0;
    93 	start = SDL_RWtell(src);
    94 	is_XV = 0;
    95 	if ( get_header(src, &w, &h) == 0 ) {
    96 		is_XV = 1;
    97 	}
    98 	SDL_RWseek(src, start, RW_SEEK_SET);
    99 	return(is_XV);
   100 }
   101 
   102 /* Load a XV thumbnail image from an SDL datasource */
   103 SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
   104 {
   105 	int start;
   106 	const char *error = NULL;
   107 	SDL_Surface *surface = NULL;
   108 	int w, h;
   109 	Uint8 *pixels;
   110 
   111 	if ( !src ) {
   112 		/* The error message has been set in SDL_RWFromFile */
   113 		return NULL;
   114 	}
   115 	start = SDL_RWtell(src);
   116 
   117 	/* Read the header */
   118 	if ( get_header(src, &w, &h) < 0 ) {
   119 		error = "Unsupported image format";
   120 		goto done;
   121 	}
   122 
   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";
   127 		goto done;
   128 	}
   129 
   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";
   134 			goto done;
   135 		}
   136 		pixels += surface->pitch;
   137 	}
   138 
   139 done:
   140 	if ( error ) {
   141 		SDL_RWseek(src, start, RW_SEEK_SET);
   142 		if ( surface ) {
   143 			SDL_FreeSurface(surface);
   144 			surface = NULL;
   145 		}
   146 		IMG_SetError(error);
   147 	}
   148 	return surface;
   149 }
   150 
   151 #else
   152 
   153 /* See if an image is contained in a data source */
   154 int IMG_isXV(SDL_RWops *src)
   155 {
   156 	return(0);
   157 }
   158 
   159 /* Load a XXX type image from an SDL datasource */
   160 SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
   161 {
   162 	return(NULL);
   163 }
   164 
   165 #endif /* LOAD_XV */