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