X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=tools%2Fif2img.cpp;fp=tools%2Fif2img.cpp;h=b00c1775f9f03093c8864d66a505b03ce53bc01e;hb=1fd4f7cc977b9f1499716de10d15656bd50f4816;hp=0000000000000000000000000000000000000000;hpb=52107baf438f31ce8930b062e7fdebf95d3fd9ee;p=ctsim.git diff --git a/tools/if2img.cpp b/tools/if2img.cpp new file mode 100644 index 0000000..b00c177 --- /dev/null +++ b/tools/if2img.cpp @@ -0,0 +1,388 @@ +/***************************************************************************** +** FILE IDENTIFICATION +** +** Name: if2img.cpp +** Purpose: Convert an image file to a viewable image +** Programmer: Kevin Rosenberg +** Date Started: April 2000 +** +** This is part of the CTSim program +** Copyright (C) 1983-2000 Kevin Rosenberg +** +** $Id: if2img.cpp,v 1.1 2000/07/13 07:01:35 kevin Exp $ +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License (version 2) as +** published by the Free Software Foundation. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +******************************************************************************/ + +#include "ct.h" + + +enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS, + O_HELP, O_VERBOSE, O_VERSION, O_DEBUG }; + +static struct option my_options[] = +{ + {"scale", 1, 0, O_SCALE}, + {"min", 1, 0, O_MIN}, + {"max", 1, 0, O_MAX}, + {"auto", 1, 0, O_AUTO}, + {"center", 1, 0, O_CENTER}, + {"format", 1, 0, O_FORMAT}, + {"labels", 0, 0, O_LABELS}, + {"stats", 0, 0, O_STATS}, + {"verbose", 0, 0, O_VERBOSE}, + {"debug", 0, 0, O_DEBUG}, + {"help", 0, 0, O_HELP}, + {"version", 0, 0, O_VERSION}, + {0, 0, 0, 0} +}; + +enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 }; +static const char O_AUTO_FULL_STR[]="full"; +static const char O_AUTO_STD0_1_STR[]="std0.1"; +static const char O_AUTO_STD0_5_STR[]="std0.5"; +static const char O_AUTO_STD1_STR[]="std1"; +static const char O_AUTO_STD2_STR[]="std2"; +static const char O_AUTO_STD3_STR[]="std3"; + +enum { O_CENTER_MEAN, O_CENTER_MODE }; +static const char O_CENTER_MEAN_STR[]="mean"; +static const char O_CENTER_MODE_STR[]="mode"; + +enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP }; +static const char O_FORMAT_GIF_STR[]="gif"; +static const char O_FORMAT_PNG_STR[]="png" ; +static const char O_FORMAT_PNG16_STR[]="png16" ; +static const char O_FORMAT_PGM_STR[]="pgm"; +static const char O_FORMAT_PGMASC_STR[]="pgmasc"; +static const char O_FORMAT_DISP_STR[]="disp"; + +void +if2img_usage (const char *program) +{ + cout << "usage: " << fileBasename(program) << " ifname outfile [OPTIONS]" << endl; + cout << "Convert IF file to an image file" << endl; + cout << endl; + cout << " ifname Name of input file" << endl; + cout << " outfile Name of output file" << endl; + cout << " --format Output format" << endl; + cout << " pgm PGM (portable graymap) format (default)" << endl; + cout << " pgmasc PGM (portable graymap) ASCII format" << endl; +#ifdef HAVE_PNG + cout << " png PNG (8-bit) format" << endl; + cout << " png16 PNG (16-bit) format" << endl; +#endif +#if HAVE_G2 + cout << " gif GIF format" << endl; +#endif + cout << " disp Display on screen" << endl; + cout << " --center Center of window" << endl; + cout << " mode Mode is center of window (default)" << endl; + cout << " mean Mean is center of window" << endl; + cout << " --auto Set auto window" << endl; + cout << " full Use full window (default)" << endl; + cout << " std0.1 Use 0.1 standard deviation about center" << endl; + cout << " std0.5 Use 0.5 standard deviation about center" << endl; + cout << " std1 Use one standard deviation about center" << endl; + cout << " std2 Use two standard deviations about center" << endl; + cout << " std3 Use three standard deviations about center" << endl; + cout << " --scale Scaling factor for output size" << endl; + cout << " --min Set minimum intensity" << endl; + cout << " --max Set maximum intensity" << endl; + cout << " --stats Print image statistics" << endl; + cout << " --labels Print image labels" << endl; + cout << " --debug Set debug mode" << endl; + cout << " --verbose Set verbose mode" << endl; + cout << " --version Print version" << endl; + cout << " --help Print this help message" << endl; +} + + +int +if2img_main (int argc, char *const argv[]) +{ + ImageFile* pim = NULL; + double densmin = HUGE_VAL, densmax = -HUGE_VAL; + char *in_file, *out_file; + int opt_verbose = 0; + int opt_scale = 1; + int opt_auto = O_AUTO_FULL; + int opt_set_max = 0; + int opt_set_min = 0; + int opt_stats = 0; + int opt_debug = 0; + int opt_center = O_CENTER_MODE; + int opt_format = O_FORMAT_PGM; + int opt_labels = 0; + + while (1) + { + int c = getopt_long (argc, argv, "", my_options, NULL); + char *endptr = NULL; + char *endstr; + + if (c == -1) + break; + + switch (c) + { + case O_MIN: + opt_set_min = 1; + densmin = strtod(optarg, &endptr); + endstr = optarg + strlen(optarg); + if (endptr != endstr) + { + sys_error (ERR_SEVERE, "Error setting --min to %s", optarg); + if2img_usage(argv[0]); + return (1); + } + break; + case O_MAX: + opt_set_max = 1; + densmax = strtod(optarg, &endptr); + endstr = optarg + strlen(optarg); + if (endptr != endstr) + { + sys_error (ERR_SEVERE, "Error setting --max to %s", optarg); + if2img_usage(argv[0]); + return (1); + } + break; + case O_SCALE: + opt_scale = strtol(optarg, &endptr, 10); + endstr = optarg + strlen(optarg); + if (endptr != endstr) + { + sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg); + if2img_usage(argv[0]); + return (1); + } + break; + case O_AUTO: + if (strcmp(optarg, O_AUTO_FULL_STR) == 0) + opt_auto = O_AUTO_FULL; + else if (strcmp(optarg, O_AUTO_STD1_STR) == 0) + opt_auto = O_AUTO_STD1; + else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0) + opt_auto = O_AUTO_STD0_5; + else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0) + opt_auto = O_AUTO_STD0_1; + else if (strcmp(optarg, O_AUTO_STD2_STR) == 0) + opt_auto = O_AUTO_STD2; + else if (strcmp(optarg, O_AUTO_STD3_STR) == 0) + opt_auto = O_AUTO_STD3; + else + { + sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg); + if2img_usage(argv[0]); + return (1); + } + break; + case O_CENTER: + if (strcmp(optarg, O_CENTER_MEAN_STR) == 0) + opt_center = O_CENTER_MEAN; + else if (strcmp(optarg, O_CENTER_MODE_STR) == 0) + opt_center = O_CENTER_MODE; + else + { + sys_error (ERR_SEVERE, "Invalid center mode %s", optarg); + if2img_usage(argv[0]); + return (1); + } + break; + case O_FORMAT: + if (strcmp(optarg, O_FORMAT_PGM_STR) == 0) + opt_format = O_FORMAT_PGM; + else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0) + opt_format = O_FORMAT_PGMASC; +#if HAVE_PNG + else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0) + opt_format = O_FORMAT_PNG; + else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0) + opt_format = O_FORMAT_PNG16; +#endif +#if HAVE_GIF + else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0) + opt_format = O_FORMAT_GIF; +#endif + else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0) + opt_format = O_FORMAT_DISP; + else { + sys_error (ERR_SEVERE, "Invalid format mode %s", optarg); + if2img_usage(argv[0]); + return (1); + } + break; + case O_LABELS: + opt_labels = 1; + break; + case O_VERBOSE: + opt_verbose = 1; + break; + case O_DEBUG: + opt_debug = 1; + break; + case O_STATS: + opt_stats = 1; + break; + case O_VERSION: +#ifdef VERSION + cout << "Version " << VERSION; +#else + cout << "Unknown version number" << endl; +#endif + return (0); + case O_HELP: + case '?': + if2img_usage(argv[0]); + return (0); + default: + if2img_usage(argv[0]); + return (1); + } + } + + if ((opt_format == O_FORMAT_DISP && optind + 1 != argc) + || (opt_format != O_FORMAT_DISP && optind + 2 != argc)) + { + if2img_usage(argv[0]); + return (1); + } + + in_file = argv[optind]; + + if (opt_format != O_FORMAT_DISP) + out_file = argv[optind+1]; + else out_file = NULL; + + pim = new ImageFile (); + ImageFile& im = *pim; + if (! im.fileRead(in_file)) { + sys_error (ERR_FATAL, "File %s does not exist", in_file); + return (1); + } + + if (opt_labels) + im.printLabels(cout); + + if (opt_stats || (! (opt_set_max && opt_set_min))) { + double min, max, mean, mode, median, stddev; + double window = 0; + im.statistics(min, max, mean, mode, median, stddev); + + if (opt_auto == O_AUTO_FULL) { + if (! opt_set_max) + densmax = max; + if (! opt_set_min) + densmin = min; + } + if (opt_stats || opt_auto != O_AUTO_FULL) { + + if (opt_auto == O_AUTO_FULL) + ; + else if (opt_auto == O_AUTO_STD1) + window = stddev; + else if (opt_auto == O_AUTO_STD0_1) + window = stddev * 0.1; + else if (opt_auto == O_AUTO_STD0_5) + window = stddev * 0.5; + else if (opt_auto == O_AUTO_STD2) + window = stddev * 2; + else if (opt_auto == O_AUTO_STD3) + window = stddev * 3; + else { + sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto); + return (1); + } + } + if (opt_stats) { + cout <<"nx: " << im.nx() << endl; + cout <<"ny: " << im.ny() << endl; + cout <<"min: " << min << endl; + cout <<"max: " << max << endl; + cout <<"mean: " << mean << endl; + cout <<"mode: " << mode << endl; + cout <<"stddev: " << stddev << endl; + } + if (opt_auto != O_AUTO_FULL) { + double center; + + if (opt_center == O_CENTER_MODE) + center = mode; + else if (opt_center == O_CENTER_MEAN) + center = mean; + else { + sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center); + return (1); + } + if (! opt_set_max) + densmax = center + window; + if (! opt_set_min) + densmin = center - window; + } + } + + if (opt_stats) { + cout << "min display: " << densmin << endl; + cout << "max display: " << densmax << endl; + } + + if (opt_format == O_FORMAT_PGM) + im.writeImagePGM (out_file, opt_scale, opt_scale, densmin, densmax); + else if (opt_format == O_FORMAT_PGMASC) + im.writeImagePGMASCII (out_file, opt_scale, opt_scale, densmin, densmax); +#if HAVE_PNG + else if (opt_format == O_FORMAT_PNG) + im.writeImagePNG (out_file, 8, opt_scale, opt_scale, densmin, densmax); + else if (opt_format == O_FORMAT_PNG16) + im.writeImagePNG (out_file, 16, opt_scale, opt_scale, densmin, densmax); +#endif +#if HAVE_GIF + else if (opt_format == O_FORMAT_GIF) + im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax); +#endif + else if (opt_format == O_FORMAT_DISP) { +#if HAVE_SGP + im.displayScaling (opt_scale, densmin, densmax); + cout << "Press enter to continue\n"; + cio_kb_getc(); + sgp2_close(sgp2_get_active_win()); +#endif + } + else + { + sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format); + return (1); + } + return (0); +} + + +#ifndef NO_MAIN +int +main (int argc, char *const argv[]) +{ + int retval = 1; + + try { + retval = if2img_main(argc, argv); + } catch (exception e) { + cerr << "Exception: " << e.what() << endl; + } catch (...) { + cerr << "Unknown exception" << endl; + } + + return (retval); +} +#endif