From cb68417f9166da28aa56f8f423bcab56c1c26f1f Mon Sep 17 00:00:00 2001 From: "Kevin M. Rosenberg" Date: Sat, 3 Jun 2000 07:57:51 +0000 Subject: [PATCH] r66: Fixed PNG 16-bit format --- src/sdf2img.c | 1447 +++++++++++++++++++++++++------------------------ 1 file changed, 732 insertions(+), 715 deletions(-) diff --git a/src/sdf2img.c b/src/sdf2img.c index f79b2cd..6c44f0a 100644 --- a/src/sdf2img.c +++ b/src/sdf2img.c @@ -1,716 +1,733 @@ -/***************************************************************************** -** This is part of the CTSim program -** Copyright (C) 1983-2000 Kevin Rosenberg -** -** $Id: sdf2img.c,v 1.5 2000/06/03 06:29:57 kevin Exp $ +/***************************************************************************** +** This is part of the CTSim program +** Copyright (C) 1983-2000 Kevin Rosenberg +** +** $Id: sdf2img.c,v 1.6 2000/06/03 07:57:51 kevin Exp $ ** $Log: sdf2img.c,v $ -** Revision 1.5 2000/06/03 06:29:57 kevin -** *** empty log message *** -** -** Revision 1.4 2000/05/24 22:50:04 kevin -** Added support for new SGP library -** -** Revision 1.3 2000/05/16 04:33:59 kevin -** Improved option processing -** -** Revision 1.2 2000/05/08 20:02:32 kevin -** ANSI C changes -** -** Revision 1.1.1.1 2000/04/28 13:02:44 kevin -** Initial CVS import for first public release -** -** -** -** 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 -******************************************************************************/ - -/* FILE - * sdf2img.c Convert an SDF file to a viewable format image - */ - -#include "ct.h" - -#if HAVE_PNG -void sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax); -#endif -#if HAVE_GIF -void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax); -#endif -void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax); -void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax); - -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 }; -#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" - -enum { O_CENTER_MEAN, O_CENTER_MODE }; -#define O_CENTER_MEAN_STR "mean" -#define O_CENTER_MODE_STR "mode" - -enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP }; -#define O_FORMAT_GIF_STR "gif" -#define O_FORMAT_PNG_STR "png" -#define O_FORMAT_PGM_STR "pgm" -#define O_FORMAT_PGMASC_STR "pgmasc" -#define O_FORMAT_DISP_STR "disp" - -void -sdf2img_usage (const char *program) -{ - fprintf(stdout, "sdf2img_usage: %s sdfname outfile [OPTIONS]\n", kbasename(program)); - fprintf(stdout, "Convert SDF2D 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"); -#ifdef HAVE_PNG - fprintf(stdout, " png PNG format\n"); -#endif -#ifdef HAVE_GIF - fprintf(stdout, " gif GIF format\n"); -#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"); -} - - -int -sdf2img_main (int argc, char *const argv[]) -{ - IMAGE *im; - 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) - { - fprintf(stderr, "Error setting --min to %s\n", optarg); - sdf2img_usage(argv[0]); - return (1); - } - break; - case O_MAX: - opt_set_max = 1; - densmax = strtod(optarg, &endptr); - endstr = optarg + strlen(optarg); - if (endptr != endstr) - { - fprintf(stderr, "Error setting --max to %s\n", optarg); - sdf2img_usage(argv[0]); - return (1); - } - break; - case O_SCALE: - opt_scale = strtol(optarg, &endptr, 10); - endstr = optarg + strlen(optarg); - if (endptr != endstr) - { - fprintf(stderr,"Error setting --scale to %s\n", optarg); - sdf2img_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 - { - fprintf(stderr, "Invalid auto mode %s\n", optarg); - sdf2img_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 - { - fprintf(stderr, "Invalid center mode %s\n", optarg); - sdf2img_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; -#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 { - fprintf(stderr, "Invalid format mode %s\n", optarg); - sdf2img_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 - fprintf(stdout, "Version %s\n", VERSION); -#else - fprintf(stderr, "Unknown version number"); -#endif - exit(0); - case O_HELP: - case '?': - sdf2img_usage(argv[0]); - return (0); - default: - sdf2img_usage(argv[0]); - return (1); - } - } - - if ((opt_format == O_FORMAT_DISP && optind + 1 != argc) - || (opt_format != O_FORMAT_DISP && optind + 2 != argc)) - { - sdf2img_usage(argv[0]); - return (1); - } - - in_file = argv[optind]; - - if (opt_format != O_FORMAT_DISP) - out_file = argv[optind+1]; - else out_file = NULL; - - if ((im = image_load (in_file)) == NULL) - { - fprintf(stderr, "File %s does not exist\n", in_file); - return (1); - } - - if (opt_labels) - { - int nlabels = im->dfp_2d->dfp->num_label; - int i; - - for (i = 0; i < nlabels; i++) - { - SDF_BLK blk; - if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK) - break; - if (blk.lab.label_type == LT_HISTORY) { - fprintf(stdout, "History: %s\n", blk.lab.label_str); - fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time); - fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate)); - } else if (blk.lab.label_type == LT_TITLE) { - fprintf(stdout, "Title: %s\n", blk.lab.label_str); - fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate)); - } else if (blk.lab.label_type == LT_NOTE) { - fprintf(stdout, "Note: %s\n", blk.lab.label_str); - fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate)); - } - } - } - - if (opt_stats || (! (opt_set_max && opt_set_min))) - { - double minfound = HUGE_VAL, maxfound = -HUGE_VAL; - double mode = 0, mean = 0, stddev = 0, window = 0; - double spread; - int hist[256]; - int ibin, nbin = 256; - int max_bin, max_binindex; - double maxbin; - int ix, iy; - - maxbin = nbin - 1; - for (ix = 0; ix < im->nx; ix++) - { - for (iy = 0; iy < im->ny; iy++) - { - if (im->v[ix][iy] > maxfound) - maxfound = im->v[ix][iy]; - if (im->v[ix][iy] < minfound) - minfound = im->v[ix][iy]; - mean += im->v[ix][iy]; - } - } - spread = maxfound - minfound; - if (spread == 0) - mode = minfound; - else - { - for (ibin = 0; ibin < nbin; ibin++) - hist[ibin] = 0; - for (ix = 0; ix < im->nx; ix++) - { - for (iy = 0; iy < im->ny; iy++) - { - int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5); - hist[b]++; - } - } - max_binindex = 0; - max_bin = -1; - for (ibin = 0; ibin < nbin; ibin++) - { - if (hist[ibin] > max_bin) - { - max_bin = hist[ibin]; - max_binindex = ibin; - } - } - mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound; - } - - if (opt_auto == O_AUTO_FULL) - { - if (! opt_set_max) - densmax = maxfound; - if (! opt_set_min) - densmin = minfound; - } - if (opt_stats || opt_auto != O_AUTO_FULL) - { - mean /= im->nx * im->ny; - for (ix = 0; ix < im->nx; ix++) - { - for (iy = 0; iy < im->ny; iy++) - { - double diff = (im->v[ix][iy] - mean); - stddev += diff * diff; - } - } - stddev = sqrt(stddev / (im->nx * im->ny)); - 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 - { - fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto); - return (1); - } - } - if (opt_stats) - { - fprintf(stdout,"nx=%d\n", im->nx); - fprintf(stdout,"ny=%d\n", im->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); - } - 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 - { - fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center); - return (1); - } - if (! opt_set_max) - densmax = center + window; - if (! opt_set_min) - densmin = center - window; - } - } - - if (opt_stats) - { - fprintf(stdout,"min_disp=%f\n", densmin); - fprintf(stdout,"max_disp=%f\n", densmax); - } - - if (opt_format == O_FORMAT_PGM) - sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax); - else if (opt_format == O_FORMAT_PGMASC) - sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax); -#if HAVE_PNG - else if (opt_format == O_FORMAT_PNG) - sdf2d_to_png (im, out_file, opt_scale, opt_scale, densmin, densmax); -#endif -#if HAVE_GIF - else if (opt_format == O_FORMAT_GIF) - sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax); -#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()); -#endif - } - else - { - fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format); - return (1); - } - return (0); -} - - -void -sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax) -{ - FILE *fp; - int irow; - unsigned char *rowp; - - rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img"); - if (rowp == NULL) - return; - - if ((fp = fopen (outfile, "wb")) == NULL) - return; - - fprintf(fp, "P5\n"); - fprintf(fp, "%d %d\n", im->nx, im->ny); - fprintf(fp, "255\n"); - - for (irow = im->ny - 1; irow >= 0; irow--) - { - int icol, ir; - - for (icol = 0; icol < im->nx; icol++) - { - double dens; - int p; - int pos = icol * nxcell; - dens = (im->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 < im->nx * nxcell; ic++) - fprintf(fp, "%c ", rowp[ic]); - } - } - sys_free(rowp, "sdf2img"); - - fclose(fp); -} - -void -sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax) -{ - FILE *fp; - int irow; - unsigned char *rowp; - - rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img"); - if (rowp == NULL) - return; - - if ((fp = fopen (outfile, "wb")) == NULL) - return; - - fprintf(fp, "P2\n"); - fprintf(fp, "%d %d\n", im->nx, im->ny); - fprintf(fp, "255\n"); - - for (irow = im->ny - 1; irow >= 0; irow--) - { - int icol, ir; - - for (icol = 0; icol < im->nx; icol++) - { - double dens; - int p; - int pos = icol * nxcell; - dens = (im->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 < im->nx * nxcell; ic++) - fprintf(fp, "%d ", rowp[ic]); - fprintf(fp, "\n"); - } - } - sys_free(rowp, "sdf2img"); - - fclose(fp); -} - - -#ifdef HAVE_PNG -void -sdf2d_to_png (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax) -{ - FILE *fp; - png_structp png_ptr; - png_infop info_ptr; - int irow; - unsigned char *rowp; - - rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img"); - if (rowp == NULL) - return; - - if ((fp = fopen (outfile, "wb")) == NULL) - return; - - png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (! png_ptr) - 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 (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, im->nx * nxcell, im->ny * nycell, 8, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_DEFAULT); - - png_write_info(png_ptr, info_ptr); - for (irow = im->ny - 1; irow >= 0; irow--) - { - png_bytep row_pointer; - int icol, ir; - - for (icol = 0; icol < im->nx; icol++) - { - double dens; - int p; - int pos = icol * nxcell; - dens = (im->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.); - } - } - row_pointer = rowp; - for (ir = 0; ir < nycell; ir++) - { - png_write_rows (png_ptr, &row_pointer, 1); - } - } - sys_free(rowp, "sdf2img"); - - png_write_end(png_ptr, info_ptr); - png_destroy_write_struct(&png_ptr, &info_ptr); - - fclose(fp); -} -#endif - -#ifdef HAVE_GD -#include "gd.h" -#define N_GRAYSCALE 256 -#endif - -void -sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax) -{ -#ifdef HAVE_GD - gdImagePtr gif; - FILE *out; - int gs_indices[N_GRAYSCALE]; - int i; - int irow; - int lastrow; - unsigned char *rowp; - - rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img"); - if (rowp == NULL) - return; - - gif = gdImageCreate(im->nx * nxcell, im->ny * nycell); - for (i = 0; i < N_GRAYSCALE; i++) - gs_indices[i] = gdImageColorAllocate(gif, i, i, i); - - lastrow = im->ny * nycell - 1; - for (irow = 0; irow < im->ny; irow++) - { - int icol, ir; - int rpos = irow * nycell; - for (ir = rpos; ir < rpos + nycell; ir++) - { - for (icol = 0; icol < im->nx; icol++) - { - double dens; - int ic; - int cpos = icol * nxcell; - dens = (im->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]]); - } - } - } - } - sys_free(rowp, "sdf2img"); - - if ((out = fopen(outfile,"w")) == NULL) - { - fprintf(stderr,"Error opening output file %s for writing\n", outfile); - exit(1); - } - gdImageGif(gif,out); - fclose(out); - gdImageDestroy(gif); -#else - fprintf(stderr, "This version does not support GIF"); -#endif -} - -#ifndef NO_MAIN -int -main (int argc, char *const argv[]) -{ - return (sdf2img_main(argc, argv)); -} -#endif +** Revision 1.6 2000/06/03 07:57:51 kevin +** Fixed PNG 16-bit format +** +** Revision 1.5 2000/06/03 06:29:57 kevin +** *** empty log message *** +** +** Revision 1.4 2000/05/24 22:50:04 kevin +** Added support for new SGP library +** +** Revision 1.3 2000/05/16 04:33:59 kevin +** Improved option processing +** +** Revision 1.2 2000/05/08 20:02:32 kevin +** ANSI C changes +** +** Revision 1.1.1.1 2000/04/28 13:02:44 kevin +** Initial CVS import for first public release +** +** +** +** 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 +******************************************************************************/ + +/* FILE + * sdf2img.c Convert an SDF file to a viewable format image + */ + +#include "ct.h" + +#if HAVE_PNG +void sdf2d_to_png (IMAGE *im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax); +#endif +#if HAVE_GIF +void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax); +#endif +void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax); +void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax); + +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 }; +#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" + +enum { O_CENTER_MEAN, O_CENTER_MODE }; +#define O_CENTER_MEAN_STR "mean" +#define 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" + +void +sdf2img_usage (const char *program) +{ + fprintf(stdout, "sdf2img_usage: %s sdfname outfile [OPTIONS]\n", kbasename(program)); + fprintf(stdout, "Convert SDF2D 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"); +#ifdef HAVE_PNG + fprintf(stdout, " png PNG (8-bit) format\n"); + fprintf(stdout, " png16 PNG (16-bit) format\n"); +#endif +#ifdef HAVE_GIF + fprintf(stdout, " gif GIF format\n"); +#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"); +} + + +int +sdf2img_main (int argc, char *const argv[]) +{ + IMAGE *im; + 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) + { + fprintf(stderr, "Error setting --min to %s\n", optarg); + sdf2img_usage(argv[0]); + return (1); + } + break; + case O_MAX: + opt_set_max = 1; + densmax = strtod(optarg, &endptr); + endstr = optarg + strlen(optarg); + if (endptr != endstr) + { + fprintf(stderr, "Error setting --max to %s\n", optarg); + sdf2img_usage(argv[0]); + return (1); + } + break; + case O_SCALE: + opt_scale = strtol(optarg, &endptr, 10); + endstr = optarg + strlen(optarg); + if (endptr != endstr) + { + fprintf(stderr,"Error setting --scale to %s\n", optarg); + sdf2img_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 + { + fprintf(stderr, "Invalid auto mode %s\n", optarg); + sdf2img_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 + { + fprintf(stderr, "Invalid center mode %s\n", optarg); + sdf2img_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 { + fprintf(stderr, "Invalid format mode %s\n", optarg); + sdf2img_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 + fprintf(stdout, "Version %s\n", VERSION); +#else + fprintf(stderr, "Unknown version number"); +#endif + exit(0); + case O_HELP: + case '?': + sdf2img_usage(argv[0]); + return (0); + default: + sdf2img_usage(argv[0]); + return (1); + } + } + + if ((opt_format == O_FORMAT_DISP && optind + 1 != argc) + || (opt_format != O_FORMAT_DISP && optind + 2 != argc)) + { + sdf2img_usage(argv[0]); + return (1); + } + + in_file = argv[optind]; + + if (opt_format != O_FORMAT_DISP) + out_file = argv[optind+1]; + else out_file = NULL; + + if ((im = image_load (in_file)) == NULL) + { + fprintf(stderr, "File %s does not exist\n", in_file); + return (1); + } + + if (opt_labels) + { + int nlabels = im->dfp_2d->dfp->num_label; + int i; + + for (i = 0; i < nlabels; i++) + { + SDF_BLK blk; + if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK) + break; + if (blk.lab.label_type == LT_HISTORY) { + fprintf(stdout, "History: %s\n", blk.lab.label_str); + fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time); + fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate)); + } else if (blk.lab.label_type == LT_TITLE) { + fprintf(stdout, "Title: %s\n", blk.lab.label_str); + fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate)); + } else if (blk.lab.label_type == LT_NOTE) { + fprintf(stdout, "Note: %s\n", blk.lab.label_str); + fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate)); + } + } + } + + if (opt_stats || (! (opt_set_max && opt_set_min))) + { + double minfound = HUGE_VAL, maxfound = -HUGE_VAL; + double mode = 0, mean = 0, stddev = 0, window = 0; + double spread; + int hist[256]; + int ibin, nbin = 256; + int max_bin, max_binindex; + double maxbin; + int ix, iy; + + maxbin = nbin - 1; + for (ix = 0; ix < im->nx; ix++) + { + for (iy = 0; iy < im->ny; iy++) + { + if (im->v[ix][iy] > maxfound) + maxfound = im->v[ix][iy]; + if (im->v[ix][iy] < minfound) + minfound = im->v[ix][iy]; + mean += im->v[ix][iy]; + } + } + spread = maxfound - minfound; + if (spread == 0) + mode = minfound; + else + { + for (ibin = 0; ibin < nbin; ibin++) + hist[ibin] = 0; + for (ix = 0; ix < im->nx; ix++) + { + for (iy = 0; iy < im->ny; iy++) + { + int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5); + hist[b]++; + } + } + max_binindex = 0; + max_bin = -1; + for (ibin = 0; ibin < nbin; ibin++) + { + if (hist[ibin] > max_bin) + { + max_bin = hist[ibin]; + max_binindex = ibin; + } + } + mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound; + } + + if (opt_auto == O_AUTO_FULL) + { + if (! opt_set_max) + densmax = maxfound; + if (! opt_set_min) + densmin = minfound; + } + if (opt_stats || opt_auto != O_AUTO_FULL) + { + mean /= im->nx * im->ny; + for (ix = 0; ix < im->nx; ix++) + { + for (iy = 0; iy < im->ny; iy++) + { + double diff = (im->v[ix][iy] - mean); + stddev += diff * diff; + } + } + stddev = sqrt(stddev / (im->nx * im->ny)); + 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 + { + fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto); + return (1); + } + } + if (opt_stats) + { + fprintf(stdout,"nx=%d\n", im->nx); + fprintf(stdout,"ny=%d\n", im->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); + } + 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 + { + fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center); + return (1); + } + if (! opt_set_max) + densmax = center + window; + if (! opt_set_min) + densmin = center - window; + } + } + + if (opt_stats) + { + fprintf(stdout,"min_disp=%f\n", densmin); + fprintf(stdout,"max_disp=%f\n", densmax); + } + + if (opt_format == O_FORMAT_PGM) + sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax); + else if (opt_format == O_FORMAT_PGMASC) + sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax); +#if HAVE_PNG + else if (opt_format == O_FORMAT_PNG) + sdf2d_to_png (im, out_file, 8, opt_scale, opt_scale, densmin, densmax); + else if (opt_format == O_FORMAT_PNG16) + sdf2d_to_png (im, out_file, 16, opt_scale, opt_scale, densmin, densmax); +#endif +#if HAVE_GIF + else if (opt_format == O_FORMAT_GIF) + sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax); +#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()); +#endif + } + else + { + fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format); + return (1); + } + return (0); +} + + +void +sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax) +{ + FILE *fp; + int irow; + unsigned char *rowp; + + rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img"); + if (rowp == NULL) + return; + + if ((fp = fopen (outfile, "wb")) == NULL) + return; + + fprintf(fp, "P5\n"); + fprintf(fp, "%d %d\n", im->nx, im->ny); + fprintf(fp, "255\n"); + + for (irow = im->ny - 1; irow >= 0; irow--) + { + int icol, ir; + + for (icol = 0; icol < im->nx; icol++) + { + double dens; + int p; + int pos = icol * nxcell; + dens = (im->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 < im->nx * nxcell; ic++) + fprintf(fp, "%c ", rowp[ic]); + } + } + sys_free(rowp, "sdf2img"); + + fclose(fp); +} + +void +sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax) +{ + FILE *fp; + int irow; + unsigned char *rowp; + + rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img"); + if (rowp == NULL) + return; + + if ((fp = fopen (outfile, "wb")) == NULL) + return; + + fprintf(fp, "P2\n"); + fprintf(fp, "%d %d\n", im->nx, im->ny); + fprintf(fp, "255\n"); + + for (irow = im->ny - 1; irow >= 0; irow--) + { + int icol, ir; + + for (icol = 0; icol < im->nx; icol++) + { + double dens; + int p; + int pos = icol * nxcell; + dens = (im->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 < im->nx * nxcell; ic++) + fprintf(fp, "%d ", rowp[ic]); + fprintf(fp, "\n"); + } + } + sys_free(rowp, "sdf2img"); + + fclose(fp); +} + + +#ifdef HAVE_PNG +void +sdf2d_to_png (IMAGE *im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax) +{ + FILE *fp; + png_structp png_ptr; + png_infop info_ptr; + int irow; + unsigned char *rowp; + double max_out_level = (1 << bitdepth) - 1; + + rowp = (unsigned char *) sys_alloc(im->nx * nxcell * (bitdepth / 8),"sdf2d_to_img"); + if (rowp == NULL) + return; + + if ((fp = fopen (outfile, "wb")) == NULL) + return; + + png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (! png_ptr) + 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 (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, im->nx * nxcell, im->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 = im->ny - 1; irow >= 0; irow--) + { + png_bytep row_pointer = rowp; + int icol, ir; + + for (icol = 0; icol < im->nx; icol++) + { + double dens; + int p; + unsigned int outval; + int pos = icol * nxcell; + dens = (im->v[icol][irow] - densmin) / (densmax - densmin); + if (dens < 0) + dens = 0; + else if (dens > 1) + dens = 1; + outval = 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); + } + } + sys_free(rowp, "sdf2img"); + + png_write_end(png_ptr, info_ptr); + png_destroy_write_struct(&png_ptr, &info_ptr); + + fclose(fp); +} +#endif + +#ifdef HAVE_GD +#include "gd.h" +#define N_GRAYSCALE 256 +#endif + +void +sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax) +{ +#ifdef HAVE_GD + gdImagePtr gif; + FILE *out; + int gs_indices[N_GRAYSCALE]; + int i; + int irow; + int lastrow; + unsigned char *rowp; + + rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img"); + if (rowp == NULL) + return; + + gif = gdImageCreate(im->nx * nxcell, im->ny * nycell); + for (i = 0; i < N_GRAYSCALE; i++) + gs_indices[i] = gdImageColorAllocate(gif, i, i, i); + + lastrow = im->ny * nycell - 1; + for (irow = 0; irow < im->ny; irow++) + { + int icol, ir; + int rpos = irow * nycell; + for (ir = rpos; ir < rpos + nycell; ir++) + { + for (icol = 0; icol < im->nx; icol++) + { + double dens; + int ic; + int cpos = icol * nxcell; + dens = (im->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]]); + } + } + } + } + sys_free(rowp, "sdf2img"); + + if ((out = fopen(outfile,"w")) == NULL) + { + fprintf(stderr,"Error opening output file %s for writing\n", outfile); + exit(1); + } + gdImageGif(gif,out); + fclose(out); + gdImageDestroy(gif); +#else + fprintf(stderr, "This version does not support GIF"); +#endif +} + +#ifndef NO_MAIN +int +main (int argc, char *const argv[]) +{ + return (sdf2img_main(argc, argv)); +} +#endif -- 2.34.1