/* gwrite.c     write out data as a GIF format file    -jpb 6/25/95    */

/* gwrite.c   uses routines from the gd 1.1.1 library:   

   gd 1.1.1 is copyright 1994, Quest Protein Database Center, Cold Spring
   Harbor Labs. Permission granted to copy and distribute this work
   provided that this notice remains intact.

	Tom Boutell, boutell@netcom.com
	http://sunsite.unc.edu/boutell/index.html
	116 14th Ave E Apt 2
	Seattle, WA 98102
*/

#include <stdio.h>
#include <gd.h>
#include <gdfontl.h>
#include <gdfonts.h>
#include "erlib.h"

double sign(double x);                 /* sign of argument */
extern unsigned int xsize, ysize;     /* size of ar array (input xsize+2) */
extern BYTE *fl;


/* ---------------------------------------------------------------------- */
/* gwrite() --  write out data in ar array (xsize x ysize) as a greyscale */
/*              GIF format image file with the name <fname>.              */
/*              ar is assumed hold values between 0.0 and 255.0           */
/* ---------------------------------------------------------------------- */

int gwrite(char *fname, float *ar)
{
			/* Input and output files */
    FILE *out;
    gdImagePtr im_out;       /* Input and output images */
    int blue;                   /* Color indexes */
    int red;
    int green;
    int x,y;

/*    printf("Gwrite.c: writing gif %d x %d\n",xsize,ysize); */

      /* Create output image, x by y pixels. */
	im_out = gdImageCreate(xsize,ysize);

	/* First color allocated is background. */
	/* this loop makes a 0-255 graylevel palette (only 64 unique tho) */
	for (x = 0; x < 256; x++) {  
	  red = x - 2;
	  if (red<0) red = 0;
	  blue = x - 3;
	  if (blue<0) blue=0;
	  green = x;
	  gdImageColorAllocate(im_out, red,green,blue);
	}

	for (x=0;x<xsize;x++) {         /* transfer ar data to pixel array */
	 for (y=0;y<ysize;y++) {
	  gdImageSetPixel(im_out,x,y,(int) El(ar,x,y));
	 }
	}

	out = fopen(fname, "wb");
	
	/* Write GIF */
	gdImageGif(im_out, out);
	fclose(out);
	gdImageDestroy(im_out);         /* free up image memory */
	return(0);
}

/* ---------------------------------------------------------------------- */
/* gvec_write() --  write gif file using vectors eg for river pattern     */
/*                  use direction array fl use white-> blue colormap      */
/* ---------------------------------------------------------------------- */

int gvec_write(char *fname, float *ar)
{

int xo[9] =                      /* 8-dir index offset arrays */
   { 0,-1,0,1,1,1,0,-1,-1 };
int yo[9] =
   { 0,-1,-1,-1,0,1,1,1,0 };


			/* Input and output files */
    FILE *out;
    gdImagePtr im_out;       /* Input and output images */
    int blue;                   /* Color indexes */
    int red;
    int green;
    int x,y;
    int ii,iinc,cmax,cmin;      /* handling writing bright color over darker */
    int tx,ty;                  /* temp. x,y */
    int x1,y1;                  /* endpoint of drawing vector */
    int x2,y2;                  /* startpoint of vector */
    int rx, ry;
    int color;
    int dir;                    /* direction */
    int d1,d2;                  /* two orthogonal directions to dir */
    int e1,e2;                  /* two nearby directions to dir */
    float sx, sy;         /* scaling factor converting between bitmaps */


	/* resolution of output imagemap */
    rx = RIVER_XRES;
    ry = RIVER_YRES;
    sx = (float) rx / (float) xsize;
    sy = (float) ry / (float) ysize;
    printf("Writing %s: %d x %d\n",fname,rx,ry); 
    /* printf("sx sy: %2.5f %2.5f  = %d/%d %d/%d\n",sx,sy,rx,xsize,ry,ysize); */

      /* Create output image, x by y pixels. */
	im_out = gdImageCreate(rx,ry);

	/* First color allocated is background. */
	/* this loop makes a 0-255 palette (only 64 unique tho) */
	
	for (x = 0; x < 256; x++) {  
	  red = 255-x;
	  green = 255-x;
	  blue = 255;
	  gdImageColorAllocate(im_out, red,green,blue);
	}
	/* for (x=0;x<9;x++) printf("%d %d\n",xo[x],yo[x]); */

	iinc = 4;
	for (ii=0;ii<256;ii+=iinc) {
	cmin = ii; cmax = ii+iinc - 1;
	for (x=1;x<(xsize-1);x++) {         /* transfer ar data to pixel array */
	 for (y=1;y<(ysize-1);y++) {
	  color = (int) El(ar,x,y);
	  if ((color >= cmin) && (color <= cmax) ) {
	   x1 = (int) (sx * (x)); 
	   y1 = (int) (sy * (y));
	   dir = El(fl,x,y);
	   d1 = dir+2; if (d1 > 8) d1 -= 8;  /* direction vector two over */
	   d2 = dir-2; if (d2 < 1) d2 += 8;
	   e1 = dir+1; if (e1 > 8) e1 -= 8;  /* direction vector two over */
	   e2 = dir-1; if (e2 < 1) e2 += 8;
	   
	   tx = xo[dir]; ty = yo[dir];
	   x2 = (int) (sx * (x + tx)); 
	   y2 = (int) (sy * (y + ty));
	 /* printf("%d,%d - %d,%d  (%d,%d)\n",x1,y1,x2,y2,xo[dir],yo[dir]); */
					 /* final point from fl direction */
	   color = (int) El(ar,x,y) - 50;
	   if (color > 0) {  /* make line thicker */
	     gdImageLine(im_out,x2+xo[d1],y2+yo[d1],x1+xo[d1],y1+yo[d1],color);
	     gdImageLine(im_out,x2+xo[d2],y2+yo[d2],x1+xo[d2],y1+yo[d2],color);
	     color += 50;
	     gdImageLine(im_out,x2+xo[e1],y2+yo[e1],x1+xo[e1],y1+yo[e1],color);
	     gdImageLine(im_out,x2+xo[e2],y2+yo[e2],x1+xo[e2],y1+yo[e2],color);
	   }
	   color = (int) El(ar,x,y);
	   gdImageLine(im_out,x1,y1,x2,y2,color);  /* draw line */
	  }  /* end if color... */
	 } /* end for y */
	} /* end for x */
	} /* end for ii */

	out = fopen(fname, "wb");
	
	/* Write GIF */
	gdImageGif(im_out, out);
	fclose(out);
	gdImageDestroy(im_out);         /* free up image memory */
	return(0);
}

double sign(double x)
{
  if (x>0) return(1);
  else if (x<0) return(-1);
  else return(0);
}
