1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: sdf2img.c,v 1.5 2000/06/03 06:29:57 kevin Exp $
7 ** Revision 1.5 2000/06/03 06:29:57 kevin
8 ** *** empty log message ***
10 ** Revision 1.4 2000/05/24 22:50:04 kevin
11 ** Added support for new SGP library
13 ** Revision 1.3 2000/05/16 04:33:59 kevin
14 ** Improved option processing
16 ** Revision 1.2 2000/05/08 20:02:32 kevin
19 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
20 ** Initial CVS import for first public release
24 ** This program is free software; you can redistribute it and/or modify
25 ** it under the terms of the GNU General Public License (version 2) as
26 ** published by the Free Software Foundation.
28 ** This program is distributed in the hope that it will be useful,
29 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
30 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 ** GNU General Public License for more details.
33 ** You should have received a copy of the GNU General Public License
34 ** along with this program; if not, write to the Free Software
35 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 ******************************************************************************/
39 * sdf2img.c Convert an SDF file to a viewable format image
45 void sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
48 void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
50 void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
51 void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
53 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS,
54 O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
56 static struct option my_options[] =
58 {"scale", 1, 0, O_SCALE},
61 {"auto", 1, 0, O_AUTO},
62 {"center", 1, 0, O_CENTER},
63 {"format", 1, 0, O_FORMAT},
64 {"labels", 0, 0, O_LABELS},
65 {"stats", 0, 0, O_STATS},
66 {"verbose", 0, 0, O_VERBOSE},
67 {"debug", 0, 0, O_DEBUG},
68 {"help", 0, 0, O_HELP},
69 {"version", 0, 0, O_VERSION},
73 enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
74 #define O_AUTO_FULL_STR "full"
75 #define O_AUTO_STD0_1_STR "std0.1"
76 #define O_AUTO_STD0_5_STR "std0.5"
77 #define O_AUTO_STD1_STR "std1"
78 #define O_AUTO_STD2_STR "std2"
79 #define O_AUTO_STD3_STR "std3"
81 enum { O_CENTER_MEAN, O_CENTER_MODE };
82 #define O_CENTER_MEAN_STR "mean"
83 #define O_CENTER_MODE_STR "mode"
85 enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
86 #define O_FORMAT_GIF_STR "gif"
87 #define O_FORMAT_PNG_STR "png"
88 #define O_FORMAT_PGM_STR "pgm"
89 #define O_FORMAT_PGMASC_STR "pgmasc"
90 #define O_FORMAT_DISP_STR "disp"
93 sdf2img_usage (const char *program)
95 fprintf(stdout, "sdf2img_usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
96 fprintf(stdout, "Convert SDF2D file to an image file\n");
97 fprintf(stdout, "\n");
98 fprintf(stdout, " sdfname Name of input SDF file\n");
99 fprintf(stdout, " outfile Name of output file\n");
100 fprintf(stdout, " --format Output format\n");
101 fprintf(stdout, " pgm PGM (portable graymap) format (default)\n");
102 fprintf(stdout, " pgmasc PGM (portable graymap) ASCII format\n");
104 fprintf(stdout, " png PNG format\n");
107 fprintf(stdout, " gif GIF format\n");
109 fprintf(stdout, " disp Display on screen\n");
110 fprintf(stdout, " --center Center of window\n");
111 fprintf(stdout, " mode Mode is center of window (default)\n");
112 fprintf(stdout, " mean Mean is center of window\n");
113 fprintf(stdout, " --auto Set auto window\n");
114 fprintf(stdout, " full Use full window (default)\n");
115 fprintf(stdout, " std0.1 Use 0.1 standard deviation about center\n");
116 fprintf(stdout, " std0.5 Use 0.5 standard deviation about center\n");
117 fprintf(stdout, " std1 Use one standard deviation about center\n");
118 fprintf(stdout, " std2 Use two standard deviations about center\n");
119 fprintf(stdout, " std3 Use three standard deviations about center\n");
120 fprintf(stdout, " --scale Scaling factor for output size\n");
121 fprintf(stdout, " --min Set minimum intensity\n");
122 fprintf(stdout, " --max Set maximum intensity\n");
123 fprintf(stdout, " --stats Print image statistics\n");
124 fprintf(stdout, " --labels Print image labels\n");
125 fprintf(stdout, " --debug Set debug mode\n");
126 fprintf(stdout, " --verbose Set verbose mode\n");
127 fprintf(stdout, " --version Print version\n");
128 fprintf(stdout, " --help Print this help message\n");
133 sdf2img_main (int argc, char *const argv[])
136 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
137 char *in_file, *out_file;
140 int opt_auto = O_AUTO_FULL;
145 int opt_center = O_CENTER_MODE;
146 int opt_format = O_FORMAT_PGM;
151 int c = getopt_long (argc, argv, "", my_options, NULL);
162 densmin = strtod(optarg, &endptr);
163 endstr = optarg + strlen(optarg);
164 if (endptr != endstr)
166 fprintf(stderr, "Error setting --min to %s\n", optarg);
167 sdf2img_usage(argv[0]);
173 densmax = strtod(optarg, &endptr);
174 endstr = optarg + strlen(optarg);
175 if (endptr != endstr)
177 fprintf(stderr, "Error setting --max to %s\n", optarg);
178 sdf2img_usage(argv[0]);
183 opt_scale = strtol(optarg, &endptr, 10);
184 endstr = optarg + strlen(optarg);
185 if (endptr != endstr)
187 fprintf(stderr,"Error setting --scale to %s\n", optarg);
188 sdf2img_usage(argv[0]);
193 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
194 opt_auto = O_AUTO_FULL;
195 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
196 opt_auto = O_AUTO_STD1;
197 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
198 opt_auto = O_AUTO_STD0_5;
199 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
200 opt_auto = O_AUTO_STD0_1;
201 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
202 opt_auto = O_AUTO_STD2;
203 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
204 opt_auto = O_AUTO_STD3;
207 fprintf(stderr, "Invalid auto mode %s\n", optarg);
208 sdf2img_usage(argv[0]);
213 if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
214 opt_center = O_CENTER_MEAN;
215 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
216 opt_center = O_CENTER_MODE;
219 fprintf(stderr, "Invalid center mode %s\n", optarg);
220 sdf2img_usage(argv[0]);
225 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
226 opt_format = O_FORMAT_PGM;
227 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
228 opt_format = O_FORMAT_PGMASC;
230 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
231 opt_format = O_FORMAT_PNG;
234 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
235 opt_format = O_FORMAT_GIF;
237 else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
238 opt_format = O_FORMAT_DISP;
240 fprintf(stderr, "Invalid format mode %s\n", optarg);
241 sdf2img_usage(argv[0]);
259 fprintf(stdout, "Version %s\n", VERSION);
261 fprintf(stderr, "Unknown version number");
266 sdf2img_usage(argv[0]);
269 sdf2img_usage(argv[0]);
274 if ((opt_format == O_FORMAT_DISP && optind + 1 != argc)
275 || (opt_format != O_FORMAT_DISP && optind + 2 != argc))
277 sdf2img_usage(argv[0]);
281 in_file = argv[optind];
283 if (opt_format != O_FORMAT_DISP)
284 out_file = argv[optind+1];
285 else out_file = NULL;
287 if ((im = image_load (in_file)) == NULL)
289 fprintf(stderr, "File %s does not exist\n", in_file);
295 int nlabels = im->dfp_2d->dfp->num_label;
298 for (i = 0; i < nlabels; i++)
301 if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
303 if (blk.lab.label_type == LT_HISTORY) {
304 fprintf(stdout, "History: %s\n", blk.lab.label_str);
305 fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time);
306 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
307 } else if (blk.lab.label_type == LT_TITLE) {
308 fprintf(stdout, "Title: %s\n", blk.lab.label_str);
309 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
310 } else if (blk.lab.label_type == LT_NOTE) {
311 fprintf(stdout, "Note: %s\n", blk.lab.label_str);
312 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
317 if (opt_stats || (! (opt_set_max && opt_set_min)))
319 double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
320 double mode = 0, mean = 0, stddev = 0, window = 0;
323 int ibin, nbin = 256;
324 int max_bin, max_binindex;
329 for (ix = 0; ix < im->nx; ix++)
331 for (iy = 0; iy < im->ny; iy++)
333 if (im->v[ix][iy] > maxfound)
334 maxfound = im->v[ix][iy];
335 if (im->v[ix][iy] < minfound)
336 minfound = im->v[ix][iy];
337 mean += im->v[ix][iy];
340 spread = maxfound - minfound;
345 for (ibin = 0; ibin < nbin; ibin++)
347 for (ix = 0; ix < im->nx; ix++)
349 for (iy = 0; iy < im->ny; iy++)
351 int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
357 for (ibin = 0; ibin < nbin; ibin++)
359 if (hist[ibin] > max_bin)
361 max_bin = hist[ibin];
365 mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
368 if (opt_auto == O_AUTO_FULL)
375 if (opt_stats || opt_auto != O_AUTO_FULL)
377 mean /= im->nx * im->ny;
378 for (ix = 0; ix < im->nx; ix++)
380 for (iy = 0; iy < im->ny; iy++)
382 double diff = (im->v[ix][iy] - mean);
383 stddev += diff * diff;
386 stddev = sqrt(stddev / (im->nx * im->ny));
387 if (opt_auto == O_AUTO_FULL)
389 else if (opt_auto == O_AUTO_STD1)
391 else if (opt_auto == O_AUTO_STD0_1)
392 window = stddev * 0.1;
393 else if (opt_auto == O_AUTO_STD0_5)
394 window = stddev * 0.5;
395 else if (opt_auto == O_AUTO_STD2)
397 else if (opt_auto == O_AUTO_STD3)
401 fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);
407 fprintf(stdout,"nx=%d\n", im->nx);
408 fprintf(stdout,"ny=%d\n", im->ny);
409 fprintf(stdout,"min=%f\n", minfound);
410 fprintf(stdout,"max=%f\n", maxfound);
411 fprintf(stdout,"mean=%f\n", mean);
412 fprintf(stdout,"mode=%f\n", mode);
413 fprintf(stdout,"stddev=%f\n", stddev);
415 if (opt_auto != O_AUTO_FULL)
419 if (opt_center == O_CENTER_MODE)
421 else if (opt_center == O_CENTER_MEAN)
425 fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);
429 densmax = center + window;
431 densmin = center - window;
437 fprintf(stdout,"min_disp=%f\n", densmin);
438 fprintf(stdout,"max_disp=%f\n", densmax);
441 if (opt_format == O_FORMAT_PGM)
442 sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
443 else if (opt_format == O_FORMAT_PGMASC)
444 sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
446 else if (opt_format == O_FORMAT_PNG)
447 sdf2d_to_png (im, out_file, opt_scale, opt_scale, densmin, densmax);
450 else if (opt_format == O_FORMAT_GIF)
451 sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
453 else if (opt_format == O_FORMAT_DISP) {
455 image_display_scale (im, opt_scale, densmin, densmax);
457 sgp2_close(sgp2_get_active_win());
462 fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);
470 sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
476 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
480 if ((fp = fopen (outfile, "wb")) == NULL)
484 fprintf(fp, "%d %d\n", im->nx, im->ny);
485 fprintf(fp, "255\n");
487 for (irow = im->ny - 1; irow >= 0; irow--)
491 for (icol = 0; icol < im->nx; icol++)
495 int pos = icol * nxcell;
496 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
501 for (p = pos; p < pos + nxcell; p++)
503 rowp[p] = (unsigned int) (dens * 255.);
506 for (ir = 0; ir < nycell; ir++)
509 for (ic = 0; ic < im->nx * nxcell; ic++)
510 fprintf(fp, "%c ", rowp[ic]);
513 sys_free(rowp, "sdf2img");
519 sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
525 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
529 if ((fp = fopen (outfile, "wb")) == NULL)
533 fprintf(fp, "%d %d\n", im->nx, im->ny);
534 fprintf(fp, "255\n");
536 for (irow = im->ny - 1; irow >= 0; irow--)
540 for (icol = 0; icol < im->nx; icol++)
544 int pos = icol * nxcell;
545 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
550 for (p = pos; p < pos + nxcell; p++)
552 rowp[p] = (unsigned int) (dens * 255.);
555 for (ir = 0; ir < nycell; ir++)
558 for (ic = 0; ic < im->nx * nxcell; ic++)
559 fprintf(fp, "%d ", rowp[ic]);
563 sys_free(rowp, "sdf2img");
571 sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
579 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
583 if ((fp = fopen (outfile, "wb")) == NULL)
586 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
590 info_ptr = png_create_info_struct(png_ptr);
593 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
598 if (setjmp(png_ptr->jmpbuf))
600 png_destroy_write_struct(&png_ptr, &info_ptr);
605 png_init_io(png_ptr, fp);
607 png_set_IHDR(png_ptr, info_ptr, im->nx * nxcell, im->ny * nycell, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);
609 png_write_info(png_ptr, info_ptr);
610 for (irow = im->ny - 1; irow >= 0; irow--)
612 png_bytep row_pointer;
615 for (icol = 0; icol < im->nx; icol++)
619 int pos = icol * nxcell;
620 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
625 for (p = pos; p < pos + nxcell; p++)
627 rowp[p] = (unsigned int) (dens * 255.);
631 for (ir = 0; ir < nycell; ir++)
633 png_write_rows (png_ptr, &row_pointer, 1);
636 sys_free(rowp, "sdf2img");
638 png_write_end(png_ptr, info_ptr);
639 png_destroy_write_struct(&png_ptr, &info_ptr);
647 #define N_GRAYSCALE 256
651 sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
656 int gs_indices[N_GRAYSCALE];
662 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
666 gif = gdImageCreate(im->nx * nxcell, im->ny * nycell);
667 for (i = 0; i < N_GRAYSCALE; i++)
668 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
670 lastrow = im->ny * nycell - 1;
671 for (irow = 0; irow < im->ny; irow++)
674 int rpos = irow * nycell;
675 for (ir = rpos; ir < rpos + nycell; ir++)
677 for (icol = 0; icol < im->nx; icol++)
681 int cpos = icol * nxcell;
682 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
687 for (ic = cpos; ic < cpos + nxcell; ic++)
689 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
690 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
695 sys_free(rowp, "sdf2img");
697 if ((out = fopen(outfile,"w")) == NULL)
699 fprintf(stderr,"Error opening output file %s for writing\n", outfile);
706 fprintf(stderr, "This version does not support GIF");
712 main (int argc, char *const argv[])
714 return (sdf2img_main(argc, argv));