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