1 /*****************************************************************************
2 ** This is part of the CTSim program
3 ** Copyright (C) 1983-2000 Kevin Rosenberg
5 ** $Id: sdf2img.c,v 1.2 2000/05/08 20:02:32 kevin Exp $
7 ** Revision 1.2 2000/05/08 20:02:32 kevin
10 ** Revision 1.1.1.1 2000/04/28 13:02:44 kevin
11 ** Initial CVS import for first public release
15 ** This program is free software; you can redistribute it and/or modify
16 ** it under the terms of the GNU General Public License (version 2) as
17 ** published by the Free Software Foundation.
19 ** This program is distributed in the hope that it will be useful,
20 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ** GNU General Public License for more details.
24 ** You should have received a copy of the GNU General Public License
25 ** along with this program; if not, write to the Free Software
26 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 ******************************************************************************/
29 * sdf2img.c Convert an SDF file to a viewable format image
35 void sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
38 void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
40 void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
41 void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
43 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS,
44 O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
46 static struct option my_options[] =
48 {"scale", 1, 0, O_SCALE},
51 {"auto", 1, 0, O_AUTO},
52 {"center", 1, 0, O_CENTER},
53 {"format", 1, 0, O_FORMAT},
54 {"labels", 0, 0, O_LABELS},
55 {"stats", 0, 0, O_STATS},
56 {"verbose", 0, 0, O_VERBOSE},
57 {"debug", 0, 0, O_DEBUG},
58 {"help", 0, 0, O_HELP},
59 {"version", 0, 0, O_VERSION},
64 #define O_AUTO_STD0_1 2
65 #define O_AUTO_STD0_5 3
70 #define O_AUTO_FULL_STR "full"
71 #define O_AUTO_STD0_1_STR "std0.1"
72 #define O_AUTO_STD0_5_STR "std0.5"
73 #define O_AUTO_STD1_STR "std1"
74 #define O_AUTO_STD2_STR "std2"
75 #define O_AUTO_STD3_STR "std3"
77 #define O_CENTER_MEAN 1
78 #define O_CENTER_MODE 2
79 #define O_CENTER_MEAN_STR "mean"
80 #define O_CENTER_MODE_STR "mode"
82 #define O_FORMAT_GIF 1
83 #define O_FORMAT_PNG 2
84 #define O_FORMAT_PGM 3
85 #define O_FORMAT_PGMASC 4
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"
92 usage (const char *program)
94 fprintf(stdout, "usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
95 fprintf(stdout, "Convert SDF2D file to an image file\n");
96 fprintf(stdout, "\n");
97 fprintf(stdout, " sdfname Name of input SDF file\n");
98 fprintf(stdout, " outfile Name of output file\n");
99 fprintf(stdout, " --format Output format\n");
100 fprintf(stdout, " pgm PGM (portable graymap) format (default)\n");
101 fprintf(stdout, " pgmasc PGM (portable graymap) ASCII format\n");
103 fprintf(stdout, " png PNG format\n");
106 fprintf(stdout, " gif GIF format\n");
108 fprintf(stdout, " --center Center of window\n");
109 fprintf(stdout, " mode Mode is center of window (default)\n");
110 fprintf(stdout, " mean Mean is center of window\n");
111 fprintf(stdout, " --auto Set auto window\n");
112 fprintf(stdout, " full Use full window (default)\n");
113 fprintf(stdout, " std0.1 Use 0.1 standard deviation about center\n");
114 fprintf(stdout, " std0.5 Use 0.5 standard deviation about center\n");
115 fprintf(stdout, " std1 Use one standard deviation about center\n");
116 fprintf(stdout, " std2 Use two standard deviations about center\n");
117 fprintf(stdout, " std3 Use three standard deviations about center\n");
118 fprintf(stdout, " --scale Scaling factor for output size\n");
119 fprintf(stdout, " --min Set minimum intensity\n");
120 fprintf(stdout, " --max Set maximum intensity\n");
121 fprintf(stdout, " --stats Print image statistics\n");
122 fprintf(stdout, " --labels Print image labels\n");
123 fprintf(stdout, " --debug Set debug mode\n");
124 fprintf(stdout, " --verbose Set verbose mode\n");
125 fprintf(stdout, " --version Print version\n");
126 fprintf(stdout, " --help Print this help message\n");
131 main (int argc, char *const argv[])
134 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
135 char *in_file, *out_file;
138 int opt_auto = O_AUTO_FULL;
143 int opt_center = O_CENTER_MODE;
144 int opt_format = O_FORMAT_PGM;
149 int c = getopt_long (argc, argv, "", my_options, NULL);
160 densmin = strtod(optarg, &endptr);
161 endstr = optarg + strlen(optarg);
162 if (endptr != endstr)
164 fprintf(stderr, "Error setting --min to %s\n", optarg);
171 densmax = strtod(optarg, &endptr);
172 endstr = optarg + strlen(optarg);
173 if (endptr != endstr)
175 fprintf(stderr, "Error setting --max to %s\n", optarg);
181 opt_scale = strtol(optarg, &endptr, 10);
182 endstr = optarg + strlen(optarg);
183 if (endptr != endstr)
185 fprintf(stderr,"Error setting --scale to %s\n", optarg);
191 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
192 opt_auto = O_AUTO_FULL;
193 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
194 opt_auto = O_AUTO_STD1;
195 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
196 opt_auto = O_AUTO_STD0_5;
197 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
198 opt_auto = O_AUTO_STD0_1;
199 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
200 opt_auto = O_AUTO_STD2;
201 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
202 opt_auto = O_AUTO_STD3;
205 fprintf(stderr, "Invalid auto mode %s\n", optarg);
211 if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
212 opt_center = O_CENTER_MEAN;
213 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
214 opt_center = O_CENTER_MODE;
217 fprintf(stderr, "Invalid center mode %s\n", optarg);
223 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
224 opt_format = O_FORMAT_PGM;
225 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
226 opt_format = O_FORMAT_PGMASC;
228 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
229 opt_format = O_FORMAT_PNG;
232 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
233 opt_format = O_FORMAT_GIF;
236 fprintf(stderr, "Invalid format mode %s\n", optarg);
255 fprintf(stdout, "Version %s\n", VERSION);
257 fprintf(stderr, "Unknown version number");
270 if (optind + 2 != argc)
276 in_file = argv[optind];
277 out_file = argv[optind+1];
279 if ((im = image_load (in_file)) == NULL)
281 fprintf(stderr, "File %s does not exist\n", in_file);
287 int nlabels = im->dfp_2d->dfp->num_label;
290 for (i = 0; i < nlabels; i++)
293 if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)
295 if (blk.lab.label_type == LT_HISTORY) {
296 fprintf(stdout, "History: %s\n", blk.lab.label_str);
297 fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time);
298 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
299 } else if (blk.lab.label_type == LT_TITLE) {
300 fprintf(stdout, "Title: %s\n", blk.lab.label_str);
301 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
302 } else if (blk.lab.label_type == LT_NOTE) {
303 fprintf(stdout, "Note: %s\n", blk.lab.label_str);
304 fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));
309 if (opt_stats || (! (opt_set_max && opt_set_min)))
311 double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
312 double mode = 0, mean = 0, stddev = 0, window = 0;
315 int ibin, nbin = 256;
316 int max_bin, max_binindex;
321 for (ix = 0; ix < im->nx; ix++)
323 for (iy = 0; iy < im->ny; iy++)
325 if (im->v[ix][iy] > maxfound)
326 maxfound = im->v[ix][iy];
327 if (im->v[ix][iy] < minfound)
328 minfound = im->v[ix][iy];
329 mean += im->v[ix][iy];
332 spread = maxfound - minfound;
337 for (ibin = 0; ibin < nbin; ibin++)
339 for (ix = 0; ix < im->nx; ix++)
341 for (iy = 0; iy < im->ny; iy++)
343 int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
349 for (ibin = 0; ibin < nbin; ibin++)
351 if (hist[ibin] > max_bin)
353 max_bin = hist[ibin];
357 mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
360 if (opt_auto == O_AUTO_FULL)
367 if (opt_stats || opt_auto != O_AUTO_FULL)
369 mean /= im->nx * im->ny;
370 for (ix = 0; ix < im->nx; ix++)
372 for (iy = 0; iy < im->ny; iy++)
374 double diff = (im->v[ix][iy] - mean);
375 stddev += diff * diff;
378 stddev = sqrt(stddev / (im->nx * im->ny));
379 if (opt_auto == O_AUTO_FULL)
381 else if (opt_auto == O_AUTO_STD1)
383 else if (opt_auto == O_AUTO_STD0_1)
384 window = stddev * 0.1;
385 else if (opt_auto == O_AUTO_STD0_5)
386 window = stddev * 0.5;
387 else if (opt_auto == O_AUTO_STD2)
389 else if (opt_auto == O_AUTO_STD3)
393 fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);
399 fprintf(stdout,"nx=%d\n", im->nx);
400 fprintf(stdout,"ny=%d\n", im->ny);
401 fprintf(stdout,"min=%f\n", minfound);
402 fprintf(stdout,"max=%f\n", maxfound);
403 fprintf(stdout,"mean=%f\n", mean);
404 fprintf(stdout,"mode=%f\n", mode);
405 fprintf(stdout,"stddev=%f\n", stddev);
407 if (opt_auto != O_AUTO_FULL)
411 if (opt_center == O_CENTER_MODE)
413 else if (opt_center == O_CENTER_MEAN)
417 fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);
421 densmax = center + window;
423 densmin = center - window;
429 fprintf(stdout,"min_disp=%f\n", densmin);
430 fprintf(stdout,"max_disp=%f\n", densmax);
433 if (opt_format == O_FORMAT_PGM)
434 sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
435 else if (opt_format == O_FORMAT_PGMASC)
436 sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
438 else if (opt_format == O_FORMAT_PNG)
439 sdf2d_to_png (im, out_file, opt_scale, opt_scale, densmin, densmax);
442 else if (opt_format == O_FORMAT_GIF)
443 sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
447 fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);
455 sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
461 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
465 if ((fp = fopen (outfile, "wb")) == NULL)
469 fprintf(fp, "%d %d\n", im->nx, im->ny);
470 fprintf(fp, "255\n");
472 for (irow = im->ny - 1; irow >= 0; irow--)
476 for (icol = 0; icol < im->nx; icol++)
480 int pos = icol * nxcell;
481 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
486 for (p = pos; p < pos + nxcell; p++)
488 rowp[p] = (unsigned int) (dens * 255.);
491 for (ir = 0; ir < nycell; ir++)
494 for (ic = 0; ic < im->nx * nxcell; ic++)
495 fprintf(fp, "%c ", rowp[ic]);
498 sys_free(rowp, "sdf2img");
504 sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
510 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
514 if ((fp = fopen (outfile, "wb")) == NULL)
518 fprintf(fp, "%d %d\n", im->nx, im->ny);
519 fprintf(fp, "255\n");
521 for (irow = im->ny - 1; irow >= 0; irow--)
525 for (icol = 0; icol < im->nx; icol++)
529 int pos = icol * nxcell;
530 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
535 for (p = pos; p < pos + nxcell; p++)
537 rowp[p] = (unsigned int) (dens * 255.);
540 for (ir = 0; ir < nycell; ir++)
543 for (ic = 0; ic < im->nx * nxcell; ic++)
544 fprintf(fp, "%d ", rowp[ic]);
548 sys_free(rowp, "sdf2img");
556 sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
564 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
568 if ((fp = fopen (outfile, "wb")) == NULL)
571 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
575 info_ptr = png_create_info_struct(png_ptr);
578 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
583 if (setjmp(png_ptr->jmpbuf))
585 png_destroy_write_struct(&png_ptr, &info_ptr);
590 png_init_io(png_ptr, fp);
592 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);
594 png_write_info(png_ptr, info_ptr);
595 for (irow = im->ny - 1; irow >= 0; irow--)
597 png_bytep row_pointer;
600 for (icol = 0; icol < im->nx; icol++)
604 int pos = icol * nxcell;
605 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
610 for (p = pos; p < pos + nxcell; p++)
612 rowp[p] = (unsigned int) (dens * 255.);
616 for (ir = 0; ir < nycell; ir++)
618 png_write_rows (png_ptr, &row_pointer, 1);
621 sys_free(rowp, "sdf2img");
623 png_write_end(png_ptr, info_ptr);
624 png_destroy_write_struct(&png_ptr, &info_ptr);
632 #define N_GRAYSCALE 256
636 sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
641 int gs_indices[N_GRAYSCALE];
647 rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");
651 gif = gdImageCreate(im->nx * nxcell, im->ny * nycell);
652 for (i = 0; i < N_GRAYSCALE; i++)
653 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
655 lastrow = im->ny * nycell - 1;
656 for (irow = 0; irow < im->ny; irow++)
659 int rpos = irow * nycell;
660 for (ir = rpos; ir < rpos + nycell; ir++)
662 for (icol = 0; icol < im->nx; icol++)
666 int cpos = icol * nxcell;
667 dens = (im->v[icol][irow] - densmin) / (densmax - densmin);
672 for (ic = cpos; ic < cpos + nxcell; ic++)
674 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
675 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
680 sys_free(rowp, "sdf2img");
682 if ((out = fopen(outfile,"w")) == NULL)
684 fprintf(stderr,"Error opening output file %s for writing\n", outfile);
691 fprintf(stderr, "This version does not support GIF");