#include #include #include #include #include #include #include #include #include #include "cement.h" #include "pnmutils.h" /* Global Variables */ int verbose=0; double powLookup[256]; /* Function declarations */ inline double createValue(unsigned char a,double weight); void initilizeLookup(double p); void usage(char *string); void parse_commandline(int argc, char ** argv, struct image_params * a_params, double *R, double *G, double *B, char ** output_filename); int main(int argc, char ** argv) { struct image_params a_params={NULL,0,0,0,0}; char * dest_filename=NULL; // FILE * a, * dest; double p=EXPONENT; /* exponent for pow */ double p_inv; /* 1.0/p */ unsigned char a_pixel[ARRAY_SIZE]; double out_pixel[ARRAY_SIZE]; int i,j; int channels_per_pixel; double R,G,B; /* 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 numberOfPixels; /* Compression variables */ int agz=0; int destgz=0; // gzFile file; char outmode[20]; char string[MAX_STRING_LENGTH]; strcpy(outmode, "wb6 "); p_inv=1.0/p; parse_commandline(argc, argv, &a_params, &R,&G,&B,&dest_filename); 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*)&inFileA; } if (dest_filename==NULL) { fprintf(stderr, "ERROR: There is no destination file.\n"); exit(EXIT_FAILURE); } if(isGZIPFile(dest_filename)==0) { if ((dest=fopen(dest_filename, "w"))==NULL) { fprintf(stderr, "Unable to open %s.\n", dest_filename); exit(EXIT_FAILURE); } } else { if ((destgz=creat(dest_filename,00600))<0) { fprintf(stderr, "Unable to open %s.\n",dest_filename); exit(EXIT_FAILURE); } compressFileOutput=1; outFileDest = gzdopen(destgz, outmode); if(outFileDest==NULL) printf("Error opening gz file.\n"); dest=(FILE*)&outFileDest; } get_image_params(a, &a_params); if (a_params.type=='6') channels_per_pixel=3; else { fprintf(stderr, "Input image must be type P6 and it is of type P%c\n",a_params.type); exit(EXIT_FAILURE); } sprintf(string, "P%c\n", 'A'); filewrite(string,sizeof(unsigned char),strlen(string),dest); sprintf(string, "# %s %s %s %s %s %s %s\n",argv[0],argv[1],argv[2],argv[3], argv[4],argv[5],argv[6]); filewrite(string,sizeof(unsigned char),strlen(string),dest); sprintf(string, "%d %d\n%d\n%d\n%f\n",a_params.width, a_params.height, a_params.max_val,1,(float)EXPONENT); filewrite(string,sizeof(unsigned char),strlen(string),dest); /* fprintf(dest, "P%c\n", 'A'); fprintf(dest, "#Generated by %s\n",argv[0]); fprintf(dest, "%d %d\n%d\n", a_params.width, a_params.height, a_params.max_val); fprintf(dest, "%f\n",EXPONENT); */ #ifdef SILENT_RUN printf("Init %s, R%1.1f G%1.1f B%1.1f\r",a_params.filename,R,G,B); #else printf("Initilizing Lightspace from file: \"%s\",",a_params.filename); printf(" P%c, %dx%d, Channels_per_pixel: %d, ",a_params.type,a_params.width,a_params.height,channels_per_pixel); printf("R=%7.3f ",R); printf("G=%7.3f ",G); printf("B=%7.3f\n",B); #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 printf("Init %s, R%1.1f G%1.1f B%1.1f %f(s)\n",a_params.filename,R,G,B,minutes*60+seconds); #endif } exit(EXIT_SUCCESS); } void initilizeLookup(double p) { int i; for(i=0;i<256;i++) powLookup[i]=pow((double)i,p); } inline double createValue(unsigned char a,double weight) { return(weight*powLookup[a]); } void usage(char *string) { fprintf(stderr, "Use: %s [Imagespace file] -o [Lightspace file]\n",string); fprintf(stderr, " [Imagespace file] is a ppm file that will start a lightspace.\n"); fprintf(stderr, " =Float for Red weight.\n"); fprintf(stderr, " =Float for Green weight.\n"); fprintf(stderr, " =Float for Blue weight.\n"); fprintf(stderr, " [LightSpaceFile] is a Portable Lightspace Map (plm) that will be created.\n"); exit(EXIT_SUCCESS); } void parse_commandline(int argc, char ** argv, struct image_params * a_params, double *R, double *G, double *B, char ** output_filename) { int i; if (argc<3) { fprintf(stderr,"number of input arguments was only %d\n",argc); usage(argv[0]); } for (i=1;i=argc) usage(argv[0]); /* e.g. if last argument not output file */ *output_filename=(char *)strdup(argv[++i]); } else if (a_params->filename==NULL) a_params->filename=(char *)strdup(argv[i]); /* if file a still null */ sscanf(argv[2],"%lf",R); /* sscanf does not recognize %g, must be %lf */ sscanf(argv[3],"%lf",G); /* sscanf does not recognize %g, must be %lf */ sscanf(argv[4],"%lf",B); /* sscanf does not recognize %g, must be %lf */ } if ((a_params->filename==NULL)) usage(argv[0]); }