IMG_xv.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Sun, 03 Feb 2013 08:37:28 -0800
changeset 343 5bf0f0d6a74e
parent 280 ec4ae96c100c
child 347 ad5034cad524
permissions -rw-r--r--
Added VS2010 and VS2012 projects; updates for VS compile; fixes from VS code analysis and code review
slouken@119
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@280
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@119
     4
slouken@280
     5
  This software is provided 'as-is', without any express or implied
slouken@280
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@280
     7
  arising from the use of this software.
slouken@119
     8
slouken@280
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@280
    10
  including commercial applications, and to alter it and redistribute it
slouken@280
    11
  freely, subject to the following restrictions:
slouken@119
    12
slouken@280
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@280
    14
     claim that you wrote the original software. If you use this software
slouken@280
    15
     in a product, an acknowledgment in the product documentation would be
slouken@280
    16
     appreciated but is not required.
slouken@280
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@280
    18
     misrepresented as being the original software.
slouken@280
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@119
    20
*/
slouken@119
    21
slouken@121
    22
/* This is a XV thumbnail image file loading framework */
slouken@119
    23
slouken@119
    24
#include <stdio.h>
slouken@119
    25
#include <string.h>
slouken@119
    26
slouken@119
    27
#include "SDL_image.h"
slouken@119
    28
slouken@119
    29
#ifdef LOAD_XV
slouken@119
    30
slouken@119
    31
static int get_line(SDL_RWops *src, char *line, int size)
slouken@119
    32
{
slouken@119
    33
	while ( size > 0 ) {
slouken@119
    34
		if ( SDL_RWread(src, line, 1, 1) <= 0 ) {
slouken@119
    35
			return -1;
slouken@119
    36
		}
slouken@119
    37
		if ( *line == '\r' ) {
slouken@119
    38
			continue;
slouken@119
    39
		}
slouken@119
    40
		if ( *line == '\n' ) {
slouken@119
    41
			*line = '\0';
slouken@119
    42
			return 0;
slouken@119
    43
		}
slouken@119
    44
		++line;
slouken@119
    45
		--size;
slouken@119
    46
	}
slouken@119
    47
	/* Out of space for the line */
slouken@119
    48
	return -1;
slouken@119
    49
}
slouken@119
    50
slouken@119
    51
static int get_header(SDL_RWops *src, int *w, int *h)
slouken@119
    52
{
slouken@119
    53
	char line[1024];
slouken@119
    54
slouken@119
    55
	*w = 0;
slouken@119
    56
	*h = 0;
slouken@119
    57
slouken@119
    58
	/* Check the header magic */
slouken@119
    59
	if ( (get_line(src, line, sizeof(line)) < 0) ||
aschiffler@343
    60
	     (SDL_memcmp(line, "P7 332", 6) != 0) ) {
slouken@119
    61
		return -1;
slouken@119
    62
	}
slouken@119
    63
slouken@119
    64
	/* Read the header */
slouken@119
    65
	while ( get_line(src, line, sizeof(line)) == 0 ) {
aschiffler@343
    66
		if ( SDL_memcmp(line, "#BUILTIN:", 9) == 0 ) {
slouken@119
    67
			/* Builtin image, no data */
slouken@119
    68
			break;
slouken@119
    69
		}
aschiffler@343
    70
		if ( SDL_memcmp(line, "#END_OF_COMMENTS", 16) == 0 ) {
slouken@119
    71
			if ( get_line(src, line, sizeof(line)) == 0 ) {
aschiffler@343
    72
				SDL_sscanf(line, "%d %d", w, h);
slouken@119
    73
				if ( *w >= 0 && *h >= 0 ) {
slouken@119
    74
					return 0;
slouken@119
    75
				}
slouken@119
    76
			}
slouken@119
    77
			break;
slouken@119
    78
		}
slouken@119
    79
	}
slouken@119
    80
	/* No image data */
slouken@119
    81
	return -1;
slouken@119
    82
}
slouken@119
    83
slouken@119
    84
/* See if an image is contained in a data source */
slouken@119
    85
int IMG_isXV(SDL_RWops *src)
slouken@119
    86
{
aschiffler@343
    87
	Sint64 start;
slouken@119
    88
	int is_XV;
slouken@119
    89
	int w, h;
slouken@119
    90
icculus@154
    91
	if ( !src )
icculus@154
    92
		return 0;
slouken@119
    93
	start = SDL_RWtell(src);
slouken@119
    94
	is_XV = 0;
slouken@119
    95
	if ( get_header(src, &w, &h) == 0 ) {
slouken@119
    96
		is_XV = 1;
slouken@119
    97
	}
slouken@236
    98
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@119
    99
	return(is_XV);
slouken@119
   100
}
slouken@119
   101
slouken@119
   102
/* Load a XV thumbnail image from an SDL datasource */
slouken@119
   103
SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
slouken@119
   104
{
aschiffler@343
   105
	Sint64 start;
slouken@119
   106
	const char *error = NULL;
slouken@119
   107
	SDL_Surface *surface = NULL;
slouken@119
   108
	int w, h;
slouken@119
   109
	Uint8 *pixels;
slouken@119
   110
slouken@119
   111
	if ( !src ) {
slouken@119
   112
		/* The error message has been set in SDL_RWFromFile */
slouken@119
   113
		return NULL;
slouken@119
   114
	}
slouken@119
   115
	start = SDL_RWtell(src);
slouken@119
   116
slouken@119
   117
	/* Read the header */
slouken@119
   118
	if ( get_header(src, &w, &h) < 0 ) {
slouken@119
   119
		error = "Unsupported image format";
slouken@119
   120
		goto done;
slouken@119
   121
	}
slouken@119
   122
slouken@119
   123
	/* Create the 3-3-2 indexed palette surface */
slouken@119
   124
	surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8, 0xe0, 0x1c, 0x03, 0);
slouken@119
   125
	if ( surface == NULL ) {
slouken@119
   126
		error = "Out of memory";
slouken@119
   127
		goto done;
slouken@119
   128
	}
slouken@119
   129
slouken@119
   130
	/* Load the image data */
slouken@119
   131
	for ( pixels = (Uint8 *)surface->pixels; h > 0; --h ) {
slouken@119
   132
		if ( SDL_RWread(src, pixels, w, 1) <= 0 ) {
slouken@119
   133
			error = "Couldn't read image data";
slouken@119
   134
			goto done;
slouken@119
   135
		}
slouken@119
   136
		pixels += surface->pitch;
slouken@119
   137
	}
slouken@119
   138
slouken@119
   139
done:
slouken@119
   140
	if ( error ) {
slouken@236
   141
		SDL_RWseek(src, start, RW_SEEK_SET);
slouken@119
   142
		if ( surface ) {
slouken@119
   143
			SDL_FreeSurface(surface);
slouken@119
   144
			surface = NULL;
slouken@119
   145
		}
slouken@119
   146
		IMG_SetError(error);
slouken@119
   147
	}
slouken@119
   148
	return surface;
slouken@119
   149
}
slouken@119
   150
slouken@119
   151
#else
slouken@119
   152
slouken@119
   153
/* See if an image is contained in a data source */
slouken@119
   154
int IMG_isXV(SDL_RWops *src)
slouken@119
   155
{
slouken@119
   156
	return(0);
slouken@119
   157
}
slouken@119
   158
slouken@119
   159
/* Load a XXX type image from an SDL datasource */
slouken@119
   160
SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
slouken@119
   161
{
slouken@119
   162
	return(NULL);
slouken@119
   163
}
slouken@119
   164
slouken@119
   165
#endif /* LOAD_XV */