r121: *** empty log message ***
[ctsim.git] / src / if2img.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          if2img.cpp
5 **   Purpose:       Convert an image file to a viewable image
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  April 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: if2img.cpp,v 1.9 2000/06/26 21:15:24 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 /* FILE
29  *   if2img.c           Convert an SDF file to a viewable format image
30  */
31
32 #include "ct.h"
33
34 #if HAVE_PNG
35 void sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax);
36 #endif
37 #if HAVE_GIF
38 void sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
39 #endif
40 void sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
41 void sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
42
43 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS, 
44        O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
45
46 static struct option my_options[] =
47 {
48   {"scale", 1, 0, O_SCALE},
49   {"min", 1, 0, O_MIN},
50   {"max", 1, 0, O_MAX},
51   {"auto", 1, 0, O_AUTO},
52   {"center", 1, 0, O_CENTER},
53   {"format", 1, 0, O_FORMAT},
54   {"labels", 0, 0, O_LABELS},
55   {"stats", 0, 0, O_STATS},
56   {"verbose", 0, 0, O_VERBOSE},
57   {"debug", 0, 0, O_DEBUG},
58   {"help", 0, 0, O_HELP},
59   {"version", 0, 0, O_VERSION},
60   {0, 0, 0, 0}
61 };
62
63 enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
64 #define O_AUTO_FULL_STR  "full"
65 #define O_AUTO_STD0_1_STR  "std0.1"
66 #define O_AUTO_STD0_5_STR  "std0.5"
67 #define O_AUTO_STD1_STR  "std1"
68 #define O_AUTO_STD2_STR  "std2"
69 #define O_AUTO_STD3_STR  "std3"
70
71 enum { O_CENTER_MEAN, O_CENTER_MODE };
72 #define O_CENTER_MEAN_STR  "mean"
73 #define O_CENTER_MODE_STR  "mode"
74
75 enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
76 #define O_FORMAT_GIF_STR   "gif"
77 #define O_FORMAT_PNG_STR   "png" 
78 #define O_FORMAT_PNG16_STR   "png16" 
79 #define O_FORMAT_PGM_STR   "pgm"
80 #define O_FORMAT_PGMASC_STR "pgmasc"
81 #define O_FORMAT_DISP_STR    "disp"
82
83 void 
84 if2img_usage (const char *program)
85 {
86   cout << "usage: " << fileBasename(program) << " ifname outfile [OPTIONS]" << endl;
87   cout << "Convert IF file to an image file" << endl;
88   cout << endl;
89   cout << "     sdfname    Name of input SDF file" << endl;
90   cout << "     outfile    Name of output file" << endl;
91   cout << "     --format   Output format" << endl;
92   cout << "         pgm    PGM (portable graymap) format (default)" << endl;
93   cout << "         pgmasc PGM (portable graymap) ASCII format" << endl;
94 #ifdef HAVE_PNG
95   cout << "         png    PNG (8-bit) format" << endl;
96   cout << "         png16  PNG (16-bit) format" << endl;
97 #endif
98 #ifdef HAVE_GIF
99   cout << "         gif    GIF format" << endl;
100 #endif
101   cout << "         disp   Display on screen" << endl;
102   cout << "     --center   Center of window" << endl;
103   cout << "         mode   Mode is center of window (default)" << endl;
104   cout << "         mean   Mean is center of window" << endl;
105   cout << "     --auto     Set auto window" << endl;
106   cout << "         full   Use full window (default)" << endl;
107   cout << "         std0.1 Use 0.1 standard deviation about center" << endl;
108   cout << "         std0.5 Use 0.5 standard deviation about center" << endl;
109   cout << "         std1   Use one standard deviation about center" << endl;
110   cout << "         std2   Use two standard deviations about center" << endl;
111   cout << "         std3   Use three standard deviations about center" << endl;
112   cout << "     --scale    Scaling factor for output size" << endl;
113   cout << "     --min      Set minimum intensity" << endl;
114   cout << "     --max      Set maximum intensity" << endl;
115   cout << "     --stats    Print image statistics" << endl;
116   cout << "     --labels   Print image labels" << endl;
117   cout << "     --debug    Set debug mode" << endl;
118   cout << "     --verbose  Set verbose mode" << endl;
119   cout << "     --version  Print version" << endl;
120   cout << "     --help     Print this help message" << endl;
121 }
122
123
124 int 
125 if2img_main (int argc, char *const argv[])
126 {
127   ImageFile* pim = NULL;
128
129   double densmin = HUGE_VAL, densmax = -HUGE_VAL;
130   char *in_file, *out_file;
131   int opt_verbose = 0;
132   int opt_scale = 1;
133   int opt_auto = O_AUTO_FULL;
134   int opt_set_max = 0;
135   int opt_set_min = 0;
136   int opt_stats = 0;
137   int opt_debug = 0;
138   int opt_center = O_CENTER_MODE;
139   int opt_format = O_FORMAT_PGM;
140   int opt_labels = 0;
141
142   while (1)
143     {
144       int c = getopt_long (argc, argv, "", my_options, NULL);
145       char *endptr = NULL;
146       char *endstr;
147       
148       if (c == -1)
149         break;
150       
151       switch (c)
152         {
153         case O_MIN:
154           opt_set_min = 1;
155           densmin = strtod(optarg, &endptr);
156           endstr = optarg + strlen(optarg);
157           if (endptr != endstr)
158             {
159               sys_error (ERR_SEVERE, "Error setting --min to %s", optarg);
160               if2img_usage(argv[0]);
161               return (1);
162             }
163           break;
164         case O_MAX:
165           opt_set_max = 1;
166           densmax = strtod(optarg, &endptr);
167           endstr = optarg + strlen(optarg);
168           if (endptr != endstr)
169             {
170               sys_error (ERR_SEVERE, "Error setting --max to %s", optarg);
171               if2img_usage(argv[0]);
172               return (1);
173             }
174           break;
175         case O_SCALE:
176           opt_scale = strtol(optarg, &endptr, 10);
177           endstr = optarg + strlen(optarg);
178           if (endptr != endstr)
179             {
180               sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg);
181               if2img_usage(argv[0]);
182               return (1);
183             }
184           break;
185         case O_AUTO:
186           if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
187             opt_auto = O_AUTO_FULL;
188           else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
189             opt_auto = O_AUTO_STD1;
190           else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
191             opt_auto = O_AUTO_STD0_5;
192           else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
193             opt_auto = O_AUTO_STD0_1;
194           else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
195             opt_auto = O_AUTO_STD2;
196           else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
197             opt_auto = O_AUTO_STD3;
198           else
199             {
200               sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg);
201               if2img_usage(argv[0]);
202               return (1);
203             }
204                 break;
205         case O_CENTER:
206           if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
207             opt_center = O_CENTER_MEAN;
208           else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
209             opt_center = O_CENTER_MODE;
210           else
211             {
212               sys_error (ERR_SEVERE, "Invalid center mode %s", optarg);
213               if2img_usage(argv[0]);
214               return (1);
215             }
216           break;
217         case O_FORMAT:
218           if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
219             opt_format = O_FORMAT_PGM;
220           else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
221             opt_format = O_FORMAT_PGMASC;
222 #if HAVE_PNG
223           else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
224             opt_format = O_FORMAT_PNG;
225           else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
226             opt_format = O_FORMAT_PNG16;
227 #endif
228 #if HAVE_GIF
229           else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
230             opt_format = O_FORMAT_GIF;
231 #endif
232           else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
233             opt_format = O_FORMAT_DISP;
234           else {
235               sys_error (ERR_SEVERE, "Invalid format mode %s", optarg);
236               if2img_usage(argv[0]);
237               return (1);
238             }
239           break;
240         case O_LABELS:
241           opt_labels = 1;
242           break;
243         case O_VERBOSE:
244           opt_verbose = 1;
245           break;
246         case O_DEBUG:
247           opt_debug = 1;
248           break;
249         case O_STATS:
250           opt_stats = 1;
251           break;
252         case O_VERSION:
253 #ifdef VERSION
254           cout << "Version " << VERSION;
255 #else
256           cout << "Unknown version number" << endl;
257 #endif
258           return (0);
259         case O_HELP:
260         case '?':
261           if2img_usage(argv[0]);
262           return (0);
263         default:
264           if2img_usage(argv[0]);
265           return (1);
266         }
267     }
268
269   if ((opt_format == O_FORMAT_DISP && optind + 1 != argc) 
270       || (opt_format != O_FORMAT_DISP && optind + 2 != argc))
271     {
272         if2img_usage(argv[0]);
273         return (1);
274     }
275   
276   in_file = argv[optind];
277
278   if (opt_format != O_FORMAT_DISP)
279       out_file = argv[optind+1];
280   else out_file = NULL;
281
282   pim = new ImageFile ();
283   ImageFile& im = *pim;
284   if (! im.fileRead(in_file)) {
285     sys_error (ERR_FATAL, "File %s does not exist", in_file);
286     return (1);
287   }
288
289   if (opt_labels)
290     im.printLabels(cout);
291
292   if (opt_stats || (! (opt_set_max && opt_set_min))) {
293     double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
294     double mode = 0, mean = 0, stddev = 0, window = 0;
295     double spread;
296     int hist[256];
297     int ibin, nbin = 256;
298     int max_bin, max_binindex; 
299     double maxbin;
300     int nx = im.nx();
301     int ny = im.ny();
302     ImageFileArray v = im.getArray();
303     
304     maxbin = nbin - 1;
305     for (int ix = 0; ix < nx; ix++) {
306       for (int iy = 0; iy < ny; iy++) {
307         if (v[ix][iy] > maxfound)
308           maxfound = v[ix][iy];
309         if (v[ix][iy] < minfound)
310           minfound = v[ix][iy];
311         mean += v[ix][iy];
312       }
313     }
314     spread = maxfound - minfound;
315     if (spread == 0)
316       mode = minfound;
317     else {
318       for (ibin = 0; ibin < nbin; ibin++)
319         hist[ibin] = 0;
320       for (int ix = 0; ix < nx; ix++) {
321         for (int iy = 0; iy < ny; iy++) {
322           int b = (int) ((((v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
323           hist[b]++;
324         }
325       }
326       max_binindex = 0;
327       max_bin = -1;
328       for (ibin = 0; ibin < nbin; ibin++) {
329         if (hist[ibin] > max_bin) {
330           max_bin = hist[ibin];
331           max_binindex = ibin;
332         }
333       }
334       mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
335     }
336     
337     if (opt_auto == O_AUTO_FULL) {
338         if (! opt_set_max)
339           densmax = maxfound;
340         if (! opt_set_min)
341           densmin = minfound;
342     }
343     if (opt_stats || opt_auto != O_AUTO_FULL) {
344       mean /= nx * ny;
345       for (int ix = 0; ix < nx; ix++) {
346         for (int iy = 0; iy < ny; iy++) {
347             double diff = (v[ix][iy] - mean);
348             stddev += diff * diff;
349         }
350       }
351       stddev = sqrt(stddev / (nx * ny));
352       if (opt_auto == O_AUTO_FULL)
353         ;
354       else if (opt_auto == O_AUTO_STD1)
355             window = stddev;
356       else if (opt_auto == O_AUTO_STD0_1)
357         window = stddev * 0.1;
358       else if (opt_auto == O_AUTO_STD0_5)
359         window = stddev * 0.5;
360           else if (opt_auto == O_AUTO_STD2)
361             window = stddev * 2;
362       else if (opt_auto == O_AUTO_STD3)
363         window = stddev * 3;
364       else {
365         sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
366         return (1);
367       }
368     }
369     if (opt_stats) {
370       cout <<"nx: " << nx << endl;
371       cout <<"ny: " << ny << endl;
372       cout <<"min: " << minfound << endl;
373       cout <<"max: " << maxfound << endl;
374       cout <<"mean: " << mean << endl;
375       cout <<"mode: " << mode << endl;
376       cout <<"stddev: " << stddev << endl;
377     }
378     if (opt_auto != O_AUTO_FULL) {
379       double center;
380       
381       if (opt_center == O_CENTER_MODE)
382         center = mode;
383       else if (opt_center == O_CENTER_MEAN)
384         center = mean;
385       else {
386         sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
387         return (1);
388       }
389       if (! opt_set_max)
390         densmax = center + window;
391       if (! opt_set_min)
392         densmin = center - window;
393       }
394   }
395   
396   if (opt_stats) {
397     cout << "min display: " << densmin << endl;
398     cout << "max display: " << densmax << endl;
399   }
400   
401   if (opt_format == O_FORMAT_PGM)
402     sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
403   else if (opt_format == O_FORMAT_PGMASC)
404     sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
405 #if HAVE_PNG
406   else if (opt_format == O_FORMAT_PNG)
407     sdf2d_to_png (im, out_file, 8, opt_scale, opt_scale, densmin, densmax);
408   else if (opt_format == O_FORMAT_PNG16)
409     sdf2d_to_png (im, out_file, 16, opt_scale, opt_scale, densmin, densmax);
410 #endif
411 #if HAVE_GIF
412   else if (opt_format == O_FORMAT_GIF) 
413     sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
414 #endif
415   else if (opt_format == O_FORMAT_DISP) {
416 #if HAVE_SGP
417     // image_display_scale (im, opt_scale, densmin, densmax);
418     //  cio_kb_getc();
419       sgp2_close(sgp2_get_active_win());
420 #endif
421   }
422   else
423     {
424       sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
425       return (1);
426     }
427   return (0);
428 }
429
430
431 void 
432 sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
433 {
434   FILE *fp;
435   int nx = im.nx();
436   int ny = im.ny();
437   ImageFileArray v = im.getArray();
438
439   unsigned char rowp [nx * nxcell];
440
441   if ((fp = fopen (outfile, "wb")) == NULL)
442      return;
443
444   fprintf(fp, "P5\n");
445   fprintf(fp, "%d %d\n", nx, ny);
446   fprintf(fp, "255\n");
447
448   for (int irow = ny - 1; irow >= 0; irow--) {
449     for (int icol = 0; icol < nx; icol++) {
450       int pos = icol * nxcell;
451       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
452       dens = clamp (dens, 0., 1.);
453       for (int p = pos; p < pos + nxcell; p++) {
454         rowp[p] = static_cast<unsigned int> (dens * 255.);
455       }
456     }
457     for (int ir = 0; ir < nycell; ir++) {
458       for (int ic = 0; ic < nx * nxcell; ic++) 
459         fprintf(fp, "%c ", rowp[ic]);
460     }
461   }
462
463   fclose(fp);
464 }
465
466 void 
467 sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
468 {
469   FILE *fp;
470   int nx = im.nx();
471   int ny = im.ny();
472   ImageFileArray v = im.getArray();
473
474   unsigned char rowp [nx * nxcell];
475   if (rowp == NULL)
476     return;
477
478   if ((fp = fopen (outfile, "wb")) == NULL)
479      return;
480
481   fprintf(fp, "P2\n");
482   fprintf(fp, "%d %d\n", nx, ny);
483   fprintf(fp, "255\n");
484
485   for (int irow = ny - 1; irow >= 0; irow--) {
486     for (int icol = 0; icol < nx; icol++) {
487       int pos = icol * nxcell;
488       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
489       dens = clamp (dens, 0., 1.);
490       for (int p = pos; p < pos + nxcell; p++) {
491         rowp[p] = static_cast<unsigned int> (dens * 255.);
492       }
493     }
494     for (int ir = 0; ir < nycell; ir++) {
495       for (int ic = 0; ic < nx * nxcell; ic++) 
496         fprintf(fp, "%d ", rowp[ic]);
497       fprintf(fp, "\n");
498     }
499   }
500
501   fclose(fp);
502 }
503
504
505 #ifdef HAVE_PNG
506 void 
507 sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
508 {
509   FILE *fp;
510   png_structp png_ptr;
511   png_infop info_ptr;
512   double max_out_level = (1 << bitdepth) - 1;
513   int nx = im.nx();
514   int ny = im.ny();
515   ImageFileArray v = im.getArray();
516
517   unsigned char rowp [nx * nxcell * (bitdepth / 8)];
518
519   if ((fp = fopen (outfile, "wb")) == NULL)
520      return;
521
522   png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
523   if (! png_ptr)
524     return;
525
526   info_ptr = png_create_info_struct(png_ptr);
527   if (! info_ptr) {
528     png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
529     fclose(fp);
530     return;
531   }
532
533   if (setjmp(png_ptr->jmpbuf)) {
534     png_destroy_write_struct(&png_ptr, &info_ptr);
535     fclose(fp);
536     return;
537   }
538
539   png_init_io(png_ptr, fp);
540
541   png_set_IHDR(png_ptr, info_ptr, nx * nxcell, ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);
542
543   png_write_info(png_ptr, info_ptr);
544   for (int irow = ny - 1; irow >= 0; irow--) {
545     png_bytep row_pointer = rowp;
546     
547     for (int icol = 0; icol < nx; icol++) {
548       int pos = icol * nxcell;
549       double dens = (v[icol][irow] - densmin) / (densmax - densmin);
550       dens = clamp (dens, 0., 1.);
551       unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
552
553       for (int p = pos; p < pos + nxcell; p++) {
554         if (bitdepth == 8)
555           rowp[p] = outval;
556         else {
557           int rowpos = p * 2;
558           rowp[rowpos] = (outval >> 8) & 0xFF;
559           rowp[rowpos+1] = (outval & 0xFF);
560         }
561       }
562     }
563     for (int ir = 0; ir < nycell; ir++)
564       png_write_rows (png_ptr, &row_pointer, 1);
565   }
566
567   png_write_end(png_ptr, info_ptr);
568   png_destroy_write_struct(&png_ptr, &info_ptr);
569
570   fclose(fp);
571 }
572 #endif
573
574 #ifdef HAVE_GD
575 #include "gd.h"
576 #define N_GRAYSCALE 256
577 #endif
578
579 void
580 sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
581 {
582 #ifdef HAVE_GD
583   gdImagePtr gif;
584   FILE *out;
585   int gs_indices[N_GRAYSCALE];
586   int nx = im.nx();
587   int ny = im.ny();
588   ImageFileArray v = im.getArray();
589
590   unsigned char rowp [nx * nxcell];
591   if (rowp == NULL)
592     return;
593
594   gif = gdImageCreate(nx * nxcell, ny * nycell);
595   for (int i = 0; i < N_GRAYSCALE; i++)
596     gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
597
598   int lastrow = ny * nycell - 1;
599   for (int irow = 0; irow < ny; irow++) {
600     int rpos = irow * nycell;
601     for (int ir = rpos; ir < rpos + nycell; ir++) {
602       for (int icol = 0; icol < nx; icol++) {
603         int cpos = icol * nxcell;
604         double dens = (v[icol][irow] - densmin) / (densmax - densmin);
605         dens = clamp(dens, 0., 1.);
606         for (int ic = cpos; ic < cpos + nxcell; ic++) {
607           rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
608           gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
609         }
610       }
611     }
612   }
613
614   if ((out = fopen(outfile,"w")) == NULL) {
615     sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile);
616     return (1);
617   }
618   gdImageGif(gif,out);
619   fclose(out);
620   gdImageDestroy(gif);
621 #else
622   cout << "This version does not support GIF" << endl;
623 #endif
624 }
625
626 #ifndef NO_MAIN
627 int 
628 main (int argc, char *const argv[])
629 {
630   int retval = 1;
631
632   try {
633     retval = if2img_main(argc, argv);
634   } catch (exception e) {
635     cerr << "Exception: " << e.what() << endl;
636   } catch (...) {
637     cerr << "Unknown exception" << endl;
638   }
639
640   return (retval);
641 }
642 #endif