X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=src%2Fif2img.cpp;h=8211d6d3ea7284cbe0932d126d841c2769ef1ad2;hb=29ceda2bc7505f0496e1cb5d1e45b489cafa7891;hp=d25c39483f15c669c5fdd865d293a46468281d08;hpb=013445d4e52c07491291289eb32fa325d9f9f8ff;p=ctsim.git diff --git a/src/if2img.cpp b/src/if2img.cpp index d25c394..8211d6d 100644 --- a/src/if2img.cpp +++ b/src/if2img.cpp @@ -9,7 +9,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: if2img.cpp,v 1.2 2000/06/08 16:43:10 kevin Exp $ +** $Id: if2img.cpp,v 1.12 2000/07/09 08:47:41 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 @@ -25,12 +25,9 @@ ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ******************************************************************************/ -/* FILE - * if2img.c Convert an SDF file to a viewable format image - */ - #include "ct.h" + #if HAVE_PNG void sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax); #endif @@ -61,63 +58,63 @@ static struct option my_options[] = }; enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 }; -#define O_AUTO_FULL_STR "full" -#define O_AUTO_STD0_1_STR "std0.1" -#define O_AUTO_STD0_5_STR "std0.5" -#define O_AUTO_STD1_STR "std1" -#define O_AUTO_STD2_STR "std2" -#define O_AUTO_STD3_STR "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 }; -#define O_CENTER_MEAN_STR "mean" -#define O_CENTER_MODE_STR "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 }; -#define O_FORMAT_GIF_STR "gif" -#define O_FORMAT_PNG_STR "png" -#define O_FORMAT_PNG16_STR "png16" -#define O_FORMAT_PGM_STR "pgm" -#define O_FORMAT_PGMASC_STR "pgmasc" -#define O_FORMAT_DISP_STR "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) { - fprintf(stdout, "usage: %s sdfname outfile [OPTIONS]\n", kbasename(program)); - fprintf(stdout, "Convert IF file to an image file\n"); - fprintf(stdout, "\n"); - fprintf(stdout, " sdfname Name of input SDF file\n"); - fprintf(stdout, " outfile Name of output file\n"); - fprintf(stdout, " --format Output format\n"); - fprintf(stdout, " pgm PGM (portable graymap) format (default)\n"); - fprintf(stdout, " pgmasc PGM (portable graymap) ASCII format\n"); + cout << "usage: " << fileBasename(program) << " ifname outfile [OPTIONS]" << endl; + cout << "Convert IF file to an image file" << endl; + cout << endl; + cout << " sdfname Name of input SDF 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 - fprintf(stdout, " png PNG (8-bit) format\n"); - fprintf(stdout, " png16 PNG (16-bit) format\n"); + cout << " png PNG (8-bit) format" << endl; + cout << " png16 PNG (16-bit) format" << endl; #endif #ifdef HAVE_GIF - fprintf(stdout, " gif GIF format\n"); + cout << " gif GIF format" << endl; #endif - fprintf(stdout, " disp Display on screen\n"); - fprintf(stdout, " --center Center of window\n"); - fprintf(stdout, " mode Mode is center of window (default)\n"); - fprintf(stdout, " mean Mean is center of window\n"); - fprintf(stdout, " --auto Set auto window\n"); - fprintf(stdout, " full Use full window (default)\n"); - fprintf(stdout, " std0.1 Use 0.1 standard deviation about center\n"); - fprintf(stdout, " std0.5 Use 0.5 standard deviation about center\n"); - fprintf(stdout, " std1 Use one standard deviation about center\n"); - fprintf(stdout, " std2 Use two standard deviations about center\n"); - fprintf(stdout, " std3 Use three standard deviations about center\n"); - fprintf(stdout, " --scale Scaling factor for output size\n"); - fprintf(stdout, " --min Set minimum intensity\n"); - fprintf(stdout, " --max Set maximum intensity\n"); - fprintf(stdout, " --stats Print image statistics\n"); - fprintf(stdout, " --labels Print image labels\n"); - fprintf(stdout, " --debug Set debug mode\n"); - fprintf(stdout, " --verbose Set verbose mode\n"); - fprintf(stdout, " --version Print version\n"); - fprintf(stdout, " --help Print this help message\n"); + 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; } @@ -156,7 +153,7 @@ if2img_main (int argc, char *const argv[]) endstr = optarg + strlen(optarg); if (endptr != endstr) { - fprintf(stderr, "Error setting --min to %s\n", optarg); + sys_error (ERR_SEVERE, "Error setting --min to %s", optarg); if2img_usage(argv[0]); return (1); } @@ -167,7 +164,7 @@ if2img_main (int argc, char *const argv[]) endstr = optarg + strlen(optarg); if (endptr != endstr) { - fprintf(stderr, "Error setting --max to %s\n", optarg); + sys_error (ERR_SEVERE, "Error setting --max to %s", optarg); if2img_usage(argv[0]); return (1); } @@ -177,7 +174,7 @@ if2img_main (int argc, char *const argv[]) endstr = optarg + strlen(optarg); if (endptr != endstr) { - fprintf(stderr,"Error setting --scale to %s\n", optarg); + sys_error (ERR_SEVERE, "Error setting --scale to %s", optarg); if2img_usage(argv[0]); return (1); } @@ -197,7 +194,7 @@ if2img_main (int argc, char *const argv[]) opt_auto = O_AUTO_STD3; else { - fprintf(stderr, "Invalid auto mode %s\n", optarg); + sys_error (ERR_SEVERE, "Invalid auto mode %s", optarg); if2img_usage(argv[0]); return (1); } @@ -209,11 +206,11 @@ if2img_main (int argc, char *const argv[]) opt_center = O_CENTER_MODE; else { - fprintf(stderr, "Invalid center mode %s\n", optarg); + sys_error (ERR_SEVERE, "Invalid center mode %s", optarg); if2img_usage(argv[0]); return (1); } - break; + break; case O_FORMAT: if (strcmp(optarg, O_FORMAT_PGM_STR) == 0) opt_format = O_FORMAT_PGM; @@ -232,7 +229,7 @@ if2img_main (int argc, char *const argv[]) else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0) opt_format = O_FORMAT_DISP; else { - fprintf(stderr, "Invalid format mode %s\n", optarg); + sys_error (ERR_SEVERE, "Invalid format mode %s", optarg); if2img_usage(argv[0]); return (1); } @@ -251,11 +248,11 @@ if2img_main (int argc, char *const argv[]) break; case O_VERSION: #ifdef VERSION - fprintf(stdout, "Version %s\n", VERSION); + cout << "Version " << VERSION; #else - fprintf(stderr, "Unknown version number"); + cout << "Unknown version number" << endl; #endif - exit(0); + return (0); case O_HELP: case '?': if2img_usage(argv[0]); @@ -279,32 +276,15 @@ if2img_main (int argc, char *const argv[]) out_file = argv[optind+1]; else out_file = NULL; - pim = new ImageFile (in_file); + pim = new ImageFile (); ImageFile& im = *pim; - if (! im.adf.fileRead()) { - fprintf(stderr, "File %s does not exist\n", in_file); + if (! im.fileRead(in_file)) { + sys_error (ERR_FATAL, "File %s does not exist", in_file); return (1); } - if (opt_labels) { - int nlabels = im.adf.getNumLabels(); - - for (int i = 0; i < nlabels; i++) { - Array2dFileLabel label; - im.adf.labelRead (label, i); - string str; - label.getDateString (str); - - if (label.getLabelType() == Array2dFileLabel::L_HISTORY) { - cout << "History: " << label.getLabelString() << endl; - cout << " calc time = " << label.getCalcTime() << " secs" << endl; - cout << " Timestamp = " << str << endl; - } else if (label.getLabelType() == Array2dFileLabel::L_USER) { - cout << "Note: " << label.getLabelString() << endl; - cout << " Timestamp = %s" << str << endl; - } - } - } + if (opt_labels) + im.printLabels(cout); if (opt_stats || (! (opt_set_max && opt_set_min))) { double minfound = HUGE_VAL, maxfound = -HUGE_VAL; @@ -379,18 +359,18 @@ if2img_main (int argc, char *const argv[]) else if (opt_auto == O_AUTO_STD3) window = stddev * 3; else { - fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto); + sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto); return (1); } } if (opt_stats) { - fprintf(stdout,"nx=%d\n", nx); - fprintf(stdout,"ny=%d\n", ny); - fprintf(stdout,"min=%f\n", minfound); - fprintf(stdout,"max=%f\n", maxfound); - fprintf(stdout,"mean=%f\n", mean); - fprintf(stdout,"mode=%f\n", mode); - fprintf(stdout,"stddev=%f\n", stddev); + cout <<"nx: " << nx << endl; + cout <<"ny: " << ny << endl; + cout <<"min: " << minfound << endl; + cout <<"max: " << maxfound << endl; + cout <<"mean: " << mean << endl; + cout <<"mode: " << mode << endl; + cout <<"stddev: " << stddev << endl; } if (opt_auto != O_AUTO_FULL) { double center; @@ -400,7 +380,7 @@ if2img_main (int argc, char *const argv[]) else if (opt_center == O_CENTER_MEAN) center = mean; else { - fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center); + sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center); return (1); } if (! opt_set_max) @@ -411,8 +391,8 @@ if2img_main (int argc, char *const argv[]) } if (opt_stats) { - fprintf(stdout,"min_disp=%f\n", densmin); - fprintf(stdout,"max_disp=%f\n", densmax); + cout << "min display: " << densmin << endl; + cout << "max display: " << densmax << endl; } if (opt_format == O_FORMAT_PGM) @@ -431,14 +411,14 @@ if2img_main (int argc, char *const argv[]) #endif else if (opt_format == O_FORMAT_DISP) { #if HAVE_SGP - // image_display_scale (im, opt_scale, densmin, densmax); - // cio_kb_getc(); - sgp2_close(sgp2_get_active_win()); + im.displayScaling (opt_scale, densmin, densmax); + cio_kb_getc(); + // sgp2_close(sgp2_get_active_win()); #endif } else { - fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format); + sys_error (ERR_SEVERE, "Internal Error: Invalid format mode %d", opt_format); return (1); } return (0); @@ -449,15 +429,11 @@ void sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax) { FILE *fp; - int irow; - unsigned char *rowp; int nx = im.nx(); int ny = im.ny(); ImageFileArray v = im.getArray(); - rowp = (unsigned char *) sys_alloc(nx * nxcell,"sdf2d_to_img"); - if (rowp == NULL) - return; + unsigned char rowp [nx * nxcell]; if ((fp = fopen (outfile, "wb")) == NULL) return; @@ -466,33 +442,20 @@ sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densm fprintf(fp, "%d %d\n", nx, ny); fprintf(fp, "255\n"); - for (irow = ny - 1; irow >= 0; irow--) - { - int icol, ir; - - for (icol = 0; icol < nx; icol++) - { - double dens; - int p; - int pos = icol * nxcell; - dens = (v[icol][irow] - densmin) / (densmax - densmin); - if (dens < 0) - dens = 0; - else if (dens > 1) - dens = 1; - for (p = pos; p < pos + nxcell; p++) - { - rowp[p] = (unsigned int) (dens * 255.); - } - } - for (ir = 0; ir < nycell; ir++) - { - int ic; - for (ic = 0; ic < nx * nxcell; ic++) - fprintf(fp, "%c ", rowp[ic]); - } + for (int irow = ny - 1; irow >= 0; irow--) { + for (int icol = 0; icol < nx; icol++) { + int pos = icol * nxcell; + double dens = (v[icol][irow] - densmin) / (densmax - densmin); + dens = clamp (dens, 0., 1.); + for (int p = pos; p < pos + nxcell; p++) { + rowp[p] = static_cast (dens * 255.); + } + } + for (int ir = 0; ir < nycell; ir++) { + for (int ic = 0; ic < nx * nxcell; ic++) + fprintf(fp, "%c ", rowp[ic]); } - sys_free(rowp, "if2img"); + } fclose(fp); } @@ -501,13 +464,11 @@ void sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax) { FILE *fp; - int irow; - unsigned char *rowp; int nx = im.nx(); int ny = im.ny(); ImageFileArray v = im.getArray(); - rowp = (unsigned char *) sys_alloc(nx * nxcell,"sdf2d_to_img"); + unsigned char rowp [nx * nxcell]; if (rowp == NULL) return; @@ -518,34 +479,21 @@ sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double de fprintf(fp, "%d %d\n", nx, ny); fprintf(fp, "255\n"); - for (irow = ny - 1; irow >= 0; irow--) - { - int icol, ir; - - for (icol = 0; icol < nx; icol++) - { - double dens; - int p; - int pos = icol * nxcell; - dens = (v[icol][irow] - densmin) / (densmax - densmin); - if (dens < 0) - dens = 0; - else if (dens > 1) - dens = 1; - for (p = pos; p < pos + nxcell; p++) - { - rowp[p] = (unsigned int) (dens * 255.); - } - } - for (ir = 0; ir < nycell; ir++) - { - int ic; - for (ic = 0; ic < nx * nxcell; ic++) - fprintf(fp, "%d ", rowp[ic]); - fprintf(fp, "\n"); - } + for (int irow = ny - 1; irow >= 0; irow--) { + for (int icol = 0; icol < nx; icol++) { + int pos = icol * nxcell; + double dens = (v[icol][irow] - densmin) / (densmax - densmin); + dens = clamp (dens, 0., 1.); + for (int p = pos; p < pos + nxcell; p++) { + rowp[p] = static_cast (dens * 255.); + } } - sys_free(rowp, "if2img"); + for (int ir = 0; ir < nycell; ir++) { + for (int ic = 0; ic < nx * nxcell; ic++) + fprintf(fp, "%d ", rowp[ic]); + fprintf(fp, "\n"); + } + } fclose(fp); } @@ -558,16 +506,12 @@ sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell FILE *fp; png_structp png_ptr; png_infop info_ptr; - int irow; - unsigned char *rowp; double max_out_level = (1 << bitdepth) - 1; int nx = im.nx(); int ny = im.ny(); ImageFileArray v = im.getArray(); - rowp = (unsigned char *) sys_alloc(nx * nxcell * (bitdepth / 8),"sdf2d_to_img"); - if (rowp == NULL) - return; + unsigned char rowp [nx * nxcell * (bitdepth / 8)]; if ((fp = fopen (outfile, "wb")) == NULL) return; @@ -577,59 +521,45 @@ sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell return; info_ptr = png_create_info_struct(png_ptr); - if (! info_ptr) - { - png_destroy_write_struct(&png_ptr, (png_infopp) NULL); - fclose(fp); - return; - } + if (! info_ptr) { + png_destroy_write_struct(&png_ptr, (png_infopp) NULL); + fclose(fp); + return; + } - if (setjmp(png_ptr->jmpbuf)) - { - png_destroy_write_struct(&png_ptr, &info_ptr); - fclose(fp); - return; - } + if (setjmp(png_ptr->jmpbuf)) { + png_destroy_write_struct(&png_ptr, &info_ptr); + fclose(fp); + return; + } png_init_io(png_ptr, fp); 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); png_write_info(png_ptr, info_ptr); - for (irow = ny - 1; irow >= 0; irow--) - { - png_bytep row_pointer = rowp; - int icol, ir; - - for (icol = 0; icol < nx; icol++) - { - double dens; - int p; - unsigned int outval; - int pos = icol * nxcell; - dens = (v[icol][irow] - densmin) / (densmax - densmin); - if (dens < 0) - dens = 0; - else if (dens > 1) - dens = 1; - outval = (unsigned int) (dens * max_out_level); - for (p = pos; p < pos + nxcell; p++) - { - if (bitdepth == 8) - rowp[p] = outval; - else { - int rowpos = p * 2; - rowp[rowpos] = (outval >> 8) & 0xFF; - rowp[rowpos+1] = (outval & 0xFF); - } - } - } - for (ir = 0; ir < nycell; ir++) - { - png_write_rows (png_ptr, &row_pointer, 1); + for (int irow = ny - 1; irow >= 0; irow--) { + png_bytep row_pointer = rowp; + + for (int icol = 0; icol < nx; icol++) { + int pos = icol * nxcell; + double dens = (v[icol][irow] - densmin) / (densmax - densmin); + dens = clamp (dens, 0., 1.); + unsigned int outval = static_cast (dens * max_out_level); + + for (int p = pos; p < pos + nxcell; p++) { + if (bitdepth == 8) + rowp[p] = outval; + else { + int rowpos = p * 2; + rowp[rowpos] = (outval >> 8) & 0xFF; + rowp[rowpos+1] = (outval & 0xFF); } + } } - sys_free(rowp, "if2img"); + for (int ir = 0; ir < nycell; ir++) + png_write_rows (png_ptr, &row_pointer, 1); + } png_write_end(png_ptr, info_ptr); png_destroy_write_struct(&png_ptr, &info_ptr); @@ -640,7 +570,7 @@ sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell #ifdef HAVE_GD #include "gd.h" -#define N_GRAYSCALE 256 +static const int N_GRAYSCALE=256; #endif void @@ -650,59 +580,43 @@ sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densm gdImagePtr gif; FILE *out; int gs_indices[N_GRAYSCALE]; - int i; - int irow; - int lastrow; - unsigned char *rowp; int nx = im.nx(); int ny = im.ny(); ImageFileArray v = im.getArray(); - rowp = (unsigned char *) sys_alloc(nx * nxcell,"sdf2d_to_img"); + unsigned char rowp [nx * nxcell]; if (rowp == NULL) return; gif = gdImageCreate(nx * nxcell, ny * nycell); - for (i = 0; i < N_GRAYSCALE; i++) + for (int i = 0; i < N_GRAYSCALE; i++) gs_indices[i] = gdImageColorAllocate(gif, i, i, i); - lastrow = ny * nycell - 1; - for (irow = 0; irow < ny; irow++) - { - int icol, ir; - int rpos = irow * nycell; - for (ir = rpos; ir < rpos + nycell; ir++) - { - for (icol = 0; icol < nx; icol++) - { - double dens; - int ic; - int cpos = icol * nxcell; - dens = (v[icol][irow] - densmin) / (densmax - densmin); - if (dens < 0) - dens = 0; - else if (dens > 1) - dens = 1; - for (ic = cpos; ic < cpos + nxcell; ic++) - { - rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1)); - gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]); - } - } + int lastrow = ny * nycell - 1; + for (int irow = 0; irow < ny; irow++) { + int rpos = irow * nycell; + for (int ir = rpos; ir < rpos + nycell; ir++) { + for (int icol = 0; icol < nx; icol++) { + int cpos = icol * nxcell; + double dens = (v[icol][irow] - densmin) / (densmax - densmin); + dens = clamp(dens, 0., 1.); + for (int ic = cpos; ic < cpos + nxcell; ic++) { + rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1)); + gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]); } + } } - sys_free(rowp, "if2img"); + } - if ((out = fopen(outfile,"w")) == NULL) - { - fprintf(stderr,"Error opening output file %s for writing\n", outfile); - exit(1); - } + if ((out = fopen(outfile,"w")) == NULL) { + sys_error(ERR_FATAL, "Error opening output file %s for writing", outfile); + return (1); + } gdImageGif(gif,out); fclose(out); gdImageDestroy(gif); #else - fprintf(stderr, "This version does not support GIF"); + cout << "This version does not support GIF" << endl; #endif } @@ -710,6 +624,16 @@ sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densm int main (int argc, char *const argv[]) { - return (if2img_main(argc, 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