#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <assert.h>
#include "/home/mq/Quasi-3D/IRCS/Quasi3D/libQ3D.h"
/*#include "./libQ3D.h"*/
#include <sys/time.h>
#include <sys/resource.h>
#include <unistd.h>

#define INTSIZE 10
  
char *itoa(int n);

char STKFileName[1024], OptionsFileName[1024];
char TGAFileName[1024];
unsigned long Width=500, Height=500, StartingLine=0, EndingLine=499;
int SuperSamplingFactor=1, Verbose;

int i;

double haze=0;

static unsigned char header_in[4],header_out[18];
static unsigned char packet_in[512];
static unsigned char packet_out[6];
static long total_read, total_written;
static FILE *infile, *outfile;

#define RAW 0
#define RLE 1

static int NoAlpha;

void progress(double ratio)
{
  printf("\r%d%%",(int)(100*ratio)); 
  fflush(stdout);
}

void message(char *text)
{
  printf("%s\n",text);
}


void process_args(int argc, char **argv)
{
  int i,  argI=0, argO=0;
  
  if (argc==1) {
    printf("options:\n");
    printf("  -i [name]   : STK input file\n");
    printf("  -o [name]   : TGA output file\n");
    printf("  -w [number] : Image width (default: 500)\n");
    printf("  -h [number] : Image height (default: 500)\n");
    printf("  -a [number] : Starting scan-line (default: 0)\n");
    printf("  -e [number] : Ending scan-line (default: height-1)\n");
    printf("  -a [number] : Antialiasing factor (default: 1)\n");
    printf("  -x [number] : Haze (default: )\n");
/********    printf("  -v [number] : Verbosity level (default: 0)\n");****/
    exit(-1);
  }

  for (i=1; i<argc; i++) {
    if (!strcmp(argv[i],"-i")) {
      strcpy(STKFileName,argv[++i]);
      argI = 1;
    }
    else if (!strcmp(argv[i],"-o")) {
      strcpy(TGAFileName,argv[++i]);
      argO = 1;
    }
    else if (!strcmp(argv[i],"-w")) {
      Width = atoi(argv[++i]);
    }
    else if (!strcmp(argv[i],"-h")) {
      Height = atoi(argv[++i]);
    }
    else if (!strcmp(argv[i],"-s")) {
      StartingLine = atoi(argv[++i]);
    }
    else if (!strcmp(argv[i],"-e")) {
      EndingLine = atoi(argv[++i]);
    }
    else if (!strcmp(argv[i],"-a")) {
      SuperSamplingFactor = atoi(argv[++i]);
    }
    else if (!strcmp(argv[i],"-x")) {
      haze = atof(argv[++i]);
    }
    else if (!strcmp(argv[i],"-v")) {
      Verbose = atoi(argv[++i]);
    }
  }

  if (argI==0) {
    fprintf(stderr,"STK input file not specified (use: -i [filename])\n");
    exit(-1);
  }
  if (argO==0) {
    fprintf(stderr,"TGA output file not specified (use: -o [filename])\n");
    exit(-2);
  }    

  if (EndingLine == 0)
    EndingLine = Height-1;


/********  if (Verbose) {
    printf("Input file: %s\n",STKFileName);
    printf("Output file: %s\n",ETGAFileName);
    printf("Size: %ld x %ld\n",Width, Height);
    printf("Starting line: %ld, Ending Line: %ld\n",StartingLine, EndingLine);
    printf("Antialising: ");
    if (SuperSamplingFactor>1) 
      printf("%d\n",SuperSamplingFactor);
    else
      printf("none\n");
    printf("Verbose Level: %d\n",Verbose);
  }
  }******/
}

static void translate_packet(const int type)
{
  int len; /* length of packet -1 */
  int i,j;


  switch(type) {
  case RLE:
    packet_out[0]=255; /* i.e. 128 pixel long packet + bit 7 set to 1 */
    len =(packet_in[0] - 128) + (packet_in[1]<<7) + 1;
    total_read += len;


    /* read packet pixel */
    fread(packet_in, 1, 4, infile);
    packet_out[1]=packet_in[0];
    packet_out[2]=packet_in[1];
    packet_out[3]=packet_in[2];
    packet_out[4]=packet_in[3];

    /*    if (verbose==2)
      printf("in: RLE packet (length=%d, colour=%d %d %d %d)\n",len,
      packet_in[0],packet_in[1],packet_in[2],packet_in[3]);*/

    
    for (i=0; i<len/128; i++) {
      if (NoAlpha)
	fwrite(packet_out,1,4,outfile);
      else
	fwrite(packet_out,1,5,outfile);
      total_written += 128;
      /*      if (verbose==2)
	      printf("out: RLE packet (length=128)\n");*/
    }
    

    /* last packet */
    if (len %= 128) {
      packet_out[0] = (unsigned char)(len - 1 + 128);
      if (NoAlpha)
	fwrite(packet_out,1,4,outfile);
      else
	fwrite(packet_out,1,5,outfile);
      total_written += len;
      /*  if (verbose==2)
	  printf("out: RLE packet (length=%d)\n",len);*/
    }
    break;

  case RAW:
    packet_out[0]=127; /* i.e. 128 pixel long packet + bit 7 set to 0 */
    len =(packet_in[0]) + (packet_in[1]<<7) + 1;
    total_read += len;

    /*    if (verbose==2)
	  printf("in: RAW packet (length=%d)\n",len);*/

    for (i=0; i<len/128; i++) {
      /* header */
      packet_out[0]=127; /* i.e. 128 pixel long packet + bit 7 set to 0 */
      fwrite(packet_out,1,1,outfile);

      /* 128 pixels */
      fread(packet_in,1,128*4,infile);

      for (j=0;j<128;j++) {
	packet_out[0]=packet_in[j*4];
	packet_out[1]=packet_in[j*4+1];
	packet_out[2]=packet_in[j*4+2];
	if (NoAlpha) {
	  fwrite(packet_out,1,3,outfile);
	}
	else {
	  packet_out[3]=packet_in[j*4+3];
	  fwrite(packet_out,1,4,outfile);
	}

      }
      total_written += 128;
      
      /*      if (verbose==2)
	      printf("out: RAW packet (length=128)\n");*/
    }
     

    /* last packet */
    if (len %= 128) {
      packet_out[0]= (unsigned char)(len - 1);
      fwrite(packet_out,1,1,outfile);

      fread(packet_in,1,(unsigned int)(4*len),infile);

      for (j=0;j<len;j++) {
	packet_out[0]=packet_in[j*4];
	packet_out[1]=packet_in[j*4+1];
	packet_out[2]=packet_in[j*4+2];
	if (NoAlpha)
	  fwrite(packet_out,1,3,outfile);
	else {
	  packet_out[3]=packet_in[j*4+3];
	  fwrite(packet_out,1,4,outfile);
	}
      }
      /*      if (verbose==2)
	      printf("out: RAW packet (length=%d)\n",len);*/
      
      total_written += len;
    }
    break;
  }
}	

main(int argc, char **argv)
{

   FILE *fp, *stkFile;
   char *index, *anotherindex, *filename;
   int  i, j, currentlen, maxlen;
   int preFrameFlag;
   char *str;
   int qm1, qm2;
   double mid, qiman=0.0;
   double li1, li2, li3, l4;
   
   typedef struct 
   {
       char *line;
       struct cfgfile *link;
       }cfgfile;
   cfgfile *cfgfilePointer, *cfgfileStartPointer;

   
   typedef struct 
   {
       char *name;
       struct celname *link;
       }celname;
   celname *celnamePointer, *celnameStartPointer;

   char *t1, *t2, *t3, *t4, *t5;
   /*   int celNumber=0, stkFileNumber=0;*/
   double celRotationX, celRotationY, celRotationZ;
   double celTranslationX, celTranslationY, celTranslationZ;
   /* double RotationX, RotationY, RotationZ;*/
   /*   double TranslationX, TranslationY, TranslationZ;*/
   double temp1, temp2, temp3, temp4, v, l;
   double tmp[3];
   double total1,total2;
   double a,b,t;
   /*   double currentpx,currentpy,currentpz,currentdx,currentdy,currentdz,currentup,currentzoom,current1,current2;*/
   /*  int judgex,judgey,judgez;*/
   
   
   IR_Stack stack;
   IR_Cel c;
   IR_Colour celfrontLight, celbackLight;
   IR_Matrix4 celRotateX, celRotateY, celRotateZ, celRotate;
   /*   IR_Matrix4 transform1, transform2;*/
   IR_Matrix4 celTranslate, celTransform;
   /*   IR_Matrix4 celspacetransform[30];
	IR_Matrix4 celinitialrotation[30];*/
   /*   IR_Matrix4 identity4 = {{1.0, 0.0, 0.0, 0.0},
			    {0.0, 1.0, 0.0, 0.0},
			    {0.0, 0.0, 1.0, 0.0},
			    {0.0, 0.0, 0.0, 1.0}};*/

   /*  char TS[3][20];*/
           
   IR_ImageInfo iminfo;

   typedef struct 
   {
       double x, y, z;
       }IR_Point3;

   typedef struct 
   {
      char *name;
      IR_Point3 startposition;
      IR_Point3 startdirection;
      double startup;
      IR_Point3 endposition;
      IR_Point3 enddirection;
      double endup; 
	struct path *link;  
      }path;

   path *pathPointer, *pathStartPointer;   

   typedef struct 
   {
      char *name;
      double startfocallength;
      unsigned long startfocus;
      double endfocallength;
      unsigned long endfocus;
	struct camera *link;  
      }camera;

   camera *cameraPointer, *cameraStartPointer;   

   typedef struct
   {
     char *name;
     struct ir_colour_rgb startfrontlight;
     struct ir_colour_rgb startbacklight;
     struct ir_colour_rgb endfrontlight;
     struct ir_colour_rgb endbacklight;
     struct light *link;  
      }light;
   
   light *lightPointer, *lightStartPointer;

   typedef struct
   {
      char *actor;
      int startFrame;
      int endFrame;
	int IN;
	int OUT;
      char *pathStatus;
      char *status;
	struct script *link;  
      }script;
   
   script *scriptPointer, *scriptStartPointer, *currentScriptPointer;
   script *currentFramePointer, *endFramePointer, *frameStartPointer, *nextScriptPointer, *cameraScriptPointer;
   int Value, currentValue, nextValue, MaxFrame=0;

   typedef struct
   {
       IR_Point3 position;
       IR_Point3 direction;
       double up;
       double zoom;
       unsigned long focus;
   }CURRENTCAMERA;

   CURRENTCAMERA currentcam,startcam,endcam,midcam;

   IR_Point2  currentcamangle;

   typedef struct
     {
       IR_Point3 position;
       IR_Point3 direction;
       double up;
       struct ir_colour_rgb frontlight;
       struct ir_colour_rgb backlight;
   }CURRENTCEL;

   CURRENTCEL currentcel, startcel, endcel, midcel;

   IR_Point2 currentcelangle;

     char ETGAFileName[]="etgatemp.etga";



     currentcam.direction.x=0;
currentcam.direction.y=0;
currentcam.direction.z=0;

 currentcel.direction.x=0;
 currentcel.direction.y=0;
 currentcel.direction.z=0;


#ifdef MYCOMMENT

    
   NoAlpha=0;
   if(argc!=2)
   {
      printf("Usage:makeframes stk.cfg\n");
      exit(1);
      }
   
   fp=fopen(argv[1],"r");
   if(fp==NULL) 
   {
      printf("\n Open stk configure File Error\n");
      exit(1);
      }

   cfgfilePointer = NULL;
   str = malloc(sizeof(char)*80);
   while(fgets(str,80,fp)!=NULL)
   {
     if(cfgfilePointer != NULL) 
      {
		while (cfgfilePointer -> link != NULL)
	    		cfgfilePointer = cfgfilePointer -> link;
		cfgfilePointer -> link = (struct cfgfile *) malloc (sizeof (cfgfile));
      	cfgfilePointer = cfgfilePointer -> link;
		}
	else
	{
		cfgfilePointer = (struct cfgfile  *) malloc (sizeof (cfgfile));
		cfgfileStartPointer = cfgfilePointer;
		}
     cfgfilePointer -> line = malloc(sizeof(char)*80);
     cfgfilePointer -> link = NULL;
     strcpy(cfgfilePointer -> line , str);
     }
   free(str);
   fclose(fp);

   /******************** Display CFG file ********************************/
   cfgfilePointer=cfgfileStartPointer;
   while (cfgfilePointer != NULL)
   {
	printf("%s", cfgfilePointer->line);
	cfgfilePointer = cfgfilePointer->link;
	}
   printf("\n");
  
   
   /****************************  Reading Cel Names  ***********************/
   celnamePointer = NULL;
   cfgfilePointer = cfgfileStartPointer;

   t1=strtok(cfgfilePointer->line," ");
   t1=strtok(NULL," ");
   j=0;
   t2=malloc(sizeof(char)*10);
   for(t2=strtok(t1,",");t2!=NULL;t2=strtok(NULL,","))
   {
      if(celnamePointer != NULL) 
      {
	   	while (celnamePointer -> link != NULL)
	    		celnamePointer = celnamePointer -> link;
		celnamePointer -> link = (struct celname *) malloc (sizeof (celname));
      	celnamePointer = celnamePointer -> link;
		}
	else
	{
		celnamePointer = (struct celname  *) malloc (sizeof (celname));
		celnameStartPointer = celnamePointer;
		}
	celnamePointer -> name = malloc(sizeof(char)*10);
	celnamePointer -> link = NULL;
	strcpy(celnamePointer -> name , t2);
	}
   free(t2);


   /******************** Display Cel Names ********************************/
   celnamePointer=celnameStartPointer;
   while (celnamePointer != NULL)
   {
	printf("%s", celnamePointer->name);
	celnamePointer = celnamePointer->link;
	}
   printf("\n");

  

   /****************************  Reading Path  *********************/
   cfgfilePointer = cfgfilePointer->link;
   cfgfilePointer = cfgfilePointer->link;
   pathPointer = NULL;
   t1 = malloc(sizeof(char)*10);
   for(;;)
   {
	t1=strtok(cfgfilePointer -> line," ");
	if(pathPointer != NULL) 
      {
	   	while (pathPointer -> link != NULL)
	    		pathPointer = pathPointer -> link;
		pathPointer -> link = (struct path *) malloc (sizeof (path));
      	pathPointer = pathPointer -> link;
		}
	else
	{
		pathPointer = (struct path  *) malloc (sizeof (path));
		pathStartPointer = pathPointer;
		}
	pathPointer -> name = malloc(sizeof(char)*10);
	pathPointer -> link = NULL;
	strcpy(pathPointer -> name , t1);
      cfgfilePointer = cfgfilePointer->link;
   	cfgfilePointer = cfgfilePointer->link;


      /************************  Reading Path Start Location *************************/
      j=0;
      t1=strtok(cfgfilePointer -> line,")");
	t2=strtok(NULL, ")");
	t3=strtok(NULL,")");
	
      pathPointer -> startup = atof(t3);

      t1=strtok(t1,"(");   
	for(t3=strtok(t1,",");t3!=NULL;t3=strtok(NULL,","))
      {
	    tmp[j]=atof(t3);
          j++;          
          }
       pathPointer -> startposition.x = tmp[0];
       pathPointer -> startposition.y = tmp[1];
       pathPointer -> startposition.z = -tmp[2];       

       j=0;
       t1=strtok(t2,"(");
       for(t4=strtok(t1,",");t4!=NULL;t4=strtok(NULL,","))
       {
	    tmp[j]=atof(t4);
          j++;          
          }
       pathPointer -> startdirection.x = tmp[0];
       pathPointer -> startdirection.y = tmp[1];
       pathPointer -> startdirection.z = -tmp[2];

	/************************  Reading Path End Location *************************/

	cfgfilePointer = cfgfilePointer->link;
	j=0;
      t1=strtok(cfgfilePointer -> line,")");
	t2=strtok(NULL, ")");
	t3=strtok(NULL,")");
	pathPointer -> endup=atof(t3);

      t1=strtok(t1,"(");   
	for(t3=strtok(t1,",");t3!=NULL;t3=strtok(NULL,","))
      {
	    tmp[j]=atof(t3);
          j++;          
          }
       pathPointer -> endposition.x = tmp[0];
       pathPointer -> endposition.y = tmp[1];
       pathPointer -> endposition.z = -tmp[2];       

       j=0;
       t1=strtok(t2,"(");
       for(t4=strtok(t1,",");t4!=NULL;t4=strtok(NULL,","))
       {
	    tmp[j]=atof(t4);
          j++;          
          }
       pathPointer -> enddirection.x = tmp[0];
       pathPointer -> enddirection.y = tmp[1];
       pathPointer -> enddirection.z = -tmp[2];
      
 	 cfgfilePointer = cfgfilePointer->link;
	 cfgfilePointer = cfgfilePointer->link;
	 if(strncmp(cfgfilePointer -> line, "CAMERA", 6)==0) break;
       }

	/*************************** Displaying Path Data *************************/

	pathPointer = pathStartPointer;
	while(pathPointer != NULL)
   	{
	   	printf("%s\n", pathPointer -> name);
	   	printf("PathStart=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", pathPointer->startposition.x, 
		pathPointer->startposition.y, pathPointer->startposition.z, pathPointer->startdirection.x, 
		pathPointer->startdirection.y, pathPointer->startdirection.z, pathPointer->startup);

		printf("PathEnd=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", pathPointer->endposition.x, 
		pathPointer->endposition.y, pathPointer->endposition.z, pathPointer->enddirection.x, 
		pathPointer->enddirection.y, pathPointer->enddirection.z, pathPointer->endup);
		pathPointer = pathPointer ->link;
		}
   free(t1);

   /**********************  Reading Camera  *************************/
   
   cfgfilePointer = cfgfilePointer->link;
   cameraPointer = NULL;
   t1 = malloc(sizeof(char)*10);
   for(;;)
   {
	t1=strtok(cfgfilePointer -> line," ");
	if(cameraPointer != NULL) 
      {
	   	while (cameraPointer -> link != NULL)
	    		cameraPointer = cameraPointer -> link;
		cameraPointer -> link = (struct camera *) malloc (sizeof (camera));
      	cameraPointer = cameraPointer -> link;
		}
	else
	{
		cameraPointer = (struct camera  *) malloc (sizeof (camera));
		cameraStartPointer = cameraPointer;
		}
    cameraPointer -> name = malloc(sizeof(char)*10);
    cameraPointer -> link = NULL;
    strcpy(cameraPointer -> name , t1);

    cfgfilePointer = cfgfilePointer->link;
    cfgfilePointer = cfgfilePointer->link;
    t1=strtok(cfgfilePointer -> line,",");
    t2=strtok(NULL,",");
    cameraPointer -> startfocallength = atof(t1);
    cameraPointer -> startfocus = atof(t2);
	
    cfgfilePointer = cfgfilePointer->link;
    t1=strtok(cfgfilePointer -> line,",");
    t2=strtok(NULL,",");
    cameraPointer -> endfocallength=atof(t1);
    cameraPointer -> endfocus=atof(t2);

    cfgfilePointer = cfgfilePointer->link;
    cfgfilePointer = cfgfilePointer->link;
    if(strncmp(cfgfilePointer -> line, "LIGHT", 5)==0) break;
    }
	
   free(t1);
   
   /***************************** Display Camera  ********************/
   cameraPointer = cameraStartPointer;
   while(cameraPointer != NULL)
   {
      printf("%s\n", cameraPointer->name);
      printf("%lg,%ld\n", cameraPointer->startfocallength,cameraPointer->startfocus);
      printf("%lg,%ld", cameraPointer->endfocallength,cameraPointer->endfocus);
	cameraPointer = cameraPointer -> link;
	}

   /************************  Reading Light  *************************/

   cfgfilePointer = cfgfilePointer->link;
   lightPointer = NULL;
   t1 = malloc(sizeof(char)*10);

   for(;;)
   {

	t1=strtok(cfgfilePointer -> line," ");
	if(lightPointer != NULL) 
      {
	   	while (lightPointer -> link != NULL)
	    		lightPointer = lightPointer -> link;
		lightPointer -> link = (struct light *) malloc (sizeof (light));
      	lightPointer = lightPointer -> link;
		}
	else
	{
		lightPointer = (struct light *) malloc (sizeof (light));
		lightStartPointer = lightPointer;
		}
    	lightPointer -> name = malloc(sizeof(char)*10);
    	lightPointer -> link = NULL;
    	strcpy(lightPointer -> name , t1);


   	cfgfilePointer = cfgfilePointer->link;
    	cfgfilePointer = cfgfilePointer->link;

      

      /************************  Reading Start Light *************************/
      j=0;
      t1=strtok(cfgfilePointer -> line,")");
	t2=strtok(NULL, ")");
	t1=strtok(t1,"(");   
	for(t3=strtok(t1,",");t3!=NULL;t3=strtok(NULL,","))
      {
	    tmp[j]=atof(t3);
          j++;          
          }
       lightPointer -> startfrontlight.r = tmp[0];
       lightPointer -> startfrontlight.g = tmp[1];
       lightPointer -> startfrontlight.b = tmp[2];       

       j=0;
       t1=strtok(t2,"(");
       for(t4=strtok(t1,",");t4!=NULL;t4=strtok(NULL,","))
       {
	    tmp[j]=atof(t4);
          j++;          
          }
       
	 lightPointer -> startbacklight.r = tmp[0];
       lightPointer -> startbacklight.g = tmp[1];
       lightPointer -> startbacklight.b = tmp[2];       

	
	/************************  Reading End Light *************************/
	   
	cfgfilePointer = cfgfilePointer->link;
	j=0;
      t1=strtok(cfgfilePointer -> line,")");
	t2=strtok(NULL, ")");

      t1=strtok(t1,"(");   
      for(t3=strtok(t1,",");t3!=NULL;t3=strtok(NULL,","))
      {
	    tmp[j]=atof(t3);
          j++;          
          }
       lightPointer -> endfrontlight.r = tmp[0];
       lightPointer -> endfrontlight.g = tmp[1];
       lightPointer -> endfrontlight.b = tmp[2];       

       j=0;
       t1=strtok(t2,"(");
       for(t4=strtok(t1,",");t4!=NULL;t4=strtok(NULL,","))
       {
	    tmp[j]=atof(t4);
          j++;          
          }

       
	 lightPointer -> endbacklight.r = tmp[0];
       lightPointer -> endbacklight.g = tmp[1];
       lightPointer -> endbacklight.b = tmp[2];       
	
	 cfgfilePointer = cfgfilePointer->link;
	 cfgfilePointer = cfgfilePointer->link;
       if(strncmp(cfgfilePointer -> line, "SCRIPT", 6)==0) break;
       }

    free(t1);

    /************************** Displaying Light *********************/


    lightPointer = lightStartPointer;
    while(lightPointer != NULL)
    {
	printf("%s\n", lightPointer -> name);
	printf("LightStart=%lg,%lg,%lg,%lg,%lg,%lg\n", lightPointer -> startfrontlight.r, 
			lightPointer -> startfrontlight.g, lightPointer -> startfrontlight.b, lightPointer -> startbacklight.r, 
			lightPointer -> startbacklight.g, lightPointer -> startbacklight.b);
	printf("LightEnd=%lg,%lg,%lg,%lg,%lg,%lg\n", lightPointer -> endfrontlight.r, 
			lightPointer -> endfrontlight.g, lightPointer -> endfrontlight.b, lightPointer -> endbacklight.r, 
			lightPointer -> endbacklight.g, lightPointer -> endbacklight.b);
	lightPointer=lightPointer -> link;
	}



   
 /*************************  Reading SCRIPT  **************************/

   cfgfilePointer = cfgfilePointer -> link;
   scriptPointer = NULL;
   t1 = malloc(sizeof(char)*20);
   t2 = malloc(sizeof(char)*20);
   t3 = malloc(sizeof(char)*20);
   while(cfgfilePointer != NULL)
   {
      t1=strtok(cfgfilePointer -> line," ");
	if(scriptPointer != NULL) 
      {
	   	while (scriptPointer -> link != NULL)
	    		scriptPointer = scriptPointer -> link;
		scriptPointer -> link = (struct script *) malloc (sizeof (script));
      	scriptPointer = scriptPointer -> link;
		}
	else
	{
		scriptPointer = (struct script *) malloc (sizeof (script));
		scriptStartPointer = scriptPointer;
		}
    	scriptPointer -> actor = malloc(sizeof(char)*20);
    	scriptPointer -> link = NULL;
    	strcpy(scriptPointer -> actor , t1);

	t1=strtok(NULL," ");
	
	t2=strtok(NULL," ");
	scriptPointer -> pathStatus = malloc(sizeof(char)*20);
	strcpy(scriptPointer -> pathStatus,t2);
	
	
	t3=strtok(NULL," ");
	scriptPointer -> status = malloc(sizeof(char)*20);
	strcpy(scriptPointer -> status , t3);
	
	/*****************New Lines for IN and OUT ************************/
	t4=strtok(NULL, " ");
	t5=strtok(NULL, " ");
	if((t4==NULL)&&(t5==NULL))
	{
		scriptPointer -> IN = 0;
		scriptPointer -> OUT = 0;
		}
	else if((t4!=NULL)&&(t5!=NULL))
	{
		t4 = strtok(t4, "=");
		t4 = strtok (NULL, "=");
		scriptPointer -> IN = atof(t4);
		t5 = strtok(t5, "=");
		t5 = strtok(NULL, "=");
		scriptPointer -> OUT = atof(t5);
		}
	else
	{
		t4 = strtok(t4, "=");
		if((strcmp(t4, "IN")==0))
		{
			t4 = strtok(NULL, "=");
			scriptPointer -> IN = atof(t4);
			scriptPointer -> OUT = 0;
			}
		else if((strcmp(t4, "OUT")==0))
		{
			t4 = strtok(NULL, "=");
			scriptPointer -> OUT = atof(t4);
			scriptPointer -> IN = 0;
			}
		}
	/****************End of New Lines *********************************/

	t2=strtok(t1,"~");
	t3=strtok(NULL,"~");
	scriptPointer -> startFrame = atof(t2);
	scriptPointer -> endFrame = atof(t3);
        cfgfilePointer = cfgfilePointer->link;
      }

    /************************************ Displaying Script  **************************/
    scriptPointer = scriptStartPointer;
    while(scriptPointer != NULL)
    {
	printf("%s ", scriptPointer -> actor);
      printf("%d~%d,",scriptPointer -> startFrame,scriptPointer -> endFrame);
	printf("IN=%d, OUT=%d,",scriptPointer -> IN,scriptPointer -> OUT);
      printf("%s,%s\n",scriptPointer -> pathStatus,scriptPointer -> status);
	scriptPointer = scriptPointer -> link;
      }
printf("\n");
/*exit(0);*/
  
  /*******************  Put Script in Order based on Start Frame **********************/ 
  
  scriptPointer = scriptStartPointer;
  str=malloc(sizeof(char)*15);
  MaxFrame=0;
  while(scriptPointer != NULL)
  {
      currentValue = scriptPointer -> startFrame;
	nextScriptPointer = scriptPointer;
	while(nextScriptPointer != NULL)
	{
		nextValue = nextScriptPointer -> startFrame;
		if(currentValue > nextValue)
 		{
			strcpy(str,scriptPointer -> actor);
			strcpy(scriptPointer -> actor, nextScriptPointer -> actor);
			strcpy(nextScriptPointer -> actor, str);
			Value = scriptPointer -> startFrame;
			scriptPointer -> startFrame = nextScriptPointer -> startFrame;
			nextScriptPointer -> startFrame = Value;
			Value = scriptPointer -> endFrame;
			scriptPointer -> endFrame = nextScriptPointer -> endFrame;
			nextScriptPointer -> endFrame = Value;

			strcpy(str,scriptPointer -> pathStatus);
			strcpy(scriptPointer -> pathStatus, nextScriptPointer -> pathStatus);
			strcpy(nextScriptPointer -> pathStatus, str);
			
			strcpy(str,scriptPointer -> status);
			strcpy(scriptPointer -> status, nextScriptPointer -> status);
			strcpy(nextScriptPointer -> status, str);
			}
		nextScriptPointer = nextScriptPointer -> link;
		}
	if((scriptPointer -> endFrame > MaxFrame) && (scriptPointer -> endFrame != 9999))
		MaxFrame = scriptPointer -> endFrame;
      printf("Max=%d\n", MaxFrame);
	scriptPointer = scriptPointer -> link;
	}
    
     /************************************ Displaying Sorted Script  **************************/
    printf("\n");

    scriptPointer = scriptStartPointer;
    while(scriptPointer != NULL)
    {
	printf("%s ", scriptPointer -> actor);
      printf("%d~%d,",scriptPointer -> startFrame,scriptPointer -> endFrame);
      printf("%s,%s",scriptPointer -> pathStatus,scriptPointer -> status);
	/*if(strncmp(scriptPointer -> status, "IIIUY", strlen("IIIUY"))==0) printf("Equal\n");
	if(strncmp(scriptPointer -> pathStatus, "P123",strlen("P123"))==0) printf("PathEqual\n");*/
	scriptPointer = scriptPointer -> link;
      }
   printf("\n"); 
  
   /*exit(0);*/
   
  /********************		 Display Frame in Order  ********************/

   scriptPointer = scriptStartPointer;
   currentScriptPointer = scriptPointer;
   frameStartPointer = scriptPointer;
   endFramePointer = frameStartPointer;
   preFrameFlag=0;
   for(i=1; i<MaxFrame+1; i++)
   {	
	if(i==1)
	{
	   for(;;)
	   {
		if(scriptPointer != NULL) 
		{
		   currentScriptPointer = scriptPointer;
		   if((i >= scriptPointer -> startFrame)&& (i <= scriptPointer -> endFrame)) endFramePointer = scriptPointer;
		   else { endFramePointer -> link = NULL; break;}
		   }
		else break;
		scriptPointer = scriptPointer -> link;
		}
	   }

	else 
	{
	   /******************** Link Previous Frame ********************/

	   currentFramePointer = frameStartPointer;
	   while(currentFramePointer != NULL)
	   {
		if((i >= currentFramePointer -> startFrame)&& (i <= currentFramePointer -> endFrame))
		{
			preFrameFlag=1;
			endFramePointer = currentFramePointer;
			if(currentFramePointer -> link == NULL) endFramePointer -> link = NULL;	
			}
		else 
       	{
			if (frameStartPointer == currentFramePointer) frameStartPointer = currentFramePointer ->link;
			else endFramePointer -> link = currentFramePointer -> link;	
			}
		currentFramePointer = currentFramePointer -> link;
		}
		

           	/******************** Link Current Frame ********************/
		scriptPointer = currentScriptPointer;
		for(;;)
		{	
			if((scriptPointer != NULL) && (scriptPointer != endFramePointer))
			{
			   currentScriptPointer = scriptPointer;
			   if((i >= scriptPointer -> startFrame) && (i <= scriptPointer -> endFrame))
	   		   {
				if(preFrameFlag==1){endFramePointer -> link = scriptPointer; endFramePointer = scriptPointer;}
				}
	   		   else {endFramePointer -> link = NULL; preFrameFlag=0; break;}
			   }
			else break;
			scriptPointer = scriptPointer -> link;
	   		}  
		}	
     
	

	/******************** 	Display Frame 	********************/

	stkFile=fopen("stktemp.stk","w");
         if(stkFile==NULL)
	   {
          printf("\nwrite file error\n");
          exit(1);
	   }
	stack = ir_stack_Make();

	printf("\ni=%d\n",i);

	scriptPointer = frameStartPointer;
	while(scriptPointer != NULL)
	{	
		if(strncmp(scriptPointer -> actor, "camera", strlen("camera"))==0)
		{
			cameraScriptPointer = scriptPointer;
			break;
			}
		scriptPointer = scriptPointer -> link;
		}

	

	/**********    qmqmqmqmqm      **************/


	/**********    qmqmqmqmqmqm    ************/

	pathPointer = pathStartPointer;
	while(pathPointer != NULL)
	 {
	   if(strncmp(cameraScriptPointer -> pathStatus, pathPointer -> name, strlen(cameraScriptPointer->pathStatus))==0)  
	     {
	       /*  printf("\nstop!\n");*/
	       startcam.position=pathPointer -> startposition;
	       startcam.direction=pathPointer -> startdirection;
	       startcam.up=pathPointer -> startup;
	       endcam.position=pathPointer -> endposition;
	       endcam.direction=pathPointer -> enddirection;
	       endcam.up=pathPointer -> endup;
	   	printf("\nPathStart=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", pathPointer->startposition.x, 
		pathPointer->startposition.y, pathPointer->startposition.z, pathPointer->startdirection.x, 
		pathPointer->startdirection.y, pathPointer->startdirection.z, pathPointer->startup);

		printf("PathEnd=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", pathPointer->endposition.x, 
		pathPointer->endposition.y, pathPointer->endposition.z, pathPointer->enddirection.x, 
		pathPointer->enddirection.y, pathPointer->enddirection.z, pathPointer->endup);
	   	printf("StartCam=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", startcam.position.x, 
		startcam.position.y, startcam.position.z, startcam.direction.x, 
		startcam.direction.y, startcam.direction.z, startcam.up);

		printf("EndCam=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", endcam.position.x, 
		endcam.position.y, endcam.position.z, endcam.direction.x, 
		endcam.direction.y, endcam.direction.z, endcam.up);

	     }
	   pathPointer = pathPointer -> link;
	 }

	/*exit(0);*/

		/*	printf("\ni=%d\n",i);

			if(i==1) printf("\nstop here\n");*/


	cameraPointer = cameraStartPointer;
	while(cameraPointer != NULL)
	 {
	   if(strncmp(cameraScriptPointer -> status, cameraPointer -> name, strlen(cameraScriptPointer -> status))==0) 
	     {
	       startcam.zoom=cameraPointer -> startfocallength;
	       startcam.focus=cameraPointer -> startfocus;
	       endcam.zoom=cameraPointer -> endfocallength;
	       endcam.focus=cameraPointer -> endfocus;

	  printf("\ncameraPointer -> startfocallength=%lg\n",cameraPointer -> startfocallength);
	     printf("\ncameraPointer -> endfocallength=%lg\n",cameraPointer -> endfocallength);

     printf("\nstartzoom=%lg\n",startcam.zoom);
     printf("\nendzoom=%lg\n",endcam.zoom);

	     }
	   cameraPointer = cameraPointer -> link;
	 }

	/*exit(0);*/
	/*  printf("\ncameraPointer -> startfocallength=%lg\n",cameraPointer -> startfocallength);
	     printf("\ncameraPointer -> endfocallength=%lg\n",cameraPointer -> endfocallength);

     printf("\nstartzoom=%lg\n",startcam.zoom);
     printf("\nendzoom=%lg\n",endcam.zoom);*/

                   /************   Dealing with direction   ************/

       if((startcam.direction.y!=0)&&(startcam.direction.z!=0)) 
	 {
	   if(startcam.direction.y>0)
	     temp1=acos(-startcam.direction.z/sqrt(startcam.direction.y*startcam.direction.y+startcam.direction.z*startcam.direction.z));
	   if(startcam.direction.y<0)
	     temp1=-acos(-startcam.direction.z/sqrt(startcam.direction.y*startcam.direction.y+startcam.direction.z*startcam.direction.z));
	 }
       else if(startcam.direction.y==0)
	 {
	   /* if(start.direction.z<=0)*/
	   temp1=0;
	   /* else temp1=3.1415926;*/}
       else
	 {
	   if(startcam.direction.y>0)
	     temp1=3.1415926/2;
	   else temp1=-3.1415926/2;
	 }

       if((endcam.direction.y!=0)&&(endcam.direction.z!=0)) 
	 {
	   if(endcam.direction.y>0)
	     temp2=acos(-endcam.direction.z/sqrt(endcam.direction.y*endcam.direction.y+endcam.direction.z*endcam.direction.z));
	   if(endcam.direction.y<0)
	     temp2=-acos(-endcam.direction.z/sqrt(endcam.direction.y*endcam.direction.y+endcam.direction.z*endcam.direction.z));
	 }
       else if(endcam.direction.y==0)
	 {
	   /*  if(end.direction.z<=0)*/
	   temp2=0;
	   /* else temp2=3.1415926;*/}
       else
	 {
	   if(endcam.direction.y>0)
	     temp2=3.1415926/2;
	   else temp2=-3.1415926/2;
	 }

       v=sqrt(startcam.direction.y*startcam.direction.y+startcam.direction.z*startcam.direction.z);
       l=sqrt(startcam.direction.x*startcam.direction.x+startcam.direction.y*startcam.direction.y+startcam.direction.z*startcam.direction.z);
       if((startcam.direction.x!=0)&&(v!=0))
	 {
	   if(startcam.direction.x>0)
	     {
	       if(startcam.direction.z>0) temp3=-acos(-v/l);
	       else temp3=-acos(v/l);}
	   if(startcam.direction.x<0)
	     {
	       if(startcam.direction.z>0) temp3=acos(-v/l);
	       else temp3=acos(v/l);}
	 }
       else if(startcam.direction.x==0)
	 {
	   if(startcam.direction.z<=0)
	     temp3=0;
	   else temp3=3.1415926;}
       else
	 {
	   if(startcam.direction.x>0)
	     temp3=-3.1415926/2;
	   else temp3=3.1415926/2;
	 }
       v=sqrt(endcam.direction.y*endcam.direction.y+endcam.direction.z*endcam.direction.z);
       l=sqrt(endcam.direction.x*endcam.direction.x+endcam.direction.y*endcam.direction.y+endcam.direction.z*endcam.direction.z);
       printf("v=%lg, l=%lg\n",v,l);
       if((endcam.direction.x!=0)&&(v!=0))
	 {
	   if(endcam.direction.x>0)
	     {
	       if(endcam.direction.z>0) temp4=-acos(-v/l);
	       else temp4=-acos(v/l);}
	   if(endcam.direction.x<0)
	     {
	       if(endcam.direction.z>0) temp4=acos(-v/l);
	       else temp4=acos(v/l);}
	 }
       else if(endcam.direction.x==0)
	 {
	   if(endcam.direction.z<=0)
	     temp4=0;
	   else temp4=3.1415926;
	 }
       else
	 {
	   if(endcam.direction.x>0)
	     temp4=-3.1415926/2;
	   else temp4=3.1415926/2;
	 }

       total1=temp2-temp1;
       total2=temp4-temp3;
       if(total1==3.1415926) total1=0;
       /*if(total2==3.1415926) total2=0;*/
       if(total1>3.1415926) total1=total1-2*3.1415926;
       if(total1<-3.1415926) total1=2*3.1415926+total1;
       if(total2>3.1415926) total2=total2-2*3.1415926;
       if(total2<-3.1415926) total2=2*3.1415926+total1;


       printf("j=%d\n",j);
       printf("startx=%lg\n",temp1);
       printf("endx=%lg\n",temp2);
       printf("starty=%lg\n",temp3);
       printf("endy=%lg\n",temp4);
       printf("total1=%lg, total2=%lg\n",total1,total2);

                /************   End of dealing with direction   ************/

      printf("cameraScriptPointer->startFrame=%d\n",cameraScriptPointer->startFrame);
      printf("cameraScriptPointer->endFrame=%d\n",cameraScriptPointer->endFrame);
      printf("cameraScriptPointer->IN=%d\n",cameraScriptPointer->IN);
      printf("cameraScriptPointer->OUT=%d\n",cameraScriptPointer->OUT);

      if(cameraScriptPointer->IN==0){
	a=0.0;}
      else{
	a=1.0*(cameraScriptPointer->IN-1)/(1.0*(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame));}
      if(cameraScriptPointer->OUT==0){
	b=1.0;}
      else{
	b=1.0*(cameraScriptPointer->endFrame-cameraScriptPointer->OUT-cameraScriptPointer->startFrame+1)/(1.0*(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame));}
      t=1.0*(i-cameraScriptPointer->startFrame)/(1.0*(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame));

    printf("\na=%lg, b=%lg, t=%lg\n", a,b,t);

       if(i<(cameraScriptPointer->startFrame+cameraScriptPointer->IN))

	 {

	     printf("Qi Man 1!\n");
	   /*
     midcam.position.x=(endcam.position.x-startcam.position.x)*(cameraScriptPointer->IN-1)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)+startcam.position.x;
     midcam.position.y=(endcam.position.y-startcam.position.y)*(cameraScriptPointer->IN-1)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)+startcam.position.y;
     midcam.position.z=(endcam.position.z-startcam.position.z)*(cameraScriptPointer->IN-1)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)+startcam.position.z;
     currentcamangle.x=total1*(i-cameraScriptPointer->startFrame)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)+temp1;
     currentcamangle.y=total2*(i-cameraScriptPointer->startFrame)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)+temp3;  
     currentcam.up=(endcam.up-startcam.up)*(i-cameraScriptPointer->startFrame)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)+startcam.up;
     currentcam.zoom=(endcam.zoom-startcam.zoom)*(i-cameraScriptPointer->startFrame)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)+startcam.zoom;
     currentcam.focus=(endcam.focus-startcam.focus)*(i-cameraScriptPointer->startFrame)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)+startcam.focus;
	   */

	     /*  mid=(sin(1.57*(i-cameraScriptPointer->startFrame)/(cameraScriptPointer->IN-1)-1.57)+1);
	      mid=((i-cameraScriptPointer->startFrame)/(cameraScriptPointer->IN-1)-1);
	      printf("\nmid=%lg\n",mid);*/

     currentcam.position.x=2*(endcam.position.x-startcam.position.x)/(b-a+1)*t*t/(2*a)+startcam.position.x;
     currentcam.position.y=2*(endcam.position.y-startcam.position.y)/(b-a+1)*t*t/(2*a)+startcam.position.y;
     currentcam.position.z=2*(endcam.position.z-startcam.position.z)/(b-a+1)*t*t/(2*a)+startcam.position.z;
     currentcamangle.x=2*total1/(b-a+1)*t*t/(2*a)+temp1;
     currentcamangle.y=2*total2/(b-a+1)*t*t/(2*a)+temp3;  
     currentcam.up=2*(endcam.up-startcam.up)/(b-a+1)*t*t/(2*a)+startcam.up;

     /*     mid=(endcam.zoom-startcam.zoom)*(cameraScriptPointer->IN-1)/(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame)*(sin(((i-cameraScriptPointer->startFrame)/(cameraScriptPointer->IN-1)-1)*1.57)+1);
     printf("\nstartzoom=%lg\n",startcam.zoom);
     printf("\nendzoom=%lg\n",endcam.zoom);
     printf("\nmid=%lg\n",mid);
     */

     currentcam.zoom=2*(endcam.zoom-startcam.zoom)/(b-a+1)*t*t/(2*a)+startcam.zoom;
     currentcam.focus=2*(endcam.focus-startcam.focus)/(b-a+1)*t*t/(2*a)+startcam.focus;
    
	 }

       if((i>=(cameraScriptPointer->startFrame+cameraScriptPointer->IN))&&(i<=(cameraScriptPointer->endFrame-cameraScriptPointer->OUT)))

	 {
	   
	   printf("\nQi Man 2!\n");

	   	printf("StartCam=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", startcam.position.x, 
		       startcam.position.y, startcam.position.z, startcam.direction.x, 
		       startcam.direction.y, startcam.direction.z, startcam.up);
		printf("EndCam=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", endcam.position.x, 
		       endcam.position.y, endcam.position.z, endcam.direction.x, 
		       endcam.direction.y, endcam.direction.z, endcam.up);
		printf("\na=%lg, b=%lg, t=%lg\n", a, b, t);
		
		printf("Maozhen b-a+1=%lg\n", b-a+1);
		li1=a*(endcam.zoom-startcam.zoom)/(b-a+1);
		li2=(t-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1);		
		li3=startcam.zoom;

		/*qiman=a*(endcam.zoom-startcam.zoom)/(b-a+1)+(t-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1)+startcam.zoom;*/
		/*     currentcam.zoom= a*(endcam.zoom-startcam.zoom)/(b-a+1)+(t-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1)+startcam.zoom; */
		/*qiman=li1+li2+li3;*/
		if((b-a+1)==0) {printf("Error happened\n");exit(1);}
		/*currentcam.zoom = a*(endcam.zoom-startcam.zoom)/(b-a+1)+(t-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1)+startcam.zooma*/;

		currentcam.zoom=li1+li2+li3;
		printf("\nqiman=%lf, CurrentcamZoom=%lf\n", qiman, currentcam.zoom);
		currentcam.focus=a*(endcam.focus-startcam.focus)/(b-a+1)+(t-a)*2*(endcam.focus-startcam.focus)/(b-a+1)+startcam.focus;	
		currentcam.up=a*(endcam.up-startcam.up)/(b-a+1)+(t-a)*2*(endcam.up-startcam.up)/(b-a+1)+startcam.up;
		
		currentcam.position.x=a*(endcam.position.x-startcam.position.x)/(b-a+1)+(t-a)*2*(endcam.position.x-startcam.position.x)/(b-a+1)+startcam.position.x;
		currentcam.position.y=a*(endcam.position.y-startcam.position.y)/(b-a+1)+(t-a)*2*(endcam.position.y-startcam.position.y)/(b-a+1)+startcam.position.y;
		currentcam.position.z=a*(endcam.position.z-startcam.position.z)/(b-a+1)+(t-a)*2*(endcam.position.z-startcam.position.z)/(b-a+1)+startcam.position.z;
		currentcamangle.x=a*total1/(b-a+1)+(t-a)*2*total1/(b-a+1)+temp1;
		currentcamangle.y=a*total2/(b-a+1)+(t-a)*2*total2/(b-a+1)+temp3;  
		
	 }

       if(i>(cameraScriptPointer->endFrame-cameraScriptPointer->OUT))

	 {

	     printf("\nQi Man 3!\n");

     currentcam.position.x=a*(endcam.position.x-startcam.position.x)/(b-a+1)+(b-a)*2*(endcam.position.x-startcam.position.x)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.position.x-startcam.position.x)/(b-a+1)/(1-b)+startcam.position.x;
     currentcam.position.y=a*(endcam.position.y-startcam.position.y)/(b-a+1)+(b-a)*2*(endcam.position.y-startcam.position.y)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.position.y-startcam.position.y)/(b-a+1)/(1-b)+startcam.position.y;
     currentcam.position.z=a*(endcam.position.z-startcam.position.z)/(b-a+1)+(b-a)*2*(endcam.position.z-startcam.position.z)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.position.z-startcam.position.z)/(b-a+1)/(1-b)+startcam.position.z;
     currentcamangle.x=a*total1/(b-a+1)+(b-a)*2*total1/(b-a+1)+(t-t*t/2-b+b*b/2)*2*total1/(b-a+1)/(1-b)+temp1;
     currentcamangle.y=a*total2/(b-a+1)+(b-a)*2*total2/(b-a+1)+(t-t*t/2-b+b*b/2)*2*total2/(b-a+1)/(1-b)+temp3; 
     currentcam.up=a*(endcam.up-startcam.up)/(b-a+1)+(b-a)*2*(endcam.up-startcam.up)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.up-startcam.up)/(b-a+1)/(1-b)+startcam.up;
     currentcam.zoom=a*(endcam.zoom-startcam.zoom)/(b-a+1)+(b-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.zoom-startcam.zoom)/(b-a+1)/(1-b)+startcam.zoom;
     currentcam.focus=a*(endcam.focus-startcam.focus)/(b-a+1)+(b-a)*2*(endcam.focus-startcam.focus)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.focus-startcam.focus)/(b-a+1)/(1-b)+startcam.focus;

	 }

     printf("\nCurrentcamPosition=%lg,%lg,%lg\n", currentcam.position.x,currentcam.position.y,currentcam.position.z);
     printf("\nCurrentcamAngle=%lg,%lg\n", currentcamangle.x,currentcamangle.y);
     printf("\nCurrentcamUp=%lg\n", currentcam.up);
     printf("\nCurrentcamZoom=%lg\n", currentcam.zoom);
     printf("\nCurrentcamFocus=%d\n", currentcam.focus);


	ir_stack_SetCOP(stack, currentcam.zoom);




	/**********    qmqmqmqmqmqm    ************/


	/**********    qmqmqmqmqm      **************/


	scriptPointer = frameStartPointer;
	while(scriptPointer != NULL)
	{
		if(scriptPointer != cameraScriptPointer)
		{
			printf("\nFrame=%d, %s ", i, scriptPointer -> actor);
      		printf("%d~%d,",scriptPointer -> startFrame,scriptPointer -> endFrame);
      		printf("%s,%s",scriptPointer -> pathStatus,scriptPointer -> status);


		/***** qmqmqm   ******/

		
		/********    qmqmqm     *******/

        fp=fopen(scriptPointer->actor, "r");
        if(fp==NULL)
         {
            printf("\n Open a Cel File:%s Error",scriptPointer->actor);
            exit(1);
            }

        if (ir_stkfile_Load(fp)==0) 
        {
           fclose(fp);
           c=ir_cel_Make();
           c=ir_stack_GetTopCel(ir_newStack);
           }
        else 
        {
           fprintf(stderr,"show: Error in load file.\n");
           fclose(fp);
           return -1;
           }


	pathPointer = pathStartPointer;
	while(pathPointer != NULL)
	 {
	   if(strncmp(scriptPointer->pathStatus, pathPointer->name, strlen(scriptPointer->pathStatus))==0)  
	     {
	       startcel.position=pathPointer->startposition;
	       startcel.direction=pathPointer->startdirection;
	       startcel.up=pathPointer->startup;
	       endcel.position=pathPointer->endposition;
	       endcel.direction=pathPointer->enddirection;
	       endcel.up=pathPointer->endup;
	   	printf("\nPathStart=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", pathPointer->startposition.x, 
		pathPointer->startposition.y, pathPointer->startposition.z, pathPointer->startdirection.x, 
		pathPointer->startdirection.y, pathPointer->startdirection.z, pathPointer->startup);

		printf("PathEnd=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", pathPointer->endposition.x, 
		pathPointer->endposition.y, pathPointer->endposition.z, pathPointer->enddirection.x, 
		pathPointer->enddirection.y, pathPointer->enddirection.z, pathPointer->endup);
	   	printf("StartCel=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", startcel.position.x, 
		startcel.position.y, startcel.position.z, startcel.direction.x, 
		startcel.direction.y, startcel.direction.z, startcel.up);

		printf("EndCel=%lg,%lg,%lg,%lg,%lg,%lg,%lg\n", endcel.position.x, 
		endcel.position.y, endcel.position.z, endcel.direction.x, 
		endcel.direction.y, endcel.direction.z, endcel.up);

	     }
	   pathPointer = pathPointer -> link;
	 }

	lightPointer = lightStartPointer;
	while(lightPointer != NULL)
	 {
	   if(strncmp(scriptPointer->status, lightPointer->name, strlen(scriptPointer->status))==0)  
	     {
	       startcel.frontlight=lightPointer->startfrontlight;
	       startcel.backlight=lightPointer->startbacklight;
	       endcel.frontlight=lightPointer->endfrontlight;
	       endcel.backlight=lightPointer->endbacklight;
	     }
	   lightPointer = lightPointer -> link;
	 }

       /************   Dealing with direction   ************/

       if((startcel.direction.y!=0)&&(startcel.direction.z!=0)) 
	 {
	   if(startcel.direction.y>0)
	     temp1=acos(-startcel.direction.z/sqrt(startcel.direction.y*startcel.direction.y+startcel.direction.z*startcel.direction.z));
	   if(startcel.direction.y<0)
	     temp1=-acos(-startcel.direction.z/sqrt(startcel.direction.y*startcel.direction.y+startcel.direction.z*startcel.direction.z));
	 }
       else if(startcel.direction.y==0)
	 {
	   temp1=0;
	   }
       else
	 {
	   if(startcel.direction.y>0)
	     temp1=3.1415926/2;
	   else temp1=-3.1415926/2;
	 }

       if((endcel.direction.y!=0)&&(endcel.direction.z!=0)) 
	 {
	   if(endcel.direction.y>0)
	     temp2=acos(-endcel.direction.z/sqrt(endcel.direction.y*endcel.direction.y+endcel.direction.z*endcel.direction.z));
	   if(endcel.direction.y<0)
	     temp2=-acos(-endcel.direction.z/sqrt(endcel.direction.y*endcel.direction.y+endcel.direction.z*endcel.direction.z));
	 }
       else if(endcel.direction.y==0)
	 {
	   temp2=0;
	   }
       else
	 {
	   if(endcel.direction.y>0)
	     temp2=3.1415926/2;
	   else temp2=-3.1415926/2;
	 }

       v=sqrt(startcel.direction.y*startcel.direction.y+startcel.direction.z*startcel.direction.z);
       l=sqrt(startcel.direction.x*startcel.direction.x+startcel.direction.y*startcel.direction.y+startcel.direction.z*startcel.direction.z);
       if((startcel.direction.x!=0)&&(v!=0))
	 {
	   if(startcel.direction.x>0)
	     {
	       if(startcel.direction.z>0) temp3=-acos(-v/l);
	       else temp3=-acos(v/l);}
	   if(startcel.direction.x<0)
	     {
	       if(startcel.direction.z>0) temp3=acos(-v/l);
	       else temp3=acos(v/l);}
	 }
       else if(startcel.direction.x==0)
	 {
	   if(startcel.direction.z<=0)
	     temp3=0;
	   else temp3=3.1415926;}
       else
	 {
	   if(startcel.direction.x>0)
	     temp3=-3.1415926/2;
	   else temp3=3.1415926/2;
	 }
       v=sqrt(endcel.direction.y*endcel.direction.y+endcel.direction.z*endcel.direction.z);
       l=sqrt(endcel.direction.x*endcel.direction.x+endcel.direction.y*endcel.direction.y+endcel.direction.z*endcel.direction.z);
       printf("v=%lg, l=%lg\n",v,l);
       if((endcel.direction.x!=0)&&(v!=0))
	 {
	   if(endcel.direction.x>0)
	     {
	       if(endcel.direction.z>0) temp4=-acos(-v/l);
	       else temp4=-acos(v/l);}
	   if(endcel.direction.x<0)
	     {
	       if(endcel.direction.z>0) temp4=acos(-v/l);
	       else temp4=acos(v/l);}
	 }
       else if(endcel.direction.x==0)
	 {
	   if(endcel.direction.z<=0)
	     temp4=0;
	   else temp4=3.1415926;
	 }
       else
	 {
	   if(endcel.direction.x>0)
	     temp4=-3.1415926/2;
	   else temp4=3.1415926/2;
	 }

       total1=temp2-temp1;
       total2=temp4-temp3;
       if(total1==3.1415926) total1=0;
       if(total1>3.1415926) total1=total1-2*3.1415926;
       if(total1<-3.1415926) total1=2*3.1415926+total1;
       if(total2>3.1415926) total2=total2-2*3.1415926;
       if(total2<-3.1415926) total2=2*3.1415926+total1;


       printf("j=%d\n",j);
       printf("startx=%lg\n",temp1);
       printf("endx=%lg\n",temp2);
       printf("starty=%lg\n",temp3);
       printf("endy=%lg\n",temp4);
       printf("total1=%lg, total2=%lg\n",total1,total2);

       /************   End of dealing with direction   ************/

       if(scriptPointer->IN==0)
       a=0.0;
else
      a=1.0*(scriptPointer->IN-1)/(1.0*(scriptPointer->endFrame-scriptPointer->startFrame));
       if(scriptPointer->OUT==0)
       b=1.0;
else
      b=1.0*(scriptPointer->endFrame-scriptPointer->OUT-scriptPointer->startFrame+1)/(1.0*(scriptPointer->endFrame-scriptPointer->startFrame));
      t=1.0*(i-scriptPointer->startFrame)/(1.0*(scriptPointer->endFrame-scriptPointer->startFrame));

      printf("\na=%lg, b=%lg, t=%lg\n", a,b,t);


       if(i<(scriptPointer->startFrame+scriptPointer->IN))

	 {

	   printf("\nMan Qi 1!\n");

     currentcel.position.x=2*(endcel.position.x-startcel.position.x)/(b-a+1)*t*t/(2*a)+startcel.position.x;
     currentcel.position.y=2*(endcel.position.y-startcel.position.y)/(b-a+1)*t*t/(2*a)+startcel.position.y;
     currentcel.position.z=2*(endcel.position.z-startcel.position.z)/(b-a+1)*t*t/(2*a)+startcel.position.z;
     currentcelangle.x=2*total1/(b-a+1)*t*t/(2*a)+temp1;
     currentcelangle.y=2*total2/(b-a+1)*t*t/(2*a)+temp3; 
     currentcel.up=2*(endcel.up-startcel.up)/(b-a+1)*t*t/(2*a)+startcel.up;
     currentcel.frontlight.r=2*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)*t*t/(2*a)+startcel.frontlight.r;
     currentcel.frontlight.g=2*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)*t*t/(2*a)+startcel.frontlight.g;
     currentcel.frontlight.b=2*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)*t*t/(2*a)+startcel.frontlight.b;
     currentcel.backlight.r=2*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)*t*t/(2*a)+startcel.backlight.r;
     currentcel.backlight.g=2*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)*t*t/(2*a)+startcel.backlight.g;
     currentcel.backlight.b=2*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)*t*t/(2*a)+startcel.backlight.b;   
    
	 }

       if((i>=(scriptPointer->startFrame+scriptPointer->IN))&&(i<=(scriptPointer->endFrame-scriptPointer->OUT)))

	 {
	   printf("\nMan Qi 2!\n");

     currentcel.position.x=a*(endcel.position.x-startcel.position.x)/(b-a+1)+(t-a)*2*(endcel.position.x-startcel.position.x)/(b-a+1)+startcel.position.x;
     currentcel.position.y=a*(endcel.position.y-startcel.position.y)/(b-a+1)+(t-a)*2*(endcel.position.y-startcel.position.y)/(b-a+1)+startcel.position.y;
     currentcel.position.z=a*(endcel.position.z-startcel.position.z)/(b-a+1)+(t-a)*2*(endcel.position.z-startcel.position.z)/(b-a+1)+startcel.position.z;
     currentcelangle.x=a*total1/(b-a+1)+(t-a)*2*total1/(b-a+1)+temp1;
     currentcelangle.y=a*total2/(b-a+1)+(t-a)*2*total1/(b-a+1)+temp3;
     currentcel.up=a*(endcel.up-startcel.up)/(b-a+1)+(t-a)*2*(endcel.up-startcel.up)/(b-a+1)+startcel.up;
     currentcel.frontlight.r=a*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)+(t-a)*2*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)+startcel.frontlight.r;
     currentcel.frontlight.g=a*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)+(t-a)*2*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)+startcel.frontlight.g;
     currentcel.frontlight.b=a*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)+(t-a)*2*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)+startcel.frontlight.b;
     currentcel.backlight.r=a*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)+(t-a)*2*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)+startcel.backlight.r;
     currentcel.backlight.g=a*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)+(t-a)*2*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)+startcel.backlight.g;
     currentcel.backlight.b=a*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)+(t-a)*2*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)+startcel.backlight.b;


	 }

       if(i>(scriptPointer->endFrame-scriptPointer->OUT))

	 {
	   printf("\nMan Qi 3!\n");

     currentcel.position.x=a*(endcel.position.x-startcel.position.x)/(b-a+1)+(b-a)*2*(endcel.position.x-startcel.position.x)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.position.x-startcel.position.x)/(b-a+1)/(1-b)+startcel.position.x;
     currentcel.position.y=a*(endcel.position.y-startcel.position.y)/(b-a+1)+(b-a)*2*(endcel.position.y-startcel.position.y)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.position.y-startcel.position.y)/(b-a+1)/(1-b)+startcel.position.y;
     currentcel.position.z=a*(endcel.position.z-startcel.position.z)/(b-a+1)+(b-a)*2*(endcel.position.z-startcel.position.z)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.position.z-startcel.position.z)/(b-a+1)/(1-b)+startcel.position.z;
     currentcelangle.x=a*total1/(b-a+1)+(b-a)*2*total1/(b-a+1)+(t-t*t/2-b+b*b/2)*2*total1/(b-a+1)/(1-b)+temp1;
     currentcelangle.y=a*total2/(b-a+1)+(b-a)*2*total2/(b-a+1)+(t-t*t/2-b+b*b/2)*2*total2/(b-a+1)/(1-b)+temp3;
     currentcel.up=a*(endcel.up-startcel.up)/(b-a+1)+(b-a)*2*(endcel.up-startcel.up)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.up-startcel.up)/(b-a+1)/(1-b)+startcel.up;
     currentcel.frontlight.r=a*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)+(b-a)*2*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)/(1-b)+startcel.frontlight.r;
     currentcel.frontlight.g=a*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)+(b-a)*2*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)/(1-b)+startcel.frontlight.g;
     currentcel.frontlight.b=a*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)+(b-a)*2*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)/(1-b)+startcel.frontlight.b;
     currentcel.backlight.r=a*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)+(b-a)*2*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)/(1-b)+startcel.backlight.r;
     currentcel.backlight.g=a*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)+(b-a)*2*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)/(1-b)+startcel.backlight.g;
     currentcel.backlight.b=a*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)+(b-a)*2*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)/(1-b)+startcel.backlight.b;


	 }



     printf("\nCurrentcelPosition=%lg,%lg,%lg\n", currentcel.position.x,currentcel.position.y,currentcel.position.z);
     printf("\nCurrentcelAngle=%lg,%lg\n", currentcelangle.x,currentcelangle.y);
     printf("\nCurrentcelUp=%lg\n", currentcel.up);
     printf("\nCurrentcelFrontlight=%lg,%lg,%lg\n", currentcel.frontlight.r,currentcel.frontlight.g,currentcel.frontlight.b);
     printf("\nCurrentcelBacklight=%lg,%lg,%lg\n", currentcel.backlight.r,currentcel.backlight.g,currentcel.backlight.b);

	   celRotationX=(currentcamangle.x)*(-1);
	   celRotationY=(currentcamangle.y)*(-1);
           celRotationZ=(currentcam.up)*3.1415926/180*(-1);

           celTranslationX=cos(-celRotationY)*cos(-celRotationZ)*(currentcel.position.x-currentcam.position.x)+(cos(-celRotationX)*sin(-celRotationZ)+sin(-celRotationX)*sin(-celRotationY)*cos(-celRotationZ))*(currentcel.position.y-currentcam.position.y)+(sin(-celRotationX)*sin(-celRotationZ)-cos(-celRotationX)*sin(-celRotationY)*cos(-celRotationZ))*(currentcel.position.z-currentcam.position.z);
           celTranslationY=-cos(-celRotationY)*sin(-celRotationZ)*(currentcel.position.x-currentcam.position.x)+(cos(-celRotationX)*cos(-celRotationZ)-sin(-celRotationX)*sin(-celRotationY)*sin(-celRotationZ))*(currentcel.position.y-currentcam.position.y)+(sin(-celRotationX)*cos(-celRotationZ)+cos(-celRotationX)*sin(-celRotationY)*sin(-celRotationZ))*(currentcel.position.z-currentcam.position.z);
           celTranslationZ=sin(-celRotationY)*(currentcel.position.x-currentcam.position.x)-sin(-celRotationX)*cos(-celRotationY)*(currentcel.position.y-currentcam.position.y)+cos(-celRotationX)*cos(-celRotationY)*(currentcel.position.z-currentcam.position.z)+currentcam.zoom;

	   /* printf("\nI am here!\n");*/

	   ir_cel_SetDepth(c, 0);

	   celRotationX=(currentcamangle.x-currentcelangle.x)*(-1);
	   celRotationY=(currentcamangle.y-currentcelangle.y)*(-1);
           celRotationZ=(currentcam.up-currentcel.up)*3.1415926/180*(-1);


	   printf("\nceltranslationx=%lg\n",celTranslationX);
	   printf("\nceltranslationy=%lg\n",celTranslationY);
	   printf("\nceltranslationz=%lg\n",celTranslationZ);
	   printf("\ncelrotationx=%lg\n",celRotationX);
	   printf("\ncelrotationy=%lg\n",celRotationY);
	   printf("\ncelrotationz=%lg\n",celRotationZ);




	   /*printf("\nI am here!\n");*/

	   ir_matrix_Construct4(IR_ROTATEX, celRotationX, 0.0, 0.0, celRotateX);
	   ir_matrix_Construct4(IR_ROTATEY, celRotationY, 0.0, 0.0, celRotateY);
    ir_matrix_Mul4(celRotateX, celRotateY, celRotateY);
    ir_matrix_Construct4(IR_ROTATEZ, celRotationZ, 0.0, 0.0, celRotateZ);
    ir_matrix_Mul4(celRotateY, celRotateZ, celRotate);
    ir_matrix_Construct4(IR_TRANSLATE, celTranslationX, celTranslationY, celTranslationZ, celTranslate);
    ir_matrix_Mul4(celRotate, celTranslate, celTransform);

    ir_cel_Set3DTransform(c, celTransform);
    

    /*printf("\nI am here!\n");*/

    ir_cel_Light(currentcel.frontlight, &celfrontLight);

   
    ir_cel_Light(currentcel.backlight, &celbackLight);



    ir_cel_SetFrontLight(c, &celfrontLight); 
    ir_cel_SetBackLight(c, &celbackLight); 


    ir_cel_SetFocus(c, currentcam.focus);




    (void)ir_stack_AddCel(stack, c, IR_ADD_ON_TOP, 0);

    /* ir_cel_Delete(c);*/	

     		/********    qmqmqm     *******/


		
		/***** qmqmqm   ******/


			}
		scriptPointer = scriptPointer -> link;
		}

	/*exit(0);	*/         
	


	/*********    qm       ********/

	/*** qm *****/

	/*printf("\nI am here!\n");*/

     ir_stack_Save(stack, stkFile);

     /*printf("\nI am here!\n");*/

     fclose(stkFile);

     /*printf("\nI am here!\n");*/

     /* ir_stack_Delete(stack);*/

     /*printf("\nI am here!\n");*/

/*** Create image data structure ***/
  iminfo = ir_imageinfo_Make(Width, Height);

  if (StartingLine!=0 || EndingLine!=Height-1)
    ir_imageinfo_SetSELines(iminfo, StartingLine, EndingLine);

  ir_imageinfo_SetAntiAliasing(iminfo,
		    SuperSamplingFactor==1?IR_AA_NONE:IR_AA_SUPERSAMPLING_BOX,
		    SuperSamplingFactor,SuperSamplingFactor);

  /*printf("\nI am here!\n");*/

fp = fopen("mystack.stk", "r");

  if (fp == NULL) {
    fprintf(stderr,"irend error: Couldn't open file %s.\n", "stktemp.stk");
    return -1;
  }

  /*printf("\nI am here!\n");*/
  
  if (ir_stkfile_Load(fp)==0) { 

    fclose(fp);

    /* printf("\nI am here!\n");*/


    ir_display_Open(IR_DISPLAY_ETGA,
		    iminfo,
		    ETGAFileName,
		    (Verbose>=2)?progress:NULL,
		    (Verbose>=1)?message:NULL);

    printf("haze=%lg\n", haze);
    ir_render(ir_newStack, iminfo, haze);


    ir_display_Close();

    
    ir_imageinfo_Delete(iminfo);


  }
  else {
    fprintf(stderr,"show: Error in load file.\n");
    fclose(fp);
    return -1;
  }

  ir_stack_Delete(ir_newStack);

   filename=malloc(sizeof(char)*10);

  strcpy(filename, "frame");
  index=itoa(i);
  currentlen=strlen(index);
  anotherindex=itoa(MaxFrame);
  maxlen=strlen(anotherindex);
  qm1=maxlen-currentlen;
  if(qm1!=0) {for(qm2=1;qm2<qm1+1;qm2++) {strncat(filename,"0",1);}}
  strncat(filename,index,strlen(index));
  strncat(filename,".tga",strlen(".tga"));


  

  assert(infile = fopen(ETGAFileName,"r"));
  assert(outfile = fopen(filename,"w"));

  fread(header_in, 1, 4, infile);

  header_out[0] = 0; /* no ImageID field */
  header_out[1] = 0; /* no colourmap */
  header_out[2] = 10; /* true colour, encoded */
  header_out[3] = header_out[4] = header_out[5] = header_out[6] = 
    header_out[7] = 0; /* colourmap specification */
  header_out[8] = 0; header_out[9] = 0; /* X origin */
  header_out[10] = 0; header_out[11] = 0; /* Y origin */
  header_out[12] = header_in[0]; header_out[13] = header_in[1]; /* width */
  header_out[14] = header_in[2]; header_out[15] = header_in[3]; /* height */

    header_out[16] = 32; /* pixel depth */
    header_out[17] = 40; /* Image Descriptor */

    fwrite(header_out, 1, 18, outfile);

  /* read packet header */
  while (fread(packet_in,1,2,infile)==2) {
    /*   if (verbose==2)
	 printf("---next packet---\n");*/
    if (packet_in[0]&128)
      translate_packet(RLE);
    else
      translate_packet(RAW);
  }

    fclose(infile);
    fclose(outfile);


        /***** qm *****/


	/********     qm       ********/



	printf("\n");

	/*if(i==1) exit(0);*/

 	} 	

   /* printf("\nI am Here!\n");*/


  /**************************** Free Memory ***********************/

  cfgfilePointer = cfgfileStartPointer;
  while(cfgfilePointer != NULL)
  {
	cfgfileStartPointer = cfgfilePointer -> link;
	free(cfgfilePointer);
	cfgfilePointer = cfgfileStartPointer;
 	}	

  /*printf("\nI am Here!\n");*/

  celnamePointer = celnameStartPointer;
  while(celnamePointer != NULL)
  {
	celnameStartPointer = celnamePointer -> link;
	free(celnamePointer);
	celnamePointer = celnameStartPointer;
 	}	 

  /*printf("\nI am Here!\n");*/

  cameraPointer = cameraStartPointer;
  while(cameraPointer != NULL)
  {
	cameraStartPointer = cameraPointer -> link;
	free(cameraPointer);
	cameraPointer = cameraStartPointer;
 	}	 

  /*printf("\nI am Here!\n");*/

  pathPointer = pathStartPointer;
  while(pathPointer != NULL)
  {
	pathStartPointer = pathPointer -> link;
	free(pathPointer);
	pathPointer = pathStartPointer;
 	}	

  /*printf("\nI am Here!\n");*/
 
  lightPointer = lightStartPointer;
  while(lightPointer != NULL)
  {
	lightStartPointer = lightPointer -> link;
	free(lightPointer);
	lightPointer = lightStartPointer;
 	}	 
  scriptPointer = scriptStartPointer;
  while(scriptPointer != NULL)
  {
	scriptStartPointer = scriptPointer -> link;
	free(scriptPointer);
	scriptPointer = scriptStartPointer;
 	}	 

printf("\nI am out of Main! \n");
exit(0);


#else




   NoAlpha=0;
   if(argc!=2)
   {
      printf("Usage : makeFrames ScriptFile\n");
      exit(1);
      }
   
   fp=fopen(argv[1],"r");
   if(fp==NULL) 
   {
      printf("\n Open Script File Error\n");
      exit(1);
      }

   cfgfilePointer = NULL;
   str = malloc(sizeof(char)*80);
   while(fgets(str,80,fp)!=NULL)
   {
     if(cfgfilePointer != NULL) 
      {
		while (cfgfilePointer -> link != NULL)
	    		cfgfilePointer = cfgfilePointer -> link;
		cfgfilePointer -> link = (struct cfgfile *) malloc (sizeof (cfgfile));
      	cfgfilePointer = cfgfilePointer -> link;
		}
	else
	{
		cfgfilePointer = (struct cfgfile  *) malloc (sizeof (cfgfile));
		cfgfileStartPointer = cfgfilePointer;
		}
     cfgfilePointer -> line = malloc(sizeof(char)*80);
     cfgfilePointer -> link = NULL;
     strcpy(cfgfilePointer -> line , str);
     }
   free(str);
   fclose(fp);

   /******************** Display CFG file ********************************/
 
   
   
   /****************************  Reading Cel Names  ***********************/
   celnamePointer = NULL;
   cfgfilePointer = cfgfileStartPointer;

   t1=strtok(cfgfilePointer->line," ");
   t1=strtok(NULL," ");
   j=0;
   t2=malloc(sizeof(char)*10);
   for(t2=strtok(t1,",");t2!=NULL;t2=strtok(NULL,","))
   {
      if(celnamePointer != NULL) 
      {
	   	while (celnamePointer -> link != NULL)
	    		celnamePointer = celnamePointer -> link;
		celnamePointer -> link = (struct celname *) malloc (sizeof (celname));
      	celnamePointer = celnamePointer -> link;
		}
	else
	{
		celnamePointer = (struct celname  *) malloc (sizeof (celname));
		celnameStartPointer = celnamePointer;
		}
	celnamePointer -> name = malloc(sizeof(char)*10);
	celnamePointer -> link = NULL;
	strcpy(celnamePointer -> name , t2);
	}
   free(t2);

   /******************** Display Cel Names ********************************/
 

  

   /****************************  Reading Path  *********************/
   cfgfilePointer = cfgfilePointer->link;
   cfgfilePointer = cfgfilePointer->link;
   pathPointer = NULL;
   t1 = malloc(sizeof(char)*10);
   for(;;)
   {
	t1=strtok(cfgfilePointer -> line," ");
	if(pathPointer != NULL) 
      {
	   	while (pathPointer -> link != NULL)
	    		pathPointer = pathPointer -> link;
		pathPointer -> link = (struct path *) malloc (sizeof (path));
      	pathPointer = pathPointer -> link;
		}
	else
	{
		pathPointer = (struct path  *) malloc (sizeof (path));
		pathStartPointer = pathPointer;
		}
	pathPointer -> name = malloc(sizeof(char)*10);
	pathPointer -> link = NULL;
	strcpy(pathPointer -> name , t1);
      cfgfilePointer = cfgfilePointer->link;
   	cfgfilePointer = cfgfilePointer->link;


      /************************  Reading Path Start Location *************************/
      j=0;
      t1=strtok(cfgfilePointer -> line,")");
	t2=strtok(NULL, ")");
	t3=strtok(NULL,")");
	
      pathPointer -> startup = atof(t3);

      t1=strtok(t1,"(");   
	for(t3=strtok(t1,",");t3!=NULL;t3=strtok(NULL,","))
      {
	    tmp[j]=atof(t3);
          j++;          
          }
       pathPointer -> startposition.x = tmp[0];
       pathPointer -> startposition.y = tmp[1];
       pathPointer -> startposition.z = -tmp[2];       

       j=0;
       t1=strtok(t2,"(");
       for(t4=strtok(t1,",");t4!=NULL;t4=strtok(NULL,","))
       {
	    tmp[j]=atof(t4);
          j++;          
          }
       pathPointer -> startdirection.x = tmp[0];
       pathPointer -> startdirection.y = tmp[1];
       pathPointer -> startdirection.z = -tmp[2];

	/************************  Reading Path End Location *************************/

	cfgfilePointer = cfgfilePointer->link;
	j=0;
      t1=strtok(cfgfilePointer -> line,")");
	t2=strtok(NULL, ")");
	t3=strtok(NULL,")");
	pathPointer -> endup=atof(t3);

      t1=strtok(t1,"(");   
	for(t3=strtok(t1,",");t3!=NULL;t3=strtok(NULL,","))
      {
	    tmp[j]=atof(t3);
          j++;          
          }
       pathPointer -> endposition.x = tmp[0];
       pathPointer -> endposition.y = tmp[1];
       pathPointer -> endposition.z = -tmp[2];       

       j=0;
       t1=strtok(t2,"(");
       for(t4=strtok(t1,",");t4!=NULL;t4=strtok(NULL,","))
       {
	    tmp[j]=atof(t4);
          j++;          
          }
       pathPointer -> enddirection.x = tmp[0];
       pathPointer -> enddirection.y = tmp[1];
       pathPointer -> enddirection.z = -tmp[2];
      
 	 cfgfilePointer = cfgfilePointer->link;
	 cfgfilePointer = cfgfilePointer->link;
	 if(strncmp(cfgfilePointer -> line, "CAMERA", 6)==0) break;
       }

	/*************************** Displaying Path Data *************************/


   free(t1);

   /**********************  Reading Camera  *************************/
   
   cfgfilePointer = cfgfilePointer->link;
   cameraPointer = NULL;
   t1 = malloc(sizeof(char)*10);
   for(;;)
   {
	t1=strtok(cfgfilePointer -> line," ");
	if(cameraPointer != NULL) 
      {
	   	while (cameraPointer -> link != NULL)
	    		cameraPointer = cameraPointer -> link;
		cameraPointer -> link = (struct camera *) malloc (sizeof (camera));
      	cameraPointer = cameraPointer -> link;
		}
	else
	{
		cameraPointer = (struct camera  *) malloc (sizeof (camera));
		cameraStartPointer = cameraPointer;
		}
    cameraPointer -> name = malloc(sizeof(char)*10);
    cameraPointer -> link = NULL;
    strcpy(cameraPointer -> name , t1);

    cfgfilePointer = cfgfilePointer->link;
    cfgfilePointer = cfgfilePointer->link;
    t1=strtok(cfgfilePointer -> line,",");
    t2=strtok(NULL,",");
    cameraPointer -> startfocallength = atof(t1);
    cameraPointer -> startfocus = atof(t2);
	
    cfgfilePointer = cfgfilePointer->link;
    t1=strtok(cfgfilePointer -> line,",");
    t2=strtok(NULL,",");
    cameraPointer -> endfocallength=atof(t1);
    cameraPointer -> endfocus=atof(t2);

    cfgfilePointer = cfgfilePointer->link;
    cfgfilePointer = cfgfilePointer->link;
    if(strncmp(cfgfilePointer -> line, "LIGHT", 5)==0) break;
    }
	
   free(t1);
   
   /***************************** Display Camera  ********************/


   /************************  Reading Light  *************************/

   cfgfilePointer = cfgfilePointer->link;
   lightPointer = NULL;
   t1 = malloc(sizeof(char)*10);

   for(;;)
   {

	t1=strtok(cfgfilePointer -> line," ");
	if(lightPointer != NULL) 
      {
	   	while (lightPointer -> link != NULL)
	    		lightPointer = lightPointer -> link;
		lightPointer -> link = (struct light *) malloc (sizeof (light));
      	lightPointer = lightPointer -> link;
		}
	else
	{
		lightPointer = (struct light *) malloc (sizeof (light));
		lightStartPointer = lightPointer;
		}
    	lightPointer -> name = malloc(sizeof(char)*10);
    	lightPointer -> link = NULL;
    	strcpy(lightPointer -> name , t1);


   	cfgfilePointer = cfgfilePointer->link;
    	cfgfilePointer = cfgfilePointer->link;

      

      /************************  Reading Start Light *************************/
      j=0;
      t1=strtok(cfgfilePointer -> line,")");
	t2=strtok(NULL, ")");
	t1=strtok(t1,"(");   
	for(t3=strtok(t1,",");t3!=NULL;t3=strtok(NULL,","))
      {
	    tmp[j]=atof(t3);
          j++;          
          }
       lightPointer -> startfrontlight.r = tmp[0];
       lightPointer -> startfrontlight.g = tmp[1];
       lightPointer -> startfrontlight.b = tmp[2];       

       j=0;
       t1=strtok(t2,"(");
       for(t4=strtok(t1,",");t4!=NULL;t4=strtok(NULL,","))
       {
	    tmp[j]=atof(t4);
          j++;          
          }
       
	 lightPointer -> startbacklight.r = tmp[0];
       lightPointer -> startbacklight.g = tmp[1];
       lightPointer -> startbacklight.b = tmp[2];       

	
	/************************  Reading End Light *************************/
	   
	cfgfilePointer = cfgfilePointer->link;
	j=0;
      t1=strtok(cfgfilePointer -> line,")");
	t2=strtok(NULL, ")");

      t1=strtok(t1,"(");   
      for(t3=strtok(t1,",");t3!=NULL;t3=strtok(NULL,","))
      {
	    tmp[j]=atof(t3);
          j++;          
          }
       lightPointer -> endfrontlight.r = tmp[0];
       lightPointer -> endfrontlight.g = tmp[1];
       lightPointer -> endfrontlight.b = tmp[2];       

       j=0;
       t1=strtok(t2,"(");
       for(t4=strtok(t1,",");t4!=NULL;t4=strtok(NULL,","))
       {
	    tmp[j]=atof(t4);
          j++;          
          }

       
	 lightPointer -> endbacklight.r = tmp[0];
       lightPointer -> endbacklight.g = tmp[1];
       lightPointer -> endbacklight.b = tmp[2];       
	
	 cfgfilePointer = cfgfilePointer->link;
	 cfgfilePointer = cfgfilePointer->link;
       if(strncmp(cfgfilePointer -> line, "SCRIPT", 6)==0) break;
       }

    free(t1);

    /************************** Displaying Light *********************/


 



   
 /*************************  Reading SCRIPT  **************************/

   cfgfilePointer = cfgfilePointer -> link;
   scriptPointer = NULL;
   t1 = malloc(sizeof(char)*20);
   t2 = malloc(sizeof(char)*20);
   t3 = malloc(sizeof(char)*20);
   while(cfgfilePointer != NULL)
   {
      t1=strtok(cfgfilePointer -> line," ");
	if(scriptPointer != NULL) 
      {
	   	while (scriptPointer -> link != NULL)
	    		scriptPointer = scriptPointer -> link;
		scriptPointer -> link = (struct script *) malloc (sizeof (script));
      	scriptPointer = scriptPointer -> link;
		}
	else
	{
		scriptPointer = (struct script *) malloc (sizeof (script));
		scriptStartPointer = scriptPointer;
		}
    	scriptPointer -> actor = malloc(sizeof(char)*20);
    	scriptPointer -> link = NULL;
    	strcpy(scriptPointer -> actor , t1);

	t1=strtok(NULL," ");
	
	t2=strtok(NULL," ");
	scriptPointer -> pathStatus = malloc(sizeof(char)*20);
	strcpy(scriptPointer -> pathStatus,t2);
	
	
	t3=strtok(NULL," ");
	scriptPointer -> status = malloc(sizeof(char)*20);
	strcpy(scriptPointer -> status , t3);


/*****************New Lines for IN and OUT ************************/
	t4=strtok(NULL, " ");
	t5=strtok(NULL, " ");
	if((t4==NULL)&&(t5==NULL))
	{
		scriptPointer -> IN = 0;
		scriptPointer -> OUT = 0;
		}
	else if((t4!=NULL)&&(t5!=NULL))
	{
		t4 = strtok(t4, "=");
		t4 = strtok (NULL, "=");
		scriptPointer -> IN = atof(t4);
		t5 = strtok(t5, "=");
		t5 = strtok(NULL, "=");
		scriptPointer -> OUT = atof(t5);
		}
	else
	{
		t4 = strtok(t4, "=");
		if((strcmp(t4, "IN")==0))
		{
			t4 = strtok(NULL, "=");
			scriptPointer -> IN = atof(t4);
			scriptPointer -> OUT = 0;
			}
		else if((strcmp(t4, "OUT")==0))
		{
			t4 = strtok(NULL, "=");
			scriptPointer -> OUT = atof(t4);
			scriptPointer -> IN = 0;
			}
		}
	/****************End of New Lines *********************************/


	t2=strtok(t1,"~");
	t3=strtok(NULL,"~");
	scriptPointer -> startFrame = atof(t2);
	scriptPointer -> endFrame = atof(t3);
        cfgfilePointer = cfgfilePointer->link;
      }

    /************************************ Displaying Script  **************************/

  
  /*******************  Put Script in Order based on Start Frame **********************/ 
  
  scriptPointer = scriptStartPointer;
  str=malloc(sizeof(char)*15);
  MaxFrame=0;
  while(scriptPointer != NULL)
  {
      currentValue = scriptPointer -> startFrame;
	nextScriptPointer = scriptPointer;
	while(nextScriptPointer != NULL)
	{
		nextValue = nextScriptPointer -> startFrame;
		if(currentValue > nextValue)
 		{
			strcpy(str,scriptPointer -> actor);
			strcpy(scriptPointer -> actor, nextScriptPointer -> actor);
			strcpy(nextScriptPointer -> actor, str);
			Value = scriptPointer -> startFrame;
			scriptPointer -> startFrame = nextScriptPointer -> startFrame;
			nextScriptPointer -> startFrame = Value;
			Value = scriptPointer -> endFrame;
			scriptPointer -> endFrame = nextScriptPointer -> endFrame;
			nextScriptPointer -> endFrame = Value;

			strcpy(str,scriptPointer -> pathStatus);
			strcpy(scriptPointer -> pathStatus, nextScriptPointer -> pathStatus);
			strcpy(nextScriptPointer -> pathStatus, str);
			
			strcpy(str,scriptPointer -> status);
			strcpy(scriptPointer -> status, nextScriptPointer -> status);
			strcpy(nextScriptPointer -> status, str);
			}
		nextScriptPointer = nextScriptPointer -> link;
		}
	if((scriptPointer -> endFrame > MaxFrame) && (scriptPointer -> endFrame != 9999))
		MaxFrame = scriptPointer -> endFrame;
  
	scriptPointer = scriptPointer -> link;
	}
    
     /************************************ Displaying Sorted Script  **************************/
    
   
  /********************		 Display Frame in Order  ********************/

  printf("\nStart to make frames...\n");

   scriptPointer = scriptStartPointer;
   currentScriptPointer = scriptPointer;
   frameStartPointer = scriptPointer;
   endFramePointer = frameStartPointer;
   preFrameFlag=0;
   for(i=1; i<MaxFrame+1; i++)
   {	
	if(i==1)
	{
	   for(;;)
	   {
		if(scriptPointer != NULL) 
		{
		   currentScriptPointer = scriptPointer;
		   if((i >= scriptPointer -> startFrame)&& (i <= scriptPointer -> endFrame)) endFramePointer = scriptPointer;
		   else { endFramePointer -> link = NULL; break;}
		   }
		else break;
		scriptPointer = scriptPointer -> link;
		}
	   }

	else 
	{
	   /******************** Link Previous Frame ********************/

	   currentFramePointer = frameStartPointer;
	   while(currentFramePointer != NULL)
	   {
		if((i >= currentFramePointer -> startFrame)&& (i <= currentFramePointer -> endFrame))
		{
			preFrameFlag=1;
			endFramePointer = currentFramePointer;
			if(currentFramePointer -> link == NULL) endFramePointer -> link = NULL;	
			}
		else 
       	{
			if (frameStartPointer == currentFramePointer) frameStartPointer = currentFramePointer ->link;
			else endFramePointer -> link = currentFramePointer -> link;	
			}
		currentFramePointer = currentFramePointer -> link;
		}
		

           	/******************** Link Current Frame ********************/
		scriptPointer = currentScriptPointer;
		for(;;)
		{	
			if((scriptPointer != NULL) && (scriptPointer != endFramePointer))
			{
			   currentScriptPointer = scriptPointer;
			   if((i >= scriptPointer -> startFrame) && (i <= scriptPointer -> endFrame))
	   		   {
				if(preFrameFlag==1){endFramePointer -> link = scriptPointer; endFramePointer = scriptPointer;}
				}
	   		   else {endFramePointer -> link = NULL; preFrameFlag=0; break;}
			   }
			else break;
			scriptPointer = scriptPointer -> link;
	   		}  
		}	
     
	

	/******************** 	Display Frame 	********************/

	stkFile=fopen("stktemp.stk","w");
         if(stkFile==NULL)
	   {
          printf("\nwrite file error\n");
          exit(1);
	   }
	stack = ir_stack_Make();

	printf("\nFrame %d\n",i);

	scriptPointer = frameStartPointer;
	while(scriptPointer != NULL)
	{	
		if(strncmp(scriptPointer -> actor, "camera", strlen("camera"))==0)
		{
			cameraScriptPointer = scriptPointer;
			break;
			}
		scriptPointer = scriptPointer -> link;
		}

	

	/**********    qmqmqmqmqm      **************/


	/**********    qmqmqmqmqmqm    ************/

	pathPointer = pathStartPointer;
	while(pathPointer != NULL)
	 {
	   if(strncmp(cameraScriptPointer -> pathStatus, pathPointer -> name, strlen(cameraScriptPointer->pathStatus))==0)  
	     {
	       /*  printf("\nstop!\n");*/
	       startcam.position=pathPointer -> startposition;
	       startcam.direction=pathPointer -> startdirection;
	       startcam.up=pathPointer -> startup;
	       endcam.position=pathPointer -> endposition;
	       endcam.direction=pathPointer -> enddirection;
	       endcam.up=pathPointer -> endup;

	     }
	   pathPointer = pathPointer -> link;
	 }

	  

		/*	printf("\ni=%d\n",i);

			if(i==1) printf("\nstop here\n");*/


	cameraPointer = cameraStartPointer;
	while(cameraPointer != NULL)
	 {
	   if(strcmp(cameraScriptPointer -> status, cameraPointer -> name)==0) 
	     {
	       startcam.zoom=cameraPointer -> startfocallength;
	       startcam.focus=cameraPointer -> startfocus;
	       endcam.zoom=cameraPointer -> endfocallength;
	       endcam.focus=cameraPointer -> endfocus;
	     }
	   cameraPointer = cameraPointer -> link;
	 }

                   /************   Dealing with direction   ************/

       if((startcam.direction.y!=0)&&(startcam.direction.z!=0)) 
	 {
	   if(startcam.direction.y>0)
	     temp1=acos(-startcam.direction.z/sqrt(startcam.direction.y*startcam.direction.y+startcam.direction.z*startcam.direction.z));
	   if(startcam.direction.y<0)
	     temp1=-acos(-startcam.direction.z/sqrt(startcam.direction.y*startcam.direction.y+startcam.direction.z*startcam.direction.z));
	 }
       else if(startcam.direction.y==0)
	 {
	   /* if(start.direction.z<=0)*/
	   temp1=0;
	   /* else temp1=3.1415926;*/}
       else
	 {
	   if(startcam.direction.y>0)
	     temp1=3.1415926/2;
	   else temp1=-3.1415926/2;
	 }

       if((endcam.direction.y!=0)&&(endcam.direction.z!=0)) 
	 {
	   if(endcam.direction.y>0)
	     temp2=acos(-endcam.direction.z/sqrt(endcam.direction.y*endcam.direction.y+endcam.direction.z*endcam.direction.z));
	   if(endcam.direction.y<0)
	     temp2=-acos(-endcam.direction.z/sqrt(endcam.direction.y*endcam.direction.y+endcam.direction.z*endcam.direction.z));
	 }
       else if(endcam.direction.y==0)
	 {
	   /*  if(end.direction.z<=0)*/
	   temp2=0;
	   /* else temp2=3.1415926;*/}
       else
	 {
	   if(endcam.direction.y>0)
	     temp2=3.1415926/2;
	   else temp2=-3.1415926/2;
	 }

       v=sqrt(startcam.direction.y*startcam.direction.y+startcam.direction.z*startcam.direction.z);
       l=sqrt(startcam.direction.x*startcam.direction.x+startcam.direction.y*startcam.direction.y+startcam.direction.z*startcam.direction.z);
       if((startcam.direction.x!=0)&&(v!=0))
	 {
	   if(startcam.direction.x>0)
	     {
	       if(startcam.direction.z>0) temp3=-acos(-v/l);
	       else temp3=-acos(v/l);}
	   if(startcam.direction.x<0)
	     {
	       if(startcam.direction.z>0) temp3=acos(-v/l);
	       else temp3=acos(v/l);}
	 }
       else if(startcam.direction.x==0)
	 {
	   if(startcam.direction.z<=0)
	     temp3=0;
	   else temp3=3.1415926;}
       else
	 {
	   if(startcam.direction.x>0)
	     temp3=-3.1415926/2;
	   else temp3=3.1415926/2;
	 }
       v=sqrt(endcam.direction.y*endcam.direction.y+endcam.direction.z*endcam.direction.z);
       l=sqrt(endcam.direction.x*endcam.direction.x+endcam.direction.y*endcam.direction.y+endcam.direction.z*endcam.direction.z);

       if((endcam.direction.x!=0)&&(v!=0))
	 {
	   if(endcam.direction.x>0)
	     {
	       if(endcam.direction.z>0) temp4=-acos(-v/l);
	       else temp4=-acos(v/l);}
	   if(endcam.direction.x<0)
	     {
	       if(endcam.direction.z>0) temp4=acos(-v/l);
	       else temp4=acos(v/l);}
	 }
       else if(endcam.direction.x==0)
	 {
	   if(endcam.direction.z<=0)
	     temp4=0;
	   else temp4=3.1415926;
	 }
       else
	 {
	   if(endcam.direction.x>0)
	     temp4=-3.1415926/2;
	   else temp4=3.1415926/2;
	 }

       total1=temp2-temp1;
       total2=temp4-temp3;
       if(total1==3.1415926) total1=0;
       /*if(total2==3.1415926) total2=0;*/
       if(total1>3.1415926) total1=total1-2*3.1415926;
       if(total1<-3.1415926) total1=2*3.1415926+total1;
       if(total2>3.1415926) total2=total2-2*3.1415926;
       if(total2<-3.1415926) total2=2*3.1415926+total1;

                /************   End of dealing with direction   ************/


      if(cameraScriptPointer->IN==0){
	a=0.0;}
      else{
	a=1.0*(cameraScriptPointer->IN-1)/(1.0*(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame));}
      if(cameraScriptPointer->OUT==0){
	b=1.0;}
      else{
	b=1.0*(cameraScriptPointer->endFrame-cameraScriptPointer->OUT-cameraScriptPointer->startFrame+1)/(1.0*(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame));}
      t=1.0*(i-cameraScriptPointer->startFrame)/(1.0*(cameraScriptPointer->endFrame-cameraScriptPointer->startFrame));



       if(i<(cameraScriptPointer->startFrame+cameraScriptPointer->IN))

	 {

	


     currentcam.position.x=2*(endcam.position.x-startcam.position.x)/(b-a+1)*t*t/(2*a)+startcam.position.x;
     currentcam.position.y=2*(endcam.position.y-startcam.position.y)/(b-a+1)*t*t/(2*a)+startcam.position.y;
     currentcam.position.z=2*(endcam.position.z-startcam.position.z)/(b-a+1)*t*t/(2*a)+startcam.position.z;
     currentcamangle.x=2*total1/(b-a+1)*t*t/(2*a)+temp1;
     currentcamangle.y=2*total2/(b-a+1)*t*t/(2*a)+temp3;  
     currentcam.up=2*(endcam.up-startcam.up)/(b-a+1)*t*t/(2*a)+startcam.up;

  

     currentcam.zoom=2*(endcam.zoom-startcam.zoom)/(b-a+1)*t*t/(2*a)+startcam.zoom;
     currentcam.focus=2*(endcam.focus-startcam.focus)/(b-a+1)*t*t/(2*a)+startcam.focus;
    
	 }

       if((i>=(cameraScriptPointer->startFrame+cameraScriptPointer->IN))&&(i<=(cameraScriptPointer->endFrame-cameraScriptPointer->OUT)))

	 {
printf("\nok\n");

		li1=a*(endcam.zoom-startcam.zoom)/(b-a+1);
		li2=(t-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1);		
		li3=startcam.zoom;

		if((b-a+1)==0) {printf("Error happened\n");exit(1);}
		/*currentcam.zoom = a*(endcam.zoom-startcam.zoom)/(b-a+1)+(t-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1)+startcam.zooma*/;

		currentcam.zoom=li1+li2+li3;
currentcam.up=a*(endcam.up-startcam.up)/(b-a+1)+(t-a)*2*(endcam.up-startcam.up)/(b-a+1)+startcam.up;
   currentcam.focus=a*(endcam.focus-startcam.focus)/(b-a+1)+(t-a)*2*(endcam.focus-startcam.focus)/(b-a+1)+startcam.focus;

 
     currentcam.position.x=a*(endcam.position.x-startcam.position.x)/(b-a+1)+(t-a)*2*(endcam.position.x-startcam.position.x)/(b-a+1)+startcam.position.x;
     currentcam.position.y=a*(endcam.position.y-startcam.position.y)/(b-a+1)+(t-a)*2*(endcam.position.y-startcam.position.y)/(b-a+1)+startcam.position.y;
     currentcam.position.z=a*(endcam.position.z-startcam.position.z)/(b-a+1)+(t-a)*2*(endcam.position.z-startcam.position.z)/(b-a+1)+startcam.position.z;
     currentcamangle.x=a*total1/(b-a+1)+(t-a)*2*total1/(b-a+1)+temp1;
     currentcamangle.y=a*total2/(b-a+1)+(t-a)*2*total2/(b-a+1)+temp3;  
    /* currentcam.up=a*(endcam.up-startcam.up)/(b-a+1)+(t-a)*2*(endcam.up-startcam.up)/(b-a+1)+startcam.up;*/
  /*   currentcam.zoom=a*(endcam.zoom-startcam.zoom)/(b-a+1)+(t-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1)+startcam.zoom;*/
    /* currentcam.focus=a*(endcam.focus-startcam.focus)/(b-a+1)+(t-a)*2*(endcam.focus-startcam.focus)/(b-a+1)+startcam.focus;*/

	 }

       if(i>(cameraScriptPointer->endFrame-cameraScriptPointer->OUT))

	 {

	    

     currentcam.position.x=a*(endcam.position.x-startcam.position.x)/(b-a+1)+(b-a)*2*(endcam.position.x-startcam.position.x)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.position.x-startcam.position.x)/(b-a+1)/(1-b)+startcam.position.x;
     currentcam.position.y=a*(endcam.position.y-startcam.position.y)/(b-a+1)+(b-a)*2*(endcam.position.y-startcam.position.y)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.position.y-startcam.position.y)/(b-a+1)/(1-b)+startcam.position.y;
     currentcam.position.z=a*(endcam.position.z-startcam.position.z)/(b-a+1)+(b-a)*2*(endcam.position.z-startcam.position.z)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.position.z-startcam.position.z)/(b-a+1)/(1-b)+startcam.position.z;
     currentcamangle.x=a*total1/(b-a+1)+(b-a)*2*total1/(b-a+1)+(t-t*t/2-b+b*b/2)*2*total1/(b-a+1)/(1-b)+temp1;
     currentcamangle.y=a*total2/(b-a+1)+(b-a)*2*total2/(b-a+1)+(t-t*t/2-b+b*b/2)*2*total2/(b-a+1)/(1-b)+temp3; 
     currentcam.up=a*(endcam.up-startcam.up)/(b-a+1)+(b-a)*2*(endcam.up-startcam.up)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.up-startcam.up)/(b-a+1)/(1-b)+startcam.up;
     currentcam.zoom=a*(endcam.zoom-startcam.zoom)/(b-a+1)+(b-a)*2*(endcam.zoom-startcam.zoom)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.zoom-startcam.zoom)/(b-a+1)/(1-b)+startcam.zoom;
     currentcam.focus=a*(endcam.focus-startcam.focus)/(b-a+1)+(b-a)*2*(endcam.focus-startcam.focus)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcam.focus-startcam.focus)/(b-a+1)/(1-b)+startcam.focus;

	 }



    

	ir_stack_SetCOP(stack, currentcam.zoom);




	/**********    qmqmqmqmqmqm    ************/


	/**********    qmqmqmqmqm      **************/


	scriptPointer = frameStartPointer;
	while(scriptPointer != NULL)
	{
		if(scriptPointer != cameraScriptPointer)
		{

		/***** qmqmqm   ******/

		
		/********    qmqmqm     *******/

        fp=fopen(scriptPointer->actor, "r");
        if(fp==NULL)
         {
            printf("\n Open a Cel File:%s Error",scriptPointer->actor);
            exit(1);
            }

        if (ir_stkfile_Load(fp)==0) 
        {
           fclose(fp);
           c=ir_cel_Make();
           c=ir_stack_GetTopCel(ir_newStack);
           }
        else 
        {
           fprintf(stderr,"show: Error in load file.\n");
           fclose(fp);
           return -1;
           }


	pathPointer = pathStartPointer;
	while(pathPointer != NULL)
	 {
	   if(strncmp(scriptPointer->pathStatus, pathPointer->name, strlen(scriptPointer->pathStatus))==0)  
	     {
	       startcel.position=pathPointer->startposition;
	       startcel.direction=pathPointer->startdirection;
	       startcel.up=pathPointer->startup;
	       endcel.position=pathPointer->endposition;
	       endcel.direction=pathPointer->enddirection;
	       endcel.up=pathPointer->endup;

	     }
	   pathPointer = pathPointer -> link;
	 }

	lightPointer = lightStartPointer;
	while(lightPointer != NULL)
	 {
	   if(strncmp(scriptPointer->status, lightPointer->name, strlen(scriptPointer->status))==0)  
	     {
	       startcel.frontlight=lightPointer->startfrontlight;
	       startcel.backlight=lightPointer->startbacklight;
	       endcel.frontlight=lightPointer->endfrontlight;
	       endcel.backlight=lightPointer->endbacklight;
	     }
	   lightPointer = lightPointer -> link;
	 }

       /************   Dealing with direction   ************/

       if((startcel.direction.y!=0)&&(startcel.direction.z!=0)) 
	 {
	   if(startcel.direction.y>0)
	     temp1=acos(-startcel.direction.z/sqrt(startcel.direction.y*startcel.direction.y+startcel.direction.z*startcel.direction.z));
	   if(startcel.direction.y<0)
	     temp1=-acos(-startcel.direction.z/sqrt(startcel.direction.y*startcel.direction.y+startcel.direction.z*startcel.direction.z));
	 }
       else if(startcel.direction.y==0)
	 {
	   temp1=0;
	   }
       else
	 {
	   if(startcel.direction.y>0)
	     temp1=3.1415926/2;
	   else temp1=-3.1415926/2;
	 }

       if((endcel.direction.y!=0)&&(endcel.direction.z!=0)) 
	 {
	   if(endcel.direction.y>0)
	     temp2=acos(-endcel.direction.z/sqrt(endcel.direction.y*endcel.direction.y+endcel.direction.z*endcel.direction.z));
	   if(endcel.direction.y<0)
	     temp2=-acos(-endcel.direction.z/sqrt(endcel.direction.y*endcel.direction.y+endcel.direction.z*endcel.direction.z));
	 }
       else if(endcel.direction.y==0)
	 {
	   temp2=0;
	   }
       else
	 {
	   if(endcel.direction.y>0)
	     temp2=3.1415926/2;
	   else temp2=-3.1415926/2;
	 }

       v=sqrt(startcel.direction.y*startcel.direction.y+startcel.direction.z*startcel.direction.z);
       l=sqrt(startcel.direction.x*startcel.direction.x+startcel.direction.y*startcel.direction.y+startcel.direction.z*startcel.direction.z);
       if((startcel.direction.x!=0)&&(v!=0))
	 {
	   if(startcel.direction.x>0)
	     {
	       if(startcel.direction.z>0) temp3=-acos(-v/l);
	       else temp3=-acos(v/l);}
	   if(startcel.direction.x<0)
	     {
	       if(startcel.direction.z>0) temp3=acos(-v/l);
	       else temp3=acos(v/l);}
	 }
       else if(startcel.direction.x==0)
	 {
	   if(startcel.direction.z<=0)
	     temp3=0;
	   else temp3=3.1415926;}
       else
	 {
	   if(startcel.direction.x>0)
	     temp3=-3.1415926/2;
	   else temp3=3.1415926/2;
	 }
       v=sqrt(endcel.direction.y*endcel.direction.y+endcel.direction.z*endcel.direction.z);
       l=sqrt(endcel.direction.x*endcel.direction.x+endcel.direction.y*endcel.direction.y+endcel.direction.z*endcel.direction.z);
 
       if((endcel.direction.x!=0)&&(v!=0))
	 {
	   if(endcel.direction.x>0)
	     {
	       if(endcel.direction.z>0) temp4=-acos(-v/l);
	       else temp4=-acos(v/l);}
	   if(endcel.direction.x<0)
	     {
	       if(endcel.direction.z>0) temp4=acos(-v/l);
	       else temp4=acos(v/l);}
	 }
       else if(endcel.direction.x==0)
	 {
	   if(endcel.direction.z<=0)
	     temp4=0;
	   else temp4=3.1415926;
	 }
       else
	 {
	   if(endcel.direction.x>0)
	     temp4=-3.1415926/2;
	   else temp4=3.1415926/2;
	 }

       total1=temp2-temp1;
       total2=temp4-temp3;
       if(total1==3.1415926) total1=0;
       if(total1>3.1415926) total1=total1-2*3.1415926;
       if(total1<-3.1415926) total1=2*3.1415926+total1;
       if(total2>3.1415926) total2=total2-2*3.1415926;
       if(total2<-3.1415926) total2=2*3.1415926+total1;

       /************   End of dealing with direction   ************/

      if(scriptPointer->IN==0)
       a=0.0;
else
       a=1.0*(scriptPointer->IN-1)/(1.0*(scriptPointer->endFrame-scriptPointer->startFrame));
       if(scriptPointer->OUT==0)
       b=1.0;
else
      b=1.0*(scriptPointer->endFrame-scriptPointer->OUT-scriptPointer->startFrame+1)/(1.0*(scriptPointer->endFrame-scriptPointer->startFrame));
      t=1.0*(i-scriptPointer->startFrame)/(1.0*(scriptPointer->endFrame-scriptPointer->startFrame));


       if(i<(scriptPointer->startFrame+scriptPointer->IN))

	 {

	

     currentcel.position.x=2*(endcel.position.x-startcel.position.x)/(b-a+1)*t*t/(2*a)+startcel.position.x;
     currentcel.position.y=2*(endcel.position.y-startcel.position.y)/(b-a+1)*t*t/(2*a)+startcel.position.y;
     currentcel.position.z=2*(endcel.position.z-startcel.position.z)/(b-a+1)*t*t/(2*a)+startcel.position.z;
     currentcelangle.x=2*total1/(b-a+1)*t*t/(2*a)+temp1;
     currentcelangle.y=2*total2/(b-a+1)*t*t/(2*a)+temp3; 
     currentcel.up=2*(endcel.up-startcel.up)/(b-a+1)*t*t/(2*a)+startcel.up;
     currentcel.frontlight.r=2*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)*t*t/(2*a)+startcel.frontlight.r;
     currentcel.frontlight.g=2*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)*t*t/(2*a)+startcel.frontlight.g;
     currentcel.frontlight.b=2*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)*t*t/(2*a)+startcel.frontlight.b;
     currentcel.backlight.r=2*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)*t*t/(2*a)+startcel.backlight.r;
     currentcel.backlight.g=2*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)*t*t/(2*a)+startcel.backlight.g;
     currentcel.backlight.b=2*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)*t*t/(2*a)+startcel.backlight.b;   
    
	 }

       if((i>=(scriptPointer->startFrame+scriptPointer->IN))&&(i<=(scriptPointer->endFrame-scriptPointer->OUT)))

	 {
	  
     currentcel.position.x=a*(endcel.position.x-startcel.position.x)/(b-a+1)+(t-a)*2*(endcel.position.x-startcel.position.x)/(b-a+1)+startcel.position.x;
     currentcel.position.y=a*(endcel.position.y-startcel.position.y)/(b-a+1)+(t-a)*2*(endcel.position.y-startcel.position.y)/(b-a+1)+startcel.position.y;
     currentcel.position.z=a*(endcel.position.z-startcel.position.z)/(b-a+1)+(t-a)*2*(endcel.position.z-startcel.position.z)/(b-a+1)+startcel.position.z;
     currentcelangle.x=a*total1/(b-a+1)+(t-a)*2*total1/(b-a+1)+temp1;
     currentcelangle.y=a*total2/(b-a+1)+(t-a)*2*total1/(b-a+1)+temp3;
     currentcel.up=a*(endcel.up-startcel.up)/(b-a+1)+(t-a)*2*(endcel.up-startcel.up)/(b-a+1)+startcel.up;
     currentcel.frontlight.r=a*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)+(t-a)*2*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)+startcel.frontlight.r;
     currentcel.frontlight.g=a*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)+(t-a)*2*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)+startcel.frontlight.g;
     currentcel.frontlight.b=a*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)+(t-a)*2*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)+startcel.frontlight.b;
     currentcel.backlight.r=a*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)+(t-a)*2*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)+startcel.backlight.r;
     currentcel.backlight.g=a*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)+(t-a)*2*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)+startcel.backlight.g;
     currentcel.backlight.b=a*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)+(t-a)*2*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)+startcel.backlight.b;


	 }

       if(i>(scriptPointer->endFrame-scriptPointer->OUT))

	 {
	

     currentcel.position.x=a*(endcel.position.x-startcel.position.x)/(b-a+1)+(b-a)*2*(endcel.position.x-startcel.position.x)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.position.x-startcel.position.x)/(b-a+1)/(1-b)+startcel.position.x;
     currentcel.position.y=a*(endcel.position.y-startcel.position.y)/(b-a+1)+(b-a)*2*(endcel.position.y-startcel.position.y)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.position.y-startcel.position.y)/(b-a+1)/(1-b)+startcel.position.y;
     currentcel.position.z=a*(endcel.position.z-startcel.position.z)/(b-a+1)+(b-a)*2*(endcel.position.z-startcel.position.z)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.position.z-startcel.position.z)/(b-a+1)/(1-b)+startcel.position.z;
     currentcelangle.x=a*total1/(b-a+1)+(b-a)*2*total1/(b-a+1)+(t-t*t/2-b+b*b/2)*2*total1/(b-a+1)/(1-b)+temp1;
     currentcelangle.y=a*total2/(b-a+1)+(b-a)*2*total2/(b-a+1)+(t-t*t/2-b+b*b/2)*2*total2/(b-a+1)/(1-b)+temp3;
     currentcel.up=a*(endcel.up-startcel.up)/(b-a+1)+(b-a)*2*(endcel.up-startcel.up)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.up-startcel.up)/(b-a+1)/(1-b)+startcel.up;
     currentcel.frontlight.r=a*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)+(b-a)*2*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.frontlight.r-startcel.frontlight.r)/(b-a+1)/(1-b)+startcel.frontlight.r;
     currentcel.frontlight.g=a*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)+(b-a)*2*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.frontlight.g-startcel.frontlight.g)/(b-a+1)/(1-b)+startcel.frontlight.g;
     currentcel.frontlight.b=a*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)+(b-a)*2*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.frontlight.b-startcel.frontlight.b)/(b-a+1)/(1-b)+startcel.frontlight.b;
     currentcel.backlight.r=a*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)+(b-a)*2*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.backlight.r-startcel.backlight.r)/(b-a+1)/(1-b)+startcel.backlight.r;
     currentcel.backlight.g=a*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)+(b-a)*2*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.backlight.g-startcel.backlight.g)/(b-a+1)/(1-b)+startcel.backlight.g;
     currentcel.backlight.b=a*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)+(b-a)*2*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)+(t-t*t/2-b+b*b/2)*2*(endcel.backlight.b-startcel.backlight.b)/(b-a+1)/(1-b)+startcel.backlight.b;


	 }




  
 

	   celRotationX=(currentcamangle.x)*(-1);
	   celRotationY=(currentcamangle.y)*(-1);
           celRotationZ=(currentcam.up)*3.1415926/180*(-1);

           celTranslationX=cos(-celRotationY)*cos(-celRotationZ)*(currentcel.position.x-currentcam.position.x)+(cos(-celRotationX)*sin(-celRotationZ)+sin(-celRotationX)*sin(-celRotationY)*cos(-celRotationZ))*(currentcel.position.y-currentcam.position.y)+(sin(-celRotationX)*sin(-celRotationZ)-cos(-celRotationX)*sin(-celRotationY)*cos(-celRotationZ))*(currentcel.position.z-currentcam.position.z);
           celTranslationY=-cos(-celRotationY)*sin(-celRotationZ)*(currentcel.position.x-currentcam.position.x)+(cos(-celRotationX)*cos(-celRotationZ)-sin(-celRotationX)*sin(-celRotationY)*sin(-celRotationZ))*(currentcel.position.y-currentcam.position.y)+(sin(-celRotationX)*cos(-celRotationZ)+cos(-celRotationX)*sin(-celRotationY)*sin(-celRotationZ))*(currentcel.position.z-currentcam.position.z);
           celTranslationZ=sin(-celRotationY)*(currentcel.position.x-currentcam.position.x)-sin(-celRotationX)*cos(-celRotationY)*(currentcel.position.y-currentcam.position.y)+cos(-celRotationX)*cos(-celRotationY)*(currentcel.position.z-currentcam.position.z)+currentcam.zoom;

	   /* printf("\nI am here!\n");*/

	   ir_cel_SetDepth(c, 0);

	   celRotationX=(currentcamangle.x-currentcelangle.x)*(-1);
	   celRotationY=(currentcamangle.y-currentcelangle.y)*(-1);
           celRotationZ=(currentcam.up-currentcel.up)*3.1415926/180*(-1);


	   /*printf("\nI am here!\n");*/

	   ir_matrix_Construct4(IR_ROTATEX, celRotationX, 0.0, 0.0, celRotateX);
	   ir_matrix_Construct4(IR_ROTATEY, celRotationY, 0.0, 0.0, celRotateY);
    ir_matrix_Mul4(celRotateX, celRotateY, celRotateY);
    ir_matrix_Construct4(IR_ROTATEZ, celRotationZ, 0.0, 0.0, celRotateZ);
    ir_matrix_Mul4(celRotateY, celRotateZ, celRotate);
    ir_matrix_Construct4(IR_TRANSLATE, celTranslationX, celTranslationY, celTranslationZ, celTranslate);
    ir_matrix_Mul4(celRotate, celTranslate, celTransform);

    ir_cel_Set3DTransform(c, celTransform);
    

    /*printf("\nI am here!\n");*/

    ir_cel_Light(currentcel.frontlight, &celfrontLight);

   
    ir_cel_Light(currentcel.backlight, &celbackLight);



    ir_cel_SetFrontLight(c, &celfrontLight); 
    ir_cel_SetBackLight(c, &celbackLight); 


    ir_cel_SetFocus(c, currentcam.focus);




    (void)ir_stack_AddCel(stack, c, IR_ADD_ON_TOP, 0);

	
     		/********    qmqmqm     *******/


		
		/***** qmqmqm   ******/


			}
		scriptPointer = scriptPointer -> link;		
		}

	         
	


	/*********    qm       ********/

	/*** qm *****/

     ir_stack_Save(stack, stkFile);
     fclose(stkFile);
     ir_stack_Delete(stack);

    

/*** Create image data structure ***/
  iminfo = ir_imageinfo_Make(Width, Height);

  if (StartingLine!=0 || EndingLine!=Height-1)
    ir_imageinfo_SetSELines(iminfo, StartingLine, EndingLine);

  ir_imageinfo_SetAntiAliasing(iminfo,
		    SuperSamplingFactor==1?IR_AA_NONE:IR_AA_SUPERSAMPLING_BOX,
		    SuperSamplingFactor,SuperSamplingFactor);

fp = fopen("stktemp.stk", "r");

  if (fp == NULL) {
    fprintf(stderr,"Error: Couldn't open tempstkfile %s.\n", "stktemp.stk");
    return -1;
  }

 
  
  if (ir_stkfile_Load(fp)==0) { 

    fclose(fp);

    

    ir_display_Open(IR_DISPLAY_ETGA,
		    iminfo,
		    ETGAFileName,
		    (Verbose>=2)?progress:NULL,
		    (Verbose>=1)?message:NULL);

   
    ir_render(ir_newStack, iminfo, haze);
   
    ir_display_Close();

    
    ir_imageinfo_Delete(iminfo);
  }
  else {
    fprintf(stderr,"show: Error in load file.\n");
    fclose(fp);
    return -1;
  }

  
  ir_stack_Delete(ir_newStack);


  filename=malloc(sizeof(char)*10);
 
 

  strcpy(filename, "frame");

  index=itoa(i);
  currentlen=strlen(index);
  anotherindex=itoa(MaxFrame);
  maxlen=strlen(anotherindex);
  qm1=maxlen-currentlen;
  if(qm1!=0) {for(qm2=1;qm2<qm1+1;qm2++) {strncat(filename,"0",1);}}
  strncat(filename,index,strlen(index));
  strncat(filename,".tga",strlen(".tga"));

 

  assert(infile = fopen(ETGAFileName,"r"));
  assert(outfile = fopen(filename,"w"));

  fread(header_in, 1, 4, infile);

  header_out[0] = 0; /* no ImageID field */
  header_out[1] = 0; /* no colourmap */
  header_out[2] = 10; /* true colour, encoded */
  header_out[3] = header_out[4] = header_out[5] = header_out[6] = 
    header_out[7] = 0; /* colourmap specification */
  header_out[8] = 0; header_out[9] = 0; /* X origin */
  header_out[10] = 0; header_out[11] = 0; /* Y origin */
  header_out[12] = header_in[0]; header_out[13] = header_in[1]; /* width */
  header_out[14] = header_in[2]; header_out[15] = header_in[3]; /* height */

    header_out[16] = 32; /* pixel depth */
    header_out[17] = 40; /* Image Descriptor */

    fwrite(header_out, 1, 18, outfile);

  /* read packet header */
  while (fread(packet_in,1,2,infile)==2) {
    /*   if (verbose==2)
	 printf("---next packet---\n");*/
    if (packet_in[0]&128)
      translate_packet(RLE);
    else
      translate_packet(RAW);
  }

    fclose(infile);
    fclose(outfile);

        /***** qm *****/


	/********     qm       ********/




	
 	} 	




  /**************************** Free Memory ***********************/

  cfgfilePointer = cfgfileStartPointer;
  while(cfgfilePointer != NULL)
  {
	cfgfileStartPointer = cfgfilePointer -> link;
	free(cfgfilePointer);
	cfgfilePointer = cfgfileStartPointer;
 	}	

 
  celnamePointer = celnameStartPointer;
  while(celnamePointer != NULL)
  {
	celnameStartPointer = celnamePointer -> link;
	free(celnamePointer);
	celnamePointer = celnameStartPointer;
 	}	 


  cameraPointer = cameraStartPointer;
  while(cameraPointer != NULL)
  {
	cameraStartPointer = cameraPointer -> link;
	free(cameraPointer);
	cameraPointer = cameraStartPointer;
 	}	 



  pathPointer = pathStartPointer;
  while(pathPointer != NULL)
  {
	pathStartPointer = pathPointer -> link;
	free(pathPointer);
	pathPointer = pathStartPointer;
 	}	


 
  lightPointer = lightStartPointer;
  while(lightPointer != NULL)
  {
	lightStartPointer = lightPointer -> link;
	free(lightPointer);
	lightPointer = lightStartPointer;
 	}	 
  scriptPointer = scriptStartPointer;
  while(scriptPointer != NULL)
  {
	scriptStartPointer = scriptPointer -> link;
	free(scriptPointer);
	scriptPointer = scriptStartPointer;
 	}	 

printf("\nThe frames are made successfully!\n\n");
exit(0);


#endif

  }











      
 char *itoa(int value)
  {
  int count,                   /* number of characters in string       */
      i,                       /* loop control variable                */
      sign;                    /* determine if the value is negative   */
  char *ptr,                   /* temporary pointer, index into string */
       *string,                /* return value                         */
       *temp;                  /* temporary string array               */

  count = 0;
  if ((sign = value) < 0)      /* assign value to sign, if negative    */
     {                         /* keep track and invert value          */
     value = -value;
     count++;                  /* increment count                      */
     }

  /* allocate INTSIZE plus 2 bytes (sign and NULL)                     */
  temp = (char *) malloc(INTSIZE + 2);
  if (temp == NULL)
     {
     return(NULL);
     }
  memset(temp,'\0', INTSIZE + 2);

  string = (char *) malloc(INTSIZE + 2);
  if (string == NULL)
     {
     return(NULL);
     }
  memset(string,'\0', INTSIZE + 2);
  ptr = string;                /* set temporary ptr to string          */

  /*--------------------------------------------------------------------+
  | NOTE: This process reverses the order of an integer, ie:            |
  |       value = -1234 equates to: char [4321-]                        |
  |       Reorder the values using for {} loop below                    |
  +--------------------------------------------------------------------*/
  do {
     *temp++ = value % 10 + '0';   /* obtain modulus and or with '0'   */
     count++;                      /* increment count, track iterations*/
     }  while (( value /= 10) >0);

  if (sign < 0)                /* add '-' when sign is negative        */
     *temp++ = '-';

  *temp-- = '\0';              /* ensure null terminated and point     */
                               /* to last char in array                */

  /*--------------------------------------------------------------------+
  | reorder the resulting char *string:                                 |
  | temp - points to the last char in the temporary array               |
  | ptr  - points to the first element in the string array              |
  +--------------------------------------------------------------------*/
  for (i = 0; i < count; i++, temp--, ptr++)
     {
     memcpy(ptr,temp,sizeof(char));
     }

  return(string);
  }





