1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: sdf2img.c,v 1.4 2000/05/24 22:50:04 kevin Exp $
7 ** Revision 1.4 2000/05/24 22:50:04 kevin
8 ** Added support for new SGP library
10 ** Revision 1.3 2000/05/16 04:33:59 kevin
11 ** Improved option processing
13 ** Revision 1.2 2000/05/08 20:02:32 kevin
16 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
17 ** Initial CVS import for first public release
21 ** This program is free software; you can redistribute it and/or modify
22 ** it under the terms of the GNU General Public License (version 2) as
23 ** published by the Free Software Foundation.
25 ** This program is distributed in the hope that it will be useful,
26 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
27 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 ** GNU General Public License for more details.
30 ** You should have received a copy of the GNU General Public License
31 ** along with this program; if not, write to the Free Software
32 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 ******************************************************************************/
36 * sdf2img.c Convert an SDF file to a viewable format image
42 void sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
45 void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
47 void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
48 void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
50 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS,
51 O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
53 static struct option my_options[] =
55 {"scale", 1, 0, O_SCALE},
58 {"auto", 1, 0, O_AUTO},
59 {"center", 1, 0, O_CENTER},
60 {"format", 1, 0, O_FORMAT},
61 {"labels", 0, 0, O_LABELS},
62 {"stats", 0, 0, O_STATS},
63 {"verbose", 0, 0, O_VERBOSE},
64 {"debug", 0, 0, O_DEBUG},
65 {"help", 0, 0, O_HELP},
66 {"version", 0, 0, O_VERSION},
70 enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
71 #define O_AUTO_FULL_STR "full"
72 #define O_AUTO_STD0_1_STR "std0.1"
73 #define O_AUTO_STD0_5_STR "std0.5"
74 #define O_AUTO_STD1_STR "std1"
75 #define O_AUTO_STD2_STR "std2"
76 #define O_AUTO_STD3_STR "std3"
78 enum { O_CENTER_MEAN, O_CENTER_MODE };
79 #define O_CENTER_MEAN_STR "mean"
80 #define O_CENTER_MODE_STR "mode"
82 enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
83 #define O_FORMAT_GIF_STR "gif"
84 #define O_FORMAT_PNG_STR "png"
85 #define O_FORMAT_PGM_STR "pgm"
86 #define O_FORMAT_PGMASC_STR "pgmasc"
87 #define O_FORMAT_DISP_STR "disp"
90 sdf2img_usage (const char *program)
92 fprintf(stdout, "sdf2img_usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
93 fprintf(stdout, "Convert SDF2D file to an image file\n");
94 fprintf(stdout, "\n");
95 fprintf(stdout, " sdfname Name of input SDF file\n");
96 fprintf(stdout, " outfile Name of output file\n");
97 fprintf(stdout, " --format Output format\n");
98 fprintf(stdout, " pgm PGM (portable graymap) format (default)\n");
99 fprintf(stdout, " pgmasc PGM (portable graymap) ASCII format\n");
101 fprintf(stdout, " png PNG format\n");
104 fprintf(stdout, " gif GIF format\n");
106 fprintf(stdout, " disp Display on screen\n");
107 fprintf(stdout, " --center Center of window\n");
108 fprintf(stdout, " mode Mode is center of window (default)\n");
109 fprintf(stdout, " mean Mean is center of window\n");
110 fprintf(stdout, " --auto Set auto window\n");
111 fprintf(stdout, " full Use full window (default)\n");
112 fprintf(stdout, " std0.1 Use 0.1 standard deviation about center\n");
113 fprintf(stdout, " std0.5 Use 0.5 standard deviation about center\n");
114 fprintf(stdout, " std1 Use one standard deviation about center\n");
115 fprintf(stdout, " std2 Use two standard deviations about center\n");
116 fprintf(stdout, " std3 Use three standard deviations about center\n");
117 fprintf(stdout, " --scale Scaling factor for output size\n");
118 fprintf(stdout, " --min Set minimum intensity\n");
119 fprintf(stdout, " --max Set maximum intensity\n");
120 fprintf(stdout, " --stats Print image statistics\n");
121 fprintf(stdout, " --labels Print image labels\n");
122 fprintf(stdout, " --debug Set debug mode\n");
123 fprintf(stdout, " --verbose Set verbose mode\n");
124 fprintf(stdout, " --version Print version\n");
125 fprintf(stdout, " --help Print this help message\n");
130 sdf2img_main (int argc, char *const argv[])
133 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
134 char *in_file, *out_file;
137 int opt_auto = O_AUTO_FULL;
142 int opt_center = O_CENTER_MODE;
143 int opt_format = O_FORMAT_PGM;
148 int c = getopt_long (argc, argv, "", my_options, NULL);
159 densmin = strtod(optarg, &endptr);
160 endstr = optarg + strlen(optarg);
161 if (endptr != endstr)
163 fprintf(stderr, "Error setting --min to %s\n", optarg);
164 sdf2img_usage(argv[0]);
170 densmax = strtod(optarg, &endptr);
171 endstr = optarg + strlen(optarg);
172 if (endptr != endstr)
174 fprintf(stderr, "Error setting --max to %s\n", optarg);
175 sdf2img_usage(argv[0]);
180 opt_scale = strtol(optarg, &endptr, 10);
181 endstr = optarg + strlen(optarg);
182 if (endptr != endstr)
184 fprintf(stderr,"Error setting --scale to %s\n", optarg);
185 sdf2img_usage(argv[0]);
190 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
191 opt_auto = O_AUTO_FULL;
192 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
193 opt_auto = O_AUTO_STD1;
194 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
195 opt_auto = O_AUTO_STD0_5;
196 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
197 opt_auto = O_AUTO_STD0_1;
198 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
199 opt_auto = O_AUTO_STD2;
200 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
201 opt_auto = O_AUTO_STD3;
204 fprintf(stderr, "Invalid auto mode %s\n", optarg);
205 sdf2img_usage(argv[0]);
210 if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
211 opt_center = O_CENTER_MEAN;
212 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
213 opt_center = O_CENTER_MODE;
216 fprintf(stderr, "Invalid center mode %s\n", optarg);
217 sdf2img_usage(argv[0]);
222 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
223 opt_format = O_FORMAT_PGM;
224 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
225 opt_format = O_FORMAT_PGMASC;
227 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
228 opt_format = O_FORMAT_PNG;
231 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
232 opt_format = O_FORMAT_GIF;
234 else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
235 opt_format = O_FORMAT_DISP;
237 fprintf(stderr, "Invalid format mode %s\n", optarg);
238 sdf2img_usage(argv[0]);
256 fprintf(stdout, "Version %s\n", VERSION);
258 fprintf(stderr, "Unknown version number");
263 sdf2img_usage(argv[0]);
266 sdf2img_usage(argv[0]);
271 if ((opt_format == O_FORMAT_DISP && optind + 1 != argc)
272 || (opt_format != O_FORMAT_DISP && optind + 2 != argc))
274 sdf2img_usage(argv[0]);
278 in_file = argv[optind];
280 if (opt_format != O_FORMAT_DISP)
281 out_file = argv[optind+1];
282 else out_file = NULL;
284 if ((im = image_load (in_file)) == NULL)
286 fprintf(stderr, "File %s does not exist\n", in_file);
292 int nlabels = im->dfp_2d->dfp->num_label;
295 for (i = 0; i < nlabels; i++)
298 if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
300 if (blk.lab.label_type == LT_HISTORY) {
301 fprintf(stdout, "History: %s\n", blk.lab.label_str);
302 fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time);
303 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
304 } else if (blk.lab.label_type == LT_TITLE) {
305 fprintf(stdout, "Title: %s\n", blk.lab.label_str);
306 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
307 } else if (blk.lab.label_type == LT_NOTE) {
308 fprintf(stdout, "Note: %s\n", blk.lab.label_str);
309 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
314 if (opt_stats || (! (opt_set_max && opt_set_min)))
316 double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
317 double mode = 0, mean = 0, stddev = 0, window = 0;
320 int ibin, nbin = 256;
321 int max_bin, max_binindex;
326 for (ix = 0; ix < im->nx; ix++)
328 for (iy = 0; iy < im->ny; iy++)
330 if (im->v[ix][iy] > maxfound)
331 maxfound = im->v[ix][iy];
332 if (im->v[ix][iy] < minfound)
333 minfound = im->v[ix][iy];
334 mean += im->v[ix][iy];
337 spread = maxfound - minfound;
342 for (ibin = 0; ibin < nbin; ibin++)
344 for (ix = 0; ix < im->nx; ix++)
346 for (iy = 0; iy < im->ny; iy++)
348 int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
354 for (ibin = 0; ibin < nbin; ibin++)
356 if (hist[ibin] > max_bin)
358 max_bin = hist[ibin];
362 mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
365 if (opt_auto == O_AUTO_FULL)
372 if (opt_stats || opt_auto != O_AUTO_FULL)
374 mean /= im->nx * im->ny;
375 for (ix = 0; ix < im->nx; ix++)
377 for (iy = 0; iy < im->ny; iy++)
379 double diff = (im->v[ix][iy] - mean);
380 stddev += diff * diff;
383 stddev = sqrt(stddev / (im->nx * im->ny));
384 if (opt_auto == O_AUTO_FULL)
386 else if (opt_auto == O_AUTO_STD1)
388 else if (opt_auto == O_AUTO_STD0_1)
389 window = stddev * 0.1;
390 else if (opt_auto == O_AUTO_STD0_5)
391 window = stddev * 0.5;
392 else if (opt_auto == O_AUTO_STD2)
394 else if (opt_auto == O_AUTO_STD3)
398 fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);
404 fprintf(stdout,"nx=%d\n", im->nx);
405 fprintf(stdout,"ny=%d\n", im->ny);
406 fprintf(stdout,"min=%f\n", minfound);
407 fprintf(stdout,"max=%f\n", maxfound);
408 fprintf(stdout,"mean=%f\n", mean);
409 fprintf(stdout,"mode=%f\n", mode);
410 fprintf(stdout,"stddev=%f\n", stddev);
412 if (opt_auto != O_AUTO_FULL)
416 if (opt_center == O_CENTER_MODE)
418 else if (opt_center == O_CENTER_MEAN)
422 fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);
426 densmax = center + window;
428 densmin = center - window;
434 fprintf(stdout,"min_disp=%f\n", densmin);
435 fprintf(stdout,"max_disp=%f\n", densmax);
438 if (opt_format == O_FORMAT_PGM)
439 sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
440 else if (opt_format == O_FORMAT_PGMASC)
441 sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
443 else if (opt_format == O_FORMAT_PNG)
444 sdf2d_to_png (im, out_file, opt_scale, opt_scale, densmin, densmax);
447 else if (opt_format == O_FORMAT_GIF)
448 sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
450 else if (opt_format == O_FORMAT_DISP) {
451 int gid = image_display_scale (im, opt_scale, densmin, densmax);
457 fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);
465 sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
471 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
475 if ((fp = fopen (outfile, "wb")) == NULL)
479 fprintf(fp, "%d %d\n", im->nx, im->ny);
480 fprintf(fp, "255\n");
482 for (irow = im->ny - 1; irow >= 0; irow--)
486 for (icol = 0; icol < im->nx; icol++)
490 int pos = icol * nxcell;
491 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
496 for (p = pos; p < pos + nxcell; p++)
498 rowp[p] = (unsigned int) (dens * 255.);
501 for (ir = 0; ir < nycell; ir++)
504 for (ic = 0; ic < im->nx * nxcell; ic++)
505 fprintf(fp, "%c ", rowp[ic]);
508 sys_free(rowp, "sdf2img");
514 sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
520 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
524 if ((fp = fopen (outfile, "wb")) == NULL)
528 fprintf(fp, "%d %d\n", im->nx, im->ny);
529 fprintf(fp, "255\n");
531 for (irow = im->ny - 1; irow >= 0; irow--)
535 for (icol = 0; icol < im->nx; icol++)
539 int pos = icol * nxcell;
540 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
545 for (p = pos; p < pos + nxcell; p++)
547 rowp[p] = (unsigned int) (dens * 255.);
550 for (ir = 0; ir < nycell; ir++)
553 for (ic = 0; ic < im->nx * nxcell; ic++)
554 fprintf(fp, "%d ", rowp[ic]);
558 sys_free(rowp, "sdf2img");
566 sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
574 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
578 if ((fp = fopen (outfile, "wb")) == NULL)
581 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
585 info_ptr = png_create_info_struct(png_ptr);
588 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
593 if (setjmp(png_ptr->jmpbuf))
595 png_destroy_write_struct(&png_ptr, &info_ptr);
600 png_init_io(png_ptr, fp);
602 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);
604 png_write_info(png_ptr, info_ptr);
605 for (irow = im->ny - 1; irow >= 0; irow--)
607 png_bytep row_pointer;
610 for (icol = 0; icol < im->nx; icol++)
614 int pos = icol * nxcell;
615 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
620 for (p = pos; p < pos + nxcell; p++)
622 rowp[p] = (unsigned int) (dens * 255.);
626 for (ir = 0; ir < nycell; ir++)
628 png_write_rows (png_ptr, &row_pointer, 1);
631 sys_free(rowp, "sdf2img");
633 png_write_end(png_ptr, info_ptr);
634 png_destroy_write_struct(&png_ptr, &info_ptr);
642 #define N_GRAYSCALE 256
646 sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
651 int gs_indices[N_GRAYSCALE];
657 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
661 gif = gdImageCreate(im->nx * nxcell, im->ny * nycell);
662 for (i = 0; i < N_GRAYSCALE; i++)
663 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
665 lastrow = im->ny * nycell - 1;
666 for (irow = 0; irow < im->ny; irow++)
669 int rpos = irow * nycell;
670 for (ir = rpos; ir < rpos + nycell; ir++)
672 for (icol = 0; icol < im->nx; icol++)
676 int cpos = icol * nxcell;
677 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
682 for (ic = cpos; ic < cpos + nxcell; ic++)
684 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
685 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
690 sys_free(rowp, "sdf2img");
692 if ((out = fopen(outfile,"w")) == NULL)
694 fprintf(stderr,"Error opening output file %s for writing\n", outfile);
701 fprintf(stderr, "This version does not support GIF");
707 main (int argc, char *const argv[])
709 return (sdf2img_main(argc, argv));