#include "pvm3.h"
#include "pvm_imagelib.h"
#include "pvm_fade.h"

void main(int argc , char *argv[]){
  char imageName[255];
  char outfilename[255];
  long offset,imageSize,width,amountToRead,amountToProcess;
  unsigned char * data;
  unsigned char * outdata;
  int step;
  int steps;
  long diffSize;
  int * diffs;
  int current;
  Image * startImage;
 
  
  pvm_recv(-1,-1);  
  pvm_upkstr(imageName);
  pvm_upkint(&steps,1,1);
  pvm_upkint(&step,1,1);
  pvm_upklong(&diffSize,1,1);
  diffs = (int *)malloc(sizeof(int)*diffSize);
  pvm_upkint(diffs,diffSize,1);
  pvm_upklong(&offset,1,1);
  
#ifdef debug
  printf("offset %d, steps %d, step %d, deffsize %d, name %s  \n",offset,steps,step,diffSize,imageName);
#endif
 
  /* read in start image */
  startImage = readImage(imageName,0);
 
  data = readData(imageName,diffSize,offset);
  outdata = (unsigned char *)malloc(sizeof(unsigned char)*diffSize);

 
 if (steps == 0){
    outdata = fade(data,outdata,diffs,step,diffSize);
    
    writeData(outfilename,outdata,diffSize,offset);
  } 
  else {
    printf("Spliting\n");
    for (step = 0; step < steps;step++){
      outdata = fade(data,outdata,diffs,step,diffSize);
      
      sprintf(outfilename,"%s%d",imageName,step);
      writeData(outfilename,outdata,diffSize,offset);
    }
  }
  pvm_initsend(PvmDataDefault);
  pvm_pklong(&offset,1,1);
  pvm_send(pvm_parent(),1);
  pvm_exit();
}

unsigned char * fade(unsigned char * startData, unsigned char * newData,int * diffs, int currentStep, int size){
  int current;
  
  currentStep += 1;
  for (current = 0 ; current < size; current++){
    //printf("--> %x %d %d ",startData[current],diffs[current],currentStep);
    newData[current] = startData[current]+(diffs[current]*currentStep);
    if (newData[current] > 255)
      printf("current = %d\n",newData[current]);
  }
  return newData;
}


