IMG_xv.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 21 May 2013 21:24:32 -0700
changeset 368 8a61842d00ce
parent 347 ad5034cad524
child 451 48116d511e5d
permissions -rw-r--r--
Cleaned up whitespace for the 2.0.0 release
slouken@119
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@347
     3
  Copyright (C) 1997-2013 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@368
    33
    while ( size > 0 ) {
slouken@368
    34
        if ( SDL_RWread(src, line, 1, 1) <= 0 ) {
slouken@368
    35
            return -1;
slouken@368
    36
        }
slouken@368
    37
        if ( *line == '\r' ) {
slouken@368
    38
            continue;
slouken@368
    39
        }
slouken@368
    40
        if ( *line == '\n' ) {
slouken@368
    41
            *line = '\0';
slouken@368
    42
            return 0;
slouken@368
    43
        }
slouken@368
    44
        ++line;
slouken@368
    45
        --size;
slouken@368
    46
    }
slouken@368
    47
    /* Out of space for the line */
slouken@368
    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@368
    53
    char line[1024];
slouken@119
    54
slouken@368
    55
    *w = 0;
slouken@368
    56
    *h = 0;
slouken@119
    57
slouken@368
    58
    /* Check the header magic */
slouken@368
    59
    if ( (get_line(src, line, sizeof(line)) < 0) ||
slouken@368
    60
         (SDL_memcmp(line, "P7 332", 6) != 0) ) {
slouken@368
    61
        return -1;
slouken@368
    62
    }
slouken@119
    63
slouken@368
    64
    /* Read the header */
slouken@368
    65
    while ( get_line(src, line, sizeof(line)) == 0 ) {
slouken@368
    66
        if ( SDL_memcmp(line, "#BUILTIN:", 9) == 0 ) {
slouken@368
    67
            /* Builtin image, no data */
slouken@368
    68
            break;
slouken@368
    69
        }
slouken@368
    70
        if ( SDL_memcmp(line, "#END_OF_COMMENTS", 16) == 0 ) {
slouken@368
    71
            if ( get_line(src, line, sizeof(line)) == 0 ) {
slouken@368
    72
                SDL_sscanf(line, "%d %d", w, h);
slouken@368
    73
                if ( *w >= 0 && *h >= 0 ) {
slouken@368
    74
                    return 0;
slouken@368
    75
                }
slouken@368
    76
            }
slouken@368
    77
            break;
slouken@368
    78
        }
slouken@368
    79
    }
slouken@368
    80
    /* No image data */
slouken@368
    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
{
slouken@368
    87
    Sint64 start;
slouken@368
    88
    int is_XV;
slouken@368
    89
    int w, h;
slouken@119
    90
slouken@368
    91
    if ( !src )
slouken@368
    92
        return 0;
slouken@368
    93
    start = SDL_RWtell(src);
slouken@368
    94
    is_XV = 0;
slouken@368
    95
    if ( get_header(src, &w, &h) == 0 ) {
slouken@368
    96
        is_XV = 1;
slouken@368
    97
    }
slouken@368
    98
    SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
    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
{
slouken@368
   105
    Sint64 start;
slouken@368
   106
    const char *error = NULL;
slouken@368
   107
    SDL_Surface *surface = NULL;
slouken@368
   108
    int w, h;
slouken@368
   109
    Uint8 *pixels;
slouken@119
   110
slouken@368
   111
    if ( !src ) {
slouken@368
   112
        /* The error message has been set in SDL_RWFromFile */
slouken@368
   113
        return NULL;
slouken@368
   114
    }
slouken@368
   115
    start = SDL_RWtell(src);
slouken@119
   116
slouken@368
   117
    /* Read the header */
slouken@368
   118
    if ( get_header(src, &w, &h) < 0 ) {
slouken@368
   119
        error = "Unsupported image format";
slouken@368
   120
        goto done;
slouken@368
   121
    }
slouken@119
   122
slouken@368
   123
    /* Create the 3-3-2 indexed palette surface */
slouken@368
   124
    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8, 0xe0, 0x1c, 0x03, 0);
slouken@368
   125
    if ( surface == NULL ) {
slouken@368
   126
        error = "Out of memory";
slouken@368
   127
        goto done;
slouken@368
   128
    }
slouken@119
   129
slouken@368
   130
    /* Load the image data */
slouken@368
   131
    for ( pixels = (Uint8 *)surface->pixels; h > 0; --h ) {
slouken@368
   132
        if ( SDL_RWread(src, pixels, w, 1) <= 0 ) {
slouken@368
   133
            error = "Couldn't read image data";
slouken@368
   134
            goto done;
slouken@368
   135
        }
slouken@368
   136
        pixels += surface->pitch;
slouken@368
   137
    }
slouken@119
   138
slouken@119
   139
done:
slouken@368
   140
    if ( error ) {
slouken@368
   141
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
   142
        if ( surface ) {
slouken@368
   143
            SDL_FreeSurface(surface);
slouken@368
   144
            surface = NULL;
slouken@368
   145
        }
slouken@368
   146
        IMG_SetError(error);
slouken@368
   147
    }
slouken@368
   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@368
   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@368
   162
    return(NULL);
slouken@119
   163
}
slouken@119
   164
slouken@119
   165
#endif /* LOAD_XV */