/*
 * PHOTOQUANTIGRAPHIC imaging toolkit (http://wearcam.org/orbits)
 * See also, http://wearcam.org/lightspaces/index.html
 * etc...
 *
 * Related references: Proc. IEEE Nov. 1998, etc.. http://wearcam.org
 *
 * photoquantigraphic image compositing toolkit
 *
 * pnmuchar2double.c - converts byte image to double precision image
 * to compile: gcc -Wall pnmuchar2double.c -o pnmuchar2double -lm
 * to run: pnmumath v080.ppm -o pork.ppm
 *
 * steve; from ndf's diff.c, with help of sbeck on parsing input images
 *
 * bugs: writes one too many pixels, e.g. color image is 24 bytes too long
 *
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "pnmutils.h"

void get_image_params(FILE * ifs, struct image_params * params)
{
  int c;
  int dims[3];
  int dims_scanned;
  if( ifs == NULL ) { /* check for file pointers to non existant files */
    fprintf(stderr,"get_frame_params: non-existant file\n");
    exit(EXIT_FAILURE);
  }

  if (fgetc(ifs)!='P') /* image must start with P5, P6, P7, or P8 */
  {
      fprintf(stderr, "get_frame_params: file not ppm file (doesn't start with"
              " ``P'').\n");
      exit(EXIT_FAILURE);
  }
  c=fgetc(ifs);
  if (c!='5' && c!='6' && c!='7' && c!='8' && c!='9' && c!='A')
    {
      fprintf(stderr, "get_frame_params: input image must begin with\n"
                      "P5 for grey byte, P6 for colour byte,\n"
                      "P7 for grey double, or P8 for colour double\n"
                      "P9 for grey lightspace double, or PA for colour lightspace double\n");
      exit(EXIT_FAILURE);
    }
  params->type=c;
  /* enter a loop
   * skip comments which start with #
   * and look for the dimensions
   */
  dims_scanned = 0;
  do {
      char line[80];
      fgets( line, sizeof(line), ifs );
      if( *line != '#' )
      {
          char *p = strtok(line," \n" );
          while( p && (*p != '#') )
          {
              sscanf(p,"%d", &dims[dims_scanned] );
              dims_scanned++;
              p = strtok( NULL, " \n" );
          }
      }
  } while( dims_scanned < 3 );

  params->width = dims[0];
  params->height = dims[1];
  params->max_val = dims[2];

  if( params->max_val != 255 )
  {
      fprintf(stderr,"get_frame_params: max value must be 255\n");
      exit( EXIT_FAILURE );
  }
  
  if(params->type=='A' || params->type=='9')
  {
      char line[80];

      fgets( line, sizeof(line), ifs );
      if( *line != '#' )
      {
          char *p = strtok(line," \n" );
          while( p && (*p != '#') )
          {
              sscanf(p,"%d", &params->numberOfImages );
              p = strtok( NULL, " \n" );
          }
      }

      fgets( line, sizeof(line), ifs );
      if( *line != '#' )
      {
          char *p = strtok(line," \n" );
          while( p && (*p != '#') )
          {
              sscanf(p,"%f", &params->exponent );
              p = strtok( NULL, " \n" );
          }
      }

      
  }

}

void get_image_type(FILE * ifs, struct image_params * params)
{
  int c;
  if( ifs == NULL ) { /* check for file pointers to non existant files */
    fprintf(stderr,"get_image_type: non-existant file\n");
    exit(EXIT_FAILURE);
  }

  if (fgetc(ifs)!='P') /* image must start with P5, P6, P7, or P8 */
  {
      fprintf(stderr, "get_image_type: file not ppm file (doesn't start with"
              " ``P'').\n");
      exit(EXIT_FAILURE);
  }
  c=fgetc(ifs);
  if (c!='5' && c!='6' && c!='7' && c!='8' && c!='9' && c!='A')
    {
      fprintf(stderr, "get_frame_params: input image must begin with\n"
                      "P5 for grey byte, P6 for colour byte,\n"
                      "P7 for grey double, or P8 for colour double\n"
                      "P9 for grey lightspace double, or PA for colour lightspace double\n");
      exit(EXIT_FAILURE);
    }
  params->type=c;
}

void get_image_params_preserve_comments(FILE * ifs, struct image_params * params, FILE * ofs)
{
  int dims[3];
  int dims_scanned;
  if( ifs == NULL ) { /* check for file pointers to non existant files */
    fprintf(stderr,"get_frame_params_preserve_comments: non-existant file\n");
    exit(EXIT_FAILURE);
  }

  if( ofs == NULL ) { /* check for file pointers to non existant files */
    fprintf(stderr,"get_frame_params_preserve_comments: non-existant file\n");
    exit(EXIT_FAILURE);
  }

  dims_scanned = 0;
  do {
      char line[80];
      fgets( line, sizeof(line), ifs );
      if( *line != '#' )
      {
          char *p = strtok(line," \n" );
          while( p && (*p != '#') )
          {
              sscanf(p,"%d", &dims[dims_scanned] );
              dims_scanned++;
              p = strtok( NULL, " \n" );
          }
      }
      else
          fprintf(ofs,"%s",line);
  } while( dims_scanned < 3 );

  params->width = dims[0];
  params->height = dims[1];
  params->max_val = dims[2];

  if( params->max_val != 255 )
  {
      fprintf(stderr,"get_frame_params: max value must be 255\n");
      exit( EXIT_FAILURE );
  }
  
  if(params->type=='A' || params->type=='9')
  {
      char line[80];

      fgets( line, sizeof(line), ifs );
      if( *line != '#' )
      {
          char *p = strtok(line," \n" );
          while( p && (*p != '#') )
          {
              sscanf(p,"%d", &params->numberOfImages );
              p = strtok( NULL, " \n" );
          }
      }

      fgets( line, sizeof(line), ifs );
      if( *line != '#' )
      {
          char *p = strtok(line," \n" );
          while( p && (*p != '#') )
          {
              sscanf(p,"%f", &params->exponent );
              p = strtok( NULL, " \n" );
          }
      }
  }

}

