Contents of file 'epsimg/epsimg.c':
1 /*6:*/
2 #line 320 "./epsimg.w"
3
4 #include <math.h>
5 #include <stdio.h>
6 #include <stddef.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <time.h>
10 #include <ctype.h>
11
12 #define VERSION_NUMBER "1.6"
13
14 #define A4_PAGE_WIDTH (594)
15 #define A4_PAGE_HEIGHT (841)
16 #define MAXIMUM_IMAGE_WIDTH (A4_PAGE_WIDTH-144)
17 #define MAXIMUM_IMAGE_HEIGHT (A4_PAGE_HEIGHT-144)
18 #define DEFAULT_IMAGE_WIDTH (0.8*MAXIMUM_IMAGE_WIDTH)
19 #define DEFAULT_IMAGE_XCENTER (A4_PAGE_WIDTH/2)
20 #define DEFAULT_IMAGE_YCENTER (A4_PAGE_HEIGHT/2)
21 #define DEFAULT_LINETHICKNESS 1
22
23 #define OUTSTREAM (outfile_specified?fpout:stdout)
24 #define SUCCESS 0
25 #define FAILURE 1
26
27 #define COMPACTIFIED_PIXELCODE 1
28 #define EXTENSIVE_PIXELCODE 2
29
30 /*7:*/
31 #line 371 "./epsimg.w"
32
33 extern char*optarg;
34 char*progname;
35
36 /*:7*/
37 #line 347 "./epsimg.w"
38
39 /*8:*/
40 #line 377 "./epsimg.w"
41
42 /*9:*/
43 #line 388 "./epsimg.w"
44
45 void showsomehelp(void){
46 fprintf(stderr,"Usage: %s -i infile [options] [-o outfile]\n",progname);
47 fprintf(stderr,"Options:\n");
48 fprintf(stderr,
49 " -i, --inputfile <str> Specifies the file where to find the intensity\n"
50 " response for the actual property.\n");
51 fprintf(stderr,
52 " -o, --outputfile <str> Specifies the file where to save the trans-\n"
53 " mitted optical pulse shape. Whenever this\n");
54 fprintf(stderr,
55 " option is not present at the command line,\n"
56 " the generated time series will be written\n"
57 " to standard terminal output instead, in\n"
58 " which case any set verbose mode will be turned\n"
59 " off (see -v option).\n");
60 fprintf(stderr,
61 " -s, --sequential Toggle sequential mode. Default: off.\n"
62 " When generating the Encapsulated PostScript,\n"
63 " in sequential mode, the data\n");
64 fprintf(stderr,
65 " is scanned column/row-wise, with an individual\n"
66 " pixel written for each data point of the input\n"
67 " matrix. In this mode the program will ignore\n"
68 " any possibilities of reducing the data through\n"
69 " a more efficient partitioning of the input\n"
70 " matrix.\n");
71 fprintf(stderr,
72 " -v, --verbose Toggle verbose mode. If no output filename was\n"
73 " specified at the command line, verbose mode\n"
74 " will automatically be turned off, in order for\n"
75 " output messages not to interfere with the\n"
76 " generated Encapsulated PostScript code.\n"
77 " Default: off\n");
78 fprintf(stderr,
79 " -h, --help Display this help message and exit clean\n");
80 fprintf(stderr,
81 "Copyright (C) 2004 Fredrik Jonsson <jonsson@uni-wuppertal.de>\n");
82 }
83
84 /*:9*/
85 #line 378 "./epsimg.w"
86
87 /*10:*/
88 #line 431 "./epsimg.w"
89
90 double*dvector(long nl,long nh){
91 double*v;
92 v= (double*)malloc((size_t)((nh-nl+2)*sizeof(double)));
93 if(!v){
94 fprintf(stderr,"Error: Allocation failure in dvector()\n");
95 exit(FAILURE);
96 }
97 return v-nl+1;
98 }
99
100 /*:10*/
101 #line 379 "./epsimg.w"
102
103 /*11:*/
104 #line 446 "./epsimg.w"
105
106 double**dmatrix(long nrl,long nrh,long ncl,long nch){
107 long i,nrow= nrh-nrl+1,ncol= nch-ncl+1;
108 double**m;
109 m= (double**)malloc((size_t)((nrow+1)*sizeof(double*)));
110 if(!m){
111 fprintf(stderr,"%s: Allocation failure 1 in dmatrix() routine!\n",
112 progname);
113 exit(FAILURE);
114 }
115 m+= 1;
116 m-= nrl;
117 m[nrl]= (double*)malloc((size_t)((nrow*ncol+1)*sizeof(double)));
118 if(!m[nrl]){
119 fprintf(stderr,"%s: Allocation failure 2 in dmatrix() routine!\n",
120 progname);
121 exit(FAILURE);
122 }
123 m[nrl]+= 1;
124 m[nrl]-= ncl;
125 for(i= nrl+1;i<=nrh;i++)m[i]= m[i-1]+ncol;
126 return m;
127 }
128
129 /*:11*/
130 #line 380 "./epsimg.w"
131
132 /*12:*/
133 #line 473 "./epsimg.w"
134
135 void free_dvector(double*v,long nl,long nh){
136 free((char*)(v+nl-1));
137 }
138
139 /*:12*/
140 #line 381 "./epsimg.w"
141
142 /*13:*/
143 #line 481 "./epsimg.w"
144
145 void free_dmatrix(double**m,long nrl,long nrh,long ncl,long nch){
146 free((char*)(m[nrl]+ncl-1));
147 free((char*)(m+nrl-1));
148 }
149
150 /*:13*/
151 #line 382 "./epsimg.w"
152
153 /*14:*/
154 #line 512 "./epsimg.w"
155
156 char validchar(char ch){
157 return(isalnum(ch)||(ch=='+')||(ch=='-')||(ch=='.'));
158 }
159
160 double**load_matrix(char inputfilename[],long*nr,long*nc,
161 double*min,double*max){
162 FILE*fpin= NULL;
163 char tmpch;
164 long j,k,nrt,nct;
165 double tmpd,**m,tmin,tmax;
166 if((fpin= fopen(inputfilename,"r"))==NULL){
167 fprintf(stderr,"%s: Could not open file %s for reading!\n",
168 progname,inputfilename);
169 exit(FAILURE);
170 }
171
172 fseek(fpin,0L,SEEK_SET);
173 fscanf(fpin,"%lf",&tmpd);
174 tmin= tmpd;
175 tmax= tmpd;
176
177 fseek(fpin,0L,SEEK_SET);
178 nct= 0;
179 while((tmpch= getc(fpin))!='\n'){
180 ungetc(tmpch,fpin);
181 while((tmpch= getc(fpin))==' ');
182 ungetc(tmpch,fpin);
183 while(validchar(tmpch= getc(fpin)));
184 ungetc(tmpch,fpin);
185 nct++;
186 while((tmpch= getc(fpin))==' ');
187 ungetc(tmpch,fpin);
188 }
189
190 fseek(fpin,0L,SEEK_SET);
191 nrt= 0;
192 while((tmpch= getc(fpin))!=EOF){
193 ungetc(tmpch,fpin);
194 for(k= 1;k<=nct;k++)fscanf(fpin,"%lf",&tmpd);
195 nrt++;
196 tmpch= getc(fpin);
197 while((tmpch==' ')||(tmpch=='\n'))tmpch= getc(fpin);
198 if(tmpch!=EOF)ungetc(tmpch,fpin);
199 }
200 m= dmatrix(1,nrt,1,nct);
201
202 fseek(fpin,0L,SEEK_SET);
203 for(j= 1;j<=nrt;j++){
204 for(k= 1;k<=nct;k++){
205 fscanf(fpin,"%lf",&tmpd);
206 m[j][k]= tmpd;
207 if(tmpd<tmin)
208 tmin= tmpd;
209 else if(tmpd> tmax)
210 tmax= tmpd;
211 }
212 }
213 fclose(fpin);
214 *nr= nrt;
215 *nc= nct;
216 *min= tmin;
217 *max= tmax;
218 return m;
219 }
220
221 /*:14*/
222 #line 383 "./epsimg.w"
223
224 /*15:*/
225 #line 581 "./epsimg.w"
226
227 void unload_matrix(double**m,long nr,long nc){
228 free_dmatrix(m,1,nr,1,nc);
229 }
230
231 /*:15*/
232 #line 384 "./epsimg.w"
233
234
235 /*:8*/
236 #line 348 "./epsimg.w"
237
238
239 int main(int argc,char*argv[])
240 {
241 /*16:*/
242 #line 588 "./epsimg.w"
243
244 double**imagematrix,min,max,dx,dy,llx,lly,urx,ury;
245 double imagewidth,imageheight,imagexcenter,imageycenter;
246 double linethickness= DEFAULT_LINETHICKNESS;
247 time_t now= time(NULL);
248 long int j,k,nr,nc;
249 int no_arg,bbllx,bblly,bburx,bbury;
250 FILE*fpout= NULL;
251 char inputfilename[256]= "",outputfilename[256]= "";
252 short verbose= 0,write_floatform= 0,write_frame= 1;
253 short infile_specified= 0,outfile_specified= 0,parse_data_sequentially= 1;
254 short write_title= 0;
255 short pixel_generation_mode= COMPACTIFIED_PIXELCODE;
256 short comments_in_postscript= 1;
257
258 /*:16*/
259 #line 352 "./epsimg.w"
260
261 /*17:*/
262 #line 609 "./epsimg.w"
263
264 {
265 progname= argv[0];
266 no_arg= argc;
267 while(--argc){
268 if(!strcmp(argv[no_arg-argc],"-o")||
269 !strcmp(argv[no_arg-argc],"--outputfile")){
270 --argc;
271 strcpy(outputfilename,argv[no_arg-argc]);
272 outfile_specified= 1;
273 }else if(!strcmp(argv[no_arg-argc],"-i")||
274 !strcmp(argv[no_arg-argc],"--inputfile")){
275 --argc;
276 strcpy(inputfilename,argv[no_arg-argc]);
277 infile_specified= 1;
278 }else if((!strcmp(argv[no_arg-argc],"-f"))||
279 (!strcmp(argv[no_arg-argc],"--floatform"))){
280 write_floatform= (write_floatform?0:1);
281 if(verbose)fprintf(stdout,"%s: Using floating number output.\n",
282 progname);
283 }else if((!strcmp(argv[no_arg-argc],"-r"))||
284 (!strcmp(argv[no_arg-argc],"--writeframe"))){
285 write_frame= (write_frame?0:1);
286 }else if(!strcmp(argv[no_arg-argc],"--commmented_postscript")){
287 comments_in_postscript= 1;
288 }else if(!strcmp(argv[no_arg-argc],"--uncommmented_postscript")){
289 comments_in_postscript= 0;
290 }else if(!strcmp(argv[no_arg-argc],"--compactified_pixelcode")){
291 pixel_generation_mode= COMPACTIFIED_PIXELCODE;
292 }else if(!strcmp(argv[no_arg-argc],"--extensive_pixelcode")){
293 pixel_generation_mode= EXTENSIVE_PIXELCODE;
294 }else if(!strcmp(argv[no_arg-argc],"-v")||
295 !strcmp(argv[no_arg-argc],"--verbose")){
296 verbose= (verbose?0:1);
297 }else if(!strcmp(argv[no_arg-argc],"-s")||
298 !strcmp(argv[no_arg-argc],"--sequential")){
299 parse_data_sequentially= (parse_data_sequentially?0:1);
300 }else{
301 fprintf(stderr,"%s: Unknown option '%s'.\n",
302 progname,argv[no_arg-argc]);
303 exit(FAILURE);
304 }
305 }
306 if(!outfile_specified)verbose= 0;
307 }
308
309 /*:17*/
310 #line 353 "./epsimg.w"
311
312 /*19:*/
313 #line 659 "./epsimg.w"
314
315 {
316 if(outfile_specified){
317 if((fpout= fopen(outputfilename,"w"))==NULL){
318 fprintf(stderr,"%s: Could not open file %s for writing!\n",
319 progname,outputfilename);
320 exit(FAILURE);
321 }
322 fseek(fpout,0L,SEEK_SET);
323 }else{
324 if(verbose)
325 fprintf(stdout,"%s: No output file specified. (Writing to stdout).\n",
326 progname);
327 }
328 }
329
330 /*:19*/
331 #line 354 "./epsimg.w"
332
333 /*20:*/
334 #line 681 "./epsimg.w"
335
336 {
337 if(infile_specified){
338 if(verbose)fprintf(stderr,"%s: Loading data from file %s.\n",
339 progname,inputfilename);
340 imagematrix= load_matrix(inputfilename,&nr,&nc,&min,&max);
341 if(verbose){
342 fprintf(stdout,
343 "%s: Detected %ld rows and %ld columns of data in file '%s'.\n",
344 progname,nr,nc,inputfilename);
345 fprintf(stdout,
346 "%s: Maximum element in '%s': %f\n",progname,inputfilename,max);
347 fprintf(stdout,
348 "%s: Minimum element in '%s': %f\n",progname,inputfilename,min);
349 }
350 }else{
351 fprintf(stderr,"%s: Error: Specify an input filename.\n",progname);
352 showsomehelp();
353 exit(FAILURE);
354 }
355 }
356
357 /*:20*/
358 #line 355 "./epsimg.w"
359
360 /*21:*/
361 #line 707 "./epsimg.w"
362
363 {
364 if(verbose)fprintf(stdout,"%s: Normalizing image matrix.\n",progname);
365 for(j= 1;j<=nr;j++){
366 for(k= 1;k<=nc;k++){
367 imagematrix[j][k]= imagematrix[j][k]-min;
368 imagematrix[j][k]= imagematrix[j][k]/(max-min);
369 }
370 }
371 }
372
373 /*:21*/
374 #line 356 "./epsimg.w"
375
376 /*22:*/
377 #line 742 "./epsimg.w"
378
379 {
380 imagewidth= ((double)(DEFAULT_IMAGE_WIDTH));
381 imageheight= (((double)nr)/((double)nc))*((double)(DEFAULT_IMAGE_WIDTH));
382 imagexcenter= DEFAULT_IMAGE_XCENTER;
383 imageycenter= DEFAULT_IMAGE_YCENTER;
384 if(imageheight> MAXIMUM_IMAGE_HEIGHT){
385 if(verbose){
386 fprintf(stdout,"%s: Warning. I found that the height of ",progname);
387 fprintf(stdout,"the image exceeds its maximum\n");
388 fprintf(stdout,"%s: value of %d pt.\n",
389 progname,((int)MAXIMUM_IMAGE_HEIGHT));
390 fprintf(stdout,"%s: Will now instead scale the width of the image.\n",
391 progname);
392 }
393 imageheight= MAXIMUM_IMAGE_HEIGHT;
394 imagewidth= (((double)nc)/((double)nr))*imageheight;
395 }else{
396 if(verbose){
397 fprintf(stdout,"%s: Image height automatically scaled to ",progname);
398 fprintf(stdout,"width (to give equal aspect ratio).\n");
399 }
400 }
401 bbllx= imagexcenter-imagewidth/2.0;
402 bblly= imageycenter-imageheight/2.0;
403 bburx= imagexcenter+imagewidth/2.0;
404 bbury= imageycenter+imageheight/2.0;
405 }
406
407 /*:22*/
408 #line 357 "./epsimg.w"
409
410 /*23:*/
411 #line 822 "./epsimg.w"
412
413 {
414 fprintf(OUTSTREAM,"%%!PS-Adobe-2.0 EPSF-1.2\n");
415 fprintf(OUTSTREAM,"%%%%BoundingBox: %d %d %d %d\n",bbllx,bblly,bburx,bbury);
416 fprintf(OUTSTREAM,"%%%%Creator: epsimg %s",VERSION_NUMBER);
417 fprintf(OUTSTREAM," Copyright (C) 2004 Fredrik Jonsson\n");
418 if(outfile_specified)
419 fprintf(OUTSTREAM,"%%%%Title: %s\n",outputfilename);
420 else
421 fprintf(OUTSTREAM,"%%%%Title: (image written to stdout)\n");
422 fprintf(OUTSTREAM,"%%%%CreationDate: %s",ctime(&now));
423 fprintf(OUTSTREAM,"%%%%Pages: 1\n");
424 fprintf(OUTSTREAM,"%%%%EndProlog\n");
425 fprintf(OUTSTREAM,"%%%%Pages: 1\n");
426 fprintf(OUTSTREAM,"%%%%Page: 1 1\n");
427 if(pixel_generation_mode==COMPACTIFIED_PIXELCODE){
428 if(comments_in_postscript){
429 fprintf(OUTSTREAM,"%%\n");
430 fprintf(OUTSTREAM,"%% Routine for duplicating the bottom-most pair");
431 fprintf(OUTSTREAM," of elements in the stack.");
432 fprintf(OUTSTREAM,"%%\n");
433 }
434 fprintf(OUTSTREAM,"/dupc {dup 3 2 roll dup 4 1 roll exch} bind def");
435 if(comments_in_postscript){
436 fprintf(OUTSTREAM,"%%\n");
437 fprintf(OUTSTREAM,"%% Routine for calculating the lower right corner");
438 fprintf(OUTSTREAM," coordinates of the pixel.\n");
439 fprintf(OUTSTREAM,"%% The syntax is simply '<llx> <lly> <urx> <ury> ");
440 fprintf(OUTSTREAM," lrc', where (<llx>,<lly>)\n");
441 fprintf(OUTSTREAM,"%% and (<urx>,<ury>) are the");
442 fprintf(OUTSTREAM," lower left and upper right corner coordinates\n");
443 fprintf(OUTSTREAM,"%% of the pixel. The resulting (<lrx>,<lry>) pair");
444 fprintf(OUTSTREAM," are after the calculation\n%% pushed onto the");
445 fprintf(OUTSTREAM," stack, preserving the previously present stack");
446 fprintf(OUTSTREAM," at above\n%% levels.\n");
447 fprintf(OUTSTREAM,"%%\n");
448 }
449 fprintf(OUTSTREAM,"/lrc {4 1 roll dup 5 2 roll dup 5 -1 roll exch");
450 fprintf(OUTSTREAM," 4 2 roll 6 2 roll} bind def\n");
451 fprintf(OUTSTREAM,"/ulc {4 3 roll dup 5 2 roll dup 6 -1 roll exch}");
452 fprintf(OUTSTREAM," bind def\n");
453 if(comments_in_postscript){
454 fprintf(OUTSTREAM,"%%\n");
455 fprintf(OUTSTREAM,"%% Routine for drawing individual pixels\n");
456 fprintf(OUTSTREAM,"%%\n");
457 }
458 fprintf(OUTSTREAM,"/pixelstack {lrc 6 2 roll ulc 4 2 roll 8 4 roll");
459 fprintf(OUTSTREAM," dupc 10 2 roll} bind def\n");
460 fprintf(OUTSTREAM,"/drawpixel {setgray pixelstack newpath moveto lineto\n");
461 fprintf(OUTSTREAM," lineto lineto lineto closepath fill} bind def\n");
462
463
464 if(comments_in_postscript){
465 fprintf(OUTSTREAM,"%%\n");
466 fprintf(OUTSTREAM,"%% The dp routine is short-hand for drawpixel\n");
467 fprintf(OUTSTREAM,"%%\n");
468 }
469 fprintf(OUTSTREAM,"/dp {drawpixel} bind def\n");
470
471
472 }
473 }
474
475 /*:23*/
476 #line 358 "./epsimg.w"
477
478 /*24:*/
479 #line 887 "./epsimg.w"
480
481 {
482 if(parse_data_sequentially){
483 /*25:*/
484 #line 904 "./epsimg.w"
485
486 {
487 dx= ((double)(bburx-bbllx))/((double)nc);
488 dy= ((double)(bbury-bblly))/((double)nr);
489 for(j= 1;j<=nr;j++){
490 lly= ((double)bblly)+((double)(j-1))*dy;
491 ury= lly+dy*(1.0+8.0e-2);;
492 for(k= 1;k<=nc;k++){
493 llx= bbllx+((double)(k-1)*dx);
494 urx= llx+dx*(1.0+8.0e-2);
495 if(pixel_generation_mode==COMPACTIFIED_PIXELCODE){
496 fprintf(OUTSTREAM,"%1.2f %1.2f %1.2f %1.2f %1.3f dp\n",
497 llx,lly,urx,ury,imagematrix[j][k]);
498 }else{
499 fprintf(OUTSTREAM,"%1.3f setgray\n",imagematrix[j][k]);
500 fprintf(OUTSTREAM,"newpath %1.2f %1.2f moveto\n",llx,lly);
501 fprintf(OUTSTREAM," %1.2f %1.2f lineto",urx,lly);
502 fprintf(OUTSTREAM," %1.2f %1.2f lineto\n",urx,ury);
503 fprintf(OUTSTREAM," %1.2f %1.2f lineto",llx,ury);
504 fprintf(OUTSTREAM," %1.2f %1.2f lineto closepath fill\n",llx,lly);
505 }
506 }
507 }
508 if(0==1){
509 for(j= 1;j<=nr;j++){
510 for(k= 1;k<=nc;k++){
511 fprintf(stdout,"%2.4f ",imagematrix[j][k]);
512 }
513 fprintf(stdout,"\n");
514 }
515 }
516 }
517
518 /*:25*/
519 #line 890 "./epsimg.w"
520
521 }else{
522 /*26:*/
523 #line 939 "./epsimg.w"
524
525 {
526 fprintf(stdout,
527 "Not yet finished with non-sequential partitioning of data\n");
528 exit(-1);
529 }
530
531 /*:26*/
532 #line 892 "./epsimg.w"
533
534 }
535 }
536
537 /*:24*/
538 #line 359 "./epsimg.w"
539
540 /*27:*/
541 #line 948 "./epsimg.w"
542
543 {
544 if(write_frame){
545 fprintf(OUTSTREAM,"0 setgray 0 %1.2f dtransform truncate ",linethickness);
546 fprintf(OUTSTREAM,"idtransform setlinewidth pop\n");
547 fprintf(OUTSTREAM," [] 0 setdash 1 setlinejoin 10 setmiterlimit\n");
548 fprintf(OUTSTREAM,"newpath %d %d moveto\n",bbllx,bblly);
549 fprintf(OUTSTREAM," %d %d lineto",bburx,bblly);
550 fprintf(OUTSTREAM," %d %d lineto\n",bburx,bbury);
551 fprintf(OUTSTREAM," %d %d lineto",bbllx,bbury);
552 fprintf(OUTSTREAM," %d %d lineto closepath stroke\n",bbllx,bblly);
553 }
554 if(write_title){
555 fprintf(stderr,"Still to be finished!!\n");
556 exit(-1);
557 fprintf(OUTSTREAM,"%%IncludeResource: font Helvetica\n");
558 fprintf(OUTSTREAM,"/Helvetica /WindowsLatin1Encoding 120 FMSR\n");
559
560 fprintf(OUTSTREAM,"2345 2372 moveto\n");
561 fprintf(OUTSTREAM,"(Intensity distribution in observation plane) s\n");
562
563 fprintf(OUTSTREAM,"504 2372 moveto -90 rotate\n");
564 fprintf(OUTSTREAM,"(y [) s\n");
565 fprintf(OUTSTREAM,"90 rotate\n");
566
567 fprintf(OUTSTREAM,"%%IncludeResource: font Symbol\n");
568 fprintf(OUTSTREAM,"/Symbol /WindowsLatin1Encoding 120 FMSR\n");
569
570 fprintf(OUTSTREAM,"504 2540 moveto -90 rotate\n");
571 fprintf(OUTSTREAM,"(m) s\n");
572 fprintf(OUTSTREAM,"90 rotate\n");
573 fprintf(OUTSTREAM,"504 2372 moveto -90 rotate\n");
574 fprintf(OUTSTREAM,"(]) s\n");
575
576
577 }
578 fprintf(OUTSTREAM,"showpage\n");
579 fprintf(OUTSTREAM,"%%%%EOF\n");
580 }
581
582
583 /*:27*/
584 #line 360 "./epsimg.w"
585
586 /*28:*/
587 #line 991 "./epsimg.w"
588
589 {
590 unload_matrix(imagematrix,nr,nc);
591 }
592
593
594 /*:28*/
595 #line 361 "./epsimg.w"
596
597 /*29:*/
598 #line 999 "./epsimg.w"
599
600 {
601 fclose(fpout);
602 }
603
604 /*:29*/
605 #line 362 "./epsimg.w"
606
607 return(SUCCESS);
608 }
609
610 /*:6*/
611
Generated by ::viewsrc::