#include #include #include #include #include #include #include #include #include #include #include "cement.h" #include "pnmutils.h" #include "file.h" #include "powLookup.h" #include "jpeg-6b/jpeglib.h" #include "readlut.h" /* Functions need in the library and the executable */ static inline double createValue(unsigned char a,double weight); 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, double *R, double *G, double *B, char ** output_filename, char ** lut_filename); void mystatus_lut(char *action, char *filename, char *lut, double R,double G, double B, double complete); #endif #ifdef NOTDEFINED /* * this function doesn't return on error, it aborts. * * the filename provided is opened and read into the global * double vector 'powLookupTable[]', overwriting the default * values therein. the variables 'p' and 'powLookupExponent' * are zeroed. * * the input text file: * 1) should always have "L0\n" as the first three bytes. * 2) can have any comments delimited by '#' and '\n' * 3) blank lines are ignored * 4) an input line has: index value [0..255], whitespace, lightspace value * 5) all index values from 0 to 255 must be assigned a value for now. * since we know the response function for a reasonable camera should be * at least semimonotonic, we could fill in gaps etc... not today. */ void readlut(char *lut_filename, double *p, double *powLookupExponent) { // some variables used in reading a text file lut File *lutfile; double x; int i, j, k, lineno, maxlinelength=1024, lutsize=256; boolean isCommentLine, postWhitespace, *lutset, lutsetall; char *s; /* maali may 9 2000 */ if(lut_filename !=NULL) { if ((lutfile = fileopen(lut_filename, "r"))==NULL) { fprintf(stderr, "Unable to open %s.\n", lut_filename); exit(EXIT_FAILURE); } else { /* read in lut. things we want to add: * 3) header info (may be in comments) * e.g. name of assoc. imaging device */ isCommentLine=FALSE; postWhitespace=FALSE; maxlinelength=1024; lineno=0; // allocate and initialize 'lutset' and 's' lutset=((boolean*)malloc(lutsize*sizeof(boolean))); for (i=0; imax) { s[k]=filegetc(lutfile); if (s[k]=='#') { if (!postWhitespace) { isCommentLine=TRUE; } s[k] = 0; while((!fileeof(lutfile)) &&(filegetc(lutfile)!='\n')) { /* here we could process info from comments */ } break; } if (!isspace(s[k])) postWhitespace=TRUE; k++; } while((s[k-1]!='\n') &&(!fileeof(lutfile)) &&(k255)) { fprintf(stderr, "Index (%d) out of range [0..255]" " on line %3d of %s\n", j, lineno, lut_filename); exit(EXIT_FAILURE); } powLookupTable[j] = x; lutset[j] = TRUE; if (verbose) printf("lut[%3d]=%10.20g\n", j, powLookupTable[j]); }//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 if(status!=NULL) status("Init",a_params.filename,R,G,B,(double)100); if(lutstatus!=NULL) lutstatus("Init",a_params.filename,lut_filename,R,G,B,(double)100); // printf("Init %s (%s) %d %d %d %4.2f(s)\n",a_params.filename,lut_filename,(int)R,(int)G,(int)B,minutes*60+seconds); #endif } if(isJpegFile(a)==1) free(image_buffer); return(EXIT_SUCCESS); } static inline double createValue(unsigned char a,double weight) { return(weight*powLookupTable[a]); } #ifndef OBJECT_COMPILE int main(int argc, char ** argv) { char * dest_filename=NULL; char * input_filename=NULL; char * lut_filename=NULL; double R,G,B; parse_commandline(argc, argv, &input_filename, &R,&G,&B,&dest_filename,&lut_filename); return(cementinit_lut(input_filename,dest_filename,lut_filename,R,G,B,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 [Imagespace file] -o [Lightspace file] \n" "\t [-lut [Imaging Device Response file]]\n\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"); fprintf(stderr, " [LookUp Table file] (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, double *R, double *G, double *B, char ** output_filename, char ** lut_filename) { if (argc<7) { fprintf(stderr,"error: number of input arguments was only %d\n",argc-1); usage(argv[0]); } *input_filename=(char*)strdup(argv[1]); 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(argv[5][0]!='-' || argv[5][1] !='o') usage(argv[0]); *output_filename=(char *)strdup(argv[6]); /* maali may 9 2000 */ if(argc>7) { if((argv[7][0]=='-')&&(argv[7][1]=='l')&&(argv[7][2]=='u')&&(argv[7][3]=='t')) { *lut_filename=(char *)strdup(argv[8]); } else { usage(argv[0]); } } } #endif