/* p_gif.c
 * Copyright (C) 1997-98 Thomas Merz. All rights reserved.
 *
 * GIF processing for PDFlib
 */

/* 
 * This module is basically a rewrite of David Koblas' giftoppm.c,
 * taken from the PBMPLUS package. It contained the following notice:
 */

/* +-------------------------------------------------------------------+ */
/* | Copyright 1990, David Koblas.                                     | */
/* |   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.           | */
/* +-------------------------------------------------------------------+ */

#include <string.h>

#include "p_intern.h"

#define CM_RED			0
#define CM_GREEN		1
#define CM_BLUE			2

#define	MAX_LWZ_BITS		12

#define INTERLACE		0x40
#define LOCALCOLORMAP		0x80
#define BitSet(byte, bit)	(((byte) & (bit)) == (bit))

#define	ReadOK(file,buffer,len)	(fread(buffer, len, 1, file) != 0)

#define LM_to_uint(a,b)			(((b)<<8)|(a))

static int ReadColorMap(FILE *fp, int number, pdf_colormap buffer);
static int DoExtension(PDF *p, PDF_image *image, int label);
static int GetDataBlock(FILE *fp, unsigned char  *buf);
static void ReadImage(PDF *p, PDF_image *image, PDF_data_source *src, int ignore);

/* imported via strange #include due to licensing issues... */
int GetCode(FILE *fp, int code_size, int flag);
int LWZReadByte(FILE *fd, int flag, int input_code_size);

static void
pdf_data_source_GIF_init(PDF *p, PDF_data_source *src)
{
    PDF_image		*image;

    image = (PDF_image *) src->private_data;

    src->buffer_length	= image->width * image->height * 1;
    src->buffer_start	= 
	    (byte*) PDF_malloc(src->buffer_length, "pdf_data_source_GIF_init");
    src->bytes_available= 0;
    src->next_byte	= NULL;

    if (!image->useGlobalColormap) {
	if (ReadColorMap(image->fp, image->bitPixel, image->colormap)) {
	    pdf_error(p, PDF_WARN, "Error reading local colormap");
	    return;
	}
    }
}

static bool
pdf_data_source_GIF_fill(PDF *p, PDF_data_source *src)
{
    PDF_image	*image;

    if (src->next_byte != NULL)		/* all finished in one turn */
	return false;

    image = (PDF_image *) src->private_data;

    src->next_byte = src->buffer_start;
    src->bytes_available = src->buffer_length;

    ReadImage(p, image, src, false);

    return true;
}

static void
pdf_data_source_GIF_terminate(PDF *p, PDF_data_source *src)
{
  PDF_free((void *) src->buffer_start);
}

PDF_image *
PDF_open_GIF(PDF *p, char *filename)
{
    unsigned char	buf[16];
    char	c;
    int		imageCount = 0;
    char	version[4];
    int		imageNumber = 1;	/* HACK:read only first image */
    PDF_image	*image;
	
    image = (PDF_image *) PDF_malloc(sizeof(PDF_image), "PDF_open_GIF");

    if (image == NULL)
	return NULL;

    image->filename	= filename;
    image->compression	= none;
    image->closefunc	= PDF_close_GIF;

    if ((image->fp = fopen(image->filename, READMODE)) == NULL) {
	pdf_error(p, PDF_WARN, "Couldn't open GIF file %s", image->filename);
	PDF_free(image);
	return NULL;
    }

    if (! ReadOK(image->fp,buf,6)) {
	pdf_error(p, PDF_WARN, "Error reading magic number" );
	return NULL;
    }

    if (strncmp((const char *) buf, "GIF",3) != 0) {
	pdf_error(p, PDF_WARN, "Not a GIF file" );
	return NULL;
    }

    strncpy(version, (const char *) buf + 3, 3);
    version[3] = '\0';

    if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) {
	pdf_error(p, PDF_WARN, "Bad version number" );
	return NULL;
    }

    if (! ReadOK(image->fp,buf,7)) {
	pdf_error(p, PDF_WARN, "Failed to read screen descriptor" );
	return NULL;
    }

    /* size of the global color table*/
    image->BitPixel		= 2<<(buf[4]&0x07);

    if (BitSet(buf[4], LOCALCOLORMAP)) {	/* Global Colormap */
	if (ReadColorMap(image->fp, image->BitPixel, image->colormap)) {
	    pdf_error(p, PDF_WARN, "Error reading global colormap");
	    return NULL;
	}
    }

#ifdef ORIG
    if (image->AspectRatio != 0 && image->AspectRatio != 49) {
	float	r;
	r = ( (float) image->AspectRatio + 15.0 ) / 64.0;
	/*pm_message("warning - non-square pixels");*/
    }
#endif

    for (;;) {
	if (! ReadOK(image->fp,&c,1)) {
	    pdf_error(p, PDF_WARN, "EOF / read error on image data" );
	    return NULL;
	}

	if (c == ';') {		/* GIF terminator */
	    if (imageCount < imageNumber) {
		pdf_error(p, PDF_WARN, "Only %d image%s found in file",
			 imageCount, imageCount > 1?"s":"" );
		return NULL;
	    }
	    break;
	}

	if (c == '!') { 	/* Extension */
	    if (! ReadOK(image->fp,&c,1)) {
		pdf_error(p, PDF_INFO, 
		    "EOF / read error on extension function code");
		return NULL;
	    }
	    DoExtension(p, image, c);
	    continue;
	}

	if (c != ',') {		/* Not a valid start character */
	    pdf_error(p, PDF_WARN,
		"Bogus character 0x%02x, ignoring", (int) c);
	    continue;
	}

	++imageCount;

	if (! ReadOK(image->fp,buf,9)) {
	    pdf_error(p, PDF_WARN, 
		"Couldn't read left/top/width/height");
	    return NULL;
	}

	image->colorspace = DeviceRGB;	/* HACK */
	image->indexed = true;
	image->components = 3;		/* HACK */
	image->bpc = 8;			/* HACK */
	image->width	= LM_to_uint(buf[4],buf[5]);
	image->height	= LM_to_uint(buf[6],buf[7]);
	image->useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP);
	image->bitPixel = 1<<((buf[8]&0x07)+1);
	image->interlace= BitSet(buf[8], INTERLACE);

	if (imageCount == 1)
	    break;
    }
    
    image->src.init		= pdf_data_source_GIF_init;
    image->src.fill		= pdf_data_source_GIF_fill;
    image->src.terminate	= pdf_data_source_GIF_terminate;
    image->src.private_data	= (void *) image;

    return image;
}

void
PDF_close_GIF(PDF *p, PDF_image *image)
{
    fclose(image->fp);
}

static int
ReadColorMap(FILE *fp, int number, pdf_colormap buffer)
{
    int		i;
    unsigned char	rgb[3];

    for (i = 0; i < number; ++i) {
	    if (! ReadOK(fp, rgb, sizeof(rgb))) {
		return true;
	    }

	    buffer[i][0] = rgb[0] ;
	    buffer[i][1] = rgb[1] ;
	    buffer[i][2] = rgb[2] ;
    }
    return false;
}

static int
DoExtension(PDF *p, PDF_image *image, int label)
{
	static char	buf[256];
	char		*str;

	switch (label) {
	case 0x01:		/* Plain Text Extension */
		str = "Plain Text Extension";
#ifdef notdef
		if (GetDataBlock(image->fp, (unsigned char*) buf) == 0)
			;

		lpos   = LM_to_uint(buf[0], buf[1]);
		tpos   = LM_to_uint(buf[2], buf[3]);
		width  = LM_to_uint(buf[4], buf[5]);
		height = LM_to_uint(buf[6], buf[7]);
		cellw  = buf[8];
		cellh  = buf[9];
		foreground = buf[10];
		background = buf[11];

		while (GetDataBlock(image->fp, (unsigned char*) buf) != 0) {
			PPM_ASSIGN(image[ypos][xpos],
					cmap[v][CM_RED],
					cmap[v][CM_GREEN],
					cmap[v][CM_BLUE]);
			++index;
		}

		return false;
#else
		break;
#endif
	case 0xff:		/* Application Extension */
		str = "Application Extension";
		break;
	case 0xfe:		/* Comment Extension */
		str = "Comment Extension";
		while (GetDataBlock(image->fp, (unsigned char*) buf) != 0) {
			/*pm_message("gif comment: %s", buf );*/
		}
		return false;
	case 0xf9:		/* Graphic Control Extension */
		str = "Graphic Control Extension";
		(void) GetDataBlock(image->fp, (unsigned char*) buf);
		image->disposal    = (buf[0] >> 2) & 0x7;
		image->inputFlag   = (buf[0] >> 1) & 0x1;
		image->delayTime   = LM_to_uint(buf[1],buf[2]);
		if ((buf[0] & 0x1) != 0)
			image->transparent = buf[3];

		while (GetDataBlock(image->fp, (unsigned char*) buf) != 0)
			;
		return false;
	default:
		str = buf;
		sprintf(buf, "UNKNOWN (0x%02x)", label);
		break;
	}

	/*pm_message("got a '%s' extension", str );*/

	while (GetDataBlock(image->fp, (unsigned char*) buf) != 0)
		;

	return false;
}

static int	ZeroDataBlock = false;

static int
GetDataBlock(FILE *fp, unsigned char *buf)
{
	unsigned char	count;

	if (! ReadOK(fp,&count,1)) {
		/*pdf_error(p, PDF_WARN, "Error in getting DataBlock size" );*/
		return -1;
	}

	ZeroDataBlock = count == 0;

	if ((count != 0) && (! ReadOK(fp, buf, count))) {
		/*pdf_error(p, PDF_WARN, "Error in reading DataBlock" );*/
		return -1;
	}

	return count;
}

/* Dummies for LZW code */
#define pm_message(x)	/* */
#define pm_error(x)	/* */

int
GetCode(FILE *fp, int code_size, int flag)
{
	static unsigned char	buf[280];
	static int		curbit, lastbit, done, last_byte;
	int			i, j, ret;
	unsigned char		count;

	if (flag) {
		curbit = 0;
		lastbit = 0;
		done = false;
		return 0;
	}

	if ( (curbit+code_size) >= lastbit) {
		if (done) {
			if (curbit >= lastbit)
				pm_error("ran off the end of my bits" );
			return -1;
		}
		buf[0] = buf[last_byte-2];
		buf[1] = buf[last_byte-1];

		if ((count = GetDataBlock(fp, &buf[2])) == 0)
			done = true;

		last_byte = 2 + count;
		curbit = (curbit - lastbit) + 16;
		lastbit = (2+count)*8 ;
	}

	ret = 0;
	for (i = curbit, j = 0; j < code_size; ++i, ++j)
		ret |= ((buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;

	curbit += code_size;

	return ret;
}

int
LWZReadByte(FILE *fd, int flag, int input_code_size)
{
	static int	fresh = false;
	int		code, incode;
	static int	code_size, set_code_size;
	static int	max_code, max_code_size;
	static int	firstcode, oldcode;
	static int	clear_code, end_code;
	static int	table[2][(1<< MAX_LWZ_BITS)];
	static int	stack[(1<<(MAX_LWZ_BITS))*2], *sp;
	register int	i;

	if (flag) {
		set_code_size = input_code_size;
		code_size = set_code_size+1;
		clear_code = 1 << set_code_size ;
		end_code = clear_code + 1;
		max_code_size = 2*clear_code;
		max_code = clear_code+2;

		GetCode(fd, 0, true);
		
		fresh = true;

		for (i = 0; i < clear_code; ++i) {
			table[0][i] = 0;
			table[1][i] = i;
		}
		for (; i < (1<<MAX_LWZ_BITS); ++i)
			table[0][i] = table[1][0] = 0;

		sp = stack;

		return 0;
	} else if (fresh) {
		fresh = false;
		do {
			firstcode = oldcode =
				GetCode(fd, code_size, false);
		} while (firstcode == clear_code);
		return firstcode;
	}

	if (sp > stack)
		return *--sp;

	while ((code = GetCode(fd, code_size, false)) >= 0) {
		if (code == clear_code) {
			for (i = 0; i < clear_code; ++i) {
				table[0][i] = 0;
				table[1][i] = i;
			}
			for (; i < (1<<MAX_LWZ_BITS); ++i)
				table[0][i] = table[1][i] = 0;
			code_size = set_code_size+1;
			max_code_size = 2*clear_code;
			max_code = clear_code+2;
			sp = stack;
			firstcode = oldcode =
					GetCode(fd, code_size, false);
			return firstcode;
		} else if (code == end_code) {
			int		count;
			unsigned char	buf[260];

			if (ZeroDataBlock)
				return -2;

			while ((count = GetDataBlock(fd, buf)) > 0)
				;

			if (count != 0)
			    pm_message("missing EOD in data stream (common occurence)");
			return -2;
		}

		incode = code;

		if (code >= max_code) {
			*sp++ = firstcode;
			code = oldcode;
		}

		while (code >= clear_code) {
			*sp++ = table[1][code];
			if (code == table[0][code])
				pm_error("circular table entry BIG ERROR");
			code = table[0][code];
		}

		*sp++ = firstcode = table[1][code];

		if ((code = max_code) <(1<<MAX_LWZ_BITS)) {
			table[0][code] = oldcode;
			table[1][code] = firstcode;
			++max_code;
			if ((max_code >= max_code_size) &&
				(max_code_size < (1<<MAX_LWZ_BITS))) {
				max_code_size *= 2;
				++code_size;
			}
		}

		oldcode = incode;

		if (sp > stack)
			return *--sp;
	}
	return code;
}


static void
ReadImage(PDF *p, PDF_image *image, PDF_data_source *src, int ignore)
{
	unsigned char	c;	
	int		v;
	int		xpos = 0, ypos = 0, pass = 0;
	byte		*dest;

	/*
	**  Initialize the Compression routines
	*/
	if (! ReadOK(image->fp,&c,1))
		pm_error("EOF / read error on image data" );

	if (LWZReadByte(image->fp, true, c) < 0)
		pm_error("error reading image" );

	/*
	**  If this is an "uninteresting picture" ignore it.
	*/
	if (ignore) {
	    pdf_error(p, PDF_INFO, "Skipping image..." );
	    while (LWZReadByte(image->fp, false, c) >= 0)
		    ;
	    return;
	}

	dest = src->buffer_start;
	while ((v = LWZReadByte(image->fp,false,c)) >= 0 ) {

		*dest++ = v;	/* fetch pixel value */
		++xpos;

		if (xpos == image->width) {
			xpos = 0;

			if (image->interlace) {
				switch (pass) {
				case 0:
				case 1:
					ypos += 8; break;
				case 2:
					ypos += 4; break;
				case 3:
					ypos += 2; break;
				}

				if (ypos >= image->height) {
					++pass;
					switch (pass) {
					case 1:
						ypos = 4; break;
					case 2:
						ypos = 2; break;
					case 3:
						ypos = 1; break;
					default:
						goto fini;
					}
				}
			} else {
				++ypos;
			}
			dest = src->buffer_start + ypos * image->width;
		}
		if (ypos >= image->height)
			break;
	}

fini:
	if (LWZReadByte(image->fp,false,c)>=0)
	    pdf_error(p, PDF_WARN, "Too much input data, ignoring extra.");
}
