1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: sdf2img.cpp,v 1.1 2000/06/07 02:29:05 kevin Exp $
6 ** $Log: sdf2img.cpp,v $
7 ** Revision 1.1 2000/06/07 02:29:05 kevin
8 ** Initial C++ versions
10 ** Revision 1.6 2000/06/03 07:57:51 kevin
11 ** Fixed PNG 16-bit format
13 ** Revision 1.5 2000/06/03 06:29:57 kevin
14 ** *** empty log message ***
16 ** Revision 1.4 2000/05/24 22:50:04 kevin
17 ** Added support for new SGP library
19 ** Revision 1.3 2000/05/16 04:33:59 kevin
20 ** Improved option processing
22 ** Revision 1.2 2000/05/08 20:02:32 kevin
25 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
26 ** Initial CVS import for first public release
30 ** This program is free software; you can redistribute it and/or modify
31 ** it under the terms of the GNU General Public License (version 2) as
32 ** published by the Free Software Foundation.
34 ** This program is distributed in the hope that it will be useful,
35 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
36 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 ** GNU General Public License for more details.
39 ** You should have received a copy of the GNU General Public License
40 ** along with this program; if not, write to the Free Software
41 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 ******************************************************************************/
45 * sdf2img.c Convert an SDF file to a viewable format image
51 void sdf2d_to_png (IMAGE *im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax);
54 void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
56 void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
57 void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
59 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS,
60 O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
62 static struct option my_options[] =
64 {"scale", 1, 0, O_SCALE},
67 {"auto", 1, 0, O_AUTO},
68 {"center", 1, 0, O_CENTER},
69 {"format", 1, 0, O_FORMAT},
70 {"labels", 0, 0, O_LABELS},
71 {"stats", 0, 0, O_STATS},
72 {"verbose", 0, 0, O_VERBOSE},
73 {"debug", 0, 0, O_DEBUG},
74 {"help", 0, 0, O_HELP},
75 {"version", 0, 0, O_VERSION},
79 enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
80 #define O_AUTO_FULL_STR "full"
81 #define O_AUTO_STD0_1_STR "std0.1"
82 #define O_AUTO_STD0_5_STR "std0.5"
83 #define O_AUTO_STD1_STR "std1"
84 #define O_AUTO_STD2_STR "std2"
85 #define O_AUTO_STD3_STR "std3"
87 enum { O_CENTER_MEAN, O_CENTER_MODE };
88 #define O_CENTER_MEAN_STR "mean"
89 #define O_CENTER_MODE_STR "mode"
91 enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
92 #define O_FORMAT_GIF_STR "gif"
93 #define O_FORMAT_PNG_STR "png"
94 #define O_FORMAT_PNG16_STR "png16"
95 #define O_FORMAT_PGM_STR "pgm"
96 #define O_FORMAT_PGMASC_STR "pgmasc"
97 #define O_FORMAT_DISP_STR "disp"
100 sdf2img_usage (const char *program)
102 fprintf(stdout, "sdf2img_usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
103 fprintf(stdout, "Convert SDF2D file to an image file\n");
104 fprintf(stdout, "\n");
105 fprintf(stdout, " sdfname Name of input SDF file\n");
106 fprintf(stdout, " outfile Name of output file\n");
107 fprintf(stdout, " --format Output format\n");
108 fprintf(stdout, " pgm PGM (portable graymap) format (default)\n");
109 fprintf(stdout, " pgmasc PGM (portable graymap) ASCII format\n");
111 fprintf(stdout, " png PNG (8-bit) format\n");
112 fprintf(stdout, " png16 PNG (16-bit) format\n");
115 fprintf(stdout, " gif GIF format\n");
117 fprintf(stdout, " disp Display on screen\n");
118 fprintf(stdout, " --center Center of window\n");
119 fprintf(stdout, " mode Mode is center of window (default)\n");
120 fprintf(stdout, " mean Mean is center of window\n");
121 fprintf(stdout, " --auto Set auto window\n");
122 fprintf(stdout, " full Use full window (default)\n");
123 fprintf(stdout, " std0.1 Use 0.1 standard deviation about center\n");
124 fprintf(stdout, " std0.5 Use 0.5 standard deviation about center\n");
125 fprintf(stdout, " std1 Use one standard deviation about center\n");
126 fprintf(stdout, " std2 Use two standard deviations about center\n");
127 fprintf(stdout, " std3 Use three standard deviations about center\n");
128 fprintf(stdout, " --scale Scaling factor for output size\n");
129 fprintf(stdout, " --min Set minimum intensity\n");
130 fprintf(stdout, " --max Set maximum intensity\n");
131 fprintf(stdout, " --stats Print image statistics\n");
132 fprintf(stdout, " --labels Print image labels\n");
133 fprintf(stdout, " --debug Set debug mode\n");
134 fprintf(stdout, " --verbose Set verbose mode\n");
135 fprintf(stdout, " --version Print version\n");
136 fprintf(stdout, " --help Print this help message\n");
141 sdf2img_main (int argc, char *const argv[])
144 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
145 char *in_file, *out_file;
148 int opt_auto = O_AUTO_FULL;
153 int opt_center = O_CENTER_MODE;
154 int opt_format = O_FORMAT_PGM;
159 int c = getopt_long (argc, argv, "", my_options, NULL);
170 densmin = strtod(optarg, &endptr);
171 endstr = optarg + strlen(optarg);
172 if (endptr != endstr)
174 fprintf(stderr, "Error setting --min to %s\n", optarg);
175 sdf2img_usage(argv[0]);
181 densmax = strtod(optarg, &endptr);
182 endstr = optarg + strlen(optarg);
183 if (endptr != endstr)
185 fprintf(stderr, "Error setting --max to %s\n", optarg);
186 sdf2img_usage(argv[0]);
191 opt_scale = strtol(optarg, &endptr, 10);
192 endstr = optarg + strlen(optarg);
193 if (endptr != endstr)
195 fprintf(stderr,"Error setting --scale to %s\n", optarg);
196 sdf2img_usage(argv[0]);
201 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
202 opt_auto = O_AUTO_FULL;
203 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
204 opt_auto = O_AUTO_STD1;
205 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
206 opt_auto = O_AUTO_STD0_5;
207 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
208 opt_auto = O_AUTO_STD0_1;
209 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
210 opt_auto = O_AUTO_STD2;
211 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
212 opt_auto = O_AUTO_STD3;
215 fprintf(stderr, "Invalid auto mode %s\n", optarg);
216 sdf2img_usage(argv[0]);
221 if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
222 opt_center = O_CENTER_MEAN;
223 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
224 opt_center = O_CENTER_MODE;
227 fprintf(stderr, "Invalid center mode %s\n", optarg);
228 sdf2img_usage(argv[0]);
233 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
234 opt_format = O_FORMAT_PGM;
235 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
236 opt_format = O_FORMAT_PGMASC;
238 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
239 opt_format = O_FORMAT_PNG;
240 else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
241 opt_format = O_FORMAT_PNG16;
244 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
245 opt_format = O_FORMAT_GIF;
247 else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
248 opt_format = O_FORMAT_DISP;
250 fprintf(stderr, "Invalid format mode %s\n", optarg);
251 sdf2img_usage(argv[0]);
269 fprintf(stdout, "Version %s\n", VERSION);
271 fprintf(stderr, "Unknown version number");
276 sdf2img_usage(argv[0]);
279 sdf2img_usage(argv[0]);
284 if ((opt_format == O_FORMAT_DISP && optind + 1 != argc)
285 || (opt_format != O_FORMAT_DISP && optind + 2 != argc))
287 sdf2img_usage(argv[0]);
291 in_file = argv[optind];
293 if (opt_format != O_FORMAT_DISP)
294 out_file = argv[optind+1];
295 else out_file = NULL;
297 if ((im = image_load (in_file)) == NULL)
299 fprintf(stderr, "File %s does not exist\n", in_file);
305 int nlabels = im->dfp_2d->dfp->num_label;
308 for (i = 0; i < nlabels; i++)
311 if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
313 if (blk.lab.label_type == LT_HISTORY) {
314 fprintf(stdout, "History: %s\n", blk.lab.label_str);
315 fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time);
316 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
317 } else if (blk.lab.label_type == LT_TITLE) {
318 fprintf(stdout, "Title: %s\n", blk.lab.label_str);
319 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
320 } else if (blk.lab.label_type == LT_NOTE) {
321 fprintf(stdout, "Note: %s\n", blk.lab.label_str);
322 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
327 if (opt_stats || (! (opt_set_max && opt_set_min)))
329 double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
330 double mode = 0, mean = 0, stddev = 0, window = 0;
333 int ibin, nbin = 256;
334 int max_bin, max_binindex;
339 for (ix = 0; ix < im->nx; ix++)
341 for (iy = 0; iy < im->ny; iy++)
343 if (im->v[ix][iy] > maxfound)
344 maxfound = im->v[ix][iy];
345 if (im->v[ix][iy] < minfound)
346 minfound = im->v[ix][iy];
347 mean += im->v[ix][iy];
350 spread = maxfound - minfound;
355 for (ibin = 0; ibin < nbin; ibin++)
357 for (ix = 0; ix < im->nx; ix++)
359 for (iy = 0; iy < im->ny; iy++)
361 int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
367 for (ibin = 0; ibin < nbin; ibin++)
369 if (hist[ibin] > max_bin)
371 max_bin = hist[ibin];
375 mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
378 if (opt_auto == O_AUTO_FULL)
385 if (opt_stats || opt_auto != O_AUTO_FULL)
387 mean /= im->nx * im->ny;
388 for (ix = 0; ix < im->nx; ix++)
390 for (iy = 0; iy < im->ny; iy++)
392 double diff = (im->v[ix][iy] - mean);
393 stddev += diff * diff;
396 stddev = sqrt(stddev / (im->nx * im->ny));
397 if (opt_auto == O_AUTO_FULL)
399 else if (opt_auto == O_AUTO_STD1)
401 else if (opt_auto == O_AUTO_STD0_1)
402 window = stddev * 0.1;
403 else if (opt_auto == O_AUTO_STD0_5)
404 window = stddev * 0.5;
405 else if (opt_auto == O_AUTO_STD2)
407 else if (opt_auto == O_AUTO_STD3)
411 fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);
417 fprintf(stdout,"nx=%d\n", im->nx);
418 fprintf(stdout,"ny=%d\n", im->ny);
419 fprintf(stdout,"min=%f\n", minfound);
420 fprintf(stdout,"max=%f\n", maxfound);
421 fprintf(stdout,"mean=%f\n", mean);
422 fprintf(stdout,"mode=%f\n", mode);
423 fprintf(stdout,"stddev=%f\n", stddev);
425 if (opt_auto != O_AUTO_FULL)
429 if (opt_center == O_CENTER_MODE)
431 else if (opt_center == O_CENTER_MEAN)
435 fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);
439 densmax = center + window;
441 densmin = center - window;
447 fprintf(stdout,"min_disp=%f\n", densmin);
448 fprintf(stdout,"max_disp=%f\n", densmax);
451 if (opt_format == O_FORMAT_PGM)
452 sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
453 else if (opt_format == O_FORMAT_PGMASC)
454 sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
456 else if (opt_format == O_FORMAT_PNG)
457 sdf2d_to_png (im, out_file, 8, opt_scale, opt_scale, densmin, densmax);
458 else if (opt_format == O_FORMAT_PNG16)
459 sdf2d_to_png (im, out_file, 16, opt_scale, opt_scale, densmin, densmax);
462 else if (opt_format == O_FORMAT_GIF)
463 sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
465 else if (opt_format == O_FORMAT_DISP) {
467 image_display_scale (im, opt_scale, densmin, densmax);
469 sgp2_close(sgp2_get_active_win());
474 fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);
482 sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
488 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
492 if ((fp = fopen (outfile, "wb")) == NULL)
496 fprintf(fp, "%d %d\n", im->nx, im->ny);
497 fprintf(fp, "255\n");
499 for (irow = im->ny - 1; irow >= 0; irow--)
503 for (icol = 0; icol < im->nx; icol++)
507 int pos = icol * nxcell;
508 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
513 for (p = pos; p < pos + nxcell; p++)
515 rowp[p] = (unsigned int) (dens * 255.);
518 for (ir = 0; ir < nycell; ir++)
521 for (ic = 0; ic < im->nx * nxcell; ic++)
522 fprintf(fp, "%c ", rowp[ic]);
525 sys_free(rowp, "sdf2img");
531 sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
537 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
541 if ((fp = fopen (outfile, "wb")) == NULL)
545 fprintf(fp, "%d %d\n", im->nx, im->ny);
546 fprintf(fp, "255\n");
548 for (irow = im->ny - 1; irow >= 0; irow--)
552 for (icol = 0; icol < im->nx; icol++)
556 int pos = icol * nxcell;
557 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
562 for (p = pos; p < pos + nxcell; p++)
564 rowp[p] = (unsigned int) (dens * 255.);
567 for (ir = 0; ir < nycell; ir++)
570 for (ic = 0; ic < im->nx * nxcell; ic++)
571 fprintf(fp, "%d ", rowp[ic]);
575 sys_free(rowp, "sdf2img");
583 sdf2d_to_png (IMAGE *im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
590 double max_out_level = (1 << bitdepth) - 1;
592 rowp = (unsigned char *) sys_alloc(im->nx * nxcell * (bitdepth / 8),"sdf2d_to_img");
596 if ((fp = fopen (outfile, "wb")) == NULL)
599 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
603 info_ptr = png_create_info_struct(png_ptr);
606 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
611 if (setjmp(png_ptr->jmpbuf))
613 png_destroy_write_struct(&png_ptr, &info_ptr);
618 png_init_io(png_ptr, fp);
620 png_set_IHDR(png_ptr, info_ptr, im->nx * nxcell, im->ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);
622 png_write_info(png_ptr, info_ptr);
623 for (irow = im->ny - 1; irow >= 0; irow--)
625 png_bytep row_pointer = rowp;
628 for (icol = 0; icol < im->nx; icol++)
633 int pos = icol * nxcell;
634 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
639 outval = (unsigned int) (dens * max_out_level);
640 for (p = pos; p < pos + nxcell; p++)
646 rowp[rowpos] = (outval >> 8) & 0xFF;
647 rowp[rowpos+1] = (outval & 0xFF);
651 for (ir = 0; ir < nycell; ir++)
653 png_write_rows (png_ptr, &row_pointer, 1);
656 sys_free(rowp, "sdf2img");
658 png_write_end(png_ptr, info_ptr);
659 png_destroy_write_struct(&png_ptr, &info_ptr);
667 #define N_GRAYSCALE 256
671 sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
676 int gs_indices[N_GRAYSCALE];
682 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
686 gif = gdImageCreate(im->nx * nxcell, im->ny * nycell);
687 for (i = 0; i < N_GRAYSCALE; i++)
688 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
690 lastrow = im->ny * nycell - 1;
691 for (irow = 0; irow < im->ny; irow++)
694 int rpos = irow * nycell;
695 for (ir = rpos; ir < rpos + nycell; ir++)
697 for (icol = 0; icol < im->nx; icol++)
701 int cpos = icol * nxcell;
702 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
707 for (ic = cpos; ic < cpos + nxcell; ic++)
709 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
710 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
715 sys_free(rowp, "sdf2img");
717 if ((out = fopen(outfile,"w")) == NULL)
719 fprintf(stderr,"Error opening output file %s for writing\n", outfile);
726 fprintf(stderr, "This version does not support GIF");
732 main (int argc, char *const argv[])
734 return (sdf2img_main(argc, argv));