1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: sdf2img.c,v 1.1 2000/04/28 13:02:44 kevin Exp $
7 ** Revision 1.1 2000/04/28 13:02:44 kevin
11 ** This program is free software; you can redistribute it and/or modify
12 ** it under the terms of the GNU General Public License (version 2) as
13 ** published by the Free Software Foundation.
15 ** This program is distributed in the hope that it will be useful,
16 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ** GNU General Public License for more details.
20 ** You should have received a copy of the GNU General Public License
21 ** along with this program; if not, write to the Free Software
22 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 ******************************************************************************/
25 * sdf2img.c Convert an SDF file to a viewable format image
31 void sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
34 void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
36 void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
37 void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
52 static struct option my_options[] =
54 {"scale", 1, 0, O_SCALE},
57 {"auto", 1, 0, O_AUTO},
58 {"center", 1, 0, O_CENTER},
59 {"format", 1, 0, O_FORMAT},
60 {"labels", 0, 0, O_LABELS},
61 {"stats", 0, 0, O_STATS},
62 {"verbose", 0, 0, O_VERBOSE},
63 {"debug", 0, 0, O_DEBUG},
64 {"help", 0, 0, O_HELP},
65 {"version", 0, 0, O_VERSION},
70 #define O_AUTO_STD0_1 2
71 #define O_AUTO_STD0_5 3
76 #define O_AUTO_FULL_STR "full"
77 #define O_AUTO_STD0_1_STR "std0.1"
78 #define O_AUTO_STD0_5_STR "std0.5"
79 #define O_AUTO_STD1_STR "std1"
80 #define O_AUTO_STD2_STR "std2"
81 #define O_AUTO_STD3_STR "std3"
83 #define O_CENTER_MEAN 1
84 #define O_CENTER_MODE 2
85 #define O_CENTER_MEAN_STR "mean"
86 #define O_CENTER_MODE_STR "mode"
88 #define O_FORMAT_GIF 1
89 #define O_FORMAT_PNG 2
90 #define O_FORMAT_PGM 3
91 #define O_FORMAT_PGMASC 4
92 #define O_FORMAT_GIF_STR "gif"
93 #define O_FORMAT_PNG_STR "png"
94 #define O_FORMAT_PGM_STR "pgm"
95 #define O_FORMAT_PGMASC_STR "pgmasc"
98 usage (const char *program)
100 fprintf(stdout, "usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
101 fprintf(stdout, "Convert SDF2D file to an image file\n");
102 fprintf(stdout, "\n");
103 fprintf(stdout, " sdfname Name of input SDF file\n");
104 fprintf(stdout, " outfile Name of output file\n");
105 fprintf(stdout, " --format Output format\n");
106 fprintf(stdout, " pgm PGM (portable graymap) format (default)\n");
107 fprintf(stdout, " pgmasc PGM (portable graymap) ASCII format\n");
109 fprintf(stdout, " png PNG format\n");
112 fprintf(stdout, " gif GIF format\n");
114 fprintf(stdout, " --center Center of window\n");
115 fprintf(stdout, " mode Mode is center of window (default)\n");
116 fprintf(stdout, " mean Mean is center of window\n");
117 fprintf(stdout, " --auto Set auto window\n");
118 fprintf(stdout, " full Use full window (default)\n");
119 fprintf(stdout, " std0.1 Use 0.1 standard deviation about center\n");
120 fprintf(stdout, " std0.5 Use 0.5 standard deviation about center\n");
121 fprintf(stdout, " std1 Use one standard deviation about center\n");
122 fprintf(stdout, " std2 Use two standard deviations about center\n");
123 fprintf(stdout, " std3 Use three standard deviations about center\n");
124 fprintf(stdout, " --scale Scaling factor for output size\n");
125 fprintf(stdout, " --min Set minimum intensity\n");
126 fprintf(stdout, " --max Set maximum intensity\n");
127 fprintf(stdout, " --stats Print image statistics\n");
128 fprintf(stdout, " --labels Print image labels\n");
129 fprintf(stdout, " --debug Set debug mode\n");
130 fprintf(stdout, " --verbose Set verbose mode\n");
131 fprintf(stdout, " --version Print version\n");
132 fprintf(stdout, " --help Print this help message\n");
137 main (int argc, char *const argv[])
140 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
141 char *in_file, *out_file;
144 int opt_auto = O_AUTO_FULL;
149 int opt_center = O_CENTER_MODE;
150 int opt_format = O_FORMAT_PGM;
155 int c = getopt_long (argc, argv, "", my_options, NULL);
166 densmin = strtod(optarg, &endptr);
167 endstr = optarg + strlen(optarg);
168 if (endptr != endstr)
170 fprintf(stderr, "Error setting --min to %s\n", optarg);
177 densmax = strtod(optarg, &endptr);
178 endstr = optarg + strlen(optarg);
179 if (endptr != endstr)
181 fprintf(stderr, "Error setting --max to %s\n", optarg);
187 opt_scale = strtol(optarg, &endptr, 10);
188 endstr = optarg + strlen(optarg);
189 if (endptr != endstr)
191 fprintf(stderr,"Error setting --scale to %s\n", optarg);
197 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
198 opt_auto = O_AUTO_FULL;
199 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
200 opt_auto = O_AUTO_STD1;
201 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
202 opt_auto = O_AUTO_STD0_5;
203 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
204 opt_auto = O_AUTO_STD0_1;
205 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
206 opt_auto = O_AUTO_STD2;
207 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
208 opt_auto = O_AUTO_STD3;
211 fprintf(stderr, "Invalid auto mode %s\n", optarg);
217 if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
218 opt_center = O_CENTER_MEAN;
219 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
220 opt_center = O_CENTER_MODE;
223 fprintf(stderr, "Invalid center mode %s\n", optarg);
229 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
230 opt_format = O_FORMAT_PGM;
231 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
232 opt_format = O_FORMAT_PGMASC;
234 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
235 opt_format = O_FORMAT_PNG;
238 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
239 opt_format = O_FORMAT_GIF;
242 fprintf(stderr, "Invalid format mode %s\n", optarg);
261 fprintf(stdout, "Version %s\n", VERSION);
263 fprintf(stderr, "Unknown version number");
276 if (optind + 2 != argc)
282 in_file = argv[optind];
283 out_file = argv[optind+1];
285 if ((im = image_load (in_file)) == NULL)
287 fprintf(stderr, "File %s does not exist\n", in_file);
293 int nlabels = im->dfp_2d->dfp->num_label;
296 for (i = 0; i < nlabels; i++)
299 if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
301 if (blk.lab.label_type == LT_HISTORY) {
302 fprintf(stdout, "History: %s\n", blk.lab.label_str);
303 fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time);
304 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
305 } else if (blk.lab.label_type == LT_TITLE) {
306 fprintf(stdout, "Title: %s\n", blk.lab.label_str);
307 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
308 } else if (blk.lab.label_type == LT_NOTE) {
309 fprintf(stdout, "Note: %s\n", blk.lab.label_str);
310 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
315 if (opt_stats || (! (opt_set_max && opt_set_min)))
317 double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
318 double mode = 0, mean = 0, stddev = 0, window = 0;
321 int ibin, nbin = 256;
322 int max_bin, max_binindex;
327 for (ix = 0; ix < im->nx; ix++)
329 for (iy = 0; iy < im->ny; iy++)
331 if (im->v[ix][iy] > maxfound)
332 maxfound = im->v[ix][iy];
333 if (im->v[ix][iy] < minfound)
334 minfound = im->v[ix][iy];
335 mean += im->v[ix][iy];
338 spread = maxfound - minfound;
343 for (ibin = 0; ibin < nbin; ibin++)
345 for (ix = 0; ix < im->nx; ix++)
347 for (iy = 0; iy < im->ny; iy++)
349 int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
355 for (ibin = 0; ibin < nbin; ibin++)
357 if (hist[ibin] > max_bin)
359 max_bin = hist[ibin];
363 mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
366 if (opt_auto == O_AUTO_FULL)
373 if (opt_stats || opt_auto != O_AUTO_FULL)
375 mean /= im->nx * im->ny;
376 for (ix = 0; ix < im->nx; ix++)
378 for (iy = 0; iy < im->ny; iy++)
380 double diff = (im->v[ix][iy] - mean);
381 stddev += diff * diff;
384 stddev = sqrt(stddev / (im->nx * im->ny));
385 if (opt_auto == O_AUTO_FULL)
387 else if (opt_auto == O_AUTO_STD1)
389 else if (opt_auto == O_AUTO_STD0_1)
390 window = stddev * 0.1;
391 else if (opt_auto == O_AUTO_STD0_5)
392 window = stddev * 0.5;
393 else if (opt_auto == O_AUTO_STD2)
395 else if (opt_auto == O_AUTO_STD3)
399 fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);
405 fprintf(stdout,"nx=%d\n", im->nx);
406 fprintf(stdout,"ny=%d\n", im->ny);
407 fprintf(stdout,"min=%f\n", minfound);
408 fprintf(stdout,"max=%f\n", maxfound);
409 fprintf(stdout,"mean=%f\n", mean);
410 fprintf(stdout,"mode=%f\n", mode);
411 fprintf(stdout,"stddev=%f\n", stddev);
413 if (opt_auto != O_AUTO_FULL)
417 if (opt_center == O_CENTER_MODE)
419 else if (opt_center == O_CENTER_MEAN)
423 fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);
427 densmax = center + window;
429 densmin = center - window;
435 fprintf(stdout,"min_disp=%f\n", densmin);
436 fprintf(stdout,"max_disp=%f\n", densmax);
439 if (opt_format == O_FORMAT_PGM)
440 sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
441 else if (opt_format == O_FORMAT_PGMASC)
442 sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
444 else if (opt_format == O_FORMAT_PNG)
445 sdf2d_to_png (im, out_file, opt_scale, opt_scale, densmin, densmax);
448 else if (opt_format == O_FORMAT_GIF)
449 sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
453 fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);
461 sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
467 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
471 if ((fp = fopen (outfile, "wb")) == NULL)
475 fprintf(fp, "%d %d\n", im->nx, im->ny);
476 fprintf(fp, "255\n");
478 for (irow = im->ny - 1; irow >= 0; irow--)
482 for (icol = 0; icol < im->nx; icol++)
486 int pos = icol * nxcell;
487 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
492 for (p = pos; p < pos + nxcell; p++)
494 rowp[p] = (unsigned int) (dens * 255.);
497 for (ir = 0; ir < nycell; ir++)
500 for (ic = 0; ic < im->nx * nxcell; ic++)
501 fprintf(fp, "%c ", rowp[ic]);
504 sys_free(rowp, "sdf2img");
510 sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
516 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
520 if ((fp = fopen (outfile, "wb")) == NULL)
524 fprintf(fp, "%d %d\n", im->nx, im->ny);
525 fprintf(fp, "255\n");
527 for (irow = im->ny - 1; irow >= 0; irow--)
531 for (icol = 0; icol < im->nx; icol++)
535 int pos = icol * nxcell;
536 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
541 for (p = pos; p < pos + nxcell; p++)
543 rowp[p] = (unsigned int) (dens * 255.);
546 for (ir = 0; ir < nycell; ir++)
549 for (ic = 0; ic < im->nx * nxcell; ic++)
550 fprintf(fp, "%d ", rowp[ic]);
554 sys_free(rowp, "sdf2img");
562 sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
570 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
574 if ((fp = fopen (outfile, "wb")) == NULL)
577 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
581 info_ptr = png_create_info_struct(png_ptr);
584 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
589 if (setjmp(png_ptr->jmpbuf))
591 png_destroy_write_struct(&png_ptr, &info_ptr);
596 png_init_io(png_ptr, fp);
598 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);
600 png_write_info(png_ptr, info_ptr);
601 for (irow = im->ny - 1; irow >= 0; irow--)
603 png_bytep row_pointer;
606 for (icol = 0; icol < im->nx; icol++)
610 int pos = icol * nxcell;
611 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
616 for (p = pos; p < pos + nxcell; p++)
618 rowp[p] = (unsigned int) (dens * 255.);
622 for (ir = 0; ir < nycell; ir++)
624 png_write_rows (png_ptr, &row_pointer, 1);
627 sys_free(rowp, "sdf2img");
629 png_write_end(png_ptr, info_ptr);
630 png_destroy_write_struct(&png_ptr, &info_ptr);
638 #define N_GRAYSCALE 256
642 sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
647 int gs_indices[N_GRAYSCALE];
653 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
657 gif = gdImageCreate(im->nx * nxcell, im->ny * nycell);
658 for (i = 0; i < N_GRAYSCALE; i++)
659 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
661 lastrow = im->ny * nycell - 1;
662 for (irow = 0; irow < im->ny; irow++)
665 int rpos = irow * nycell;
666 for (ir = rpos; ir < rpos + nycell; ir++)
668 for (icol = 0; icol < im->nx; icol++)
672 int cpos = icol * nxcell;
673 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
678 for (ic = cpos; ic < cpos + nxcell; ic++)
680 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
681 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
686 sys_free(rowp, "sdf2img");
688 if ((out = fopen(outfile,"w")) == NULL)
690 fprintf(stderr,"Error opening output file %s for writing\n", outfile);
697 fprintf(stderr, "This version does not support GIF");