/*
 * Berechnung des Schwerpunkts aller Landflächen der Erde
 * (geographisches Zentrum)
 *
 * Der Schwerpunkt sei hier die Punktemenge,
 * deren Summe aller Entfernungen zu allen Punkten
 * auf Landflächen minimal ist.
 * Die Entfernung wird dabei ueber den sphärischen
 * Grosskreis bestimmt.
 *
 * Autor: Holger Isenberg 
 * 
 * H.Isenberg@ping.de
 * http://mars-news.de
 *
 * Data-Source:
 * NOAA Satellites & Data
 * ETOPO2 (Global 2-min gridded data)
 * http://www.ngdc.noaa.gov/mgg/global/global.html
 *
 * GIF-Reader from giftopnm.c by David Koblas (koblas@netcom.com)
 *
 */

#include <stdlib.h>
#include <unistd.h>
#include <values.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#define PI M_PI
#define MAXCOLORMAPSIZE  256

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

#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))

#define GRAYSCALE        1
#define COLOR            2

typedef unsigned char CMap[3][MAXCOLORMAPSIZE];

static struct
{
  unsigned int Width;
  unsigned int Height;
  CMap ColorMap;
  unsigned int BitPixel;
  unsigned int ColorResolution;
  unsigned int Background;
  unsigned int AspectRatio;
  /*
   **
   */
  int GrayScale;
} GifScreen;

static struct
{
  int transparent;
  int delayTime;
  int inputFlag;
  int disposal;
} Gif89 = { -1, -1, -1, 0 };

int  highest_used_index = 0;
static unsigned char   used_cmap[3][256];
static unsigned char   gimp_cmap[768];
int ZeroDataBlock = FALSE;

static int ReadColorMap (FILE *, int, CMap, int *);
static int DoExtension (FILE *, int);
static int GetDataBlock (FILE *, unsigned char *);
static int GetCode (FILE *, int, int);
static int LWZReadByte (FILE *, int, int);
unsigned char* ReadImage (FILE *fd, char *filename, int   len, int   height, CMap  cmap,
	   int   ncols, int   format, int   interlace, int   number, unsigned int   leftpos,
	   unsigned int   toppos,  unsigned int screenwidth, unsigned int screenheight);
unsigned char* loadGIF(char* filename, int* width, int* height);

int
main(int argc, char* argv[])
{
	double xtrad, ytrad,
		sum, max_sum, min_sum, min,
		xs, ys,
		piWidth, piRes,
		xrad, yrad,
		d;
	long x,y,xt,yt,xc,yc,xstep,ystep,step,res=128,level=0,i;
	long area_x=-1, area_y=-1, area_s=-1;
	double area_xt, area_yt, area_st, ratio_l, ratio_w;
	int calc;
	long fx,fy;
	long lastx, lasty;
	char c, newcalc,newy;
	FILE* outfile;
	char* inputfile=NULL;
	char* outputfile=NULL;
	double* result;
	unsigned char* land;
	int land_h, land_w,map;

	while (1)
	{
		int this_option_optind = optind ? optind : 1;
		int option_index = 0;
		
		c = getopt(argc, argv, "i:x:y:s:o:r:l:h");
		if (c == -1)
			break; 
		
		switch (c)    
		{
		    case 0:
			    printf ("option");
			    if (optarg)
				    printf (" with arg %s", optarg);
			    printf ("0");
			    break;
			    break;
			    
		    case 'h':
			    printf("usage: %s -i input-GIF -o output-PGM\n", argv[0]);
			    printf("\t\t-x xpos -y ypos -s width (of calculation area)\n");
			    printf("\t\t-r result-width -l water-level\n");
			    printf("Input is a grayscale GIF as linear map projected global DEM.\n\n");
			    break;

		    case 'x':
			    area_x=atoi(optarg);
			    break;

		    case 'y':
			    area_y=atoi(optarg);
			    break;

		    case 's':
			    area_s=atoi(optarg);
			    break;

		    case 'i':
			    inputfile=strdup(optarg);
			    break;

		    case 'o':
			    outputfile=malloc(strlen(optarg)+5);
			    strcpy(outputfile, optarg);
			    break;
			    
		    case 'r':
			    res=atoi(optarg);
			    break;

		    case 'l':
			    level=atoi(optarg);
			    break;
			    
		    default:
			    printf ("?? getopt returned character code 0 ??", c);
		}
	}
	
	if(res<=0 || level<0 || inputfile==0) {
		printf("usage: %s -i input-GIF -o output-PGM\n", argv[0]);
		printf("\t\t-r result-width -l water-level\n");
		printf("Input is a grayscale GIF as linear map projected global DEM.\n\n");
		exit(1);
	}
	if(!outputfile) {
		outputfile=malloc(strlen(inputfile)+3);
		strcpy(outputfile, inputfile);
		outputfile[strlen(inputfile)-4]='\0';
		strcat(outputfile, "_c.pgm");
	}
	land = loadGIF(inputfile, &land_w, &land_h);
	if(!land) {
		fprintf(stderr, "could not load GIF\n");
		exit(1);
	}
	if((result=(double*)malloc(res*res/2*sizeof(double)))==NULL) {
		printf("cannot allocate %ld bytes\n", res*res/2*sizeof(double));
		exit(1);
	}

	//step=land_w/res;
	step=1;
	if(area_x==-1 || area_y==-1 || area_s==-1) {
		area_x=30;
		area_y=30;
		area_s=30;
	}
	piWidth=2*M_PI/land_w;
	piRes=2*M_PI/res;
	min_sum=MAXDOUBLE;
	max_sum=0.0;
	ystep=step;
	xstep=step;
	area_xt=(long)(res*(180+area_x)/360.0);
	area_yt=(long)(res*(90-area_y)/360.0);
	area_st=(long)(res*area_s/360.0);
	for(yt=0; yt<res/2; yt++)
		for(xt=0; xt<res; xt++)
			result[xt+yt*res]=land[xt*land_w/res+yt*land_w/res*land_w]>level?.0:1000.0;
	printf("PNM: output %d x %d\n", res, res/2);
	printf("calculation area %d°N %d°E, +-%d°\n", area_y, area_x, area_s);
	for(y=0,ratio_w=.0,ratio_l=.0; y<land_h; y++) {
		yrad=(double)(y)*piWidth;
		for(x=0; x<land_w; x++)
			if(land[x+y*land_w]>level) 
				ratio_l+=sin(yrad);
			else
				ratio_w+=sin(yrad);
	}
	printf("land: %d%%\n", (int)(ratio_l/(ratio_l+ratio_w)*100.0));
	for(yt=area_yt-area_st; yt<(area_yt+area_st); yt+=ystep) {
		for(xt=area_xt-area_st, ytrad=yt*piRes; xt<(area_xt+area_st); xt+=xstep) {
			printf("\r%3d%% [%5d, %5d]", (int)(50.0*(yt-area_yt+area_st)/(float)area_st), xt, yt);
			fflush(0);
			//printf("\rxtrad=%3.0f° ytrad=%3.0f° xt=%5d yt=%5d", xtrad*180/PI, ytrad*180/PI, xt, yt);
			for(y=0, sum=.0, xtrad=xt*piRes; y<land_h; y++)
				for(x=0, yrad=y*piWidth; x<land_w; x++)
					if(land[x+y*land_w]>level) {
						xrad=x*piWidth;
						
						/* Entfernung zwischen den Punkten (yrad, xrad) und (ytrad,xtrad)
						 * über den sphärischen Großkreis. Die Koordinaten sind im Bogenmaß
						 * angegeben: Nullpunkt im Nordpol, Null-Meridian ist bei 180°W.
						 */
						d=acos(cos(yrad)*cos(ytrad)+sin(yrad)*sin(ytrad)*cos(xrad-xtrad));
						
						/* Transformation der Zylinderprojektion
						 * (die verwendeten Daten stammen aus einer Zyl.projektion
						 * mit äquidistanten Längen- und Breitengeraden)
						 */
						sum+=d*sin(yrad);
					}
			result[xt+yt*res]=sum;
			if(sum>.0 && sum<min_sum) {
				min_sum=sum;
				xc=xt;
				yc=yt;
			} else if(sum>max_sum)
				max_sum=sum;
		}
		
		// komplette Zeile berechnet, Zwischenstand speichern
		if(!(outfile=fopen(outputfile, "w"))) {
			fprintf(stderr, "cannot write to %s", outputfile);
			exit(2);
		}
		fprintf(outfile, "P2\n# CREATOR: gravcenter by Holger Isenberg, H.Isenberg@ping.de\n");
		fprintf(outfile, "# CREATOR: http://mars-news.de\n");
		fprintf(outfile, "# CREATOR: geographic center at %.3f N, %.3f E [deg], output %dx, %dy\n",
			90.0-(double)(yc)*360.0/res, (double)(xc)*360.0/res-180.0, xc, yc);
		fprintf(outfile, "%d %d\n255\n", res, res/2);
		for(y=0; y<res/2; y++) {
			for(x=0; x<res; x++)
				fprintf(outfile, "%4d", result[x+y*res]?(unsigned char)((result[x+y*res]-min_sum)/(max_sum-min_sum)*255.0):255);
			fprintf(outfile, "\n");
		}
		fclose(outfile);
	}
	printf("\ngeographic center:\n %dx, %dy [pixel]\n %.3f°N, %.3f°E\n",
	       xc, yc,
	       90.0-(double)(yc)*360.0/res, (double)(xc)*360.0/res-180.0);
	return 0;
}
	

unsigned char *
loadGIF(char* filename, int* width, int* height)
{
  FILE *fd;
  char * name_buf;
  unsigned char buf[16];
  unsigned char c;
  CMap localColorMap;
  int grayScale;
  int useGlobalColormap;
  int bitPixel;
  int imageCount = 0;
  char version[4];
  unsigned char* pixmap;

  fd = fopen (filename, "rb");
  if (!fd) {
	  fprintf(stderr, "can't open \"%s\"\n", filename);
	  return 0;
  }

  name_buf = (char*)malloc (strlen (filename) + 11);

  if (!ReadOK (fd, buf, 6)) {
	  fprintf(stderr, "error reading magic number\n");
	  return 0;
  }

  if (strncmp ((char *) buf, "GIF", 3) != 0) {
	  fprintf(stderr, "not a GIF file\n");
	  return 0;
    }

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

  if ((strcmp (version, "87a") != 0) && (strcmp (version, "89a") != 0)) {
	  fprintf(stderr, "bad version number, not '87a' or '89a'\n");
	  return 0;
  }
  
  if (!ReadOK (fd, buf, 7)) {
	  fprintf(stderr, "failed to read screen descriptor\n");
	  return 0;
  }

  GifScreen.Width = LM_to_uint (buf[0], buf[1]);
  GifScreen.Height = LM_to_uint (buf[2], buf[3]);
  GifScreen.BitPixel = 2 << (buf[4] & 0x07);
  GifScreen.ColorResolution = (((buf[4] & 0x70) >> 3) + 1);
  GifScreen.Background = buf[5];
  GifScreen.AspectRatio = buf[6];

  *width =   GifScreen.Width;
  *height = GifScreen.Height;

  if (BitSet (buf[4], LOCALCOLORMAP)) {
	  /* Global Colormap */
	  if (ReadColorMap (fd, GifScreen.BitPixel, GifScreen.ColorMap, &GifScreen.GrayScale)) {
		  fprintf(stderr, "error reading global colormap\n");
		  return 0;
	  }
  }

  if (GifScreen.AspectRatio != 0 && GifScreen.AspectRatio != 49)
    {
      fprintf(stderr, "GIF: warning - non-square pixels\n");
    }


  highest_used_index = 0;
  pixmap=0;

  for(;;) {
  if (!ReadOK (fd, &c, 1))
  {
	  fprintf(stderr, "GIF: EOF / read error on image data\n");
	  return 0;
  }
  
  if (c == ';')
  {
	  /* GIF terminator */
	  fprintf(stderr, "GIF: got ;\n");
	  return pixmap;
  }

  if (c == '!')
  {
	  /* Extension */
	  if (!ReadOK (fd, &c, 1))
	  {
		  fprintf(stderr, "GIF: OF / read error on extention function code\n");
		  return 0;
	  }
	  DoExtension (fd, c);
	  continue;
  }
  
  if (c != ',')
  {
	  /* Not a valid start character */
	  fprintf(stderr, "GIF: bogus character 0x%02x, ignoring\n", (int) c);
	  continue;
  }
  
  ++imageCount;
  
  if (!ReadOK (fd, buf, 9))
  {
	  fprintf(stderr, "GIF: couldn't read left/top/width/height\n");
	  return 0;
  }
  
  useGlobalColormap = !BitSet (buf[8], LOCALCOLORMAP);
  
  bitPixel = 1 << ((buf[8] & 0x07) + 1);

  pixmap = ReadImage (fd, filename, LM_to_uint (buf[4], buf[5]),
		      LM_to_uint (buf[6], buf[7]),
		      GifScreen.ColorMap, GifScreen.BitPixel,
		      GifScreen.GrayScale,
		      BitSet (buf[8], INTERLACE), imageCount,
		      (unsigned int) LM_to_uint (buf[0], buf[1]),
		      (unsigned int) LM_to_uint (buf[2], buf[3]),
		      GifScreen.Width,
		      GifScreen.Height
		      );
  return pixmap;
  }
  return pixmap;
}

int
ReadColorMap (FILE *fd,
	      int   number,
	      CMap  buffer,
	      int  *format)
{
  int i;
  unsigned char rgb[3];
  int flag;

  flag = TRUE;

  for (i = 0; i < number; ++i)
    {
      if (!ReadOK (fd, rgb, sizeof (rgb)))
	{
	  fprintf(stderr, "GIF: bad colormap\n");
	  return TRUE;
	}

      buffer[CM_RED][i] = rgb[0];
      buffer[CM_GREEN][i] = rgb[1];
      buffer[CM_BLUE][i] = rgb[2];

      flag &= (rgb[0] == rgb[1] && rgb[1] == rgb[2]);
    }

  *format = (flag) ? GRAYSCALE : COLOR;

  return FALSE;
}

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

  if (!ReadOK (fd, &count, 1))
    {
      fprintf(stderr, "GIF: error in getting DataBlock size\n");
      return -1;
    }

  ZeroDataBlock = count == 0;

  if ((count != 0) && (!ReadOK (fd, buf, count)))
    {
      fprintf(stderr, "GIF: error in reading DataBlock\n");
      return -1;
    }

  return count;
}

static int
GetCode (FILE *fd,
	 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)
	    {
	      fprintf(stderr, "GIF: ran off the end of by bits\n");
	      exit(2);
	    }
	  return -1;
	}
      buf[0] = buf[last_byte - 2];
      buf[1] = buf[last_byte - 1];

      if ((count = GetDataBlock (fd, &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;
}

static 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)
	    fprintf (stderr, "GIF: 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])
	    {
	      fprintf(stderr, "GIF: circular table entry BIG ERROR\n");
	      exit(2);
	    }
	  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;
}

unsigned char *
ReadImage (FILE *fd,
	   char *filename,
	   int   len,
	   int   height,
	   CMap  cmap,
	   int   ncols,
	   int   format,
	   int   interlace,
	   int   number,
	   unsigned int   leftpos,
	   unsigned int   toppos,
	   unsigned int screenwidth,
	   unsigned int screenheight)
{
  static int frame_number = 1;

  unsigned char *dest, *temp;
  unsigned char c;
  int xpos = 0, ypos = 0, pass = 0;
  int cur_progress, max_progress;
  int v;
  int i, j;
  char framename[200]; /* FIXME */
  char alpha_frame = FALSE;
  int nreturn_vals;
  static int previous_disposal;

  /*
   **  Initialize the Compression routines
   */
  if (!ReadOK (fd, &c, 1))
    {
      fprintf(stderr, "GIF: EOF / read error on image data\n");
      return 0;
    }

  if (LWZReadByte (fd, TRUE, c) < 0)
    {
      fprintf(stderr, "GIF: error while reading\n");
      return 0;
    }

  for (i = 0, j = 0; i < ncols; i++)
	{
		used_cmap[0][i] = gimp_cmap[j++] = cmap[0][i];
		used_cmap[1][i] = gimp_cmap[j++] = cmap[1][i];
		used_cmap[2][i] = gimp_cmap[j++] = cmap[2][i];
	}
  
  if (Gif89.delayTime < 0)
	  strcpy(framename, "Background");
  else
	  sprintf(framename, "Background (%dms)", 10*Gif89.delayTime);
  
  previous_disposal = Gif89.disposal;
  
  if (Gif89.delayTime < 0)
	  sprintf(framename, "Frame %d", frame_number);
  else
	  sprintf(framename, "Frame %d (%dms)",
		  frame_number, 10*Gif89.delayTime);
  
  switch (previous_disposal)
  {
      case 0x00: break; /* 'don't care' */
      case 0x01: strcat(framename," (combine)"); break;
      case 0x02: strcat(framename," (replace)"); break;
      case 0x03: strcat(framename," (combine)"); break;
      case 0x04:
      case 0x05:
      case 0x06:
      case 0x07:
	      strcat(framename," (unknown disposal)");
	      fprintf(stderr, "GIF: Hmm... please forward this GIF to the "
		      "GIF plugin author!\n  (adam@foxbox.org)\n");
	      break;
      default: fprintf(stderr, "GIF: Something got corrupted.\n"); break;
  }
  
  previous_disposal = Gif89.disposal;
  
  dest = (unsigned char *) malloc (len * height);
  if(!dest) {
	  fprintf(stderr, "cannot allocate %d bytes\n", len*height);
	  return 0;
  }
  fprintf (stderr, "GIF: reading %d by %d%s GIF image, ncols=%d\n",
	   len, height, interlace ? " interlaced" : "", ncols);

  while ((v = LWZReadByte (fd, FALSE, c)) >= 0) {
	  if (((unsigned char)v > highest_used_index) && !(v == Gif89.transparent))
		  highest_used_index = (unsigned char)v;
	  
	  temp = dest + (ypos * len) + xpos;
	  *(temp  ) = (unsigned char) cmap[0][v];
	  ++xpos;
	  if (xpos == len)
	  {
		  xpos = 0;
		  if (interlace)
		  {
			  switch (pass)
			  {
			      case 0:
			      case 1:
				      ypos += 8;
				      break;
			      case 2:
				      ypos += 4;
				      break;
			      case 3:
				      ypos += 2;
				      break;
			  }

			  if (ypos >= height)
			  {
				  ++pass;
				  switch (pass)
				  {
				      case 1:
					      ypos = 4;
					      break;
				      case 2:
					      ypos = 2;
					      break;
				      case 3:
					      ypos = 1;
					      break;
				      default:
					      goto fini;
				  }
			  }
		  }
		  else
		  {
			  ++ypos;
		  }
	  }
	  if (ypos >= height)
		  break;
  }

  fini:
  if (LWZReadByte (fd, FALSE, c) >= 0)
	  fprintf(stderr, "GIF: too much input data, ignoring extra...\n");

  return dest;
}

static int
DoExtension (FILE *fd,
	     int   label)
{
  static unsigned char buf[256];
  char *str;

  switch (label)
    {
    case 0x01:			/* Plain Text Extension */
      str = "Plain Text Extension";
#ifdef notdef
      if (GetDataBlock (fd, (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 (fd, (unsigned char *) buf) != 0)
	{
	  PPM_ASSIGN (image[ypos][xpos],
		      cmap[CM_RED][v],
		      cmap[CM_GREEN][v],
		      cmap[CM_BLUE][v]);
	  ++index;
	}

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

      while (GetDataBlock (fd, (unsigned char *) buf) != 0)
	;
      return FALSE;
    default:
      str = (char *)buf;
      sprintf ((char *)buf, "UNKNOWN (0x%02x)", label);
      break;
    }

  fprintf(stderr, "GIF: got a '%s' extension\n", str);

  while (GetDataBlock (fd, (unsigned char *) buf) != 0)
    ;

  return FALSE;
}

