/*6:*/ #line 320 "./epsimg.w" #include #include #include #include #include #include #include #define VERSION_NUMBER "1.6" #define A4_PAGE_WIDTH (594) #define A4_PAGE_HEIGHT (841) #define MAXIMUM_IMAGE_WIDTH (A4_PAGE_WIDTH-144) #define MAXIMUM_IMAGE_HEIGHT (A4_PAGE_HEIGHT-144) #define DEFAULT_IMAGE_WIDTH (0.8*MAXIMUM_IMAGE_WIDTH) #define DEFAULT_IMAGE_XCENTER (A4_PAGE_WIDTH/2) #define DEFAULT_IMAGE_YCENTER (A4_PAGE_HEIGHT/2) #define DEFAULT_LINETHICKNESS 1 #define OUTSTREAM (outfile_specified?fpout:stdout) #define SUCCESS 0 #define FAILURE 1 #define COMPACTIFIED_PIXELCODE 1 #define EXTENSIVE_PIXELCODE 2 /*7:*/ #line 371 "./epsimg.w" extern char*optarg; char*progname; /*:7*/ #line 347 "./epsimg.w" /*8:*/ #line 377 "./epsimg.w" /*9:*/ #line 388 "./epsimg.w" void showsomehelp(void){ fprintf(stderr,"Usage: %s -i infile [options] [-o outfile]\n",progname); fprintf(stderr,"Options:\n"); fprintf(stderr, " -i, --inputfile Specifies the file where to find the intensity\n" " response for the actual property.\n"); fprintf(stderr, " -o, --outputfile Specifies the file where to save the trans-\n" " mitted optical pulse shape. Whenever this\n"); fprintf(stderr, " option is not present at the command line,\n" " the generated time series will be written\n" " to standard terminal output instead, in\n" " which case any set verbose mode will be turned\n" " off (see -v option).\n"); fprintf(stderr, " -s, --sequential Toggle sequential mode. Default: off.\n" " When generating the Encapsulated PostScript,\n" " in sequential mode, the data\n"); fprintf(stderr, " is scanned column/row-wise, with an individual\n" " pixel written for each data point of the input\n" " matrix. In this mode the program will ignore\n" " any possibilities of reducing the data through\n" " a more efficient partitioning of the input\n" " matrix.\n"); fprintf(stderr, " -v, --verbose Toggle verbose mode. If no output filename was\n" " specified at the command line, verbose mode\n" " will automatically be turned off, in order for\n" " output messages not to interfere with the\n" " generated Encapsulated PostScript code.\n" " Default: off\n"); fprintf(stderr, " -h, --help Display this help message and exit clean\n"); fprintf(stderr, "Copyright (C) 2004 Fredrik Jonsson \n"); } /*:9*/ #line 378 "./epsimg.w" /*10:*/ #line 431 "./epsimg.w" double*dvector(long nl,long nh){ double*v; v= (double*)malloc((size_t)((nh-nl+2)*sizeof(double))); if(!v){ fprintf(stderr,"Error: Allocation failure in dvector()\n"); exit(FAILURE); } return v-nl+1; } /*:10*/ #line 379 "./epsimg.w" /*11:*/ #line 446 "./epsimg.w" double**dmatrix(long nrl,long nrh,long ncl,long nch){ long i,nrow= nrh-nrl+1,ncol= nch-ncl+1; double**m; m= (double**)malloc((size_t)((nrow+1)*sizeof(double*))); if(!m){ fprintf(stderr,"%s: Allocation failure 1 in dmatrix() routine!\n", progname); exit(FAILURE); } m+= 1; m-= nrl; m[nrl]= (double*)malloc((size_t)((nrow*ncol+1)*sizeof(double))); if(!m[nrl]){ fprintf(stderr,"%s: Allocation failure 2 in dmatrix() routine!\n", progname); exit(FAILURE); } m[nrl]+= 1; m[nrl]-= ncl; for(i= nrl+1;i<=nrh;i++)m[i]= m[i-1]+ncol; return m; } /*:11*/ #line 380 "./epsimg.w" /*12:*/ #line 473 "./epsimg.w" void free_dvector(double*v,long nl,long nh){ free((char*)(v+nl-1)); } /*:12*/ #line 381 "./epsimg.w" /*13:*/ #line 481 "./epsimg.w" void free_dmatrix(double**m,long nrl,long nrh,long ncl,long nch){ free((char*)(m[nrl]+ncl-1)); free((char*)(m+nrl-1)); } /*:13*/ #line 382 "./epsimg.w" /*14:*/ #line 512 "./epsimg.w" char validchar(char ch){ return(isalnum(ch)||(ch=='+')||(ch=='-')||(ch=='.')); } double**load_matrix(char inputfilename[],long*nr,long*nc, double*min,double*max){ FILE*fpin= NULL; char tmpch; long j,k,nrt,nct; double tmpd,**m,tmin,tmax; if((fpin= fopen(inputfilename,"r"))==NULL){ fprintf(stderr,"%s: Could not open file %s for reading!\n", progname,inputfilename); exit(FAILURE); } fseek(fpin,0L,SEEK_SET); fscanf(fpin,"%lf",&tmpd); tmin= tmpd; tmax= tmpd; fseek(fpin,0L,SEEK_SET); nct= 0; while((tmpch= getc(fpin))!='\n'){ ungetc(tmpch,fpin); while((tmpch= getc(fpin))==' '); ungetc(tmpch,fpin); while(validchar(tmpch= getc(fpin))); ungetc(tmpch,fpin); nct++; while((tmpch= getc(fpin))==' '); ungetc(tmpch,fpin); } fseek(fpin,0L,SEEK_SET); nrt= 0; while((tmpch= getc(fpin))!=EOF){ ungetc(tmpch,fpin); for(k= 1;k<=nct;k++)fscanf(fpin,"%lf",&tmpd); nrt++; tmpch= getc(fpin); while((tmpch==' ')||(tmpch=='\n'))tmpch= getc(fpin); if(tmpch!=EOF)ungetc(tmpch,fpin); } m= dmatrix(1,nrt,1,nct); fseek(fpin,0L,SEEK_SET); for(j= 1;j<=nrt;j++){ for(k= 1;k<=nct;k++){ fscanf(fpin,"%lf",&tmpd); m[j][k]= tmpd; if(tmpd tmax) tmax= tmpd; } } fclose(fpin); *nr= nrt; *nc= nct; *min= tmin; *max= tmax; return m; } /*:14*/ #line 383 "./epsimg.w" /*15:*/ #line 581 "./epsimg.w" void unload_matrix(double**m,long nr,long nc){ free_dmatrix(m,1,nr,1,nc); } /*:15*/ #line 384 "./epsimg.w" /*:8*/ #line 348 "./epsimg.w" int main(int argc,char*argv[]) { /*16:*/ #line 588 "./epsimg.w" double**imagematrix,min,max,dx,dy,llx,lly,urx,ury; double imagewidth,imageheight,imagexcenter,imageycenter; double linethickness= DEFAULT_LINETHICKNESS; time_t now= time(NULL); long int j,k,nr,nc; int no_arg,bbllx,bblly,bburx,bbury; FILE*fpout= NULL; char inputfilename[256]= "",outputfilename[256]= ""; short verbose= 0,write_floatform= 0,write_frame= 1; short infile_specified= 0,outfile_specified= 0,parse_data_sequentially= 1; short write_title= 0; short pixel_generation_mode= COMPACTIFIED_PIXELCODE; short comments_in_postscript= 1; /*:16*/ #line 352 "./epsimg.w" /*17:*/ #line 609 "./epsimg.w" { progname= argv[0]; no_arg= argc; while(--argc){ if(!strcmp(argv[no_arg-argc],"-o")|| !strcmp(argv[no_arg-argc],"--outputfile")){ --argc; strcpy(outputfilename,argv[no_arg-argc]); outfile_specified= 1; }else if(!strcmp(argv[no_arg-argc],"-i")|| !strcmp(argv[no_arg-argc],"--inputfile")){ --argc; strcpy(inputfilename,argv[no_arg-argc]); infile_specified= 1; }else if((!strcmp(argv[no_arg-argc],"-f"))|| (!strcmp(argv[no_arg-argc],"--floatform"))){ write_floatform= (write_floatform?0:1); if(verbose)fprintf(stdout,"%s: Using floating number output.\n", progname); }else if((!strcmp(argv[no_arg-argc],"-r"))|| (!strcmp(argv[no_arg-argc],"--writeframe"))){ write_frame= (write_frame?0:1); }else if(!strcmp(argv[no_arg-argc],"--commmented_postscript")){ comments_in_postscript= 1; }else if(!strcmp(argv[no_arg-argc],"--uncommmented_postscript")){ comments_in_postscript= 0; }else if(!strcmp(argv[no_arg-argc],"--compactified_pixelcode")){ pixel_generation_mode= COMPACTIFIED_PIXELCODE; }else if(!strcmp(argv[no_arg-argc],"--extensive_pixelcode")){ pixel_generation_mode= EXTENSIVE_PIXELCODE; }else if(!strcmp(argv[no_arg-argc],"-v")|| !strcmp(argv[no_arg-argc],"--verbose")){ verbose= (verbose?0:1); }else if(!strcmp(argv[no_arg-argc],"-s")|| !strcmp(argv[no_arg-argc],"--sequential")){ parse_data_sequentially= (parse_data_sequentially?0:1); }else{ fprintf(stderr,"%s: Unknown option '%s'.\n", progname,argv[no_arg-argc]); exit(FAILURE); } } if(!outfile_specified)verbose= 0; } /*:17*/ #line 353 "./epsimg.w" /*19:*/ #line 659 "./epsimg.w" { if(outfile_specified){ if((fpout= fopen(outputfilename,"w"))==NULL){ fprintf(stderr,"%s: Could not open file %s for writing!\n", progname,outputfilename); exit(FAILURE); } fseek(fpout,0L,SEEK_SET); }else{ if(verbose) fprintf(stdout,"%s: No output file specified. (Writing to stdout).\n", progname); } } /*:19*/ #line 354 "./epsimg.w" /*20:*/ #line 681 "./epsimg.w" { if(infile_specified){ if(verbose)fprintf(stderr,"%s: Loading data from file %s.\n", progname,inputfilename); imagematrix= load_matrix(inputfilename,&nr,&nc,&min,&max); if(verbose){ fprintf(stdout, "%s: Detected %ld rows and %ld columns of data in file '%s'.\n", progname,nr,nc,inputfilename); fprintf(stdout, "%s: Maximum element in '%s': %f\n",progname,inputfilename,max); fprintf(stdout, "%s: Minimum element in '%s': %f\n",progname,inputfilename,min); } }else{ fprintf(stderr,"%s: Error: Specify an input filename.\n",progname); showsomehelp(); exit(FAILURE); } } /*:20*/ #line 355 "./epsimg.w" /*21:*/ #line 707 "./epsimg.w" { if(verbose)fprintf(stdout,"%s: Normalizing image matrix.\n",progname); for(j= 1;j<=nr;j++){ for(k= 1;k<=nc;k++){ imagematrix[j][k]= imagematrix[j][k]-min; imagematrix[j][k]= imagematrix[j][k]/(max-min); } } } /*:21*/ #line 356 "./epsimg.w" /*22:*/ #line 742 "./epsimg.w" { imagewidth= ((double)(DEFAULT_IMAGE_WIDTH)); imageheight= (((double)nr)/((double)nc))*((double)(DEFAULT_IMAGE_WIDTH)); imagexcenter= DEFAULT_IMAGE_XCENTER; imageycenter= DEFAULT_IMAGE_YCENTER; if(imageheight> MAXIMUM_IMAGE_HEIGHT){ if(verbose){ fprintf(stdout,"%s: Warning. I found that the height of ",progname); fprintf(stdout,"the image exceeds its maximum\n"); fprintf(stdout,"%s: value of %d pt.\n", progname,((int)MAXIMUM_IMAGE_HEIGHT)); fprintf(stdout,"%s: Will now instead scale the width of the image.\n", progname); } imageheight= MAXIMUM_IMAGE_HEIGHT; imagewidth= (((double)nc)/((double)nr))*imageheight; }else{ if(verbose){ fprintf(stdout,"%s: Image height automatically scaled to ",progname); fprintf(stdout,"width (to give equal aspect ratio).\n"); } } bbllx= imagexcenter-imagewidth/2.0; bblly= imageycenter-imageheight/2.0; bburx= imagexcenter+imagewidth/2.0; bbury= imageycenter+imageheight/2.0; } /*:22*/ #line 357 "./epsimg.w" /*23:*/ #line 822 "./epsimg.w" { fprintf(OUTSTREAM,"%%!PS-Adobe-2.0 EPSF-1.2\n"); fprintf(OUTSTREAM,"%%%%BoundingBox: %d %d %d %d\n",bbllx,bblly,bburx,bbury); fprintf(OUTSTREAM,"%%%%Creator: epsimg %s",VERSION_NUMBER); fprintf(OUTSTREAM," Copyright (C) 2004 Fredrik Jonsson\n"); if(outfile_specified) fprintf(OUTSTREAM,"%%%%Title: %s\n",outputfilename); else fprintf(OUTSTREAM,"%%%%Title: (image written to stdout)\n"); fprintf(OUTSTREAM,"%%%%CreationDate: %s",ctime(&now)); fprintf(OUTSTREAM,"%%%%Pages: 1\n"); fprintf(OUTSTREAM,"%%%%EndProlog\n"); fprintf(OUTSTREAM,"%%%%Pages: 1\n"); fprintf(OUTSTREAM,"%%%%Page: 1 1\n"); if(pixel_generation_mode==COMPACTIFIED_PIXELCODE){ if(comments_in_postscript){ fprintf(OUTSTREAM,"%%\n"); fprintf(OUTSTREAM,"%% Routine for duplicating the bottom-most pair"); fprintf(OUTSTREAM," of elements in the stack."); fprintf(OUTSTREAM,"%%\n"); } fprintf(OUTSTREAM,"/dupc {dup 3 2 roll dup 4 1 roll exch} bind def"); if(comments_in_postscript){ fprintf(OUTSTREAM,"%%\n"); fprintf(OUTSTREAM,"%% Routine for calculating the lower right corner"); fprintf(OUTSTREAM," coordinates of the pixel.\n"); fprintf(OUTSTREAM,"%% The syntax is simply ' "); fprintf(OUTSTREAM," lrc', where (,)\n"); fprintf(OUTSTREAM,"%% and (,) are the"); fprintf(OUTSTREAM," lower left and upper right corner coordinates\n"); fprintf(OUTSTREAM,"%% of the pixel. The resulting (,) pair"); fprintf(OUTSTREAM," are after the calculation\n%% pushed onto the"); fprintf(OUTSTREAM," stack, preserving the previously present stack"); fprintf(OUTSTREAM," at above\n%% levels.\n"); fprintf(OUTSTREAM,"%%\n"); } fprintf(OUTSTREAM,"/lrc {4 1 roll dup 5 2 roll dup 5 -1 roll exch"); fprintf(OUTSTREAM," 4 2 roll 6 2 roll} bind def\n"); fprintf(OUTSTREAM,"/ulc {4 3 roll dup 5 2 roll dup 6 -1 roll exch}"); fprintf(OUTSTREAM," bind def\n"); if(comments_in_postscript){ fprintf(OUTSTREAM,"%%\n"); fprintf(OUTSTREAM,"%% Routine for drawing individual pixels\n"); fprintf(OUTSTREAM,"%%\n"); } fprintf(OUTSTREAM,"/pixelstack {lrc 6 2 roll ulc 4 2 roll 8 4 roll"); fprintf(OUTSTREAM," dupc 10 2 roll} bind def\n"); fprintf(OUTSTREAM,"/drawpixel {setgray pixelstack newpath moveto lineto\n"); fprintf(OUTSTREAM," lineto lineto lineto closepath fill} bind def\n"); if(comments_in_postscript){ fprintf(OUTSTREAM,"%%\n"); fprintf(OUTSTREAM,"%% The dp routine is short-hand for drawpixel\n"); fprintf(OUTSTREAM,"%%\n"); } fprintf(OUTSTREAM,"/dp {drawpixel} bind def\n"); } } /*:23*/ #line 358 "./epsimg.w" /*24:*/ #line 887 "./epsimg.w" { if(parse_data_sequentially){ /*25:*/ #line 904 "./epsimg.w" { dx= ((double)(bburx-bbllx))/((double)nc); dy= ((double)(bbury-bblly))/((double)nr); for(j= 1;j<=nr;j++){ lly= ((double)bblly)+((double)(j-1))*dy; ury= lly+dy*(1.0+8.0e-2);; for(k= 1;k<=nc;k++){ llx= bbllx+((double)(k-1)*dx); urx= llx+dx*(1.0+8.0e-2); if(pixel_generation_mode==COMPACTIFIED_PIXELCODE){ fprintf(OUTSTREAM,"%1.2f %1.2f %1.2f %1.2f %1.3f dp\n", llx,lly,urx,ury,imagematrix[j][k]); }else{ fprintf(OUTSTREAM,"%1.3f setgray\n",imagematrix[j][k]); fprintf(OUTSTREAM,"newpath %1.2f %1.2f moveto\n",llx,lly); fprintf(OUTSTREAM," %1.2f %1.2f lineto",urx,lly); fprintf(OUTSTREAM," %1.2f %1.2f lineto\n",urx,ury); fprintf(OUTSTREAM," %1.2f %1.2f lineto",llx,ury); fprintf(OUTSTREAM," %1.2f %1.2f lineto closepath fill\n",llx,lly); } } } if(0==1){ for(j= 1;j<=nr;j++){ for(k= 1;k<=nc;k++){ fprintf(stdout,"%2.4f ",imagematrix[j][k]); } fprintf(stdout,"\n"); } } } /*:25*/ #line 890 "./epsimg.w" }else{ /*26:*/ #line 939 "./epsimg.w" { fprintf(stdout, "Not yet finished with non-sequential partitioning of data\n"); exit(-1); } /*:26*/ #line 892 "./epsimg.w" } } /*:24*/ #line 359 "./epsimg.w" /*27:*/ #line 948 "./epsimg.w" { if(write_frame){ fprintf(OUTSTREAM,"0 setgray 0 %1.2f dtransform truncate ",linethickness); fprintf(OUTSTREAM,"idtransform setlinewidth pop\n"); fprintf(OUTSTREAM," [] 0 setdash 1 setlinejoin 10 setmiterlimit\n"); fprintf(OUTSTREAM,"newpath %d %d moveto\n",bbllx,bblly); fprintf(OUTSTREAM," %d %d lineto",bburx,bblly); fprintf(OUTSTREAM," %d %d lineto\n",bburx,bbury); fprintf(OUTSTREAM," %d %d lineto",bbllx,bbury); fprintf(OUTSTREAM," %d %d lineto closepath stroke\n",bbllx,bblly); } if(write_title){ fprintf(stderr,"Still to be finished!!\n"); exit(-1); fprintf(OUTSTREAM,"%%IncludeResource: font Helvetica\n"); fprintf(OUTSTREAM,"/Helvetica /WindowsLatin1Encoding 120 FMSR\n"); fprintf(OUTSTREAM,"2345 2372 moveto\n"); fprintf(OUTSTREAM,"(Intensity distribution in observation plane) s\n"); fprintf(OUTSTREAM,"504 2372 moveto -90 rotate\n"); fprintf(OUTSTREAM,"(y [) s\n"); fprintf(OUTSTREAM,"90 rotate\n"); fprintf(OUTSTREAM,"%%IncludeResource: font Symbol\n"); fprintf(OUTSTREAM,"/Symbol /WindowsLatin1Encoding 120 FMSR\n"); fprintf(OUTSTREAM,"504 2540 moveto -90 rotate\n"); fprintf(OUTSTREAM,"(m) s\n"); fprintf(OUTSTREAM,"90 rotate\n"); fprintf(OUTSTREAM,"504 2372 moveto -90 rotate\n"); fprintf(OUTSTREAM,"(]) s\n"); } fprintf(OUTSTREAM,"showpage\n"); fprintf(OUTSTREAM,"%%%%EOF\n"); } /*:27*/ #line 360 "./epsimg.w" /*28:*/ #line 991 "./epsimg.w" { unload_matrix(imagematrix,nr,nc); } /*:28*/ #line 361 "./epsimg.w" /*29:*/ #line 999 "./epsimg.w" { fclose(fpout); } /*:29*/ #line 362 "./epsimg.w" return(SUCCESS); } /*:6*/