/*  read in ppm or pgm file; for use with Matlab 4.0 */
/*  to compile: cmex loadppm.c CFLAGS=-Aa   */

#include "mex.h"
#include "engine.h"
#include<stdio.h>

#define True 1
#define False 0

/* - - prototypes - - */
void load_ppm_file(char *);

Matrix *red_matrix, *green_matrix, *blue_matrix, *grey_matrix;
double *red_ptr, *green_ptr, *blue_ptr, *grey_ptr;
int IsColor = False;

/* - - - GATEWAY ROUTINE - - - */
#ifdef __STDC__
void mexFunction(int nlhs, Matrix  *plhs[],
		 int nrhs, Matrix  *prhs[])
#else
mexFunction(nlhs, plhs, nrhs, prhs)
     int nlhs, nrhs;
     Matrix *plhs[], *prhs[];
#endif
{ 
  char * name;
  int n;

  /* check numbers of arguements */

  mexPrintf("running ppm read program\n");

  if(nrhs != 1)
    {
      mexErrMsgTxt("loadppm: you must have exactly 1 rhs argument.\n");
    }
  
  if (mxIsString(prhs[0]))
    {
      mexPrintf("loadppm: checked that right hand argument is a string\n");
      n = mxGetN(prhs[0]) + 1;
      name = mxCalloc(n, sizeof(char));
      mxGetString(prhs[0], name, n);
      if(nlhs == 3)
        {
	  IsColor = True;
          load_ppm_file(name);
          /* - - WANT RED, GREEN, AND BLUE MATRICES - - */
          mexPrintf("loadppm: reading 3 matrices\n");
	  plhs[0] = red_matrix;
	  plhs[1] = green_matrix;
	  plhs[2] = blue_matrix;
	}
      else if(nlhs == 1)
	{
          load_ppm_file(name);
	  /* - - WANT GREYSCALE IMAGE MATRIX BACK - - */
	  IsColor = False;
	  plhs[0] = grey_matrix;
	}
      else
        {
          mexErrMsgTxt("loadppm: you must have either 1 or 3 lhs arguments.\n");
        }
    }
  else
    mexErrMsgTxt("loadppm: filename must be a string.\n");
  mexPrintf("loadppm: done\n");
}

/* - - - LOAD IN A PPM FILE INTO AN MATRIX RETURNED IN MATLAB - - - */
void load_ppm_file(char * file_name)
{
  int magic_number;
  char * comments;
  int width, height, depth;
  
  unsigned int pixelr, pixelb, pixelg, grey_pixel;
  FILE * file_ptr;
  unsigned int file_index;
  unsigned int row_index, col_index;

  mexPrintf("loadppm: loading image %s . . . . . . \n", file_name);
  fflush(stdout);

  file_ptr = fopen(file_name, "r");
  
  /* - - GET INFO FROM PPM HEADER - - */
  fscanf(file_ptr,"P%d", &magic_number);
  /*  fscanf(file_ptr,"#%40c", comments );*/

  fscanf(file_ptr,"%d %d %d", &width, &height, &depth); 
  mexPrintf("loadppm: width=%d, height=%d, depth=%d, magic_number=%d\n\n",
             width, height, depth, magic_number);

  if(IsColor)
    {
      red_matrix = mxCreateFull(height, width, REAL);
      red_ptr = mxGetPr(red_matrix);
      green_matrix = mxCreateFull(height, width, REAL);
      green_ptr = mxGetPr(green_matrix);
      blue_matrix = mxCreateFull(height, width,  REAL);
      blue_ptr = mxGetPr(blue_matrix);
    }
  else
    {
      grey_matrix = mxCreateFull(height, width, REAL);
      grey_ptr = mxGetPr(grey_matrix);
    }

/*
  if(IsColor)
    {
      for(row_index = 0; row_index < height; row_index++)
	{
	  for(col_index = 0; col_index < width; col_index++)
	    {
	      fread(&pixelr, 1, 1, file_ptr);
	      fread(&pixelg, 1, 1, file_ptr);
	      fread(&pixelb, 1, 1, file_ptr);
	      
	      red_ptr[(row_index*height) + col_index] = pixelr;
	      green_ptr[(row_index*height) + col_index] = pixelg;
	      blue_ptr[(row_index*height) + col_index] = pixelb;

	    }
	}
    }
  else
    {
      fscanf(file_ptr,"%c", &grey_pixel);	  
      grey_ptr[file_index] = grey_pixel;
    }    
*/



  if(IsColor)
    {
      for(row_index = 0; row_index < height; row_index++)	    
	{
	  for(col_index = 0; col_index < width; col_index++)
	    {
	      fread(&pixelr, 1, 1, file_ptr);
	      fread(&pixelg, 1, 1, file_ptr);
	      fread(&pixelb, 1, 1, file_ptr);
	      
	      red_ptr[(col_index*height) + row_index] = pixelr;
	      green_ptr[(col_index*height) + row_index] = pixelg;
	      blue_ptr[(col_index*height) + row_index] = pixelb; 

	      if(row_index == 0)
		mexPrintf("R:%d, G:%d, B:%d", pixelr, pixelg, pixelb);
	      

	    }
	}
    }
  else
    {
      fscanf(file_ptr,"%c", &grey_pixel);	  
      grey_ptr[file_index] = grey_pixel;
    }    
      
  fclose(file_ptr);
  printf("loadppm: function    void load_ppm_file(char * file_name)    done\n");

}


