#include #include #include #include #include #include #include #include #include "cement.h" #include #include #include #include #include "pnmutils.h" /* Global Variables */ int verbose=0; double powLookup[256]; extern int errno; extern char **environ; /* Function prototypes */ int my_system (char *command); inline double createValue(double a, unsigned char b, double weight); void initilizeLookup(double p); void usage(char * string); void parse_commandline(int argc, char ** argv, struct image_params * a_params, struct image_params * b_params, double *R, double *G, double *B); int main(int argc, char ** argv) { struct image_params a_params={NULL,0,0,0,0,0.0}; struct image_params b_params={NULL,0,0,0,0,0.0}; // char * dest_filename=NULL; // FILE * a, * b, * dest; double p=EXPONENT; /* exponent for pow */ double a_pixel[ARRAY_SIZE]; unsigned char b_pixel[ARRAY_SIZE]; double out_pixel[ARRAY_SIZE]; int i,j; int channels_per_pixel; double R, G, B; /* weights for cement */ /* Timing measurments */ struct timeval timeValStart; struct timeval timeValEnd; struct timezone timeZone; int minutes; double seconds=0.0; #ifdef GET_TIME_MEASUREMENTS struct timeval runTimeStart; struct timeval runTimeStop; double fileReadTime=0.0; double fileWriteTime=0.0; double computeTime=0.0; #endif unsigned int pixels=0; unsigned int numberOfPixels; /* Compression variables */ int agz=0; int bgz=0; int destgz=0; // gzFile file; char outmode[20]; char string[MAX_STRING_LENGTH]; strcpy(outmode, "wb6 "); parse_commandline(argc, argv, &a_params, &b_params, &R, &G, &B); if(isGZIPFile(a_params.filename)==0) { if ((a=fopen(a_params.filename, "r"))==NULL) { fprintf(stderr, "Unable to open %s.\n", a_params.filename); exit(EXIT_FAILURE); } } else { if ((agz=open(a_params.filename,O_RDONLY))<0) { fprintf(stderr, "Unable to open %s. %d\n", ".gz",destgz); exit(EXIT_FAILURE); } compressFileInputA=1; inFileA = gzdopen(agz, "rb"); a=(FILE*)&agz; } /* if ((a=fopen(a_params.filename, "r"))==NULL) { fprintf(stderr, "Unable to open %s.\n", a_params.filename); exit(EXIT_FAILURE); }*/ if(isGZIPFile(b_params.filename)==0) { if ((b=fopen(b_params.filename, "r"))==NULL) { fprintf(stderr, "Unable to open %s.\n", b_params.filename); exit(EXIT_FAILURE); } } else { if ((bgz=open(b_params.filename,O_RDONLY))<0) { fprintf(stderr, "Unable to open %s.\n",b_params.filename); exit(EXIT_FAILURE); } compressFileInputB=1; inFileB = gzdopen(bgz, "rb"); b=(FILE*)&bgz; } /* if ((b=fopen(b_params.filename, "r"))==NULL) { fprintf(stderr, "Unable to open %s.\n", b_params.filename); exit(EXIT_FAILURE); }*/ sprintf(string,"%s.tmp",a_params.filename); /* if ((dest=fopen(string, "w"))==NULL) { fprintf(stderr, "Unable to open %s.\n", string); exit(EXIT_FAILURE); }*/ if(isGZIPFile(a_params.filename)==0) { if ((dest=fopen(string, "w"))==NULL) { fprintf(stderr, "Unable to open %s.\n", string); exit(EXIT_FAILURE); } } else { if ((destgz=creat(string,00600))<0) { fprintf(stderr, "Unable to open %s. %d\n", string,destgz); exit(EXIT_FAILURE); } compressFileOutput=1; outFileDest = gzdopen(destgz, outmode); if(outFileDest==NULL) printf("ERROR could not open gz file\n"),exit(1); dest=(FILE*)&outFileDest; } get_image_type(a, &a_params); if (a_params.type=='A') { channels_per_pixel=3; sprintf(string, "P%c\n", a_params.type); filewrite(string,sizeof(unsigned char),strlen(string),dest); get_image_params_preserve_comments(a,&a_params,dest); } else { fprintf(stderr, "Portable Lightspace Map (plm) must be type A and it is type %c\n",a_params.type); exit(EXIT_FAILURE); } get_image_params(b, &b_params); if ((a_params.width!=b_params.width) || (a_params.height!=b_params.height) || (a_params.max_val!=b_params.max_val) || (a_params.exponent!=(float)EXPONENT)) { printf("%f %f\n",a_params.exponent,EXPONENT); fprintf(stderr, "Lightspace must have the same dimensions, maximum " "values, and exponent.\n"); exit(EXIT_FAILURE); } sprintf(string, "# %s %s %s %s %s %s\n",argv[0],argv[1],argv[2],argv[3], argv[4],argv[5]); filewrite(string,sizeof(unsigned char),strlen(string),dest); #ifndef CEMENT_REMOVE sprintf(string, "%d %d\n%d\n%d\n%f\n",a_params.width, a_params.height,a_params.max_val, ++a_params.numberOfImages,EXPONENT); #else sprintf(string, "%d %d\n%d\n%d\n%f\n",a_params.width, a_params.height, a_params.max_val,--a_params.numberOfImages,EXPONENT); #endif filewrite(string,sizeof(unsigned char),strlen(string),dest); #ifdef SILENT_RUN #ifndef CEMENT_REMOVE printf("Add %s, R%1.1f G%1.1f B%1.1f\r",b_params.filename,R,G,B); #else printf("Del %s, R%1.1f G%1.1f B%1.1f\r",b_params.filename,R,G,B); #endif #else #ifndef CEMENT_REMOVE printf("Adding file: \"%s\", ",b_params.filename); #else printf("Removing file: \"%s\", ",b_params.filename); #endif printf("P%c, %dx%d, ",b_params.type,b_params.width,b_params.height); printf("R=%7.3f ",R); printf("G=%7.3f ",G); printf("B=%7.3f to ",B); printf("lightspace \"%s\", ",a_params.filename); printf("P%c\n",a_params.type); #endif initilizeLookup(p); /* Start Timer */ if(gettimeofday(&timeValStart,&timeZone)==-1) { fprintf(stderr,"Error getting the time\n"); exit(0); } numberOfPixels=a_params.width*a_params.height; if(((float)numberOfPixels/NUMBER_PIXELS_PER_READ)!=(int)((float)numberOfPixels/NUMBER_PIXELS_PER_READ)) { if(verbose) printf("\nERROR: The number of pixels %d is not evenly divisible by %d. ",numberOfPixels,NUMBER_PIXELS_PER_READ); numberOfPixels=(int)((float)numberOfPixels/NUMBER_PIXELS_PER_READ)*NUMBER_PIXELS_PER_READ; if(verbose) printf("We will go up to %d pixels.\n",numberOfPixels); } for(i=0;i60) { minutes=seconds/60; seconds=seconds-minutes*60; } else minutes=0; if(verbose || GET_RUN_TIME) { #ifndef SILENT_RUN if(minutes==1) printf("Run Time is about %d minute %f seconds.\n",minutes,seconds); else printf("Run Time is about %d minutes %f seconds.\n",minutes,seconds); #else #ifndef CEMENT_REMOVE printf("Add %s, R%1.1f G%1.1f B%1.1f %f(s)\n",b_params.filename,R,G,B,minutes*60+seconds); #else printf("Del %s, R%1.1f G%1.1f B%1.1f %f(s)\n",b_params.filename,R,G,B,minutes*60+seconds); #endif #endif } sprintf(string,"rm %s; mv %s.tmp %s",a_params.filename,a_params.filename,a_params.filename); if(verbose) printf("%s\n",string); if(my_system (string)==-1) { printf("Error calling %s\n",string); exit(EXIT_FAILURE); } exit(EXIT_SUCCESS); } void initilizeLookup(double p) { int i; for(i=0;i<256;i++) powLookup[i]=pow((double)i,p); } inline double createValue(double a, unsigned char b, double weight) { #ifndef CEMENT_REMOVE return(a+weight*powLookup[b]); #else return(a-weight*powLookup[b]); #endif } int my_system (char *command) { int pid, status; if (command == 0) return 1; pid = fork(); if (pid == -1) return -1; if (pid == 0) { char *argv[4]; argv[0] = "sh"; argv[1] = "-c"; argv[2] = command; argv[3] = 0; execve("/bin/sh", argv, environ); exit(127); } do { if (waitpid(pid, &status, 0) == -1) { if (errno != EINTR) return -1; } else return status; } while(1); } void usage(char * string) { fprintf(stderr, "Use: %s [Lightspace file] [Imagespace file] \n",string); fprintf(stderr, " [Lightspace file] is Portable Lightspace Map (plm) file image to be added to\n"); fprintf(stderr, " [Imagespace file] is ppm file of image to add to the lightspace.\n"); fprintf(stderr, " =Float for Red weight.\n"); fprintf(stderr, " =Float for Green weight.\n"); fprintf(stderr, " =Float for Blue weight.\n"); exit(EXIT_SUCCESS); } void parse_commandline(int argc, char ** argv, struct image_params * a_params, struct image_params * b_params, double *R, double *G, double *B) { int i; if (argc<3) usage(argv[0]); for (i=1;ifilename==NULL) a_params->filename=(char *)strdup(argv[i]); /* if file a still null */ else if (b_params->filename==NULL) b_params->filename=(char *)strdup(argv[i]); /* else use(); */ //*R: scanf(...R) double must be a pointer, choice exists only with interger sscanf(argv[3],"%lf",R); /* sscanf does not recognize %g, must be %lf */ //R: sscanf(argv[3],"%lf",&R); sscanf(argv[4],"%lf",G); /* sscanf does not recognize %g, must be %lf */ sscanf(argv[5],"%lf",B); /* sscanf does not recognize %g, must be %lf */ } if ((a_params->filename==NULL) || (b_params->filename==NULL)) usage(argv[0]); }