/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "gd.h"
#include "gdfontl.h"
#include "gdfonts.h"

main(int argc,char *argv[])
{
   FILE *fp1,*fp2;
   gdImagePtr img;
   int white,black,red,green,blue,yellow,pink,saphir;
   int l,l1,l2,n,nn,i,j,r1,r2,col[6];
   float value[10][50],maxval;
   char title[40],name[10][5];
   fp1 = fopen(argv[1],"r");
   fp2 = fopen(argv[2],"wb");
   img = gdImageCreate(200,200);
   white = gdImageColorAllocate(img,255,255,255);
   black = gdImageColorAllocate(img,0,0,0);
   red = gdImageColorAllocate(img,255,0,0);
   green = gdImageColorAllocate(img,0,255,0);
   blue = gdImageColorAllocate(img,0,0,255);
   yellow = gdImageColorAllocate(img,255,255,0);
   pink = gdImageColorAllocate(img,255,0,255);
   saphir = gdImageColorAllocate(img,0,255,255);
   col[0] = red;
   col[1] = green;
   col[2] = blue;
   col[3] = yellow;
   col[4] = pink;
   col[5] = saphir;
   gdImageRectangle(img,10,10,190,190,black);
   fscanf(fp1,"%s",title);
   l = strlen(title)*gdFontLarge->w;
   gdImageString(img,gdFontLarge,90-l/2,20,title,black);
   fscanf(fp1,"%d",&nn);
   n = 0;
   maxval = 0.0;
   while(fscanf(fp1,"%s",name[n])>0) {
      for(j=0;j<nn;j++) {
         fscanf(fp1,"%f",&value[n][j]);
         if(value[n][j]>maxval) maxval=value[n][j];
      }
      n++;
   }
   l1 = 160/n;
   l2 = 160/(nn-1);
   for(i=0;i<n;i++) {
      for(j=0;j<nn-1;j++) {
         r1 = (140*value[i][j])/maxval;
         r2 = (140*value[i][j+1])/maxval;
         gdImageLine(img,20+l2*j,170-r1,20+l2*(j+1),170-r2,col[i%6]);
      }
      gdImageLine(img,20+l1*i,175,20+l1*(i+1)-5,175,col[i%6]);
      gdImageString(img,gdFontSmall,20+l1*i,180,name[i],black);
   }
   gdImageGif(img,fp2);
   gdImageDestroy(img);
   fclose(fp1);
   fclose(fp2);
}
