#include #include #include #include #include #include #include #include #include "cement.h" #include "pnmutils.h" #include "file.h" #include "powLookup.h" #include "readlut.h" #include //#define JPEG_QUALITY 90 (now defined in cement.h) /* Functions need in the library and the executable */ static inline double createValue(double a,double p_inv); static unsigned char binarySearch(double value); static int verbose=0; #ifndef OBJECT_COMPILE /* Functions only needed in the executable */ void usage(char * string); void parse_commandline(int argc, char ** argv, char ** input_filename, char ** output_filename, char ** lut_filename); void mystatus_lut(char *action, char *filename, char *lut_filename, double red, double green, double blue, double complete); #endif int plm2pnm(char *input_filename, char *dest_filename, char *command, void (*status)(char *action, char *filename, double R,double G, double B, double complete) ) { return plm2pnm_lut(input_filename, dest_filename, NULL, command, status, NULL); } /* set one of the status functions to NULL when calling */ int plm2pnm_lut(char *input_filename, char *dest_filename, char *lut_filename, char *command, void (*status)(char *action, char *filename, double R,double G, double B, double complete), void (*status_lut)(char *action, char *filename, char *lut, double R,double G, double B, double complete) ) { struct image_params a_params={NULL,0,0,0,0,0.0}; // char *dest_filename=NULL; File *a, *dest; double p=EXPONENT; /* exponent for pow */ double p_inv=1.0/p; double a_pixel[ARRAY_SIZE]; unsigned char out_pixel[ARRAY_SIZE]; int i,j=0; int channels_per_pixel; /* 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; char string[MAX_STRING_LENGTH]; int image_ptr; a_params.filename=input_filename; if ((a=fileopen(a_params.filename, "r"))==NULL) { fprintf(stderr, "Unable to open %s.\n", a_params.filename); exit(EXIT_FAILURE); } if ((dest=fileopen(dest_filename, "w"))==NULL) { fprintf(stderr, "Unable to open %s.\n", dest_filename); exit(EXIT_FAILURE); } get_image_type(a, &a_params); if (a_params.type=='A') { channels_per_pixel=3; sprintf(string,"P%c\n", '6'); 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); } /* maali may 9 2000 */ if(lut_filename !=NULL) { readlut(lut_filename, &p, &powLookupExponent); } if ((a_params.exponent!=(float)EXPONENT)) { fprintf(stderr, "Lightspace must have the same exponent.\n"); exit(EXIT_FAILURE); } sprintf(string,"# %s %s -o %s \n",command,input_filename,dest_filename); filewrite(string,sizeof(unsigned char),strlen(string),dest); sprintf(string, "%d %d\n%d\n", a_params.width, a_params.height, a_params.max_val); filewrite(string,sizeof(unsigned char),strlen(string),dest); image_width=a_params.width; image_height=a_params.height; image_buffer=malloc(3*image_width*image_height*sizeof(unsigned char)); if(image_buffer==NULL) printf("ERROR: Could not allocate buffer for image\n"); image_ptr=0; #ifdef SILENT_RUN // printf("Create %s \r",dest_filename); #else printf("Generate ppm file: \"%s\", ",dest_filename); printf("P%c, %dx%d, Channels_per_pixel: %d ",'6',a_params.width,a_params.height,channels_per_pixel); printf("from Lightspace file: \"%s\", ",a_params.filename); printf("P%c\n",a_params.type); #endif /* 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("Create %s, %f(s)\n",dest_filename,minutes*60+seconds); if(status!=NULL) status("Create", dest_filename,-1,-1,-1,(double)100); if(status_lut!=NULL) status_lut("Create", dest_filename,lut_filename, -1,-1,-1,(double)100); #endif } else printf("\n"); if(isJpegFile(dest)==1) { fileclose(dest); write_JPEG_file (dest_filename,JPEG_QUALITY); } else fileclose(dest); free(image_buffer); return(EXIT_SUCCESS); } static inline double createValue(double a, double p_inv) { return(pow(a,p_inv)); } static double *inverseLookup=powLookupTable; static unsigned char binarySearch(double value) { int low=0; int high=0x100; int middle=0x7f; while(1) { if(high==middle || low==middle) return(middle); if(value>=inverseLookup[middle]) { low=middle; middle=((high-low)>>1)+middle; } else if(value>1)+low; } } return(middle); } #ifndef OBJECT_COMPILE int main(int argc, char ** argv) { char * dest_filename=NULL; char * input_filename=NULL; char * lut_filename=NULL; parse_commandline(argc, argv, &input_filename, &dest_filename,&lut_filename); return(plm2pnm_lut(input_filename,dest_filename,lut_filename,argv[0],NULL,mystatus_lut)); } void mystatus_lut(char *action, char *filename, char *lut_filename, double R,double G, double B, double complete) { if (lut_filename!=NULL) printf("%s %s (%s) %d %d %d %d%%\r", action,filename,lut_filename,(int)R,(int)G,(int)B,(int)complete); else printf("%s %s %d %d %d %d%%\r",action,filename,(int)R,(int)G,(int)B,(int)complete); fflush(stdout); if((int)complete==100) printf("\n"); } void usage(char * string) { fprintf(stderr, "Use: %s [LightSpaceFile] -o [ImageSpaceFile] " "[-lut [LookUpTableFile]]\n",string); fprintf(stderr, "Example: %s trowel_out.plm -o pork.jpg\n",string); // fprintf(stderr, " [LightSpaceFile] is plm (portable light space map) input file.\n"); // fprintf(stderr, " [ImageSpaceFile] is ppm or jpg output (autodetected by filename you specify).\n"); // fprintf(stderr, " [LookUpTableFile] (optional, lut) specifies a map from \n"); // fprintf(stderr, " imagespace to lightspace; if none specified, a default map is used.\n"); exit(EXIT_SUCCESS); } void parse_commandline(int argc, char ** argv, char ** input_filename, char ** output_filename, char ** lut_filename) { if (argc<3) usage(argv[0]); *input_filename=(char *)strdup(argv[1]); if(argv[2][0]!='-' || argv[2][1] !='o') usage(argv[0]); *output_filename=(char *)strdup(argv[3]); /* maali may 9 2000 */ if(argc>4) { if((strlen(argv[4])>=4) &&(argv[4][0]=='-')&&(argv[4][1]=='l') &&(argv[4][2]=='u')&&(argv[4][3]=='t')) { *lut_filename=(char *)strdup(argv[5]); } else { usage(argv[0]); } } } #endif