#include "pvm_imagelib.h"

int main(int argc , char *argv[]){
  Image * image;
  long offset;
  long offset1;

  int remainder;
  int nodes = 12;
  int current_node = 0;
  int threshold = 100;
  unsigned int amount;
  unsigned char * data;

  //read in image
  image = readImage(argv[1],0);
  //print out size
  printf("Image size = %d\n",image->isize);
  //find out where data starts
  offset = image->boffset;
  //split the data into two for the purposes of test in should be relative to node number in pvm
  //isize specifies pixels, not bytes. Since pixels are in RGB, byte size should be pixel * 3
  amount = image->isize*3/nodes;
  printf("amount = %d\n",amount);
  //deterine how much we are going to miss by dividing data in half
  remainder = image->isize%nodes;
  printf("remainder = %d\n",remainder);
  //get the offset of the data
  offset1 = offset;
  //write data header
  writeHeader("data1.bmp",image->FileHeader,image->InfoHeader);

  for ( current_node = 0; current_node < nodes;current_node++){
    //figure out offset of current node of data
    offset1 = offset+amount*current_node;
    //for each node read the needed data
    //run the edge detection on it
    //write it to the file
    if (current_node != nodes-1){
      data = readData(argv[1],amount,offset1);
      data = sobel(data,amount,image->width,threshold);
      writeData("data1.bmp",data,amount,offset1); 
    }
    else { //this is for the last node only, so that we pick up theremainder
      data = readData(argv[1],amount+remainder,offset1);
      data = sobel(data,amount,image->width,threshold);
      writeData("data1.bmp",data,amount+remainder,offset1); 
    }
    
    printf("wrote data %d\n",current_node);
  }
  /**
     printf("offset1 = %d\n",offset1);
     //get the offset of the seconf chunnk of data
     offset2 = offset+amount;
     printf("offset2 = %d\n",offset2);
     
     
     //load the data into our array
     data = readData(argv[1],amount,offset1);
     printf("read data 1\n");
     //load the remainder of our data into the other array
     data2 = readData(argv[1],amount+remainder,offset2);
     printf("read data 2\n");
     //do the actual edge detection
     data = sobel(data,amount,image->width,threshold);
     printf("processed data 1\n");
     //same as above
     data2 = sobel(data2,amount+remainder,image->width,threshold);
     printf("processed data 2\n");
     //write out the data header
     writeHeader("data1.bmp",image->FileHeader,image->InfoHeader);
     //write out first node of data
     writeData("data1.bmp",data,amount,offset1);
     //write out second node of data
     writeData("data1.bmp",data2,amount+remainder,offset2); 
     printf("wrote data\n");
  **/

}

