#include "ioimage.h"

char* add_suffix(char* debut,char* fin)
{
  static char result[255];
  int i0,i;
  for (i0=0;debut[i0];i0++)
    result[i0]=debut[i0];
  for (i=0;(result[i0+i]=fin[i])!='\0';i++);
  return result;
}


char* load_in_file(FILE *f)
{
  static char result[255];
  int i=0;
  while(1)
    {
      for(i=0; (result[i]=getc(f))!='\n';i++);
      if (result[0]!='#')
	break;
    }
  return result;
}

/****************************************************/
/* load an image from the disk, reading PGM format  */
/* input:
   name  : name of the file
   errio : a pointer on a short integer
   option: a pointer on the Option stucture
   output:
   allocates and return an array of bytes containing the image.
   option->x_size, option->y_size are the dimension of
   the image.
   errio : type of error if any
   4 : file problem, 3 : format error
   2 : memory allocation error
   1 : data transmission problem
   0 : no error                                */
/****************************************************/
char* loadpgm_alloc (char *name,short *errio,int* taille,int* nombre)
{
  char* image;
  FILE* f;
  char* chaine;
  int sz;

  if ((f=fopen(name,"r"))==NULL)
    {
      fprintf(stderr,"Unable to open file %s.\n",name);
      *errio=4;
      return NULL;
    }
  else 
  
  chaine=load_in_file(f);
  if ((chaine[0]!='P') || (chaine[1]!='5'))
    {
      printf ("format error %s\n",name);
      *errio=3;
      return NULL;
    }
  else 

  chaine=load_in_file(f);

  /* read the size of the image and store them in taille and nombre*/
  sscanf(chaine,"%d %d",taille,nombre);
  printf("%s : %d colonnes x %d lignes \n",name,*taille,*nombre);
  /* skip the dynamic of the image */
  sscanf(load_in_file(f),"%*d");
  /* memory allocation */
  sz=*taille**nombre;
  image=(char*)malloc((1+sz)*sizeof(char));
  if (image==NULL)
    {
      *errio=2;
      printf("Memory allocation error\n");
      fclose(f);
      return NULL;
    }
  if (sz!=fread(image+1,sizeof(char),sz,f))
    {
      printf("Problem reading %s\n",name); 
      *errio=1;
    }
  else 
    *errio=0;
  fclose(f);
  *image='\0';
  return image;
}

/****************************************************/
/* load an image from the disk, reading .ima and .dim format  */
/* input:
   name  : name of the file without suffixes
   errio : a pointer on a short integer
   option: a pointer on the Option stucture
   output:
   allocates and return an array of bytes containing the image.
   option->x_size, option->y_size are the dimension of
   the image.
   errio : type of error if any
   4 : file problem, 3 : format error
   2 : memory allocation error
   1 : data transmission problem
   0 : no error                                */
/****************************************************/
char* loadima_alloc (char *name,short *errio,int* taille,int* nombre)
{
  char* image;
  FILE *fdim,*fima;
  int sz;

  if ((fdim=fopen(add_suffix(name,".dim"),"r"))==NULL)
    {
      fprintf(stderr,"Unable to open file %s.\n",name);
      *errio=4;
      return NULL;
    }
  /* read the size of the image and store them in the 'option' structure*/
  fscanf(fdim,"%d %d",taille,nombre);
  printf("%s : %d x %d chars\n",name,*taille,*nombre);
  fclose(fdim);

  /* ouverture du deuxieme fichier */
  if ((fima=fopen(add_suffix(name,".ima"),"r"))==NULL)
    {
      fprintf(stderr,"Unable to open file %s.\n",name);
      *errio=4;
      return NULL;
    }
  /* memory allocation */
  sz=*taille**nombre;
  image=(char*)malloc((sz)*sizeof(char));
  if (image==NULL)
    {
      *errio=2;
      printf("Memory allocation error\n");
      fclose(fima);
      return NULL;
    }
  if (sz!=fread(image,sizeof(char),sz,fima))
    {
      printf("Problem reading %s\n",name); 
      *errio=1;
    }
  else 
    *errio=0;
  fclose(fima);
  *image='\0';
  return image;
}



void savepgm(char* image, char* name,short *errio,int taille,int nombre)
{
  FILE *f;
  long sz;
  if ((f=fopen(name,"w"))==NULL)
    {
      printf("Unable to create file %s.\n",name);
      *errio=4;
      return ;
    }
  
  sz=taille*nombre;
  fprintf(f,"%s\n%s\n%d %d\n%u\n","P5","# cree par ",
	  taille,nombre,255);
  if (sz!=fwrite(image,sizeof(char),sz,f))
    {
      printf("Problem writing file %s.\n",name);
      *errio=1;
    }
  else 
    *errio=0;
  fclose(f);  
}






