IMG_xv.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 26 Sep 2018 15:19:34 -0400
changeset 587 32a18ca05935
parent 575 36e9e2255178
child 638 e3e9d7430674
permissions -rw-r--r--
pnm: Don't get into infinite loops on truncated files.
     1 /*
     2   SDL_image:  An example image loading library for use with SDL
     3   Copyright (C) 1997-2018 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 "SDL_image.h"
    25 
    26 #ifdef LOAD_XV
    27 
    28 static int get_line(SDL_RWops *src, char *line, int size)
    29 {
    30     while ( size > 0 ) {
    31         if ( SDL_RWread(src, line, 1, 1) <= 0 ) {
    32             return -1;
    33         }
    34         if ( *line == '\r' ) {
    35             continue;
    36         }
    37         if ( *line == '\n' ) {
    38             *line = '\0';
    39             return 0;
    40         }
    41         ++line;
    42         --size;
    43     }
    44     /* Out of space for the line */
    45     return -1;
    46 }
    47 
    48 static int get_header(SDL_RWops *src, int *w, int *h)
    49 {
    50     char line[1024];
    51 
    52     *w = 0;
    53     *h = 0;
    54 
    55     /* Check the header magic */
    56     if ( (get_line(src, line, sizeof(line)) < 0) ||
    57          (SDL_memcmp(line, "P7 332", 6) != 0) ) {
    58         return -1;
    59     }
    60 
    61     /* Read the header */
    62     while ( get_line(src, line, sizeof(line)) == 0 ) {
    63         if ( SDL_memcmp(line, "#BUILTIN:", 9) == 0 ) {
    64             /* Builtin image, no data */
    65             break;
    66         }
    67         if ( SDL_memcmp(line, "#END_OF_COMMENTS", 16) == 0 ) {
    68             if ( get_line(src, line, sizeof(line)) == 0 ) {
    69                 SDL_sscanf(line, "%d %d", w, h);
    70                 if ( *w >= 0 && *h >= 0 ) {
    71                     return 0;
    72                 }
    73             }
    74             break;
    75         }
    76     }
    77     /* No image data */
    78     return -1;
    79 }
    80 
    81 /* See if an image is contained in a data source */
    82 int IMG_isXV(SDL_RWops *src)
    83 {
    84     Sint64 start;
    85     int is_XV;
    86     int w, h;
    87 
    88     if ( !src )
    89         return 0;
    90     start = SDL_RWtell(src);
    91     is_XV = 0;
    92     if ( get_header(src, &w, &h) == 0 ) {
    93         is_XV = 1;
    94     }
    95     SDL_RWseek(src, start, RW_SEEK_SET);
    96     return(is_XV);
    97 }
    98 
    99 /* Load a XV thumbnail image from an SDL datasource */
   100 SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
   101 {
   102     Sint64 start;
   103     const char *error = NULL;
   104     SDL_Surface *surface = NULL;
   105     int w, h;
   106     Uint8 *pixels;
   107 
   108     if ( !src ) {
   109         /* The error message has been set in SDL_RWFromFile */
   110         return NULL;
   111     }
   112     start = SDL_RWtell(src);
   113 
   114     /* Read the header */
   115     if ( get_header(src, &w, &h) < 0 ) {
   116         error = "Unsupported image format";
   117         goto done;
   118     }
   119 
   120     /* Create the 3-3-2 indexed palette surface */
   121     surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8, 0xe0, 0x1c, 0x03, 0);
   122     if ( surface == NULL ) {
   123         error = "Out of memory";
   124         goto done;
   125     }
   126 
   127     /* Load the image data */
   128     for ( pixels = (Uint8 *)surface->pixels; h > 0; --h ) {
   129         if ( SDL_RWread(src, pixels, w, 1) <= 0 ) {
   130             error = "Couldn't read image data";
   131             goto done;
   132         }
   133         pixels += surface->pitch;
   134     }
   135 
   136 done:
   137     if ( error ) {
   138         SDL_RWseek(src, start, RW_SEEK_SET);
   139         if ( surface ) {
   140             SDL_FreeSurface(surface);
   141             surface = NULL;
   142         }
   143         IMG_SetError("%s", error);
   144     }
   145     return surface;
   146 }
   147 
   148 #else
   149 
   150 /* See if an image is contained in a data source */
   151 int IMG_isXV(SDL_RWops *src)
   152 {
   153     return(0);
   154 }
   155 
   156 /* Load a XXX type image from an SDL datasource */
   157 SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
   158 {
   159     return(NULL);
   160 }
   161 
   162 #endif /* LOAD_XV */