/*	MiscGIFData.h

	Copyright 1996 Uwe Hoffmann.
	Copyright 1996 Netsurfer Inc.

	This notice may not be removed from this source code.
	The use and distribution of this software is governed by the
	terms of the MiscKit license agreement.  Refer to the license
	document included with the MiscKit distribution for the terms.

	Author: <bbum@friday.com>
	Converted to OpenStep, September 1996, Uwe Hoffmann.
*/

/*
** Copyright 1990, 1991, 1993, David Koblas.  (koblas@netcom.com)
** 
** Permission to use, copy, modify, and distribute this software  and its
** documentation for any purpose and without fee is hereby  granted,
** provided that the above copyright notice appear in all copies and that
** both that copyright notice and this permission  notice appear in
** supporting documentation.  This software is    provided "as is" without
** express or implied warranty.
*/

#import <AppKit/AppKit.h>

typedef struct _MiscColorEntry32 {
	unsigned char 	red:8;
	unsigned char	green:8;
	unsigned char	blue:8;
	unsigned char	alpha:8;
} MiscColorEntry32;

typedef struct _MiscColorEntry16 {
	unsigned char 	red:4;
	unsigned char	green:4;
	unsigned char	blue:4;
	unsigned char	alpha:4;
} MiscColorEntry16;

/*"
 * Maximum number of bits present in any single LWZ code.
"*/

#define        MAX_LWZ_BITS				12

#ifndef FALSE
#define FALSE NO
#endif

#ifndef TRUE
#define TRUE YES
#endif

/// RETURN CODES
#define LWZ_NO_ERROR		 0	// nothing amiss
#define LWZ_DONE 		-1  	// missing EOD-- common, usually ignored.
#define LWZ_NO_EOD 		-2  	// missing EOD-- common, usually ignored.
#define LWZ_CIRCULAR_ERR	-3  	// circular code-- fatal
#define LWZ_BIT_OVERRUN		-4	// ran off end of bits
#define LWZ_DATA_BLOCK_ERR	-5	// error reading data block

/*"
 * Lempel-Welch-Ziv decompression state (rather large).
"*/
typedef struct __MiscLWZState {
    /*" state used by miscLWZReadByte
     "*/
	int				code_size;
	int				set_code_size;
	int				max_code;
	int				max_code_size;
	int				firstcode;
	int				oldcode;
	int				clear_code;
	int				end_code;
	int				table[2][ (1 << MAX_LWZ_BITS) ];
	int     			stack[ (1 << MAX_LWZ_BITS) * 2];
	int				*stackPtr;
	BOOL				fresh;
	BOOL				zeroDataBlock;
    /*" state used by GetCode, GetDataBlock
     "*/
	unsigned char 			buf[280];
	int				curbit;
	int				lastbit;
	BOOL				done;
	int				last_byte;
	unsigned int			alreadyRead;
} MiscLWZState;

@interface MiscGIFData:NSObject
{
@private
	NSData *data;	
	unsigned int length;
	unsigned int nbytes;
	unsigned int alreadyRead;
	NSRange range;
	MiscLWZState *decompressionState;
}

- initWithGIFData:(NSData *)gifData;

- (void)resetStream;
- (void)setStream:(unsigned int)aPosition;
- (BOOL)atEOS;

- (BOOL)getHeader:(char *)buffer;
- (BOOL)getVersion:(char *)buffer;
- (BOOL)getLSD:(char *)buffer;
- (BOOL)getColorTable16:(MiscColorEntry16 *)buffer size:(unsigned int)tableSize;
- (BOOL)getColorTable32:(MiscColorEntry32 *)buffer size:(unsigned int)tableSize;
- (BOOL)getGCE:(char *)buffer;
- (BOOL)getImageDescriptor:(char *)buffer;
- (BOOL)getCharacter:(unsigned char *)buffer;
- (BOOL)startDecompressionEngine;
- (int)LWZReadByte;
- (BOOL)skipDataBlocks;

@end


