IMG_gif.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 09:41:39 -0500
changeset 280 ec4ae96c100c
parent 237 d1a4fae44bd0
child 288 cbf4a9d168ff
permissions -rw-r--r--
Happy New Year!
slouken@0
     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@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@117
    36
	int start;
slouken@0
    37
	int is_GIF;
slouken@0
    38
	char magic[6];
slouken@0
    39
icculus@154
    40
	if ( !src )
icculus@154
    41
		return 0;
slouken@117
    42
	start = SDL_RWtell(src);
slouken@0
    43
	is_GIF = 0;
slouken@117
    44
	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
slouken@0
    45
		if ( (strncmp(magic, "GIF", 3) == 0) &&
slouken@0
    46
		     ((memcmp(magic + 3, "87a", 3) == 0) ||
slouken@0
    47
		      (memcmp(magic + 3, "89a", 3) == 0)) ) {
slouken@0
    48
			is_GIF = 1;
slouken@0
    49
		}
slouken@0
    50
	}
slouken@236
    51
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@0
    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@0
    57
/* | Copyright 1990, 1991, 1993 David Koblas.			       | */
slouken@0
    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@0
    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@0
    82
#define Image			SDL_Surface
slouken@0
    83
#define RWSetMsg		IMG_SetError
slouken@0
    84
#define ImageNewCmap(w, h, s)	SDL_AllocSurface(SDL_SWSURFACE,w,h,8,0,0,0,0)
slouken@0
    85
#define ImageSetCmap(s, i, R, G, B) do { \
slouken@0
    86
				s->format->palette->colors[i].r = R; \
slouken@0
    87
				s->format->palette->colors[i].g = G; \
slouken@0
    88
				s->format->palette->colors[i].b = B; \
slouken@0
    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@0
    99
#define SDL_RWops	FILE
slouken@0
   100
#define SDL_RWclose	fclose
slouken@0
   101
slouken@0
   102
#endif /* USED_BY_SDL */
slouken@0
   103
slouken@0
   104
slouken@0
   105
#define	MAXCOLORMAPSIZE		256
slouken@0
   106
slouken@0
   107
#define	TRUE	1
slouken@0
   108
#define	FALSE	0
slouken@0
   109
slouken@0
   110
#define CM_RED		0
slouken@0
   111
#define CM_GREEN	1
slouken@0
   112
#define CM_BLUE		2
slouken@0
   113
slouken@0
   114
#define	MAX_LWZ_BITS		12
slouken@0
   115
slouken@0
   116
#define INTERLACE		0x40
slouken@0
   117
#define LOCALCOLORMAP	0x80
slouken@0
   118
#define BitSet(byte, bit)	(((byte) & (bit)) == (bit))
slouken@0
   119
slouken@0
   120
#define	ReadOK(file,buffer,len)	SDL_RWread(file, buffer, len, 1)
slouken@0
   121
slouken@0
   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@0
   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@0
   149
			unsigned char cmap[3][MAXCOLORMAPSIZE],
slouken@0
   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
{
slouken@118
   155
    int 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@118
   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@0
   173
	RWSetMsg("error reading magic number");
slouken@0
   174
        goto done;
slouken@0
   175
    }
slouken@0
   176
    if (strncmp((char *) buf, "GIF", 3) != 0) {
slouken@0
   177
	RWSetMsg("not a GIF file");
slouken@0
   178
        goto done;
slouken@0
   179
    }
slouken@0
   180
    strncpy(version, (char *) buf + 3, 3);
slouken@0
   181
    version[3] = '\0';
slouken@0
   182
slouken@0
   183
    if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) {
slouken@0
   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@0
   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@0
   203
    if (BitSet(buf[4], LOCALCOLORMAP)) {	/* Global Colormap */
slouken@0
   204
	if (ReadColorMap(src, GifScreen.BitPixel, GifScreen.ColorMap,
slouken@0
   205
			 &GifScreen.GrayScale)) {
slouken@0
   206
	    RWSetMsg("error reading global colormap");
slouken@0
   207
            goto done;
slouken@0
   208
	}
slouken@0
   209
    }
slouken@0
   210
    do {
slouken@0
   211
	if (!ReadOK(src, &c, 1)) {
slouken@0
   212
	    RWSetMsg("EOF / read error on image data");
slouken@0
   213
            goto done;
slouken@0
   214
	}
slouken@0
   215
	if (c == ';') {		/* GIF terminator */
slouken@0
   216
	    if (imageCount < imageNumber) {
slouken@0
   217
		RWSetMsg("only %d image%s found in file",
slouken@0
   218
			 imageCount, imageCount > 1 ? "s" : "");
slouken@0
   219
                goto done;
slouken@0
   220
	    }
slouken@0
   221
	}
slouken@0
   222
	if (c == '!') {		/* Extension */
slouken@0
   223
	    if (!ReadOK(src, &c, 1)) {
slouken@0
   224
		RWSetMsg("EOF / read error on extention function code");
slouken@0
   225
                goto done;
slouken@0
   226
	    }
slouken@0
   227
	    DoExtension(src, c);
slouken@0
   228
	    continue;
slouken@0
   229
	}
slouken@0
   230
	if (c != ',') {		/* Not a valid start character */
slouken@0
   231
	    continue;
slouken@0
   232
	}
slouken@0
   233
	++imageCount;
slouken@0
   234
slouken@0
   235
	if (!ReadOK(src, buf, 9)) {
slouken@0
   236
	    RWSetMsg("couldn't read left/top/width/height");
slouken@0
   237
            goto done;
slouken@0
   238
	}
slouken@0
   239
	useGlobalColormap = !BitSet(buf[8], LOCALCOLORMAP);
slouken@0
   240
slouken@0
   241
	bitPixel = 1 << ((buf[8] & 0x07) + 1);
slouken@0
   242
slouken@0
   243
	if (!useGlobalColormap) {
slouken@0
   244
	    if (ReadColorMap(src, bitPixel, localColorMap, &grayScale)) {
slouken@0
   245
		RWSetMsg("error reading local colormap");
slouken@0
   246
                goto done;
slouken@0
   247
	    }
slouken@0
   248
	    image = ReadImage(src, LM_to_uint(buf[4], buf[5]),
slouken@0
   249
			      LM_to_uint(buf[6], buf[7]),
slouken@0
   250
			      bitPixel, localColorMap, grayScale,
slouken@0
   251
			      BitSet(buf[8], INTERLACE),
slouken@0
   252
			      imageCount != imageNumber);
slouken@0
   253
	} else {
slouken@0
   254
	    image = ReadImage(src, LM_to_uint(buf[4], buf[5]),
slouken@0
   255
			      LM_to_uint(buf[6], buf[7]),
slouken@0
   256
			      GifScreen.BitPixel, GifScreen.ColorMap,
slouken@0
   257
			      GifScreen.GrayScale, BitSet(buf[8], INTERLACE),
slouken@0
   258
			      imageCount != imageNumber);
slouken@0
   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@7
   264
        SDL_SetColorKey(image, SDL_SRCCOLORKEY, 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@0
   286
	if (!ReadOK(src, rgb, sizeof(rgb))) {
slouken@0
   287
	    RWSetMsg("bad colormap");
slouken@0
   288
	    return 1;
slouken@0
   289
	}
slouken@0
   290
	buffer[CM_RED][i] = rgb[0];
slouken@0
   291
	buffer[CM_GREEN][i] = rgb[1];
slouken@0
   292
	buffer[CM_BLUE][i] = rgb[2];
slouken@0
   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@0
   298
	*gray = (number == 2) ? PBM_TYPE : PGM_TYPE;
slouken@0
   299
    else
slouken@0
   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@0
   315
    case 0x01:			/* Plain Text Extension */
slouken@0
   316
	str = "Plain Text Extension";
slouken@0
   317
	break;
slouken@0
   318
    case 0xff:			/* Application Extension */
slouken@0
   319
	str = "Application Extension";
slouken@0
   320
	break;
slouken@0
   321
    case 0xfe:			/* Comment Extension */
slouken@0
   322
	str = "Comment Extension";
slouken@148
   323
	while (GetDataBlock(src, (unsigned char *) buf) != 0)
slouken@148
   324
	    ;
slouken@0
   325
	return FALSE;
slouken@0
   326
    case 0xf9:			/* Graphic Control Extension */
slouken@0
   327
	str = "Graphic Control Extension";
slouken@0
   328
	(void) GetDataBlock(src, (unsigned char *) buf);
slouken@0
   329
	Gif89.disposal = (buf[0] >> 2) & 0x7;
slouken@0
   330
	Gif89.inputFlag = (buf[0] >> 1) & 0x1;
slouken@0
   331
	Gif89.delayTime = LM_to_uint(buf[1], buf[2]);
slouken@0
   332
	if ((buf[0] & 0x1) != 0)
slouken@0
   333
	    Gif89.transparent = buf[3];
slouken@0
   334
slouken@148
   335
	while (GetDataBlock(src, (unsigned char *) buf) != 0)
slouken@148
   336
	    ;
slouken@0
   337
	return FALSE;
slouken@0
   338
    default:
slouken@0
   339
	str = (char *)buf;
slouken@0
   340
	sprintf(str, "UNKNOWN (0x%02x)", label);
slouken@0
   341
	break;
slouken@0
   342
    }
slouken@0
   343
slouken@148
   344
    while (GetDataBlock(src, (unsigned char *) buf) != 0)
slouken@148
   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@0
   358
	/* pm_message("error in getting DataBlock size" ); */
slouken@0
   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@0
   364
	/* pm_message("error in reading DataBlock" ); */
slouken@0
   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@0
   379
	curbit = 0;
slouken@0
   380
	lastbit = 0;
slouken@0
   381
	done = FALSE;
slouken@0
   382
	return 0;
slouken@0
   383
    }
slouken@0
   384
    if ((curbit + code_size) >= lastbit) {
slouken@0
   385
	if (done) {
slouken@0
   386
	    if (curbit >= lastbit)
slouken@0
   387
		RWSetMsg("ran off the end of my bits");
slouken@0
   388
	    return -1;
slouken@0
   389
	}
slouken@0
   390
	buf[0] = buf[last_byte - 2];
slouken@0
   391
	buf[1] = buf[last_byte - 1];
slouken@0
   392
slouken@0
   393
	if ((count = GetDataBlock(src, &buf[2])) == 0)
slouken@0
   394
	    done = TRUE;
slouken@0
   395
slouken@0
   396
	last_byte = 2 + count;
slouken@0
   397
	curbit = (curbit - lastbit) + 16;
slouken@0
   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@0
   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@0
   427
	set_code_size = input_code_size;
slouken@0
   428
	code_size = set_code_size + 1;
slouken@0
   429
	clear_code = 1 << set_code_size;
slouken@0
   430
	end_code = clear_code + 1;
slouken@0
   431
	max_code_size = 2 * clear_code;
slouken@0
   432
	max_code = clear_code + 2;
slouken@0
   433
slouken@0
   434
	GetCode(src, 0, TRUE);
slouken@0
   435
slouken@0
   436
	fresh = TRUE;
slouken@0
   437
slouken@0
   438
	for (i = 0; i < clear_code; ++i) {
slouken@0
   439
	    table[0][i] = 0;
slouken@0
   440
	    table[1][i] = i;
slouken@0
   441
	}
slouken@0
   442
	for (; i < (1 << MAX_LWZ_BITS); ++i)
slouken@0
   443
	    table[0][i] = table[1][0] = 0;
slouken@0
   444
slouken@0
   445
	sp = stack;
slouken@0
   446
slouken@0
   447
	return 0;
slouken@0
   448
    } else if (fresh) {
slouken@0
   449
	fresh = FALSE;
slouken@0
   450
	do {
slouken@0
   451
	    firstcode = oldcode = GetCode(src, code_size, FALSE);
slouken@0
   452
	} while (firstcode == clear_code);
slouken@0
   453
	return firstcode;
slouken@0
   454
    }
slouken@0
   455
    if (sp > stack)
slouken@0
   456
	return *--sp;
slouken@0
   457
slouken@0
   458
    while ((code = GetCode(src, code_size, FALSE)) >= 0) {
slouken@0
   459
	if (code == clear_code) {
slouken@0
   460
	    for (i = 0; i < clear_code; ++i) {
slouken@0
   461
		table[0][i] = 0;
slouken@0
   462
		table[1][i] = i;
slouken@0
   463
	    }
slouken@0
   464
	    for (; i < (1 << MAX_LWZ_BITS); ++i)
slouken@0
   465
		table[0][i] = table[1][i] = 0;
slouken@0
   466
	    code_size = set_code_size + 1;
slouken@0
   467
	    max_code_size = 2 * clear_code;
slouken@0
   468
	    max_code = clear_code + 2;
slouken@0
   469
	    sp = stack;
slouken@0
   470
	    firstcode = oldcode = GetCode(src, code_size, FALSE);
slouken@0
   471
	    return firstcode;
slouken@0
   472
	} else if (code == end_code) {
slouken@0
   473
	    int count;
slouken@0
   474
	    unsigned char buf[260];
slouken@0
   475
slouken@0
   476
	    if (ZeroDataBlock)
slouken@0
   477
		return -2;
slouken@0
   478
slouken@148
   479
	    while ((count = GetDataBlock(src, buf)) > 0)
slouken@148
   480
		;
slouken@0
   481
slouken@0
   482
	    if (count != 0) {
slouken@0
   483
		/*
slouken@0
   484
		 * pm_message("missing EOD in data stream (common occurence)");
slouken@0
   485
		 */
slouken@0
   486
	    }
slouken@0
   487
	    return -2;
slouken@0
   488
	}
slouken@0
   489
	incode = code;
slouken@0
   490
slouken@0
   491
	if (code >= max_code) {
slouken@0
   492
	    *sp++ = firstcode;
slouken@0
   493
	    code = oldcode;
slouken@0
   494
	}
slouken@0
   495
	while (code >= clear_code) {
slouken@0
   496
	    *sp++ = table[1][code];
slouken@0
   497
	    if (code == table[0][code])
slouken@0
   498
		RWSetMsg("circular table entry BIG ERROR");
slouken@0
   499
	    code = table[0][code];
slouken@0
   500
	}
slouken@0
   501
slouken@0
   502
	*sp++ = firstcode = table[1][code];
slouken@0
   503
slouken@0
   504
	if ((code = max_code) < (1 << MAX_LWZ_BITS)) {
slouken@0
   505
	    table[0][code] = oldcode;
slouken@0
   506
	    table[1][code] = firstcode;
slouken@0
   507
	    ++max_code;
slouken@0
   508
	    if ((max_code >= max_code_size) &&
slouken@0
   509
		(max_code_size < (1 << MAX_LWZ_BITS))) {
slouken@0
   510
		max_code_size *= 2;
slouken@0
   511
		++code_size;
slouken@0
   512
	    }
slouken@0
   513
	}
slouken@0
   514
	oldcode = incode;
slouken@0
   515
slouken@0
   516
	if (sp > stack)
slouken@0
   517
	    return *--sp;
slouken@0
   518
    }
slouken@0
   519
    return code;
slouken@0
   520
}
slouken@0
   521
slouken@0
   522
static Image *
slouken@0
   523
ReadImage(SDL_RWops * src, int len, int height, int cmapSize,
slouken@0
   524
	  unsigned char cmap[3][MAXCOLORMAPSIZE],
slouken@0
   525
	  int gray, int interlace, int ignore)
slouken@0
   526
{
slouken@0
   527
    Image *image;
slouken@0
   528
    unsigned char c;
slouken@0
   529
    int i, v;
slouken@0
   530
    int xpos = 0, ypos = 0, pass = 0;
slouken@0
   531
slouken@0
   532
    /*
slouken@0
   533
    **	Initialize the compression routines
slouken@0
   534
     */
slouken@0
   535
    if (!ReadOK(src, &c, 1)) {
slouken@0
   536
	RWSetMsg("EOF / read error on image data");
slouken@0
   537
	return NULL;
slouken@0
   538
    }
slouken@0
   539
    if (LWZReadByte(src, TRUE, c) < 0) {
slouken@0
   540
	RWSetMsg("error reading image");
slouken@0
   541
	return NULL;
slouken@0
   542
    }
slouken@0
   543
    /*
slouken@0
   544
    **	If this is an "uninteresting picture" ignore it.
slouken@0
   545
     */
slouken@0
   546
    if (ignore) {
slouken@148
   547
	while (LWZReadByte(src, FALSE, c) >= 0)
slouken@148
   548
	    ;
slouken@0
   549
	return NULL;
slouken@0
   550
    }
slouken@0
   551
    image = ImageNewCmap(len, height, cmapSize);
slouken@0
   552
slouken@0
   553
    for (i = 0; i < cmapSize; i++)
slouken@0
   554
	ImageSetCmap(image, i, cmap[CM_RED][i],
slouken@0
   555
		     cmap[CM_GREEN][i], cmap[CM_BLUE][i]);
slouken@0
   556
slouken@0
   557
    while ((v = LWZReadByte(src, FALSE, c)) >= 0) {
slouken@0
   558
#ifdef USED_BY_SDL
slouken@0
   559
	((Uint8 *)image->pixels)[xpos + ypos * image->pitch] = v;
slouken@0
   560
#else
slouken@0
   561
	image->data[xpos + ypos * len] = v;
slouken@0
   562
#endif
slouken@0
   563
	++xpos;
slouken@0
   564
	if (xpos == len) {
slouken@0
   565
	    xpos = 0;
slouken@0
   566
	    if (interlace) {
slouken@0
   567
		switch (pass) {
slouken@0
   568
		case 0:
slouken@0
   569
		case 1:
slouken@0
   570
		    ypos += 8;
slouken@0
   571
		    break;
slouken@0
   572
		case 2:
slouken@0
   573
		    ypos += 4;
slouken@0
   574
		    break;
slouken@0
   575
		case 3:
slouken@0
   576
		    ypos += 2;
slouken@0
   577
		    break;
slouken@0
   578
		}
slouken@0
   579
slouken@0
   580
		if (ypos >= height) {
slouken@0
   581
		    ++pass;
slouken@0
   582
		    switch (pass) {
slouken@0
   583
		    case 1:
slouken@0
   584
			ypos = 4;
slouken@0
   585
			break;
slouken@0
   586
		    case 2:
slouken@0
   587
			ypos = 2;
slouken@0
   588
			break;
slouken@0
   589
		    case 3:
slouken@0
   590
			ypos = 1;
slouken@0
   591
			break;
slouken@0
   592
		    default:
slouken@0
   593
			goto fini;
slouken@0
   594
		    }
slouken@0
   595
		}
slouken@0
   596
	    } else {
slouken@0
   597
		++ypos;
slouken@0
   598
	    }
slouken@0
   599
	}
slouken@0
   600
	if (ypos >= height)
slouken@0
   601
	    break;
slouken@0
   602
    }
slouken@0
   603
slouken@0
   604
  fini:
slouken@0
   605
slouken@0
   606
    return image;
slouken@0
   607
}
slouken@0
   608
slouken@0
   609
#else
slouken@0
   610
slouken@0
   611
/* See if an image is contained in a data source */
slouken@0
   612
int IMG_isGIF(SDL_RWops *src)
slouken@0
   613
{
slouken@0
   614
	return(0);
slouken@0
   615
}
slouken@0
   616
slouken@0
   617
/* Load a GIF type image from an SDL datasource */
slouken@0
   618
SDL_Surface *IMG_LoadGIF_RW(SDL_RWops *src)
slouken@0
   619
{
slouken@0
   620
	return(NULL);
slouken@0
   621
}
slouken@0
   622
slouken@0
   623
#endif /* LOAD_GIF */
slouken@237
   624
slouken@237
   625
#endif /* !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) */