1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: sdf2img.c,v 1.3 2000/05/16 04:33:59 kevin Exp $
7 ** Revision 1.3 2000/05/16 04:33:59 kevin
8 ** Improved option processing
10 ** Revision 1.2 2000/05/08 20:02:32 kevin
13 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
14 ** Initial CVS import for first public release
18 ** This program is free software; you can redistribute it and/or modify
19 ** it under the terms of the GNU General Public License (version 2) as
20 ** published by the Free Software Foundation.
22 ** This program is distributed in the hope that it will be useful,
23 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ** GNU General Public License for more details.
27 ** You should have received a copy of the GNU General Public License
28 ** along with this program; if not, write to the Free Software
29 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 ******************************************************************************/
33 * sdf2img.c Convert an SDF file to a viewable format image
39 void sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
42 void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
44 void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
45 void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
47 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS,
48 O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
50 static struct option my_options[] =
52 {"scale", 1, 0, O_SCALE},
55 {"auto", 1, 0, O_AUTO},
56 {"center", 1, 0, O_CENTER},
57 {"format", 1, 0, O_FORMAT},
58 {"labels", 0, 0, O_LABELS},
59 {"stats", 0, 0, O_STATS},
60 {"verbose", 0, 0, O_VERBOSE},
61 {"debug", 0, 0, O_DEBUG},
62 {"help", 0, 0, O_HELP},
63 {"version", 0, 0, O_VERSION},
68 #define O_AUTO_STD0_1 2
69 #define O_AUTO_STD0_5 3
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 #define O_CENTER_MEAN 1
82 #define O_CENTER_MODE 2
83 #define O_CENTER_MEAN_STR "mean"
84 #define O_CENTER_MODE_STR "mode"
86 #define O_FORMAT_GIF 1
87 #define O_FORMAT_PNG 2
88 #define O_FORMAT_PGM 3
89 #define O_FORMAT_PGMASC 4
90 #define O_FORMAT_GIF_STR "gif"
91 #define O_FORMAT_PNG_STR "png"
92 #define O_FORMAT_PGM_STR "pgm"
93 #define O_FORMAT_PGMASC_STR "pgmasc"
96 sdf2img_usage (const char *program)
98 fprintf(stdout, "sdf2img_usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
99 fprintf(stdout, "Convert SDF2D file to an image file\n");
100 fprintf(stdout, "\n");
101 fprintf(stdout, " sdfname Name of input SDF file\n");
102 fprintf(stdout, " outfile Name of output file\n");
103 fprintf(stdout, " --format Output format\n");
104 fprintf(stdout, " pgm PGM (portable graymap) format (default)\n");
105 fprintf(stdout, " pgmasc PGM (portable graymap) ASCII format\n");
107 fprintf(stdout, " png PNG format\n");
110 fprintf(stdout, " gif GIF format\n");
112 fprintf(stdout, " --center Center of window\n");
113 fprintf(stdout, " mode Mode is center of window (default)\n");
114 fprintf(stdout, " mean Mean is center of window\n");
115 fprintf(stdout, " --auto Set auto window\n");
116 fprintf(stdout, " full Use full window (default)\n");
117 fprintf(stdout, " std0.1 Use 0.1 standard deviation about center\n");
118 fprintf(stdout, " std0.5 Use 0.5 standard deviation about center\n");
119 fprintf(stdout, " std1 Use one standard deviation about center\n");
120 fprintf(stdout, " std2 Use two standard deviations about center\n");
121 fprintf(stdout, " std3 Use three standard deviations about center\n");
122 fprintf(stdout, " --scale Scaling factor for output size\n");
123 fprintf(stdout, " --min Set minimum intensity\n");
124 fprintf(stdout, " --max Set maximum intensity\n");
125 fprintf(stdout, " --stats Print image statistics\n");
126 fprintf(stdout, " --labels Print image labels\n");
127 fprintf(stdout, " --debug Set debug mode\n");
128 fprintf(stdout, " --verbose Set verbose mode\n");
129 fprintf(stdout, " --version Print version\n");
130 fprintf(stdout, " --help Print this help message\n");
135 sdf2img_main (int argc, char *const argv[])
138 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
139 char *in_file, *out_file;
142 int opt_auto = O_AUTO_FULL;
147 int opt_center = O_CENTER_MODE;
148 int opt_format = O_FORMAT_PGM;
153 int c = getopt_long (argc, argv, "", my_options, NULL);
164 densmin = strtod(optarg, &endptr);
165 endstr = optarg + strlen(optarg);
166 if (endptr != endstr)
168 fprintf(stderr, "Error setting --min to %s\n", optarg);
169 sdf2img_usage(argv[0]);
175 densmax = strtod(optarg, &endptr);
176 endstr = optarg + strlen(optarg);
177 if (endptr != endstr)
179 fprintf(stderr, "Error setting --max to %s\n", optarg);
180 sdf2img_usage(argv[0]);
185 opt_scale = strtol(optarg, &endptr, 10);
186 endstr = optarg + strlen(optarg);
187 if (endptr != endstr)
189 fprintf(stderr,"Error setting --scale to %s\n", optarg);
190 sdf2img_usage(argv[0]);
195 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
196 opt_auto = O_AUTO_FULL;
197 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
198 opt_auto = O_AUTO_STD1;
199 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
200 opt_auto = O_AUTO_STD0_5;
201 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
202 opt_auto = O_AUTO_STD0_1;
203 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
204 opt_auto = O_AUTO_STD2;
205 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
206 opt_auto = O_AUTO_STD3;
209 fprintf(stderr, "Invalid auto mode %s\n", optarg);
210 sdf2img_usage(argv[0]);
215 if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
216 opt_center = O_CENTER_MEAN;
217 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
218 opt_center = O_CENTER_MODE;
221 fprintf(stderr, "Invalid center mode %s\n", optarg);
222 sdf2img_usage(argv[0]);
227 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
228 opt_format = O_FORMAT_PGM;
229 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
230 opt_format = O_FORMAT_PGMASC;
232 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
233 opt_format = O_FORMAT_PNG;
236 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
237 opt_format = O_FORMAT_GIF;
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 (optind + 2 != argc)
276 sdf2img_usage(argv[0]);
280 in_file = argv[optind];
281 out_file = argv[optind+1];
283 if ((im = image_load (in_file)) == NULL)
285 fprintf(stderr, "File %s does not exist\n", in_file);
291 int nlabels = im->dfp_2d->dfp->num_label;
294 for (i = 0; i < nlabels; i++)
297 if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
299 if (blk.lab.label_type == LT_HISTORY) {
300 fprintf(stdout, "History: %s\n", blk.lab.label_str);
301 fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time);
302 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
303 } else if (blk.lab.label_type == LT_TITLE) {
304 fprintf(stdout, "Title: %s\n", blk.lab.label_str);
305 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
306 } else if (blk.lab.label_type == LT_NOTE) {
307 fprintf(stdout, "Note: %s\n", blk.lab.label_str);
308 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
313 if (opt_stats || (! (opt_set_max && opt_set_min)))
315 double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
316 double mode = 0, mean = 0, stddev = 0, window = 0;
319 int ibin, nbin = 256;
320 int max_bin, max_binindex;
325 for (ix = 0; ix < im->nx; ix++)
327 for (iy = 0; iy < im->ny; iy++)
329 if (im->v[ix][iy] > maxfound)
330 maxfound = im->v[ix][iy];
331 if (im->v[ix][iy] < minfound)
332 minfound = im->v[ix][iy];
333 mean += im->v[ix][iy];
336 spread = maxfound - minfound;
341 for (ibin = 0; ibin < nbin; ibin++)
343 for (ix = 0; ix < im->nx; ix++)
345 for (iy = 0; iy < im->ny; iy++)
347 int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
353 for (ibin = 0; ibin < nbin; ibin++)
355 if (hist[ibin] > max_bin)
357 max_bin = hist[ibin];
361 mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
364 if (opt_auto == O_AUTO_FULL)
371 if (opt_stats || opt_auto != O_AUTO_FULL)
373 mean /= im->nx * im->ny;
374 for (ix = 0; ix < im->nx; ix++)
376 for (iy = 0; iy < im->ny; iy++)
378 double diff = (im->v[ix][iy] - mean);
379 stddev += diff * diff;
382 stddev = sqrt(stddev / (im->nx * im->ny));
383 if (opt_auto == O_AUTO_FULL)
385 else if (opt_auto == O_AUTO_STD1)
387 else if (opt_auto == O_AUTO_STD0_1)
388 window = stddev * 0.1;
389 else if (opt_auto == O_AUTO_STD0_5)
390 window = stddev * 0.5;
391 else if (opt_auto == O_AUTO_STD2)
393 else if (opt_auto == O_AUTO_STD3)
397 fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);
403 fprintf(stdout,"nx=%d\n", im->nx);
404 fprintf(stdout,"ny=%d\n", im->ny);
405 fprintf(stdout,"min=%f\n", minfound);
406 fprintf(stdout,"max=%f\n", maxfound);
407 fprintf(stdout,"mean=%f\n", mean);
408 fprintf(stdout,"mode=%f\n", mode);
409 fprintf(stdout,"stddev=%f\n", stddev);
411 if (opt_auto != O_AUTO_FULL)
415 if (opt_center == O_CENTER_MODE)
417 else if (opt_center == O_CENTER_MEAN)
421 fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);
425 densmax = center + window;
427 densmin = center - window;
433 fprintf(stdout,"min_disp=%f\n", densmin);
434 fprintf(stdout,"max_disp=%f\n", densmax);
437 if (opt_format == O_FORMAT_PGM)
438 sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
439 else if (opt_format == O_FORMAT_PGMASC)
440 sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
442 else if (opt_format == O_FORMAT_PNG)
443 sdf2d_to_png (im, out_file, opt_scale, opt_scale, densmin, densmax);
446 else if (opt_format == O_FORMAT_GIF)
447 sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
451 fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);
459 sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
465 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
469 if ((fp = fopen (outfile, "wb")) == NULL)
473 fprintf(fp, "%d %d\n", im->nx, im->ny);
474 fprintf(fp, "255\n");
476 for (irow = im->ny - 1; irow >= 0; irow--)
480 for (icol = 0; icol < im->nx; icol++)
484 int pos = icol * nxcell;
485 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
490 for (p = pos; p < pos + nxcell; p++)
492 rowp[p] = (unsigned int) (dens * 255.);
495 for (ir = 0; ir < nycell; ir++)
498 for (ic = 0; ic < im->nx * nxcell; ic++)
499 fprintf(fp, "%c ", rowp[ic]);
502 sys_free(rowp, "sdf2img");
508 sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
514 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
518 if ((fp = fopen (outfile, "wb")) == NULL)
522 fprintf(fp, "%d %d\n", im->nx, im->ny);
523 fprintf(fp, "255\n");
525 for (irow = im->ny - 1; irow >= 0; irow--)
529 for (icol = 0; icol < im->nx; icol++)
533 int pos = icol * nxcell;
534 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
539 for (p = pos; p < pos + nxcell; p++)
541 rowp[p] = (unsigned int) (dens * 255.);
544 for (ir = 0; ir < nycell; ir++)
547 for (ic = 0; ic < im->nx * nxcell; ic++)
548 fprintf(fp, "%d ", rowp[ic]);
552 sys_free(rowp, "sdf2img");
560 sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
568 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
572 if ((fp = fopen (outfile, "wb")) == NULL)
575 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
579 info_ptr = png_create_info_struct(png_ptr);
582 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
587 if (setjmp(png_ptr->jmpbuf))
589 png_destroy_write_struct(&png_ptr, &info_ptr);
594 png_init_io(png_ptr, fp);
596 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);
598 png_write_info(png_ptr, info_ptr);
599 for (irow = im->ny - 1; irow >= 0; irow--)
601 png_bytep row_pointer;
604 for (icol = 0; icol < im->nx; icol++)
608 int pos = icol * nxcell;
609 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
614 for (p = pos; p < pos + nxcell; p++)
616 rowp[p] = (unsigned int) (dens * 255.);
620 for (ir = 0; ir < nycell; ir++)
622 png_write_rows (png_ptr, &row_pointer, 1);
625 sys_free(rowp, "sdf2img");
627 png_write_end(png_ptr, info_ptr);
628 png_destroy_write_struct(&png_ptr, &info_ptr);
636 #define N_GRAYSCALE 256
640 sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
645 int gs_indices[N_GRAYSCALE];
651 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
655 gif = gdImageCreate(im->nx * nxcell, im->ny * nycell);
656 for (i = 0; i < N_GRAYSCALE; i++)
657 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
659 lastrow = im->ny * nycell - 1;
660 for (irow = 0; irow < im->ny; irow++)
663 int rpos = irow * nycell;
664 for (ir = rpos; ir < rpos + nycell; ir++)
666 for (icol = 0; icol < im->nx; icol++)
670 int cpos = icol * nxcell;
671 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
676 for (ic = cpos; ic < cpos + nxcell; ic++)
678 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
679 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
684 sys_free(rowp, "sdf2img");
686 if ((out = fopen(outfile,"w")) == NULL)
688 fprintf(stderr,"Error opening output file %s for writing\n", outfile);
695 fprintf(stderr, "This version does not support GIF");
701 main (int argc, char *const argv[])
703 return (sdf2img_main(argc, argv));