IMG_gif.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 26 Sep 2018 15:19:17 -0400
changeset 586 19beb4a1bb54
parent 575 36e9e2255178
child 638 e3e9d7430674
permissions -rw-r--r--
gif: Don't get into infinite loops on truncated files.
slouken@0
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@575
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@0
     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@0
     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@0
    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@0
    20
*/
slouken@0
    21
slouken@237
    22
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
slouken@237
    23
slouken@0
    24
/* This is a GIF image file loading framework */
slouken@0
    25
slouken@0
    26
#include "SDL_image.h"
slouken@0
    27
slouken@0
    28
#ifdef LOAD_GIF
slouken@0
    29
slouken@0
    30
/* See if an image is contained in a data source */
slouken@0
    31
int IMG_isGIF(SDL_RWops *src)
slouken@0
    32
{
slouken@368
    33
    Sint64 start;
slouken@368
    34
    int is_GIF;
slouken@368
    35
    char magic[6];
slouken@0
    36
slouken@368
    37
    if ( !src )
slouken@368
    38
        return 0;
slouken@368
    39
    start = SDL_RWtell(src);
slouken@368
    40
    is_GIF = 0;
slouken@368
    41
    if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
slouken@368
    42
        if ( (SDL_strncmp(magic, "GIF", 3) == 0) &&
slouken@368
    43
             ((SDL_memcmp(magic + 3, "87a", 3) == 0) ||
slouken@368
    44
              (SDL_memcmp(magic + 3, "89a", 3) == 0)) ) {
slouken@368
    45
            is_GIF = 1;
slouken@368
    46
        }
slouken@368
    47
    }
slouken@368
    48
    SDL_RWseek(src, start, RW_SEEK_SET);
slouken@368
    49
    return(is_GIF);
slouken@0
    50
}
slouken@0
    51
slouken@0
    52
/* Code from here to end of file has been adapted from XPaint:           */
slouken@0
    53
/* +-------------------------------------------------------------------+ */
slouken@368
    54
/* | Copyright 1990, 1991, 1993 David Koblas.                  | */
slouken@368
    55
/* | Copyright 1996 Torsten Martinsen.                     | */
slouken@0
    56
/* |   Permission to use, copy, modify, and distribute this software   | */
slouken@0
    57
/* |   and its documentation for any purpose and without fee is hereby | */
slouken@0
    58
/* |   granted, provided that the above copyright notice appear in all | */
slouken@0
    59
/* |   copies and that both that copyright notice and this permission  | */
slouken@0
    60
/* |   notice appear in supporting documentation.  This software is    | */
slouken@368
    61
/* |   provided "as is" without express or implied warranty.           | */
slouken@0
    62
/* +-------------------------------------------------------------------+ */
slouken@0
    63
slouken@0
    64
/* Adapted for use in SDL by Sam Lantinga -- 7/20/98 */
slouken@0
    65
#define USED_BY_SDL
slouken@0
    66
slouken@0
    67
#include <stdio.h>
slouken@0
    68
#include <string.h>
slouken@0
    69
slouken@0
    70
#ifdef USED_BY_SDL
slouken@0
    71
/* Changes to work with SDL:
slouken@0
    72
slouken@0
    73
   Include SDL header file
slouken@0
    74
   Use SDL_Surface rather than xpaint Image structure
slouken@0
    75
   Define SDL versions of RWSetMsg(), ImageNewCmap() and ImageSetCmap()
slouken@0
    76
*/
slouken@0
    77
#include "SDL.h"
slouken@0
    78
slouken@368
    79
#define Image           SDL_Surface
slouken@368
    80
#define RWSetMsg        IMG_SetError
slouken@368
    81
#define ImageNewCmap(w, h, s)   SDL_CreateRGBSurface(SDL_SWSURFACE,w,h,8,0,0,0,0)
slouken@0
    82
#define ImageSetCmap(s, i, R, G, B) do { \
slouken@368
    83
                s->format->palette->colors[i].r = R; \
slouken@368
    84
                s->format->palette->colors[i].g = G; \
slouken@368
    85
                s->format->palette->colors[i].b = B; \
slouken@368
    86
            } while (0)
slouken@0
    87
/* * * * * */
slouken@0
    88
slouken@0
    89
#else
slouken@0
    90
slouken@0
    91
/* Original XPaint sources */
slouken@0
    92
slouken@0
    93
#include "image.h"
slouken@0
    94
#include "rwTable.h"
slouken@0
    95
slouken@368
    96
#define SDL_RWops   FILE
slouken@368
    97
#define SDL_RWclose fclose
slouken@0
    98
slouken@0
    99
#endif /* USED_BY_SDL */
slouken@0
   100
slouken@0
   101
slouken@368
   102
#define MAXCOLORMAPSIZE     256
slouken@0
   103
slouken@368
   104
#define TRUE    1
slouken@368
   105
#define FALSE   0
slouken@0
   106
slouken@368
   107
#define CM_RED      0
slouken@368
   108
#define CM_GREEN    1
slouken@368
   109
#define CM_BLUE     2
slouken@0
   110
slouken@368
   111
#define MAX_LWZ_BITS        12
slouken@0
   112
slouken@368
   113
#define INTERLACE       0x40
slouken@368
   114
#define LOCALCOLORMAP   0x80
slouken@368
   115
#define BitSet(byte, bit)   (((byte) & (bit)) == (bit))
slouken@0
   116
slouken@368
   117
#define ReadOK(file,buffer,len) SDL_RWread(file, buffer, len, 1)
slouken@0
   118
slouken@368
   119
#define LM_to_uint(a,b)         (((b)<<8)|(a))
slouken@0
   120
slouken@27
   121
static struct {
slouken@0
   122
    unsigned int Width;
slouken@0
   123
    unsigned int Height;
slouken@0
   124
    unsigned char ColorMap[3][MAXCOLORMAPSIZE];
slouken@0
   125
    unsigned int BitPixel;
slouken@0
   126
    unsigned int ColorResolution;
slouken@0
   127
    unsigned int Background;
slouken@0
   128
    unsigned int AspectRatio;
slouken@0
   129
    int GrayScale;
slouken@0
   130
} GifScreen;
slouken@0
   131
slouken@0
   132
static struct {
slouken@0
   133
    int transparent;
slouken@0
   134
    int delayTime;
slouken@0
   135
    int inputFlag;
slouken@0
   136
    int disposal;
slouken@0
   137
} Gif89;
slouken@0
   138
slouken@0
   139
static int ReadColorMap(SDL_RWops * src, int number,
slouken@368
   140
            unsigned char buffer[3][MAXCOLORMAPSIZE], int *flag);
slouken@0
   141
static int DoExtension(SDL_RWops * src, int label);
slouken@0
   142
static int GetDataBlock(SDL_RWops * src, unsigned char *buf);
slouken@0
   143
static int GetCode(SDL_RWops * src, int code_size, int flag);
slouken@0
   144
static int LWZReadByte(SDL_RWops * src, int flag, int input_code_size);
slouken@0
   145
static Image *ReadImage(SDL_RWops * src, int len, int height, int,
slouken@368
   146
            unsigned char cmap[3][MAXCOLORMAPSIZE],
slouken@368
   147
            int gray, int interlace, int ignore);
slouken@0
   148
slouken@0
   149
Image *
slouken@0
   150
IMG_LoadGIF_RW(SDL_RWops *src)
slouken@0
   151
{
aschiffler@343
   152
    Sint64 start;
slouken@0
   153
    unsigned char buf[16];
slouken@0
   154
    unsigned char c;
slouken@0
   155
    unsigned char localColorMap[3][MAXCOLORMAPSIZE];
slouken@0
   156
    int grayScale;
slouken@0
   157
    int useGlobalColormap;
slouken@0
   158
    int bitPixel;
slouken@0
   159
    int imageCount = 0;
slouken@0
   160
    char version[4];
slouken@0
   161
    int imageNumber = 1;
slouken@0
   162
    Image *image = NULL;
slouken@0
   163
slouken@0
   164
    if ( src == NULL ) {
slouken@368
   165
    return NULL;
slouken@0
   166
    }
slouken@118
   167
    start = SDL_RWtell(src);
slouken@118
   168
slouken@0
   169
    if (!ReadOK(src, buf, 6)) {
slouken@368
   170
    RWSetMsg("error reading magic number");
slouken@0
   171
        goto done;
slouken@0
   172
    }
aschiffler@343
   173
    if (SDL_strncmp((char *) buf, "GIF", 3) != 0) {
slouken@368
   174
    RWSetMsg("not a GIF file");
slouken@0
   175
        goto done;
slouken@0
   176
    }
aschiffler@343
   177
    SDL_memcpy(version, (char *) buf + 3, 3);
slouken@0
   178
    version[3] = '\0';
slouken@0
   179
aschiffler@343
   180
    if ((SDL_strcmp(version, "87a") != 0) && (SDL_strcmp(version, "89a") != 0)) {
slouken@368
   181
    RWSetMsg("bad version number, not '87a' or '89a'");
slouken@0
   182
        goto done;
slouken@0
   183
    }
slouken@0
   184
    Gif89.transparent = -1;
slouken@0
   185
    Gif89.delayTime = -1;
slouken@0
   186
    Gif89.inputFlag = -1;
slouken@0
   187
    Gif89.disposal = 0;
slouken@0
   188
slouken@0
   189
    if (!ReadOK(src, buf, 7)) {
slouken@368
   190
    RWSetMsg("failed to read screen descriptor");
slouken@0
   191
        goto done;
slouken@0
   192
    }
slouken@0
   193
    GifScreen.Width = LM_to_uint(buf[0], buf[1]);
slouken@0
   194
    GifScreen.Height = LM_to_uint(buf[2], buf[3]);
slouken@0
   195
    GifScreen.BitPixel = 2 << (buf[4] & 0x07);
slouken@0
   196
    GifScreen.ColorResolution = (((buf[4] & 0x70) >> 3) + 1);
slouken@0
   197
    GifScreen.Background = buf[5];
slouken@0
   198
    GifScreen.AspectRatio = buf[6];
slouken@0
   199
slouken@368
   200
    if (BitSet(buf[4], LOCALCOLORMAP)) {    /* Global Colormap */
slouken@368
   201
    if (ReadColorMap(src, GifScreen.BitPixel, GifScreen.ColorMap,
slouken@368
   202
             &GifScreen.GrayScale)) {
slouken@368
   203
        RWSetMsg("error reading global colormap");
slouken@0
   204
            goto done;
slouken@368
   205
    }
slouken@0
   206
    }
slouken@0
   207
    do {
slouken@368
   208
    if (!ReadOK(src, &c, 1)) {
slouken@368
   209
        RWSetMsg("EOF / read error on image data");
slouken@0
   210
            goto done;
slouken@368
   211
    }
slouken@368
   212
    if (c == ';') {     /* GIF terminator */
slouken@368
   213
        if (imageCount < imageNumber) {
slouken@368
   214
        RWSetMsg("only %d image%s found in file",
slouken@368
   215
             imageCount, imageCount > 1 ? "s" : "");
slouken@0
   216
                goto done;
slouken@368
   217
        }
slouken@368
   218
    }
slouken@368
   219
    if (c == '!') {     /* Extension */
slouken@368
   220
        if (!ReadOK(src, &c, 1)) {
slouken@368
   221
        RWSetMsg("EOF / read error on extention function code");
slouken@0
   222
                goto done;
slouken@368
   223
        }
slouken@368
   224
        DoExtension(src, c);
slouken@368
   225
        continue;
slouken@368
   226
    }
slouken@368
   227
    if (c != ',') {     /* Not a valid start character */
slouken@368
   228
        continue;
slouken@368
   229
    }
slouken@368
   230
    ++imageCount;
slouken@0
   231
slouken@368
   232
    if (!ReadOK(src, buf, 9)) {
slouken@368
   233
        RWSetMsg("couldn't read left/top/width/height");
slouken@0
   234
            goto done;
slouken@368
   235
    }
slouken@368
   236
    useGlobalColormap = !BitSet(buf[8], LOCALCOLORMAP);
slouken@0
   237
slouken@368
   238
    bitPixel = 1 << ((buf[8] & 0x07) + 1);
slouken@0
   239
slouken@368
   240
    if (!useGlobalColormap) {
slouken@368
   241
        if (ReadColorMap(src, bitPixel, localColorMap, &grayScale)) {
slouken@368
   242
        RWSetMsg("error reading local colormap");
slouken@0
   243
                goto done;
slouken@368
   244
        }
slouken@368
   245
        image = ReadImage(src, LM_to_uint(buf[4], buf[5]),
slouken@368
   246
                  LM_to_uint(buf[6], buf[7]),
slouken@368
   247
                  bitPixel, localColorMap, grayScale,
slouken@368
   248
                  BitSet(buf[8], INTERLACE),
slouken@368
   249
                  imageCount != imageNumber);
slouken@368
   250
    } else {
slouken@368
   251
        image = ReadImage(src, LM_to_uint(buf[4], buf[5]),
slouken@368
   252
                  LM_to_uint(buf[6], buf[7]),
slouken@368
   253
                  GifScreen.BitPixel, GifScreen.ColorMap,
slouken@368
   254
                  GifScreen.GrayScale, BitSet(buf[8], INTERLACE),
slouken@368
   255
                  imageCount != imageNumber);
slouken@368
   256
    }
slouken@0
   257
    } while (image == NULL);
slouken@0
   258
slouken@0
   259
#ifdef USED_BY_SDL
slouken@50
   260
    if ( Gif89.transparent >= 0 ) {
slouken@322
   261
        SDL_SetColorKey(image, SDL_TRUE, Gif89.transparent);
slouken@0
   262
    }
slouken@0
   263
#endif
slouken@0
   264
slouken@0
   265
done:
slouken@118
   266
    if ( image == NULL ) {
slouken@236
   267
        SDL_RWseek(src, start, RW_SEEK_SET);
slouken@118
   268
    }
slouken@0
   269
    return image;
slouken@0
   270
}
slouken@0
   271
slouken@0
   272
static int
slouken@0
   273
ReadColorMap(SDL_RWops *src, int number,
slouken@0
   274
             unsigned char buffer[3][MAXCOLORMAPSIZE], int *gray)
slouken@0
   275
{
slouken@0
   276
    int i;
slouken@0
   277
    unsigned char rgb[3];
slouken@0
   278
    int flag;
slouken@0
   279
slouken@0
   280
    flag = TRUE;
slouken@0
   281
slouken@0
   282
    for (i = 0; i < number; ++i) {
slouken@368
   283
    if (!ReadOK(src, rgb, sizeof(rgb))) {
slouken@368
   284
        RWSetMsg("bad colormap");
slouken@368
   285
        return 1;
slouken@368
   286
    }
slouken@368
   287
    buffer[CM_RED][i] = rgb[0];
slouken@368
   288
    buffer[CM_GREEN][i] = rgb[1];
slouken@368
   289
    buffer[CM_BLUE][i] = rgb[2];
slouken@368
   290
    flag &= (rgb[0] == rgb[1] && rgb[1] == rgb[2]);
slouken@0
   291
    }
slouken@0
   292
slouken@0
   293
#if 0
slouken@0
   294
    if (flag)
slouken@368
   295
    *gray = (number == 2) ? PBM_TYPE : PGM_TYPE;
slouken@0
   296
    else
slouken@368
   297
    *gray = PPM_TYPE;
slouken@0
   298
#else
slouken@0
   299
    *gray = 0;
slouken@0
   300
#endif
slouken@0
   301
slouken@0
   302
    return FALSE;
slouken@0
   303
}
slouken@0
   304
slouken@0
   305
static int
slouken@0
   306
DoExtension(SDL_RWops *src, int label)
slouken@0
   307
{
slouken@0
   308
    static unsigned char buf[256];
slouken@0
   309
    char *str;
slouken@0
   310
slouken@0
   311
    switch (label) {
slouken@368
   312
    case 0x01:          /* Plain Text Extension */
slouken@368
   313
    str = "Plain Text Extension";
slouken@368
   314
    break;
slouken@368
   315
    case 0xff:          /* Application Extension */
slouken@368
   316
    str = "Application Extension";
slouken@368
   317
    break;
slouken@368
   318
    case 0xfe:          /* Comment Extension */
slouken@368
   319
    str = "Comment Extension";
icculus@586
   320
    while (GetDataBlock(src, (unsigned char *) buf) > 0)
slouken@368
   321
        ;
slouken@368
   322
    return FALSE;
slouken@368
   323
    case 0xf9:          /* Graphic Control Extension */
slouken@368
   324
    str = "Graphic Control Extension";
slouken@368
   325
    (void) GetDataBlock(src, (unsigned char *) buf);
slouken@368
   326
    Gif89.disposal = (buf[0] >> 2) & 0x7;
slouken@368
   327
    Gif89.inputFlag = (buf[0] >> 1) & 0x1;
slouken@368
   328
    Gif89.delayTime = LM_to_uint(buf[1], buf[2]);
slouken@368
   329
    if ((buf[0] & 0x1) != 0)
slouken@368
   330
        Gif89.transparent = buf[3];
slouken@0
   331
icculus@586
   332
    while (GetDataBlock(src, (unsigned char *) buf) > 0)
slouken@368
   333
        ;
slouken@368
   334
    return FALSE;
slouken@0
   335
    default:
slouken@368
   336
    str = (char *)buf;
slouken@368
   337
    SDL_snprintf(str, 256, "UNKNOWN (0x%02x)", label);
slouken@368
   338
    break;
slouken@0
   339
    }
slouken@0
   340
icculus@586
   341
    while (GetDataBlock(src, (unsigned char *) buf) > 0)
slouken@368
   342
    ;
slouken@0
   343
slouken@0
   344
    return FALSE;
slouken@0
   345
}
slouken@0
   346
slouken@0
   347
static int ZeroDataBlock = FALSE;
slouken@0
   348
slouken@0
   349
static int
slouken@0
   350
GetDataBlock(SDL_RWops *src, unsigned char *buf)
slouken@0
   351
{
slouken@0
   352
    unsigned char count;
slouken@0
   353
slouken@0
   354
    if (!ReadOK(src, &count, 1)) {
slouken@368
   355
    /* pm_message("error in getting DataBlock size" ); */
slouken@368
   356
    return -1;
slouken@0
   357
    }
slouken@0
   358
    ZeroDataBlock = count == 0;
slouken@0
   359
slouken@0
   360
    if ((count != 0) && (!ReadOK(src, buf, count))) {
slouken@368
   361
    /* pm_message("error in reading DataBlock" ); */
slouken@368
   362
    return -1;
slouken@0
   363
    }
slouken@0
   364
    return count;
slouken@0
   365
}
slouken@0
   366
slouken@0
   367
static int
slouken@0
   368
GetCode(SDL_RWops *src, int code_size, int flag)
slouken@0
   369
{
slouken@0
   370
    static unsigned char buf[280];
slouken@0
   371
    static int curbit, lastbit, done, last_byte;
slouken@0
   372
    int i, j, ret;
slouken@0
   373
    unsigned char count;
slouken@0
   374
slouken@0
   375
    if (flag) {
slouken@368
   376
    curbit = 0;
slouken@368
   377
    lastbit = 0;
slouken@368
   378
    done = FALSE;
slouken@368
   379
    return 0;
slouken@0
   380
    }
slouken@0
   381
    if ((curbit + code_size) >= lastbit) {
slouken@368
   382
    if (done) {
slouken@368
   383
        if (curbit >= lastbit)
slouken@368
   384
        RWSetMsg("ran off the end of my bits");
slouken@368
   385
        return -1;
slouken@368
   386
    }
slouken@368
   387
    buf[0] = buf[last_byte - 2];
slouken@368
   388
    buf[1] = buf[last_byte - 1];
slouken@0
   389
icculus@586
   390
    if ((count = GetDataBlock(src, &buf[2])) <= 0)
slouken@368
   391
        done = TRUE;
slouken@0
   392
slouken@368
   393
    last_byte = 2 + count;
slouken@368
   394
    curbit = (curbit - lastbit) + 16;
slouken@368
   395
    lastbit = (2 + count) * 8;
slouken@0
   396
    }
slouken@0
   397
    ret = 0;
slouken@0
   398
    for (i = curbit, j = 0; j < code_size; ++i, ++j)
slouken@368
   399
    ret |= ((buf[i / 8] & (1 << (i % 8))) != 0) << j;
slouken@0
   400
slouken@0
   401
    curbit += code_size;
slouken@0
   402
slouken@0
   403
    return ret;
slouken@0
   404
}
slouken@0
   405
slouken@0
   406
static int
slouken@0
   407
LWZReadByte(SDL_RWops *src, int flag, int input_code_size)
slouken@0
   408
{
slouken@0
   409
    static int fresh = FALSE;
slouken@0
   410
    int code, incode;
slouken@0
   411
    static int code_size, set_code_size;
slouken@0
   412
    static int max_code, max_code_size;
slouken@0
   413
    static int firstcode, oldcode;
slouken@0
   414
    static int clear_code, end_code;
slouken@0
   415
    static int table[2][(1 << MAX_LWZ_BITS)];
slouken@0
   416
    static int stack[(1 << (MAX_LWZ_BITS)) * 2], *sp;
slouken@0
   417
    register int i;
slouken@0
   418
slouken@181
   419
    /* Fixed buffer overflow found by Michael Skladnikiewicz */
slouken@181
   420
    if (input_code_size > MAX_LWZ_BITS)
slouken@181
   421
        return -1;
slouken@181
   422
slouken@0
   423
    if (flag) {
slouken@368
   424
    set_code_size = input_code_size;
slouken@368
   425
    code_size = set_code_size + 1;
slouken@368
   426
    clear_code = 1 << set_code_size;
slouken@368
   427
    end_code = clear_code + 1;
slouken@368
   428
    max_code_size = 2 * clear_code;
slouken@368
   429
    max_code = clear_code + 2;
slouken@0
   430
slouken@368
   431
    GetCode(src, 0, TRUE);
slouken@0
   432
slouken@368
   433
    fresh = TRUE;
slouken@0
   434
slouken@368
   435
    for (i = 0; i < clear_code; ++i) {
slouken@368
   436
        table[0][i] = 0;
slouken@368
   437
        table[1][i] = i;
slouken@368
   438
    }
slouken@368
   439
    table[1][0] = 0;
slouken@368
   440
    for (; i < (1 << MAX_LWZ_BITS); ++i)
slouken@368
   441
        table[0][i] = 0;
slouken@0
   442
slouken@368
   443
    sp = stack;
slouken@0
   444
slouken@368
   445
    return 0;
slouken@0
   446
    } else if (fresh) {
slouken@368
   447
    fresh = FALSE;
slouken@368
   448
    do {
slouken@368
   449
        firstcode = oldcode = GetCode(src, code_size, FALSE);
slouken@368
   450
    } while (firstcode == clear_code);
slouken@368
   451
    return firstcode;
slouken@0
   452
    }
slouken@0
   453
    if (sp > stack)
slouken@368
   454
    return *--sp;
slouken@0
   455
slouken@0
   456
    while ((code = GetCode(src, code_size, FALSE)) >= 0) {
slouken@368
   457
    if (code == clear_code) {
slouken@368
   458
        for (i = 0; i < clear_code; ++i) {
slouken@368
   459
        table[0][i] = 0;
slouken@368
   460
        table[1][i] = i;
slouken@368
   461
        }
slouken@368
   462
        for (; i < (1 << MAX_LWZ_BITS); ++i)
slouken@368
   463
        table[0][i] = table[1][i] = 0;
slouken@368
   464
        code_size = set_code_size + 1;
slouken@368
   465
        max_code_size = 2 * clear_code;
slouken@368
   466
        max_code = clear_code + 2;
slouken@368
   467
        sp = stack;
slouken@368
   468
        firstcode = oldcode = GetCode(src, code_size, FALSE);
slouken@368
   469
        return firstcode;
slouken@368
   470
    } else if (code == end_code) {
slouken@368
   471
        int count;
slouken@368
   472
        unsigned char buf[260];
slouken@0
   473
slouken@368
   474
        if (ZeroDataBlock)
slouken@368
   475
        return -2;
slouken@0
   476
slouken@368
   477
        while ((count = GetDataBlock(src, buf)) > 0)
slouken@368
   478
        ;
slouken@0
   479
slouken@368
   480
        if (count != 0) {
slouken@368
   481
        /*
slouken@368
   482
         * pm_message("missing EOD in data stream (common occurence)");
slouken@368
   483
         */
slouken@368
   484
        }
slouken@368
   485
        return -2;
slouken@368
   486
    }
slouken@368
   487
    incode = code;
slouken@0
   488
slouken@368
   489
    if (code >= max_code) {
slouken@368
   490
        *sp++ = firstcode;
slouken@368
   491
        code = oldcode;
slouken@368
   492
    }
slouken@368
   493
    while (code >= clear_code) {
slouken@368
   494
        /* Guard against buffer overruns */
slouken@368
   495
        if (code < 0 || code >= (1 << MAX_LWZ_BITS)) {
slouken@368
   496
            RWSetMsg("invalid LWZ data");
slouken@368
   497
            return -3;
slouken@368
   498
        }
slouken@368
   499
        *sp++ = table[1][code];
icculus@563
   500
        if (code == table[0][code]) {
icculus@563
   501
            RWSetMsg("circular table entry BIG ERROR");
icculus@563
   502
            return -3;
icculus@563
   503
        }
slouken@368
   504
        code = table[0][code];
slouken@368
   505
    }
slouken@0
   506
slouken@368
   507
    /* Guard against buffer overruns */
slouken@368
   508
    if (code < 0 || code >= (1 << MAX_LWZ_BITS)) {
slouken@368
   509
        RWSetMsg("invalid LWZ data");
slouken@368
   510
        return -4;
slouken@368
   511
    }
slouken@368
   512
    *sp++ = firstcode = table[1][code];
slouken@0
   513
slouken@368
   514
    if ((code = max_code) < (1 << MAX_LWZ_BITS)) {
slouken@368
   515
        table[0][code] = oldcode;
slouken@368
   516
        table[1][code] = firstcode;
slouken@368
   517
        ++max_code;
slouken@368
   518
        if ((max_code >= max_code_size) &&
slouken@368
   519
        (max_code_size < (1 << MAX_LWZ_BITS))) {
slouken@368
   520
        max_code_size *= 2;
slouken@368
   521
        ++code_size;
slouken@368
   522
        }
slouken@368
   523
    }
slouken@368
   524
    oldcode = incode;
slouken@0
   525
slouken@368
   526
    if (sp > stack)
slouken@368
   527
        return *--sp;
slouken@0
   528
    }
slouken@0
   529
    return code;
slouken@0
   530
}
slouken@0
   531
slouken@0
   532
static Image *
slouken@0
   533
ReadImage(SDL_RWops * src, int len, int height, int cmapSize,
slouken@368
   534
      unsigned char cmap[3][MAXCOLORMAPSIZE],
slouken@368
   535
      int gray, int interlace, int ignore)
slouken@0
   536
{
slouken@0
   537
    Image *image;
slouken@0
   538
    unsigned char c;
slouken@0
   539
    int i, v;
slouken@0
   540
    int xpos = 0, ypos = 0, pass = 0;
slouken@0
   541
slouken@0
   542
    /*
slouken@368
   543
    **  Initialize the compression routines
slouken@0
   544
     */
slouken@0
   545
    if (!ReadOK(src, &c, 1)) {
slouken@368
   546
    RWSetMsg("EOF / read error on image data");
slouken@368
   547
    return NULL;
slouken@0
   548
    }
slouken@0
   549
    if (LWZReadByte(src, TRUE, c) < 0) {
slouken@368
   550
    RWSetMsg("error reading image");
slouken@368
   551
    return NULL;
slouken@0
   552
    }
slouken@0
   553
    /*
slouken@368
   554
    **  If this is an "uninteresting picture" ignore it.
slouken@0
   555
     */
slouken@0
   556
    if (ignore) {
slouken@368
   557
    while (LWZReadByte(src, FALSE, c) >= 0)
slouken@368
   558
        ;
slouken@368
   559
    return NULL;
slouken@0
   560
    }
slouken@0
   561
    image = ImageNewCmap(len, height, cmapSize);
slouken@0
   562
slouken@0
   563
    for (i = 0; i < cmapSize; i++)
slouken@368
   564
    ImageSetCmap(image, i, cmap[CM_RED][i],
slouken@368
   565
             cmap[CM_GREEN][i], cmap[CM_BLUE][i]);
slouken@0
   566
slouken@0
   567
    while ((v = LWZReadByte(src, FALSE, c)) >= 0) {
slouken@0
   568
#ifdef USED_BY_SDL
slouken@368
   569
    ((Uint8 *)image->pixels)[xpos + ypos * image->pitch] = v;
slouken@0
   570
#else
slouken@368
   571
    image->data[xpos + ypos * len] = v;
slouken@0
   572
#endif
slouken@368
   573
    ++xpos;
slouken@368
   574
    if (xpos == len) {
slouken@368
   575
        xpos = 0;
slouken@368
   576
        if (interlace) {
slouken@368
   577
        switch (pass) {
slouken@368
   578
        case 0:
slouken@368
   579
        case 1:
slouken@368
   580
            ypos += 8;
slouken@368
   581
            break;
slouken@368
   582
        case 2:
slouken@368
   583
            ypos += 4;
slouken@368
   584
            break;
slouken@368
   585
        case 3:
slouken@368
   586
            ypos += 2;
slouken@368
   587
            break;
slouken@368
   588
        }
slouken@0
   589
slouken@368
   590
        if (ypos >= height) {
slouken@368
   591
            ++pass;
slouken@368
   592
            switch (pass) {
slouken@368
   593
            case 1:
slouken@368
   594
            ypos = 4;
slouken@368
   595
            break;
slouken@368
   596
            case 2:
slouken@368
   597
            ypos = 2;
slouken@368
   598
            break;
slouken@368
   599
            case 3:
slouken@368
   600
            ypos = 1;
slouken@368
   601
            break;
slouken@368
   602
            default:
slouken@368
   603
            goto fini;
slouken@368
   604
            }
slouken@368
   605
        }
slouken@368
   606
        } else {
slouken@368
   607
        ++ypos;
slouken@368
   608
        }
slouken@368
   609
    }
slouken@368
   610
    if (ypos >= height)
slouken@368
   611
        break;
slouken@0
   612
    }
slouken@0
   613
slouken@0
   614
  fini:
slouken@0
   615
slouken@0
   616
    return image;
slouken@0
   617
}
slouken@0
   618
slouken@0
   619
#else
slouken@0
   620
slouken@0
   621
/* See if an image is contained in a data source */
slouken@0
   622
int IMG_isGIF(SDL_RWops *src)
slouken@0
   623
{
slouken@368
   624
    return(0);
slouken@0
   625
}
slouken@0
   626
slouken@0
   627
/* Load a GIF type image from an SDL datasource */
slouken@0
   628
SDL_Surface *IMG_LoadGIF_RW(SDL_RWops *src)
slouken@0
   629
{
slouken@368
   630
    return(NULL);
slouken@0
   631
}
slouken@0
   632
slouken@0
   633
#endif /* LOAD_GIF */
slouken@237
   634
slouken@237
   635
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */