1 /*****************************************************************************
5 ** Purpose: Convert an image file to a viewable image
6 ** Programmer: Kevin Rosenberg
7 ** Date Started: April 2000
9 ** This is part of the CTSim program
10 ** Copyright (C) 1983-2000 Kevin Rosenberg
12 ** $Id: if2img.cpp,v 1.6 2000/06/18 10:27:11 kevin Exp $
14 ** This program is free software; you can redistribute it and/or modify
15 ** it under the terms of the GNU General Public License (version 2) as
16 ** published by the Free Software Foundation.
18 ** This program is distributed in the hope that it will be useful,
19 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ** GNU General Public License for more details.
23 ** You should have received a copy of the GNU General Public License
24 ** along with this program; if not, write to the Free Software
25 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 ******************************************************************************/
29 * if2img.c Convert an SDF file to a viewable format image
35 void sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax);
38 void sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
40 void sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
41 void sdf2d_to_pgmasc (ImageFile& 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},
63 enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
64 #define O_AUTO_FULL_STR "full"
65 #define O_AUTO_STD0_1_STR "std0.1"
66 #define O_AUTO_STD0_5_STR "std0.5"
67 #define O_AUTO_STD1_STR "std1"
68 #define O_AUTO_STD2_STR "std2"
69 #define O_AUTO_STD3_STR "std3"
71 enum { O_CENTER_MEAN, O_CENTER_MODE };
72 #define O_CENTER_MEAN_STR "mean"
73 #define O_CENTER_MODE_STR "mode"
75 enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
76 #define O_FORMAT_GIF_STR "gif"
77 #define O_FORMAT_PNG_STR "png"
78 #define O_FORMAT_PNG16_STR "png16"
79 #define O_FORMAT_PGM_STR "pgm"
80 #define O_FORMAT_PGMASC_STR "pgmasc"
81 #define O_FORMAT_DISP_STR "disp"
84 if2img_usage (const char *program)
86 cout << "usage: " << fileBasename(program) << " ifname outfile [OPTIONS]" << endl;
87 cout << "Convert IF file to an image file" << endl;
89 cout << " sdfname Name of input SDF file" << endl;
90 cout << " outfile Name of output file" << endl;
91 cout << " --format Output format" << endl;
92 cout << " pgm PGM (portable graymap) format (default)" << endl;
93 cout << " pgmasc PGM (portable graymap) ASCII format" << endl;
95 cout << " png PNG (8-bit) format" << endl;
96 cout << " png16 PNG (16-bit) format" << endl;
99 cout << " gif GIF format" << endl;
101 cout << " disp Display on screen" << endl;
102 cout << " --center Center of window" << endl;
103 cout << " mode Mode is center of window (default)" << endl;
104 cout << " mean Mean is center of window" << endl;
105 cout << " --auto Set auto window" << endl;
106 cout << " full Use full window (default)" << endl;
107 cout << " std0.1 Use 0.1 standard deviation about center" << endl;
108 cout << " std0.5 Use 0.5 standard deviation about center" << endl;
109 cout << " std1 Use one standard deviation about center" << endl;
110 cout << " std2 Use two standard deviations about center" << endl;
111 cout << " std3 Use three standard deviations about center" << endl;
112 cout << " --scale Scaling factor for output size" << endl;
113 cout << " --min Set minimum intensity" << endl;
114 cout << " --max Set maximum intensity" << endl;
115 cout << " --stats Print image statistics" << endl;
116 cout << " --labels Print image labels" << endl;
117 cout << " --debug Set debug mode" << endl;
118 cout << " --verbose Set verbose mode" << endl;
119 cout << " --version Print version" << endl;
120 cout << " --help Print this help message" << endl;
125 if2img_main (int argc, char *const argv[])
127 ImageFile* pim = NULL;
129 double densmin = HUGE_VAL, densmax = -HUGE_VAL;
130 char *in_file, *out_file;
133 int opt_auto = O_AUTO_FULL;
138 int opt_center = O_CENTER_MODE;
139 int opt_format = O_FORMAT_PGM;
144 int c = getopt_long (argc, argv, "", my_options, NULL);
155 densmin = strtod(optarg, &endptr);
156 endstr = optarg + strlen(optarg);
157 if (endptr != endstr)
159 sys_error (ERR_SEVERE, "Error setting --min to %s", optarg);
160 if2img_usage(argv[0]);
166 densmax = strtod(optarg, &endptr);
167 endstr = optarg + strlen(optarg);
168 if (endptr != endstr)
170 sys_error (ERR_SEVERE, "Error setting --max to %s", optarg);
171 if2img_usage(argv[0]);
176 opt_scale = strtol(optarg, &endptr, 10);
177 endstr = optarg + strlen(optarg);
178 if (endptr != endstr)
180 sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg);
181 if2img_usage(argv[0]);
186 if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
187 opt_auto = O_AUTO_FULL;
188 else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
189 opt_auto = O_AUTO_STD1;
190 else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
191 opt_auto = O_AUTO_STD0_5;
192 else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
193 opt_auto = O_AUTO_STD0_1;
194 else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
195 opt_auto = O_AUTO_STD2;
196 else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
197 opt_auto = O_AUTO_STD3;
200 sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg);
201 if2img_usage(argv[0]);
206 if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
207 opt_center = O_CENTER_MEAN;
208 else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
209 opt_center = O_CENTER_MODE;
212 sys_error (ERR_SEVERE, "Invalid center mode %s", optarg);
213 if2img_usage(argv[0]);
218 if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
219 opt_format = O_FORMAT_PGM;
220 else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
221 opt_format = O_FORMAT_PGMASC;
223 else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
224 opt_format = O_FORMAT_PNG;
225 else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
226 opt_format = O_FORMAT_PNG16;
229 else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
230 opt_format = O_FORMAT_GIF;
232 else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
233 opt_format = O_FORMAT_DISP;
235 sys_error (ERR_SEVERE, "Invalid format mode %s", optarg);
236 if2img_usage(argv[0]);
254 cout << "Version " << VERSION;
256 cout << "Unknown version number" << endl;
261 if2img_usage(argv[0]);
264 if2img_usage(argv[0]);
269 if ((opt_format == O_FORMAT_DISP && optind + 1 != argc)
270 || (opt_format != O_FORMAT_DISP && optind + 2 != argc))
272 if2img_usage(argv[0]);
276 in_file = argv[optind];
278 if (opt_format != O_FORMAT_DISP)
279 out_file = argv[optind+1];
280 else out_file = NULL;
282 pim = new ImageFile (in_file);
283 ImageFile& im = *pim;
284 if (! im.fileRead()) {
285 sys_error (ERR_SEVERE, "File %s does not exist", in_file);
290 int nlabels = im.getNumLabels();
292 for (int i = 0; i < nlabels; i++) {
293 Array2dFileLabel label;
294 im.labelRead (label, i);
296 if (label.getLabelType() == Array2dFileLabel::L_HISTORY) {
297 cout << "History: " << label.getLabelString() << endl;
298 cout << " calc time = " << label.getCalcTime() << " secs" << endl;
299 cout << " Timestamp = " << label.getDateString() << endl;
300 } else if (label.getLabelType() == Array2dFileLabel::L_USER) {
301 cout << "Note: " << label.getLabelString() << endl;
302 cout << " Timestamp = %s" << label.getDateString() << endl;
307 if (opt_stats || (! (opt_set_max && opt_set_min))) {
308 double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
309 double mode = 0, mean = 0, stddev = 0, window = 0;
312 int ibin, nbin = 256;
313 int max_bin, max_binindex;
317 ImageFileArray v = im.getArray();
320 for (int ix = 0; ix < nx; ix++) {
321 for (int iy = 0; iy < ny; iy++) {
322 if (v[ix][iy] > maxfound)
323 maxfound = v[ix][iy];
324 if (v[ix][iy] < minfound)
325 minfound = v[ix][iy];
329 spread = maxfound - minfound;
333 for (ibin = 0; ibin < nbin; ibin++)
335 for (int ix = 0; ix < nx; ix++) {
336 for (int iy = 0; iy < ny; iy++) {
337 int b = (int) ((((v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
343 for (ibin = 0; ibin < nbin; ibin++) {
344 if (hist[ibin] > max_bin) {
345 max_bin = hist[ibin];
349 mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
352 if (opt_auto == O_AUTO_FULL) {
358 if (opt_stats || opt_auto != O_AUTO_FULL) {
360 for (int ix = 0; ix < nx; ix++) {
361 for (int iy = 0; iy < ny; iy++) {
362 double diff = (v[ix][iy] - mean);
363 stddev += diff * diff;
366 stddev = sqrt(stddev / (nx * ny));
367 if (opt_auto == O_AUTO_FULL)
369 else if (opt_auto == O_AUTO_STD1)
371 else if (opt_auto == O_AUTO_STD0_1)
372 window = stddev * 0.1;
373 else if (opt_auto == O_AUTO_STD0_5)
374 window = stddev * 0.5;
375 else if (opt_auto == O_AUTO_STD2)
377 else if (opt_auto == O_AUTO_STD3)
380 sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
385 cout <<"nx: " << nx << endl;
386 cout <<"ny: " << ny << endl;
387 cout <<"min: " << minfound << endl;
388 cout <<"max: " << maxfound << endl;
389 cout <<"mean: " << mean << endl;
390 cout <<"mode: " << mode << endl;
391 cout <<"stddev: " << stddev << endl;
393 if (opt_auto != O_AUTO_FULL) {
396 if (opt_center == O_CENTER_MODE)
398 else if (opt_center == O_CENTER_MEAN)
401 sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
405 densmax = center + window;
407 densmin = center - window;
412 cout << "min display: " << densmin << endl;
413 cout << "max display: " << densmax << endl;
416 if (opt_format == O_FORMAT_PGM)
417 sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
418 else if (opt_format == O_FORMAT_PGMASC)
419 sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
421 else if (opt_format == O_FORMAT_PNG)
422 sdf2d_to_png (im, out_file, 8, opt_scale, opt_scale, densmin, densmax);
423 else if (opt_format == O_FORMAT_PNG16)
424 sdf2d_to_png (im, out_file, 16, opt_scale, opt_scale, densmin, densmax);
427 else if (opt_format == O_FORMAT_GIF)
428 sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
430 else if (opt_format == O_FORMAT_DISP) {
432 // image_display_scale (im, opt_scale, densmin, densmax);
434 sgp2_close(sgp2_get_active_win());
439 sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format);
447 sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
452 ImageFileArray v = im.getArray();
454 unsigned char* rowp = new unsigned char [nx * nxcell];
456 if ((fp = fopen (outfile, "wb")) == NULL)
460 fprintf(fp, "%d %d\n", nx, ny);
461 fprintf(fp, "255\n");
463 for (int irow = ny - 1; irow >= 0; irow--) {
464 for (int icol = 0; icol < nx; icol++) {
465 int pos = icol * nxcell;
466 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
467 dens = clamp (dens, 0., 1.);
468 for (int p = pos; p < pos + nxcell; p++) {
469 rowp[p] = static_cast<unsigned int> (dens * 255.);
472 for (int ir = 0; ir < nycell; ir++) {
473 for (int ic = 0; ic < nx * nxcell; ic++)
474 fprintf(fp, "%c ", rowp[ic]);
483 sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
488 ImageFileArray v = im.getArray();
490 unsigned char* rowp = new unsigned char [nx * nxcell];
494 if ((fp = fopen (outfile, "wb")) == NULL)
498 fprintf(fp, "%d %d\n", nx, ny);
499 fprintf(fp, "255\n");
501 for (int irow = ny - 1; irow >= 0; irow--) {
502 for (int icol = 0; icol < nx; icol++) {
503 int pos = icol * nxcell;
504 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
505 dens = clamp (dens, 0., 1.);
506 for (int p = pos; p < pos + nxcell; p++) {
507 rowp[p] = static_cast<unsigned int> (dens * 255.);
510 for (int ir = 0; ir < nycell; ir++) {
511 for (int ic = 0; ic < nx * nxcell; ic++)
512 fprintf(fp, "%d ", rowp[ic]);
524 sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
529 double max_out_level = (1 << bitdepth) - 1;
532 ImageFileArray v = im.getArray();
534 unsigned char* rowp = new unsigned char [nx * nxcell * (bitdepth / 8)];
536 if ((fp = fopen (outfile, "wb")) == NULL)
539 png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
543 info_ptr = png_create_info_struct(png_ptr);
545 png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
550 if (setjmp(png_ptr->jmpbuf)) {
551 png_destroy_write_struct(&png_ptr, &info_ptr);
556 png_init_io(png_ptr, fp);
558 png_set_IHDR(png_ptr, info_ptr, nx * nxcell, ny * nycell, bitdepth, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT);
560 png_write_info(png_ptr, info_ptr);
561 for (int irow = ny - 1; irow >= 0; irow--) {
562 png_bytep row_pointer = rowp;
564 for (int icol = 0; icol < nx; icol++) {
565 int pos = icol * nxcell;
566 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
567 dens = clamp (dens, 0., 1.);
568 unsigned int outval = static_cast<unsigned int> (dens * max_out_level);
570 for (int p = pos; p < pos + nxcell; p++) {
575 rowp[rowpos] = (outval >> 8) & 0xFF;
576 rowp[rowpos+1] = (outval & 0xFF);
580 for (int ir = 0; ir < nycell; ir++)
581 png_write_rows (png_ptr, &row_pointer, 1);
585 png_write_end(png_ptr, info_ptr);
586 png_destroy_write_struct(&png_ptr, &info_ptr);
594 #define N_GRAYSCALE 256
598 sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
603 int gs_indices[N_GRAYSCALE];
606 ImageFileArray v = im.getArray();
608 usnigned char* rowp = new unsigned char [nx * nxcell];
612 gif = gdImageCreate(nx * nxcell, ny * nycell);
613 for (int i = 0; i < N_GRAYSCALE; i++)
614 gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
616 int lastrow = ny * nycell - 1;
617 for (int irow = 0; irow < ny; irow++) {
618 int rpos = irow * nycell;
619 for (int ir = rpos; ir < rpos + nycell; ir++) {
620 for (int icol = 0; icol < nx; icol++) {
621 int cpos = icol * nxcell;
622 double dens = (v[icol][irow] - densmin) / (densmax - densmin);
623 dens = clamp(dens, 0., 1.);
624 for (int ic = cpos; ic < cpos + nxcell; ic++) {
625 rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
626 gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
633 if ((out = fopen(outfile,"w")) == NULL) {
634 sys_error(ERR_SEVERE,"Error opening output file %s for writing", outfile);
641 cout << "This version does not support GIF" << endl;
647 main (int argc, char *const argv[])
649 return (if2img_main(argc, argv));