Logo Search packages:      
Sourcecode: grads version File versions  Download package

gxgif.c

/***********************************************************
 *
 * GXGIF: a grads metafile to GIF converter.
 * Written by Matthias Muennich 
 *
 *   $Log: gxgif.c,v $
 *   Revision 1.5  2009/04/30 15:25:31  jma
 *   changed max number of colors from 99 to 255
 *
 *   Revision 1.4  2009/01/05 12:48:24  jma
 *   changed
 *   #include <config.h>
 *   to
 *   #include "config.h"
 *
 *   Revision 1.3  2003/06/24 21:31:08  joew
 *   put conditionals around #include malloc.h, for Mac OS X
 *
 *   Revision 1.2  2002/10/28 19:08:33  joew
 *   Preliminary change for 'autonconfiscation' of GrADS: added a conditional
 *   #include "config.h" to each C file. The GNU configure script generates a unique config.h for each platform in place of -D arguments to the compiler.
 *   The include is only done when GNU configure is used.
 *
 *   Revision 1.1.1.1  2002/06/27 19:44:05  cvsadmin
 *   initial GrADS CVS import - release 1.8sl10
 *
 *   Revision 1.1.1.1  2001/10/18 02:00:54  Administrator
 *   Initial repository: v1.8SL8 plus slight MSDOS mods
 *
 *   Revision 0.4  1997/12/19 10:43:56  m211033
 *   Added a version statement in verbose mode.
 *
 *   Revision 0.3  1997/12/04 10:08:26  m211033
 *   Copied the subroutines used from gd1.2 into gxgif.c
 *   to make the code independent of the gd library.
 *   Added an option "-h" for horizontal filling of polygons
 *   (=> horizontal spurious lines).
 *   Fixed the output file names for more that 1 image.
 *   Fixed the "-r" option.
 *
 *   Revision 0.2  1997/02/25 13:30:35  m211033
 *   Added support for different line thicknesses and
 *   cleaned up the code.
 *
 *   Revision 0.1  1997/02/21 15:20:51  m211033
 *   different line width are not yet supported.
 *   The gdImageFilledPolygon routine gives wrong height
 *   when filling horizontally. This led to flaud horizontal
 *   lines in the GIF file. I rewrote it switching x and y
 *   coordinates. The lines disappeared. Now vertical lines
 *   may show up. For a fix we have to wait for a better
 *   version on gdImageFilledPolygon.
 *   No black and white mode is available right now.
 *
 *
 *
 ***********************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"

/* If autoconfed, only include malloc.h when it's presen */
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif

#else /* undef HAVE_CONFIG_H */

#include <malloc.h>

#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
/*  #include "gd.h" */
static char rcsid[] = "$Id: gxgif.c,v 1.5 2009/04/30 15:25:31 jma Exp $";

/* ---------------------------- begin gd.h -------------------------------------- */
#ifndef GD_H
#define GD_H 1

/* gd.h: declarations file for the gifdraw module.

      Written by Tom Boutell, 5/94.
      Copyright 1994, Cold Spring Harbor Labs.
      Permission granted to use this code in any fashion provided
      that this notice is retained and any alterations are
      labeled as such. It is requested, but not required, that
      you share extensions to this module with us so that we
      can incorporate them into new versions. */

/* stdio is needed for file I/O. */
#include <stdio.h>

/* This can't be changed, it's part of the GIF specification. */

#define gdMaxColors 256

/* Image type. See functions below; you will not need to change
      the elements directly. Use the provided macros to
      access sx, sy, the color table, and colorsTotal for 
      read-only purposes. */

typedef struct gdImageStruct {
      unsigned char ** pixels;
      int sx;
      int sy;
      int colorsTotal;
      int red[gdMaxColors];
      int green[gdMaxColors];
      int blue[gdMaxColors]; 
      int open[gdMaxColors];
      int transparent;
      int *polyInts;
      int polyAllocated;
      struct gdImageStruct *brush;
      struct gdImageStruct *tile;   
      int brushColorMap[gdMaxColors];
      int tileColorMap[gdMaxColors];
      int styleLength;
      int stylePos;
      int *style;
      int interlace;
} gdImage;

typedef gdImage * gdImagePtr;

typedef struct {
      /* # of characters in font */
      int nchars;
      /* First character is numbered... (usually 32 = space) */
      int offset;
      /* Character width and height */
      int w;
      int h;
      /* Font data; array of characters, one row after another.
            Easily included in code, also easily loaded from
            data files. */
      char *data;
} gdFont;

/* Text functions take these. */
typedef gdFont *gdFontPtr;

/* For backwards compatibility only. Use gdImageSetStyle()
      for MUCH more flexible line drawing. Also see
      gdImageSetBrush(). */
#define gdDashSize 4

/* Special colors. */

#define gdStyled (-2)
#define gdBrushed (-3)
#define gdStyledBrushed (-4)
#define gdTiled (-5)

/* NOT the same as the transparent color index.
      This is used in line styles only. */
#define gdTransparent (-6)

/* Functions to manipulate images. */

gdImagePtr gdImageCreate(int sx, int sy);
gdImagePtr gdImageCreateFromGif(FILE *fd);
gdImagePtr gdImageCreateFromGd(FILE *in);
gdImagePtr gdImageCreateFromXbm(FILE *fd);
void gdImageDestroy(gdImagePtr im);
void gdImageSetPixel(gdImagePtr im, int x, int y, int color);
int gdImageGetPixel(gdImagePtr im, int x, int y);
void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
/* For backwards compatibility only. Use gdImageSetStyle()
      for much more flexible line drawing. */
void gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
/* Corners specified (not width and height). Upper left first, lower right
      second. */
void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
/* Solid bar. Upper left corner first, lower right corner second. */
void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
int gdImageBoundsSafe(gdImagePtr im, int x, int y);
void gdImageChar(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color);
void gdImageCharUp(gdImagePtr im, gdFontPtr f, int x, int y, char c, int color);
void gdImageString(gdImagePtr im, gdFontPtr f, int x, int y, char *s, int color);
void gdImageStringUp(gdImagePtr im, gdFontPtr f, int x, int y, char *s, int color);

/* Point type for use in polygon drawing. */

typedef struct {
      int x, y;
} gdPoint, *gdPointPtr;

void gdImagePolygon(gdImagePtr im, gdPointPtr p, int n, int c);
void gdImageFilledPolygon(gdImagePtr im, gdPointPtr p, int n, int c);

int gdImageColorAllocate(gdImagePtr im, int r, int g, int b);
int gdImageColorClosest(gdImagePtr im, int r, int g, int b);
int gdImageColorExact(gdImagePtr im, int r, int g, int b);
void gdImageColorDeallocate(gdImagePtr im, int color);
void gdImageColorTransparent(gdImagePtr im, int color);
void gdImageGif(gdImagePtr im, FILE *out);
void gdImageGd(gdImagePtr im, FILE *out);
void gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color);
void gdImageFillToBorder(gdImagePtr im, int x, int y, int border, int color);
void gdImageFill(gdImagePtr im, int x, int y, int color);
void gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h);
/* Stretches or shrinks to fit, as needed */
void gdImageCopyResized(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);
void gdImageSetBrush(gdImagePtr im, gdImagePtr brush);
void gdImageSetTile(gdImagePtr im, gdImagePtr tile);
void gdImageSetStyle(gdImagePtr im, int *style, int noOfPixels);
/* On or off (1 or 0) */
void gdImageInterlace(gdImagePtr im, int interlaceArg);

/* Macros to access information about images. READ ONLY. Changing
      these values will NOT have the desired result. */
#define gdImageSX(im) ((im)->sx)
#define gdImageSY(im) ((im)->sy)
#define gdImageColorsTotal(im) ((im)->colorsTotal)
#define gdImageRed(im, c) ((im)->red[(c)])
#define gdImageGreen(im, c) ((im)->green[(c)])
#define gdImageBlue(im, c) ((im)->blue[(c)])
#define gdImageGetTransparent(im) ((im)->transparent)
#define gdImageGetInterlaced(im) ((im)->interlace)
#endif

/* ---------------------------- end gd.h -------------------------------------- */



/* default file extensions */
#define IN_EXT "gm"
#define OUT_EXT "gif"

/* default size of the graph */
#define SX 550
#define SY 425

/* PNMAX: maximal points in polygons */
#define PNMAX 4096
#ifdef __GNUC__
#define fpos_t long
#endif 

/* ---------------------------- global vars -------------------------------------- */

struct options {int reverse, fillx, verbose, sx, sy;}; /* command line options (flags) */
FILE *infile, *outfile;
gdImagePtr im,w[12];
int cidx[13][100];  /* color index table [0][*]: filling, [1-12][*] brush images */
int r[100]={0, 255, 240,   0,  31,   0, 220, 230, 240, 161, 161,   0, 230,   0, 110, 125},
    g[100]={0, 255,   0, 220,  61, 199,   0, 220, 130,   0, 230, 161, 176, 209,   0, 125},
    b[100]={0, 255,   0,   0, 250, 199,  99,  51,  41, 199,  51, 230,  46, 140, 220, 125};

/* ---------------------------- prototypes --------------------------------------- */

void printOptions(char *argv[]); /* print command line options */
void drawLine(gdPoint pnts[],short *pcnt,int wd,int color); /* draw a polygon */
void parseArg(int argc,char *argv[],struct options *o,char **fin, char **fout); /* parse command line */
void openFiles(char **fin, char **fout, short verbose); /* Open files */
void defBGround(gdImagePtr im,short reverse); /* set background */

/* changed gd-library routines */
void gdImagePolygonUnclosed(gdImagePtr im, gdPointPtr p, int n, int c);
void gdImageFilledPolygonx(gdImagePtr im, gdPointPtr p, int n, int c);
extern int gdCompareInt(const void *a, const void *b);

/* --------------------------------------------------------------------------------- */
int main (int argc, char *argv[])  {
   short cmd,opts[4], rotate=0;
   int col=0, wd=0;
   int sx=SX,sy=SY,sh,lly,ury;
   short coldef[100];
   float blowx,blowy;
   gdPoint pnts[PNMAX];                    /* points of a polygon */
   struct options o;                      /* command line options */
   register int  i,j;
   short pcnt,fcnt;
   char *fin=NULL,*fout=NULL,*fout_new=NULL;              /* file names */
   fpos_t infile_pos;
   short width[12] = {                      /* width (units: .001 inch) */
               2 ,7 ,10 ,14 ,17 ,20 ,
             24 ,27 ,31 ,34 ,38 ,41 };

   o.reverse=o.verbose=o.fillx=o.sx=o.sy=0;
   for(i=0;i<13;i++)
     for(j=0;j<100;j++) cidx[i][j]=-1;
   for(i=0;i<16;i++) coldef[i]=1;
   for(i=16;i<100;i++) coldef[i]=0;

  parseArg(argc,argv,&o,&fin,&fout);     /* Parse command line arguments */
  openFiles(&fin,&fout,o.verbose);                  /* open files */

  /* Translate metafile */

  fcnt = 1;
  while (1) {
    fread (&cmd, sizeof(short), 1, infile);

    if (cmd==-11){                        /* Draw to */
      fread (opts, sizeof(short), 2, infile);
      pnts[++pcnt].x=opts[0]*blowx;
      pnts[pcnt].y= sy-opts[1]*blowy;
    }
    else if (cmd==-10){                   /* Move to */
      if (pcnt) drawLine(pnts,&pcnt,wd,col);
      fread ((char *) opts, sizeof(short), 2, infile);
      pnts[pcnt].x=opts[0]*blowx;
      pnts[pcnt].y= sy-opts[1]*blowy;
    }
    else if (cmd==-4) {                   /* Set line width */
      if (pcnt) drawLine(pnts,&pcnt,wd,col);
      fread ((char *)opts, sizeof(short), 2, infile);
      i = opts[0];
      if (i>12) i=12;
      else if (i<1) i=1;
      wd=width[i-1]*blowx+1;
    }
    else if (cmd==-3) {                   /* Set color */
      if (pcnt) drawLine(pnts,&pcnt,wd,col);
      fread ((char *)opts, sizeof(short), 1, infile);
      col = opts[0];
      if (col<0) col=0;
      if (col>255) col=255; 
      if (!(coldef[i])) col=15;
      if(o.reverse){
        if(col==1) col=0;
        else if(col==0) col=1;
      }
    }
    else if (cmd==-7){                    /* Start fill */
      fread ((char *)opts, sizeof(short), 1, infile);
    }
    else if (cmd==-8){                    /* End fill */
      if (pcnt>1)
        if(cidx[0][col]<0){
        cidx[0][col]=gdImageColorAllocate(im, r[col],g[col],b[col]);
      }
        if(o.fillx) gdImageFilledPolygon(im, pnts, ++pcnt, cidx[0][col]);
        else gdImageFilledPolygonx(im, pnts, ++pcnt, cidx[0][col]);
        pcnt=0;
    }
    else if (cmd==-6){                    /* Rectangle fill */
      if (pcnt) drawLine(pnts,&pcnt,wd,col);
      fread ((char *)opts, sizeof(short), 4, infile);
      lly=sy-opts[2]*blowy;
      ury=sy-opts[3]*blowy;
        if(cidx[0][col]<0)
        cidx[0][col]=gdImageColorAllocate(im, r[col],g[col],b[col]);
      gdImageFilledRectangle(im, (int) opts[0]*blowx,ury,
                       (int) opts[1]*blowx,lly, cidx[0][col]);
    }
    else if (cmd==-9) {                   /* End of plotting */
      if(o.verbose) printf ("Number of pages = %i\n",fcnt);
      /*       gdImageInterlace(im, 1); */
      gdImageGif(im, outfile);
      fclose(outfile);
      gdImageDestroy(im);
      return(0);
    }
    else if (cmd==-1) {                   /* Start of plotting */
      fread ((char *)opts, sizeof(short), 2, infile);
      if(opts[0]<opts[1]) {sh=sx; sx=sy; sy=sh;}
      if(o.sx) {
          sx=o.sx;
         if(o.sy==0) sy=sx *(opts[1]/100)/(opts[0]/100);
      }
      if(o.sy) {
          sy=o.sy;
          if(o.sx==0) sx=sy*(opts[0]/100)/(opts[1]/100);
      }
      blowx=(float)sx/(float)opts[0];  /* picture size in metafile: opts[0] x opts[1] */ 
      blowy=(float)sy/(float)opts[1];
      if(o.verbose) printf("Image size: %d x %d pixels\n",sx,sy);

      for (i=0;i<=width[11]*blowx;i++)
        w[i] = gdImageCreate(i+1,i+1);
      im = gdImageCreate(sx,sy);
      defBGround(im,o.reverse);
      col=1;
      pcnt = 0;
    }
    else if (cmd==-2) {                   /* New Page */
      if (pcnt) drawLine(pnts,&pcnt,wd,col);
      /* gdImageInterlace(im, 1); */
      gdImageGif(im, outfile);
      fclose(outfile);
      gdImageDestroy(im);
      for (i=0;i<=width[11]*blowx;i++)
        gdImageDestroy(w[i]);
      fgetpos(infile,&infile_pos);
      fread (&cmd , sizeof(short), 1, infile); 
      fsetpos(infile,&infile_pos);
      if (cmd != -9) {  /* if next command is not end_of_plotting */
        if(fout_new==NULL){
             fout[strlen(fout)-4]='\0';
             fout_new = (char *) malloc(sizeof(char)*150);
        }
          strcpy(fout_new,fout);
        sprintf(fout_new+strlen(fout_new), "_%d",++fcnt);
          strcpy((fout_new)+strlen(fout_new),"."OUT_EXT);
          if(o.verbose) printf ("New GIF-file:%s\n",fout_new);
          outfile = fopen(fout_new,"wb");
#ifdef NOTDEF
        strcpy(fout+strlen(fout),"1");
          outfile = fopen(fout,"wb");
#endif
          for (i=0;i<=width[11]*blowx;i++)
            w[i] = gdImageCreate(i+1,i+1);
          im = gdImageCreate(sx,sy);
          defBGround(im,o.reverse);
          col=1;
          pcnt = 0;
      }
      else return(0);
    }
    else if (cmd==-5){                    /* Define new color */
      fread ((char *)opts, sizeof(short), 4, infile);
      i = opts[0];
      if (i>15 && i<100) {
        r[i]=opts[1];
        g[i]=opts[2];
        b[i]=opts[3];
        coldef[i]=1;
      }
    }
    else if (cmd==-20) {            /* Draw button -- ignore */
      fread ((char *)opts, sizeof(short), 1, infile);
    }
    else {
       printf ("Fatal error: Invalid command \"%i\" found in metafile\"%s\".\n",cmd,fin);
       printf ("Is \"%s\" really a GrADS (v1.5 or higher) metafile?\n",fin);
      return(1);
    }
  }
}
/* --------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------- */

void defBGround(gdImagePtr im, short reverse){
    if(reverse) 
         cidx[0][1]=gdImageColorAllocate(im, 255, 255, 255); /* white background */
    else
         cidx[0][0]=gdImageColorAllocate(im, 0, 0, 0);       /* black background */
    return;
}

/* --------------------------------------------------------------------------------- */

void drawLine(gdPoint pnts[],short *pcnt,int wd, int col){
    if(cidx[wd][col]<0)
      cidx[wd][col]=gdImageColorAllocate(w[wd-1],r[col],g[col],b[col]);
    gdImageFilledRectangle(w[wd-1],0,0,wd,wd, cidx[wd][col]);
    gdImageSetBrush(im, w[wd-1]);
    if(*pcnt>1) gdImagePolygonUnclosed(im, pnts, (*pcnt)+1, gdBrushed);
    else gdImageLine(im, pnts[0].x, pnts[0].y,pnts[1].x,pnts[1].y,gdBrushed);
   *pcnt=0;
   return;
}
/* --------------------------------------------------------------------------------- */
void parseArg(int argc,char *argv[],struct options *o,char **fin, char **fout){
     register int i,j;
     if(argc==1) printOptions(argv);
     for (i=1;i<argc;i++) {
       if (*(argv[i])=='-') {  /* parse options */
         j = 0;
         while (*(argv[i]+(++j))) {
           if (*(argv[i]+j)=='i') {*fin = argv[++i];break;}
           else if (*(argv[i]+j)=='h') o->fillx = 1;
           else if (*(argv[i]+j)=='o') {*fout = argv[++i];break;}
           else if (*(argv[i]+j)=='r') o->reverse = 1;
           else if (*(argv[i]+j)=='x') {
           sscanf(argv[++i],"%d",&(o->sx));break;
         }
           else if (*(argv[i]+j)=='y') {
           sscanf(argv[++i],"%d",&(o->sy));break;
         }
           else if (*(argv[i]+j)=='v') {
             o->verbose = 1;
             printf("This is gxgif $Revision: 1.5 $, $Date: 2009/04/30 15:25:31 $\n");
         }
           else { 
           fprintf(stderr,"Unknown option: %s\n\n",argv[i]);
           printOptions(argv);
           exit(1);
         }
         }
       }
      else  /* No command line "-" */
        *fin=argv[i];
    }
    return;
}
/* --------------------------------------------------------------------------------- */

void printOptions(char *argv[]){
       fprintf(stderr,"%s%s%s","Usage: ",argv[0],
         " [-hrv -x <pixels> -y <pixels> -i <in_file>[."  IN_EXT
           "] -o <out_file>] [<in_file>[."IN_EXT"]].\n");
       fprintf(stderr,"Options:\n");
       fprintf(stderr,"     -i   <in_file>[."IN_EXT"].\n");;
       fprintf(stderr,"     -h   Fill polygons horizontally.\n");
       fprintf(stderr,"     -o   <out_file> (default: basename(in_file)."OUT_EXT", '-' = stdout).\n");
       fprintf(stderr,"     -r   Black background.\n");
       fprintf(stderr,"     -v   Verbose.\n");
       fprintf(stderr,"     -x <pixels>  # pixels horizontally.\n");
       fprintf(stderr,"     -y <pixels>  # pixels vertically.\n");
     exit(8);
     }
/* --------------------------------------------------------------------------------- */

void openFiles(char **fin, char **fout,short verbose){ /* Open files */
  int i;

  if (*fin==NULL) {
    *fin = (char *) malloc(sizeof(char)*150);
     fgets(*fin,150,stdin);
     printf("read infile = %s\n",*fin);
  }
  infile = fopen(*fin ,"rb");
  if (infile == NULL) {
    *fin=strcat(*fin,"."IN_EXT);
    infile = fopen(*fin,"rb");
    if (infile == NULL) {
      (*fin)[strlen(*fin)-3]='\0';
      printf ("Input file %s[."IN_EXT"] not found.\n",*fin);
      exit(1);
    }
  }
  if (*fout==NULL) {
    *fout = (char *) malloc(sizeof(char)*150);
    strcpy(*fout,*fin);
    for (i=strlen(*fout)-1;i>=0;i--) {
      if((*fout)[i]=='.') {strcpy((*fout)+i+1,OUT_EXT);
      break;
      }
      if(i==0){strcpy((*fout)+strlen(*fout),"."OUT_EXT);}
    }
  }
  if(strcmp(*fout,"-")==0) outfile=stdout;
  else outfile = fopen(*fout,"wb");
  if (outfile==NULL) {
    printf ("Error opening output file %s \n",*fout);
    exit(1);
  }
  if(verbose) {
    printf("GrADS metafile: %s\n",*fin);
    if(strcmp(*fout,"-")==0) printf("output to stdout\n");
    else printf("GIF-file: %s\n",*fout);
  }
  return;
}
/* --------------------------------------------------------------------------------- */
void gdImagePolygonUnclosed(gdImagePtr im, gdPointPtr p, int n, int c)
{
        int i;
        int lx, ly;
        if (!n) {
                return;
        }
        lx = p->x;
        ly = p->y;
        /* gdImageLine(im, lx, ly, p[n-1].x, p[n-1].y, c); */
        for (i=1; (i < n); i++) {
                p++;
                gdImageLine(im, lx, ly, p->x, p->y, c);
                lx = p->x;
                ly = p->y;
        }
}       

/* --------------------------------------------------------------------------------- */
      
void gdImageFilledPolygonx(gdImagePtr im, gdPointPtr p, int n, int c)
{
      int i;
      int x;
      int x1, x2;
      int ints;
      if (!n) {
            return;
      }
      if (!im->polyAllocated) {
            im->polyInts = (int *) malloc(sizeof(int) * n);
            im->polyAllocated = n;
      }           
      if (im->polyAllocated < n) {
            while (im->polyAllocated < n) {
                  im->polyAllocated *= 2;
            }     
            im->polyInts = (int *) realloc(im->polyInts,
                  sizeof(int) * im->polyAllocated);
      }
      x1 = p[0].x;
      x2 = p[0].x;
      for (i=1; (i < n); i++) {
            if (p[i].x < x1) {
                  x1 = p[i].x;
            }
            if (p[i].x > x2) {
                  x2 = p[i].x;
            }
      }
      for (x=x1; (x <= x2); x++) {
            int interLast = 0;
            int dirLast = 0;
            int interFirst = 1;
            ints = 0;
            for (i=0; (i <= n); i++) {
                  int y1, y2;
                  int x1, x2;
                  int dir;
                  int ind1, ind2;
                  int lastInd1 = 0;
                  if ((i == n) || (!i)) {
                        ind1 = n-1;
                        ind2 = 0;
                  } else {
                        ind1 = i-1;
                        ind2 = i;
                  }
                  x1 = p[ind1].x;
                  x2 = p[ind2].x;
                  if (x1 < x2) {
                        x1 = p[ind1].x;
                        x2 = p[ind2].x;
                        y1 = p[ind1].y;
                        y2 = p[ind2].y;
                        dir = -1;
                  } else if (x1 > x2) {
                        x2 = p[ind1].x;
                        x1 = p[ind2].x;
                        y2 = p[ind1].y;
                        y1 = p[ind2].y;
                        dir = 1;
                  } else {
                        /* Horizontal; just draw it */
                        gdImageLine(im, 
                               x1, p[ind1].y, 
                              x1, p[ind2].y, 
                              c);
                        continue;
                  }
                  if ((x >= x1) && (x <= x2)) {
                        int inter = 
                              (x-x1) * (y2-y1) / (x2-x1) + y1;
                        /* Only count intersections once
                              except at maxima and minima. Also, 
                              if two consecutive intersections are
                              endpoints of the same horizontal line
                              that is not at a maxima or minima,  
                              discard the leftmost of the two. */
                        if (!interFirst) {
                              if ((p[ind1].x == p[lastInd1].x) &&
                                    (p[ind1].y != p[lastInd1].y)) {
                                    if (dir == dirLast) {
                                          if (inter > interLast) {
                                                /* Replace the old one */
                                                im->polyInts[ints] = inter;
                                          } else {
                                                /* Discard this one */
                                          }     
                                          continue;
                                    }
                              }
                              if (inter == interLast) {
                                    if (dir == dirLast) {
                                          continue;
                                    }
                              }
                        } 
                        if (i > 0) {
                              im->polyInts[ints++] = inter;
                        }
                        lastInd1 = i;
                        dirLast = dir;
                        interLast = inter;
                        interFirst = 0;
                  }
            }
            qsort(im->polyInts, ints, sizeof(int), gdCompareInt);
            for (i=0; (i < (ints-1)); i+=2) {
                  gdImageLine(im, x, im->polyInts[i],
                         x, im->polyInts[i+1],  c);
            }
      }
}
/* ---------------------------- begin gd.c -------------------------------------- */
static void gdImageBrushApply(gdImagePtr im, int x, int y);
static void gdImageTileApply(gdImagePtr im, int x, int y);

gdImagePtr gdImageCreate(int sx, int sy)
{
      int i;
      gdImagePtr im;
      im = (gdImage *) malloc(sizeof(gdImage));
      im->pixels = (unsigned char **) malloc(sizeof(unsigned char *) * sx);
      im->polyInts = 0;
      im->polyAllocated = 0;
      im->brush = 0;
      im->tile = 0;
      im->style = 0;
      for (i=0; (i<sx); i++) {
            im->pixels[i] = (unsigned char *) calloc(
                  sy, sizeof(unsigned char));
      }     
      im->sx = sx;
      im->sy = sy;
      im->colorsTotal = 0;
      im->transparent = (-1);
      im->interlace = 0;
      return im;
}

void gdImageDestroy(gdImagePtr im)
{
      int i;
      for (i=0; (i<im->sx); i++) {
            free(im->pixels[i]);
      }     
      free(im->pixels);
      if (im->polyInts) {
                  free(im->polyInts);
      }
      if (im->style) {
            free(im->style);
      }
      free(im);
}

int gdImageColorClosest(gdImagePtr im, int r, int g, int b)
{
      int i;
      long rd, gd, bd;
      int ct = (-1);
      long mindist = 0;
      for (i=0; (i<(im->colorsTotal)); i++) {
            long dist;
            if (im->open[i]) {
                  continue;
            }
            rd = (im->red[i] - r);  
            gd = (im->green[i] - g);
            bd = (im->blue[i] - b);
            dist = rd * rd + gd * gd + bd * bd;
            if ((i == 0) || (dist < mindist)) {
                  mindist = dist;   
                  ct = i;
            }
      }
      return ct;
}

int gdImageColorExact(gdImagePtr im, int r, int g, int b)
{
      int i;
      for (i=0; (i<(im->colorsTotal)); i++) {
            if (im->open[i]) {
                  continue;
            }
            if ((im->red[i] == r) && 
                  (im->green[i] == g) &&
                  (im->blue[i] == b)) {
                  return i;
            }
      }
      return -1;
}

int gdImageColorAllocate(gdImagePtr im, int r, int g, int b)
{
      int i;
      int ct = (-1);
      for (i=0; (i<(im->colorsTotal)); i++) {
            if (im->open[i]) {
                  ct = i;
                  break;
            }
      }     
      if (ct == (-1)) {
            ct = im->colorsTotal;
            if (ct == gdMaxColors) {
                  return -1;
            }
            im->colorsTotal++;
      }
      im->red[ct] = r;
      im->green[ct] = g;
      im->blue[ct] = b;
      im->open[ct] = 0;
      return ct;
}

void gdImageColorDeallocate(gdImagePtr im, int color)
{
      /* Mark it open. */
      im->open[color] = 1;
}

void gdImageColorTransparent(gdImagePtr im, int color)
{
      im->transparent = color;
}

void gdImageSetPixel(gdImagePtr im, int x, int y, int color)
{
      int p;
      switch(color) {
            case gdStyled:
            if (!im->style) {
                  /* Refuse to draw if no style is set. */
                  return;
            } else {
                  p = im->style[im->stylePos++];
            }
            if (p != (gdTransparent)) {
                  gdImageSetPixel(im, x, y, p);
            }
            im->stylePos = im->stylePos %  im->styleLength;
            break;
            case gdStyledBrushed:
            if (!im->style) {
                  /* Refuse to draw if no style is set. */
                  return;
            }
            p = im->style[im->stylePos++];
            if ((p != gdTransparent) && (p != 0)) {
                  gdImageSetPixel(im, x, y, gdBrushed);
            }
            im->stylePos = im->stylePos %  im->styleLength;
            break;
            case gdBrushed:
            gdImageBrushApply(im, x, y);
            break;
            case gdTiled:
            gdImageTileApply(im, x, y);
            break;
            default:
            if (gdImageBoundsSafe(im, x, y)) {
                   im->pixels[x][y] = color;
            }
            break;
      }
}

static void gdImageBrushApply(gdImagePtr im, int x, int y)
{
      int lx, ly;
      int hy;
      int hx;
      int x1, y1, x2, y2;
      int srcx, srcy;
      if (!im->brush) {
            return;
      }
      hy = gdImageSY(im->brush)/2;
      y1 = y - hy;
      y2 = y1 + gdImageSY(im->brush);     
      hx = gdImageSX(im->brush)/2;
      x1 = x - hx;
      x2 = x1 + gdImageSX(im->brush);
      srcy = 0;
      for (ly = y1; (ly < y2); ly++) {
            srcx = 0;
            for (lx = x1; (lx < x2); lx++) {
                  int p;
                  p = gdImageGetPixel(im->brush, srcx, srcy);
                  /* Allow for non-square brushes! */
                  if (p != gdImageGetTransparent(im->brush)) {
                        gdImageSetPixel(im, lx, ly,
                              im->brushColorMap[p]);
                  }
                  srcx++;
            }
            srcy++;
      }     
}           

static void gdImageTileApply(gdImagePtr im, int x, int y)
{
      int srcx, srcy;
      int p;
      if (!im->tile) {
            return;
      }
      srcx = x % gdImageSX(im->tile);
      srcy = y % gdImageSY(im->tile);
      p = gdImageGetPixel(im->tile, srcx, srcy);
      /* Allow for transparency */
      if (p != gdImageGetTransparent(im->tile)) {
            gdImageSetPixel(im, x, y,
                  im->tileColorMap[p]);
      }
}           

int gdImageGetPixel(gdImagePtr im, int x, int y)
{
      if (gdImageBoundsSafe(im, x, y)) {
            return im->pixels[x][y];
      } else {
            return 0;
      }
}

/* Bresenham as presented in Foley & Van Dam */

void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
{
      int dx, dy, incr1, incr2, d, x, y, xend, yend, xdirflag, ydirflag;
      dx = abs(x2-x1);
      dy = abs(y2-y1);
      if (dy <= dx) {
            d = 2*dy - dx;
            incr1 = 2*dy;
            incr2 = 2 * (dy - dx);
            if (x1 > x2) {
                  x = x2;
                  y = y2;
                  ydirflag = (-1);
                  xend = x1;
            } else {
                  x = x1;
                  y = y1;
                  ydirflag = 1;
                  xend = x2;
            }
            gdImageSetPixel(im, x, y, color);
            if (((y2 - y1) * ydirflag) > 0) {
                  while (x < xend) {
                        x++;
                        if (d <0) {
                              d+=incr1;
                        } else {
                              y++;
                              d+=incr2;
                        }
                        gdImageSetPixel(im, x, y, color);
                  }
            } else {
                  while (x < xend) {
                        x++;
                        if (d <0) {
                              d+=incr1;
                        } else {
                              y--;
                              d+=incr2;
                        }
                        gdImageSetPixel(im, x, y, color);
                  }
            }           
      } else {
            d = 2*dx - dy;
            incr1 = 2*dx;
            incr2 = 2 * (dx - dy);
            if (y1 > y2) {
                  y = y2;
                  x = x2;
                  yend = y1;
                  xdirflag = (-1);
            } else {
                  y = y1;
                  x = x1;
                  yend = y2;
                  xdirflag = 1;
            }
            gdImageSetPixel(im, x, y, color);
            if (((x2 - x1) * xdirflag) > 0) {
                  while (y < yend) {
                        y++;
                        if (d <0) {
                              d+=incr1;
                        } else {
                              x++;
                              d+=incr2;
                        }
                        gdImageSetPixel(im, x, y, color);
                  }
            } else {
                  while (y < yend) {
                        y++;
                        if (d <0) {
                              d+=incr1;
                        } else {
                              x--;
                              d+=incr2;
                        }
                        gdImageSetPixel(im, x, y, color);
                  }
            }
      }
}


int gdImageBoundsSafe(gdImagePtr im, int x, int y)
{
      return (!(((y < 0) || (y >= im->sy)) ||
            ((x < 0) || (x >= im->sx))));
}


/* Code drawn from ppmtogif.c, from the pbmplus package
**
** Based on GIFENCOD by David Rowley <mgardi@watdscu.waterloo.edu>. A
** Lempel-Zim compression based on "compress".
**
** Modified by Marcel Wijkstra <wijkstra@fwi.uva.nl>
**
** Copyright (C) 1989 by Jef Poskanzer.
**
** 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.
**
** The Graphics Interchange Format(c) is the Copyright property of
** CompuServe Incorporated.  GIF(sm) is a Service Mark property of
** CompuServe Incorporated.
*/

/*
 * a code_int must be able to hold 2**GIFBITS values of type int, and also -1
 */
typedef int             code_int;

#ifdef SIGNED_COMPARE_SLOW
typedef unsigned long int count_int;
typedef unsigned short int count_short;
#else /*SIGNED_COMPARE_SLOW*/
typedef long int          count_int;
#endif /*SIGNED_COMPARE_SLOW*/

static int colorstobpp(int colors);
static void BumpPixel (void);
static int GIFNextPixel (gdImagePtr im);
static void GIFEncode (FILE *fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im);
static void Putword (int w, FILE *fp);
static void compress (int init_bits, FILE *outfile, gdImagePtr im);
static void output (code_int code);
static void cl_block (void);
static void cl_hash (register count_int hsize);
static void char_init (void);
static void char_out (int c);
static void flush_char (void);
/* Allows for reuse */
static void init_statics(void);

void gdImageGif(gdImagePtr im, FILE *out)
{
      int interlace, transparent, BitsPerPixel;
      interlace = im->interlace;
      transparent = im->transparent;

      BitsPerPixel = colorstobpp(im->colorsTotal);
      /* Clear any old values in statics strewn through the GIF code */
      init_statics();
      /* All set, let's do it. */
      GIFEncode(
            out, im->sx, im->sy, interlace, 0, transparent, BitsPerPixel,
            im->red, im->green, im->blue, im);
}

static int
colorstobpp(int colors)
{
    int bpp = 0;

    if ( colors <= 2 )
        bpp = 1;
    else if ( colors <= 4 )
        bpp = 2;
    else if ( colors <= 8 )
        bpp = 3;
    else if ( colors <= 16 )
        bpp = 4;
    else if ( colors <= 32 )
        bpp = 5;
    else if ( colors <= 64 )
        bpp = 6;
    else if ( colors <= 128 )
        bpp = 7;
    else if ( colors <= 256 )
        bpp = 8;
    return bpp;
    }

/*****************************************************************************
 *
 * GIFENCODE.C    - GIF Image compression interface
 *
 * GIFEncode( FName, GHeight, GWidth, GInterlace, Background, Transparent,
 *            BitsPerPixel, Red, Green, Blue, gdImagePtr )
 *
 *****************************************************************************/

#define TRUE 1
#define FALSE 0

static int Width, Height;
static int curx, cury;
static long CountDown;
static int Pass = 0;
static int Interlace;

/*
 * Bump the 'curx' and 'cury' to point to the next pixel
 */
static void
BumpPixel(void)
{
        /*
         * Bump the current X position
         */
        ++curx;

        /*
         * If we are at the end of a scan line, set curx back to the beginning
         * If we are interlaced, bump the cury to the appropriate spot,
         * otherwise, just increment it.
         */
        if( curx == Width ) {
                curx = 0;

                if( !Interlace )
                        ++cury;
                else {
                     switch( Pass ) {

                       case 0:
                          cury += 8;
                          if( cury >= Height ) {
                                ++Pass;
                                cury = 4;
                          }
                          break;

                       case 1:
                          cury += 8;
                          if( cury >= Height ) {
                                ++Pass;
                                cury = 2;
                          }
                          break;

                       case 2:
                          cury += 4;
                          if( cury >= Height ) {
                             ++Pass;
                             cury = 1;
                          }
                          break;

                       case 3:
                          cury += 2;
                          break;
                        }
                }
        }
}

/*
 * Return the next pixel from the image
 */
static int
GIFNextPixel(gdImagePtr im)
{
        int r;

        if( CountDown == 0 )
                return EOF;

        --CountDown;

        r = gdImageGetPixel(im, curx, cury);

        BumpPixel();

        return r;
}

/* public */

static void
GIFEncode(FILE *fp, int GWidth, int GHeight, int GInterlace, int Background, int Transparent, int BitsPerPixel, int *Red, int *Green, int *Blue, gdImagePtr im)
{
        int B;
        int RWidth, RHeight;
        int LeftOfs, TopOfs;
        int Resolution;
        int ColorMapSize;
        int InitCodeSize;
        int i;

        Interlace = GInterlace;

        ColorMapSize = 1 << BitsPerPixel;

        RWidth = Width = GWidth;
        RHeight = Height = GHeight;
        LeftOfs = TopOfs = 0;

        Resolution = BitsPerPixel;

        /*
         * Calculate number of bits we are expecting
         */
        CountDown = (long)Width * (long)Height;

        /*
         * Indicate which pass we are on (if interlace)
         */
        Pass = 0;

        /*
         * The initial code size
         */
        if( BitsPerPixel <= 1 )
                InitCodeSize = 2;
        else
                InitCodeSize = BitsPerPixel;

        /*
         * Set up the current x and y position
         */
        curx = cury = 0;

        /*
         * Write the Magic header
         */
        fwrite( Transparent < 0 ? "GIF87a" : "GIF89a", 1, 6, fp );

        /*
         * Write out the screen width and height
         */
        Putword( RWidth, fp );
        Putword( RHeight, fp );

        /*
         * Indicate that there is a global colour map
         */
        B = 0x80;       /* Yes, there is a color map */

        /*
         * OR in the resolution
         */
        B |= (Resolution - 1) << 5;

        /*
         * OR in the Bits per Pixel
         */
        B |= (BitsPerPixel - 1);

        /*
         * Write it out
         */
        fputc( B, fp );

        /*
         * Write out the Background colour
         */
        fputc( Background, fp );

        /*
         * Byte of 0's (future expansion)
         */
        fputc( 0, fp );

        /*
         * Write out the Global Colour Map
         */
        for( i=0; i<ColorMapSize; ++i ) {
                fputc( Red[i], fp );
                fputc( Green[i], fp );
                fputc( Blue[i], fp );
        }

      /*
       * Write out extension for transparent colour index, if necessary.
       */
      if ( Transparent >= 0 ) {
          fputc( '!', fp );
          fputc( 0xf9, fp );
          fputc( 4, fp );
          fputc( 1, fp );
          fputc( 0, fp );
          fputc( 0, fp );
          fputc( (unsigned char) Transparent, fp );
          fputc( 0, fp );
      }

        /*
         * Write an Image separator
         */
        fputc( ',', fp );

        /*
         * Write the Image header
         */

        Putword( LeftOfs, fp );
        Putword( TopOfs, fp );
        Putword( Width, fp );
        Putword( Height, fp );

        /*
         * Write out whether or not the image is interlaced
         */
        if( Interlace )
                fputc( 0x40, fp );
        else
                fputc( 0x00, fp );

        /*
         * Write out the initial code size
         */
        fputc( InitCodeSize, fp );

        /*
         * Go and actually compress the data
         */
        compress( InitCodeSize+1, fp, im );

        /*
         * Write out a Zero-length packet (to end the series)
         */
        fputc( 0, fp );

        /*
         * Write the GIF file terminator
         */
        fputc( ';', fp );
}

/*
 * Write out a word to the GIF file
 */
static void
Putword(int w, FILE *fp)
{
        fputc( w & 0xff, fp );
        fputc( (w / 256) & 0xff, fp );
}


/***************************************************************************
 *
 *  GIFCOMPR.C       - GIF Image compression routines
 *
 *  Lempel-Ziv compression based on 'compress'.  GIF modifications by
 *  David Rowley (mgardi@watdcsu.waterloo.edu)
 *
 ***************************************************************************/

/*
 * General DEFINEs
 */

#define GIFBITS    12

#define HSIZE  5003            /* 80% occupancy */

#ifdef NO_UCHAR
 typedef char   char_type;
#else /*NO_UCHAR*/
 typedef        unsigned char   char_type;
#endif /*NO_UCHAR*/

/*
 *
 * GIF Image compression - modified 'compress'
 *
 * Based on: compress.c - File compression ala IEEE Computer, June 1984.
 *
 * By Authors:  Spencer W. Thomas       (decvax!harpo!utah-cs!utah-gr!thomas)
 *              Jim McKie               (decvax!mcvax!jim)
 *              Steve Davies            (decvax!vax135!petsd!peora!srd)
 *              Ken Turkowski           (decvax!decwrl!turtlevax!ken)
 *              James A. Woods          (decvax!ihnp4!ames!jaw)
 *              Joe Orost               (decvax!vax135!petsd!joe)
 *
 */
#include <ctype.h>

#define ARGVAL() (*++(*argv) || (--argc && *++argv))

static int n_bits;                        /* number of bits/code */
static int maxbits = GIFBITS;                /* user settable max # bits/code */
static code_int maxcode;                  /* maximum code, given n_bits */
static code_int maxmaxcode = (code_int)1 << GIFBITS; /* should NEVER generate this code */
#ifdef COMPATIBLE               /* But wrong! */
# define MAXCODE(n_bits)        ((code_int) 1 << (n_bits) - 1)
#else /*COMPATIBLE*/
# define MAXCODE(n_bits)        (((code_int) 1 << (n_bits)) - 1)
#endif /*COMPATIBLE*/

static count_int htab [HSIZE];
static unsigned short codetab [HSIZE];
#define HashTabOf(i)       htab[i]
#define CodeTabOf(i)    codetab[i]

static code_int hsize = HSIZE;                 /* for dynamic table sizing */

/*
 * To save much memory, we overlay the table used by compress() with those
 * used by decompress().  The tab_prefix table is the same size and type
 * as the codetab.  The tab_suffix table needs 2**GIFBITS characters.  We
 * get this from the beginning of htab.  The output stack uses the rest
 * of htab, and contains characters.  There is plenty of room for any
 * possible stack (stack used to be 8000 characters).
 */

#define tab_prefixof(i) CodeTabOf(i)
#define tab_suffixof(i)        ((char_type*)(htab))[i]
#define de_stack               ((char_type*)&tab_suffixof((code_int)1<<GIFBITS))

static code_int free_ent = 0;                  /* first unused entry */

/*
 * block compression parameters -- after all codes are used up,
 * and compression rate changes, start over.
 */
static int clear_flg = 0;

static int offset;
static long int in_count = 1;            /* length of input */
static long int out_count = 0;           /* # of codes output (for debugging) */

/*
 * compress stdin to stdout
 *
 * Algorithm:  use open addressing double hashing (no chaining) on the
 * prefix code / next character combination.  We do a variant of Knuth's
 * algorithm D (vol. 3, sec. 6.4) along with G. Knott's relatively-prime
 * secondary probe.  Here, the modular division first probe is gives way
 * to a faster exclusive-or manipulation.  Also do block compression with
 * an adaptive reset, whereby the code table is cleared when the compression
 * ratio decreases, but after the table fills.  The variable-length output
 * codes are re-sized at this point, and a special CLEAR code is generated
 * for the decompressor.  Late addition:  construct the table according to
 * file size for noticeable speed improvement on small files.  Please direct
 * questions about this implementation to ames!jaw.
 */

static int g_init_bits;
static FILE* g_outfile;

static int ClearCode;
static int EOFCode;

static void
compress(int init_bits, FILE *outfile, gdImagePtr im)
{
    register long fcode;
    register code_int i /* = 0 */;
    register int c;
    register code_int ent;
    register code_int disp;
    register code_int hsize_reg;
    register int hshift;

    /*
     * Set up the globals:  g_init_bits - initial number of bits
     *                      g_outfile   - pointer to output file
     */
    g_init_bits = init_bits;
    g_outfile = outfile;

    /*
     * Set up the necessary values
     */
    offset = 0;
    out_count = 0;
    clear_flg = 0;
    in_count = 1;
    maxcode = MAXCODE(n_bits = g_init_bits);

    ClearCode = (1 << (init_bits - 1));
    EOFCode = ClearCode + 1;
    free_ent = ClearCode + 2;

    char_init();

    ent = GIFNextPixel( im );

    hshift = 0;
    for ( fcode = (long) hsize;  fcode < 65536L; fcode *= 2L )
        ++hshift;
    hshift = 8 - hshift;                /* set hash code range bound */

    hsize_reg = hsize;
    cl_hash( (count_int) hsize_reg);            /* clear hash table */

    output( (code_int)ClearCode );

#ifdef SIGNED_COMPARE_SLOW
    while ( (c = GIFNextPixel( im )) != (unsigned) EOF ) {
#else /*SIGNED_COMPARE_SLOW*/
    while ( (c = GIFNextPixel( im )) != EOF ) {  /* } */
#endif /*SIGNED_COMPARE_SLOW*/

        ++in_count;

        fcode = (long) (((long) c << maxbits) + ent);
        i = (((code_int)c << hshift) ^ ent);    /* xor hashing */

        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        } else if ( (long)HashTabOf (i) < 0 )      /* empty slot */
            goto nomatch;
        disp = hsize_reg - i;           /* secondary hash (after G. Knott) */
        if ( i == 0 )
            disp = 1;
probe:
        if ( (i -= disp) < 0 )
            i += hsize_reg;

        if ( HashTabOf (i) == fcode ) {
            ent = CodeTabOf (i);
            continue;
        }
        if ( (long)HashTabOf (i) > 0 )
            goto probe;
nomatch:
        output ( (code_int) ent );
        ++out_count;
        ent = c;
#ifdef SIGNED_COMPARE_SLOW
        if ( (unsigned) free_ent < (unsigned) maxmaxcode) {
#else /*SIGNED_COMPARE_SLOW*/
        if ( free_ent < maxmaxcode ) {  /* } */
#endif /*SIGNED_COMPARE_SLOW*/
            CodeTabOf (i) = free_ent++; /* code -> hashtable */
            HashTabOf (i) = fcode;
        } else
                cl_block();
    }
    /*
     * Put out the final code.
     */
    output( (code_int)ent );
    ++out_count;
    output( (code_int) EOFCode );
}

/*****************************************************************
 * TAG( output )
 *
 * Output the given code.
 * Inputs:
 *      code:   A n_bits-bit integer.  If == -1, then EOF.  This assumes
 *              that n_bits =< (long)wordsize - 1.
 * Outputs:
 *      Outputs code to the file.
 * Assumptions:
 *      Chars are 8 bits long.
 * Algorithm:
 *      Maintain a GIFBITS character long buffer (so that 8 codes will
 * fit in it exactly).  Use the VAX insv instruction to insert each
 * code in turn.  When the buffer fills up empty it and start over.
 */

static unsigned long cur_accum = 0;
static int cur_bits = 0;

static unsigned long masks[] = { 0x0000, 0x0001, 0x0003, 0x0007, 0x000F,
                                  0x001F, 0x003F, 0x007F, 0x00FF,
                                  0x01FF, 0x03FF, 0x07FF, 0x0FFF,
                                  0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF };

static void
output(code_int code)
{
    cur_accum &= masks[ cur_bits ];

    if( cur_bits > 0 )
        cur_accum |= ((long)code << cur_bits);
    else
        cur_accum = code;

    cur_bits += n_bits;

    while( cur_bits >= 8 ) {
        char_out( (unsigned int)(cur_accum & 0xff) );
        cur_accum >>= 8;
        cur_bits -= 8;
    }

    /*
     * If the next entry is going to be too big for the code size,
     * then increase it, if possible.
     */
   if ( free_ent > maxcode || clear_flg ) {

            if( clear_flg ) {

                maxcode = MAXCODE (n_bits = g_init_bits);
                clear_flg = 0;

            } else {

                ++n_bits;
                if ( n_bits == maxbits )
                    maxcode = maxmaxcode;
                else
                    maxcode = MAXCODE(n_bits);
            }
        }

    if( code == EOFCode ) {
        /*
         * At EOF, write the rest of the buffer.
         */
        while( cur_bits > 0 ) {
                char_out( (unsigned int)(cur_accum & 0xff) );
                cur_accum >>= 8;
                cur_bits -= 8;
        }

        flush_char();

        fflush( g_outfile );

        if( ferror( g_outfile ) )
            return;
    }
}

/*
 * Clear out the hash table
 */
static void
cl_block (void)             /* table clear for block compress */
{

        cl_hash ( (count_int) hsize );
        free_ent = ClearCode + 2;
        clear_flg = 1;

        output( (code_int)ClearCode );
}

static void
cl_hash(register count_int hsize)          /* reset code table */
                         
{

        register count_int *htab_p = htab+hsize;

        register long i;
        register long m1 = -1;

        i = hsize - 16;
        do {                            /* might use Sys V memset(3) here */
                *(htab_p-16) = m1;
                *(htab_p-15) = m1;
                *(htab_p-14) = m1;
                *(htab_p-13) = m1;
                *(htab_p-12) = m1;
                *(htab_p-11) = m1;
                *(htab_p-10) = m1;
                *(htab_p-9) = m1;
                *(htab_p-8) = m1;
                *(htab_p-7) = m1;
                *(htab_p-6) = m1;
                *(htab_p-5) = m1;
                *(htab_p-4) = m1;
                *(htab_p-3) = m1;
                *(htab_p-2) = m1;
                *(htab_p-1) = m1;
                htab_p -= 16;
        } while ((i -= 16) >= 0);

        for ( i += 16; i > 0; --i )
                *--htab_p = m1;
}

/******************************************************************************
 *
 * GIF Specific routines
 *
 ******************************************************************************/

/*
 * Number of characters so far in this 'packet'
 */
static int a_count;

/*
 * Set up the 'byte output' routine
 */
static void
char_init(void)
{
        a_count = 0;
}

/*
 * Define the storage for the packet accumulator
 */
static char accum[ 256 ];

/*
 * Add a character to the end of the current packet, and if it is 254
 * characters, flush the packet to disk.
 */
static void
char_out(int c)
{
        accum[ a_count++ ] = c;
        if( a_count >= 254 )
                flush_char();
}

/*
 * Flush the packet to disk, and reset the accumulator
 */
static void
flush_char(void)
{
        if( a_count > 0 ) {
                fputc( a_count, g_outfile );
                fwrite( accum, 1, a_count, g_outfile );
                a_count = 0;
        }
}

static void init_statics(void) {
      /* Some of these are properly initialized later. What I'm doing
            here is making sure code that depends on C's initialization
            of statics doesn't break when the code gets called more
            than once. */
      Width = 0;
      Height = 0;
      curx = 0;
      cury = 0;
      CountDown = 0;
      Pass = 0;
      Interlace = 0;
      a_count = 0;
      cur_accum = 0;
      cur_bits = 0;
      g_init_bits = 0;
      g_outfile = 0;
      ClearCode = 0;
      EOFCode = 0;
      free_ent = 0;
      clear_flg = 0;
      offset = 0;
      in_count = 1;
      out_count = 0;    
      hsize = HSIZE;
      n_bits = 0;
      maxbits = GIFBITS;
      maxcode = 0;
      maxmaxcode = (code_int)1 << GIFBITS;
}


/* +-------------------------------------------------------------------+ */
/* | 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.           | */
/* +-------------------------------------------------------------------+ */


#define        MAXCOLORMAPSIZE         256

#define        TRUE    1
#define        FALSE   0

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

/* We may eventually want to use this information, but def it out for now */
#if 0
static struct {
       unsigned int    Width;
       unsigned int    Height;
       unsigned char   ColorMap[3][MAXCOLORMAPSIZE];
       unsigned int    BitPixel;
       unsigned int    ColorResolution;
       unsigned int    Background;
       unsigned int    AspectRatio;
} GifScreen;
#endif

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

static int ReadColorMap (FILE *fd, int number, unsigned char (*buffer)[256]);
static int DoExtension (FILE *fd, int label, int *Transparent);
static int GetDataBlock (FILE *fd, unsigned char *buf);
static int GetCode (FILE *fd, int code_size, int flag);
static int LWZReadByte (FILE *fd, int flag, int input_code_size);
static void ReadImage (gdImagePtr im, FILE *fd, int len, int height, unsigned char (*cmap)[256], int interlace, int ignore);

int ZeroDataBlock;

gdImagePtr
gdImageCreateFromGif(FILE *fd)
{
       int imageNumber;
       int BitPixel;
       int ColorResolution;
       int Background;
       int AspectRatio;
       int Transparent = (-1);
       unsigned char   buf[16];
       unsigned char   c;
       unsigned char   ColorMap[3][MAXCOLORMAPSIZE];
       unsigned char   localColorMap[3][MAXCOLORMAPSIZE];
       int             imw, imh;
       int             useGlobalColormap;
       int             bitPixel;
       int             imageCount = 0;
       char            version[4];
       gdImagePtr im = 0;
       ZeroDataBlock = FALSE;

       imageNumber = 1;
       if (! ReadOK(fd,buf,6)) {
            return 0;
      }
       if (strncmp((char *)buf,"GIF",3) != 0) {
            return 0;
      }
       strncpy(version, (char *)buf + 3, 3);
       version[3] = '\0';

       if ((strcmp(version, "87a") != 0) && (strcmp(version, "89a") != 0)) {
            return 0;
      }
       if (! ReadOK(fd,buf,7)) {
            return 0;
      }
       BitPixel        = 2<<(buf[4]&0x07);
       ColorResolution = (int) (((buf[4]&0x70)>>3)+1);
       Background      = buf[5];
       AspectRatio     = buf[6];

       if (BitSet(buf[4], LOCALCOLORMAP)) {    /* Global Colormap */
               if (ReadColorMap(fd, BitPixel, ColorMap)) {
                  return 0;
            }
       }
       for (;;) {
               if (! ReadOK(fd,&c,1)) {
                       return 0;
               }
               if (c == ';') {         /* GIF terminator */
                       int i;
                       if (imageCount < imageNumber) {
                               return 0;
                       }
                       /* Terminator before any image was declared! */
                       if (!im) {
                              return 0;
                       }
                   /* Check for open colors at the end, so
                          we can reduce colorsTotal and ultimately
                          BitsPerPixel */
                       for (i=((im->colorsTotal-1)); (i>=0); i--) {
                               if (im->open[i]) {
                                       im->colorsTotal--;
                               } else {
                                       break;
                               }
                       } 
                       return im;
               }

               if (c == '!') {         /* Extension */
                       if (! ReadOK(fd,&c,1)) {
                               return 0;
                       }
                       DoExtension(fd, c, &Transparent);
                       continue;
               }

               if (c != ',') {         /* Not a valid start character */
                       continue;
               }

               ++imageCount;

               if (! ReadOK(fd,buf,9)) {
                     return 0;
               }

               useGlobalColormap = ! BitSet(buf[8], LOCALCOLORMAP);

               bitPixel = 1<<((buf[8]&0x07)+1);

               imw = LM_to_uint(buf[4],buf[5]);
               imh = LM_to_uint(buf[6],buf[7]);
             if (!(im = gdImageCreate(imw, imh))) {
                   return 0;
             }
               im->interlace = BitSet(buf[8], INTERLACE);
               if (! useGlobalColormap) {
                       if (ReadColorMap(fd, bitPixel, localColorMap)) { 
                                 return 0;
                       }
                       ReadImage(im, fd, imw, imh, localColorMap, 
                                 BitSet(buf[8], INTERLACE), 
                                 imageCount != imageNumber);
               } else {
                       ReadImage(im, fd, imw, imh,
                                 ColorMap, 
                                 BitSet(buf[8], INTERLACE), 
                                 imageCount != imageNumber);
               }
               if (Transparent != (-1)) {
                       gdImageColorTransparent(im, Transparent);
               }     
       }
}

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


       for (i = 0; i < number; ++i) {
               if (! ReadOK(fd, rgb, sizeof(rgb))) {
                       return TRUE;
               }
               buffer[CM_RED][i] = rgb[0] ;
               buffer[CM_GREEN][i] = rgb[1] ;
               buffer[CM_BLUE][i] = rgb[2] ;
       }


       return FALSE;
}

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

       switch (label) {
       case 0xf9:              /* 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)
                       *Transparent = buf[3];

               while (GetDataBlock(fd, (unsigned char*) buf) != 0)
                       ;
               return FALSE;
       default:
               break;
       }
       while (GetDataBlock(fd, (unsigned char*) buf) != 0)
               ;

       return FALSE;
}

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

       if (! ReadOK(fd,&count,1)) {
               return -1;
       }

       ZeroDataBlock = count == 0;

       if ((count != 0) && (! ReadOK(fd, buf, count))) {
               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) {
                                /* Oh well */
                       }                        
                       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)
                       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]) {
                               /* Oh well */
                       }
                       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(gdImagePtr im, FILE *fd, int len, int height, unsigned char (*cmap)[256], int interlace, int ignore)
{
       unsigned char   c;      
       int             v;
       int             xpos = 0, ypos = 0, pass = 0;
       int i;
       /* Stash the color map into the image */
       for (i=0; (i<gdMaxColors); i++) {
               im->red[i] = cmap[CM_RED][i];    
               im->green[i] = cmap[CM_GREEN][i];      
               im->blue[i] = cmap[CM_BLUE][i];  
               im->open[i] = 1;
       }
       /* Many (perhaps most) of these colors will remain marked open. */
       im->colorsTotal = gdMaxColors;
       /*
       **  Initialize the Compression routines
       */
       if (! ReadOK(fd,&c,1)) {
               return; 
       }
       if (LWZReadByte(fd, TRUE, c) < 0) {
               return;
       }

       /*
       **  If this is an "uninteresting picture" ignore it.
       */
       if (ignore) {
               while (LWZReadByte(fd, FALSE, c) >= 0)
                       ;
               return;
       }

       while ((v = LWZReadByte(fd,FALSE,c)) >= 0 ) {
               /* This how we recognize which colors are actually used. */
               if (im->open[v]) {
                       im->open[v] = 0;
               }
               gdImageSetPixel(im, xpos, ypos, 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) {
               /* Ignore extra */
       }
}

void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
{
      gdImageLine(im, x1, y1, x2, y1, color);         
      gdImageLine(im, x1, y2, x2, y2, color);         
      gdImageLine(im, x1, y1, x1, y2, color);
      gdImageLine(im, x2, y1, x2, y2, color);
}

void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color)
{
      int x, y;
      for (y=y1; (y<=y2); y++) {
            for (x=x1; (x<=x2); x++) {
                  gdImageSetPixel(im, x, y, color);
            }
      }
}

int gdGetWord(int *result, FILE *in)
{
      int r;
      r = getc(in);
      if (r == EOF) {
            return 0;
      }
      *result = r << 8;
      r = getc(in);     
      if (r == EOF) {
            return 0;
      }
      *result += r;
      return 1;
}

void gdPutWord(int w, FILE *out)
{
      putc((unsigned char)(w >> 8), out);
      putc((unsigned char)(w & 0xFF), out);
}

int gdGetByte(int *result, FILE *in)
{
      int r;
      r = getc(in);
      if (r == EOF) {
            return 0;
      }
      *result = r;
      return 1;
}

gdImagePtr gdImageCreateFromGd(FILE *in)
{
      int sx, sy;
      int x, y;
      int i;
      gdImagePtr im;
      if (!gdGetWord(&sx, in)) {
            goto fail1;
      }
      if (!gdGetWord(&sy, in)) {
            goto fail1;
      }
      im = gdImageCreate(sx, sy);
      if (!gdGetByte(&im->colorsTotal, in)) {
            goto fail2;
      }
      if (!gdGetWord(&im->transparent, in)) {
            goto fail2;
      }
      if (im->transparent == 257) {
            im->transparent = (-1);
      }
      for (i=0; (i<gdMaxColors); i++) {
            if (!gdGetByte(&im->red[i], in)) {
                  goto fail2;
            }
            if (!gdGetByte(&im->green[i], in)) {
                  goto fail2;
            }
            if (!gdGetByte(&im->blue[i], in)) {
                  goto fail2;
            }
      }     
      for (y=0; (y<sy); y++) {
            for (x=0; (x<sx); x++) {      
                  int ch;
                  ch = getc(in);
                  if (ch == EOF) {
                        gdImageDestroy(im);
                        return 0;
                  }
                  im->pixels[x][y] = ch;
            }
      }
      return im;
fail2:
      gdImageDestroy(im);
fail1:
      return 0;
}
      
void gdImageGd(gdImagePtr im, FILE *out)
{
      int x, y;
      int i;
      int trans;
      gdPutWord(im->sx, out);
      gdPutWord(im->sy, out);
      putc((unsigned char)im->colorsTotal, out);
      trans = im->transparent;
      if (trans == (-1)) {
            trans = 257;
      }     
      gdPutWord(trans, out);
      for (i=0; (i<gdMaxColors); i++) {
            putc((unsigned char)im->red[i], out);
            putc((unsigned char)im->green[i], out);   
            putc((unsigned char)im->blue[i], out);    
      }
      for (y=0; (y < im->sy); y++) {      
            for (x=0; (x < im->sx); x++) {      
                  putc((unsigned char)im->pixels[x][y], out);
            }
      }
}

gdImagePtr
gdImageCreateFromXbm(FILE *fd)
{
      gdImagePtr im;    
      int bit;
      int w, h;
      int bytes;
      int ch;
      int i, x, y;
      char *sp;
      char s[161];
      if (!fgets(s, 160, fd)) {
            return 0;
      }
      sp = &s[0];
      /* Skip #define */
      sp = strchr(sp, ' ');
      if (!sp) {
            return 0;
      }
      /* Skip width label */
      sp++;
      sp = strchr(sp, ' ');
      if (!sp) {
            return 0;
      }
      /* Get width */
      w = atoi(sp + 1);
      if (!w) {
            return 0;
      }
      if (!fgets(s, 160, fd)) {
            return 0;
      }
      sp = s;
      /* Skip #define */
      sp = strchr(sp, ' ');
      if (!sp) {
            return 0;
      }
      /* Skip height label */
      sp++;
      sp = strchr(sp, ' ');
      if (!sp) {
            return 0;
      }
      /* Get height */
      h = atoi(sp + 1);
      if (!h) {
            return 0;
      }
      /* Skip declaration line */
      if (!fgets(s, 160, fd)) {
            return 0;
      }
      bytes = (w * h / 8) + 1;
      im = gdImageCreate(w, h);
      gdImageColorAllocate(im, 255, 255, 255);
      gdImageColorAllocate(im, 0, 0, 0);
      x = 0;
      y = 0;
      for (i=0; (i < bytes); i++) {
            char h[3];
            int b;
            /* Skip spaces, commas, CRs, 0x */
            while(1) {
                  ch = getc(fd);
                  if (ch == EOF) {
                        goto fail;
                  }
                  if (ch == 'x') {
                        break;
                  }     
            }
            /* Get hex value */
            ch = getc(fd);
            if (ch == EOF) {
                  goto fail;
            }
            h[0] = ch;
            ch = getc(fd);
            if (ch == EOF) {
                  goto fail;
            }
            h[1] = ch;
            h[2] = '\0';
            sscanf(h, "%x", &b);          
            for (bit = 1; (bit <= 128); (bit = bit << 1)) {
                  gdImageSetPixel(im, x++, y, (b & bit) ? 1 : 0); 
                  if (x == im->sx) {
                        x = 0;
                        y++;
                        if (y == im->sy) {
                              return im;
                        }
                        /* Fix 8/8/95 */
                        break;
                  }
            }
      }
      /* Shouldn't happen */
      fprintf(stderr, "Error: bug in gdImageCreateFromXbm!\n");
      return 0;
fail:
      gdImageDestroy(im);
      return 0;
}

void gdImagePolygon(gdImagePtr im, gdPointPtr p, int n, int c)
{
      int i;
      int lx, ly;
      if (!n) {
            return;
      }
      lx = p->x;
      ly = p->y;
      gdImageLine(im, lx, ly, p[n-1].x, p[n-1].y, c);
      for (i=1; (i < n); i++) {
            p++;
            gdImageLine(im, lx, ly, p->x, p->y, c);
            lx = p->x;
            ly = p->y;
      }
}     
      
int gdCompareInt(const void *a, const void *b);
      
void gdImageFilledPolygon(gdImagePtr im, gdPointPtr p, int n, int c)
{
      int i;
      int y;
      int y1, y2;
      int ints;
      if (!n) {
            return;
      }
      if (!im->polyAllocated) {
            im->polyInts = (int *) malloc(sizeof(int) * n);
            im->polyAllocated = n;
      }           
      if (im->polyAllocated < n) {
            while (im->polyAllocated < n) {
                  im->polyAllocated *= 2;
            }     
            im->polyInts = (int *) realloc(im->polyInts,
                  sizeof(int) * im->polyAllocated);
      }
      y1 = p[0].y;
      y2 = p[0].y;
      for (i=1; (i < n); i++) {
            if (p[i].y < y1) {
                  y1 = p[i].y;
            }
            if (p[i].y > y2) {
                  y2 = p[i].y;
            }
      }
      for (y=y1; (y <= y2); y++) {
            int interLast = 0;
            int dirLast = 0;
            int interFirst = 1;
            ints = 0;
            for (i=0; (i <= n); i++) {
                  int x1, x2;
                  int y1, y2;
                  int dir;
                  int ind1, ind2;
                  int lastInd1 = 0;
                  if ((i == n) || (!i)) {
                        ind1 = n-1;
                        ind2 = 0;
                  } else {
                        ind1 = i-1;
                        ind2 = i;
                  }
                  y1 = p[ind1].y;
                  y2 = p[ind2].y;
                  if (y1 < y2) {
                        y1 = p[ind1].y;
                        y2 = p[ind2].y;
                        x1 = p[ind1].x;
                        x2 = p[ind2].x;
                        dir = -1;
                  } else if (y1 > y2) {
                        y2 = p[ind1].y;
                        y1 = p[ind2].y;
                        x2 = p[ind1].x;
                        x1 = p[ind2].x;
                        dir = 1;
                  } else {
                        /* Horizontal; just draw it */
                        gdImageLine(im, 
                              p[ind1].x, y1, 
                              p[ind2].x, y1,
                              c);
                        continue;
                  }
                  if ((y >= y1) && (y <= y2)) {
                        int inter = 
                              (y-y1) * (x2-x1) / (y2-y1) + x1;
                        /* Only count intersections once
                              except at maxima and minima. Also, 
                              if two consecutive intersections are
                              endpoints of the same horizontal line
                              that is not at a maxima or minima,  
                              discard the leftmost of the two. */
                        if (!interFirst) {
                              if ((p[ind1].y == p[lastInd1].y) &&
                                    (p[ind1].x != p[lastInd1].x)) {
                                    if (dir == dirLast) {
                                          if (inter > interLast) {
                                                /* Replace the old one */
                                                im->polyInts[ints] = inter;
                                          } else {
                                                /* Discard this one */
                                          }     
                                          continue;
                                    }
                              }
                              if (inter == interLast) {
                                    if (dir == dirLast) {
                                          continue;
                                    }
                              }
                        } 
                        if (i > 0) {
                              im->polyInts[ints++] = inter;
                        }
                        lastInd1 = i;
                        dirLast = dir;
                        interLast = inter;
                        interFirst = 0;
                  }
            }
            qsort(im->polyInts, ints, sizeof(int), gdCompareInt);
            for (i=0; (i < (ints-1)); i+=2) {
                  gdImageLine(im, im->polyInts[i], y,
                        im->polyInts[i+1], y, c);
            }
      }
}
      
int gdCompareInt(const void *a, const void *b)
{
      return (*(const int *)a) - (*(const int *)b);
}

void gdImageSetStyle(gdImagePtr im, int *style, int noOfPixels)
{
      if (im->style) {
            free(im->style);
      }
      im->style = (int *) 
            malloc(sizeof(int) * noOfPixels);
      memcpy(im->style, style, sizeof(int) * noOfPixels);
      im->styleLength = noOfPixels;
      im->stylePos = 0;
}

void gdImageSetBrush(gdImagePtr im, gdImagePtr brush)
{
      int i;
      im->brush = brush;
      for (i=0; (i < gdImageColorsTotal(brush)); i++) {
            int index;
            index = gdImageColorExact(im, 
                  gdImageRed(brush, i),
                  gdImageGreen(brush, i),
                  gdImageBlue(brush, i));
            if (index == (-1)) {
                  index = gdImageColorAllocate(im,
                        gdImageRed(brush, i),
                        gdImageGreen(brush, i),
                        gdImageBlue(brush, i));
                  if (index == (-1)) {
                        index = gdImageColorClosest(im,
                              gdImageRed(brush, i),
                              gdImageGreen(brush, i),
                              gdImageBlue(brush, i));
                  }
            }
            im->brushColorMap[i] = index;
      }
}
      
void gdImageSetTile(gdImagePtr im, gdImagePtr tile)
{
      int i;
      im->tile = tile;
      for (i=0; (i < gdImageColorsTotal(tile)); i++) {
            int index;
            index = gdImageColorExact(im, 
                  gdImageRed(tile, i),
                  gdImageGreen(tile, i),
                  gdImageBlue(tile, i));
            if (index == (-1)) {
                  index = gdImageColorAllocate(im,
                        gdImageRed(tile, i),
                        gdImageGreen(tile, i),
                        gdImageBlue(tile, i));
                  if (index == (-1)) {
                        index = gdImageColorClosest(im,
                              gdImageRed(tile, i),
                              gdImageGreen(tile, i),
                              gdImageBlue(tile, i));
                  }
            }
            im->tileColorMap[i] = index;
      }
}

void gdImageInterlace(gdImagePtr im, int interlaceArg)
{
      im->interlace = interlaceArg;
}


Generated by  Doxygen 1.6.0   Back to index