/* -------------------------------------------------------------------------- */
/*
                      NAME : image.c
               DESCRIPTION : Image-handling routines

                    AUTHOR : Heini Withagen
                    E-MAIL : heini@w4.nl
	          WWW-SITE : http://sparkie.riv.net/w4/index.html
                  CREATION : 23-08-1995
         LAST MODIFICATION : 30-08-1995

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "gd/gd.h"
#include "misc.h"
#include "count.h"
#include "cgi-util.h"
#include "image.h"
#include "conf.h"

static gdImagePtr counter_image_ptr;

gdImagePtr load_digit(char digit_char);

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

int construct_counter_image(struct info *info)
{
  gdImagePtr digit_ptr;
  int count, digit_count = 0;
  char count_string[MAX_LENGTH];
  char *str_ptr = count_string;

  /* adjust image width */
  if ( info->width > 10 )
    info->width = 10;

  sprintf(count_string, "%d", info->count_value);
  if ( strlen(count_string) > info->width )
    info->width = strlen(count_string);

  if (info->show)
  {
    /* create new image to contain the new counter image */
    if ( !(counter_image_ptr = gdImageCreate(info->width * DIGIT_WIDTH, DIGIT_HEIGHT)) )
    {
      errorlog("unable to create new counter image");
      return ERROR;
    }

    if ( !(digit_ptr = load_digit('0')) )
      return ERROR;
 
    for (count = 0; count < (info->width - strlen(count_string)); count++)
    {
      gdImageCopy(counter_image_ptr, digit_ptr, digit_count * DIGIT_WIDTH, 0, 0, 0, DIGIT_WIDTH, DIGIT_HEIGHT);
      digit_count++;
    }  

    for (count = 0; count < strlen(count_string); count++, str_ptr++)
    {
      if ( !(digit_ptr = load_digit(*str_ptr)) )
        return ERROR;

      gdImageCopy(counter_image_ptr, digit_ptr, digit_count * DIGIT_WIDTH, 0, 0, 0, DIGIT_WIDTH, DIGIT_HEIGHT);
      digit_count++;
    }
  }
  else
  {
    int black;

    if ( !(counter_image_ptr = gdImageCreate(1,1)) )
    {
      errorlog("unable to create new counter image");
      return ERROR;
    }
        
    black = gdImageColorAllocate(counter_image_ptr, 0, 0, 0);
    gdImageSetPixel(counter_image_ptr, 0, 0, black);
    gdImageColorTransparent(counter_image_ptr, black);
  }

  return OK;
}

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

gdImagePtr load_digit(char digit_char)
{
  char file_name[256], str[256];
  FILE *fp;
  gdImagePtr digit_ptr;

  sprintf(file_name,"%s%c.gd", PIC_PATH, digit_char);
  if ( !(fp = fopen(file_name, "r")) )
  {
    sprintf(str, "could not open digit file %c.gd", digit_char);
    errorlog(str);
    return NULL;
  }

  if ( !(digit_ptr = gdImageCreateFromGd(fp)) )
  {
    sprintf(str, "%c.gd digit file doesn't contain a gd-image", digit_char);
    errorlog(str);  
    return NULL;
  }

  fclose(fp);
  return digit_ptr;
}

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

int send_counter_image(void)
{
  fputs("HTTP/1.0 200 OK\n", stdout);
  fputs("Content-type: image/gif\n\n", stdout);
  gdImageGif(counter_image_ptr, stdout);

  gdImageDestroy(counter_image_ptr);
  return OK;
}

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

