-/*****************************************************************************
-** This is part of the CTSim program
-** Copyright (C) 1983-2000 Kevin Rosenberg
-**
-** $Id: sdf2img.c,v 1.3 2000/05/16 04:33:59 kevin Exp $
+/*****************************************************************************\r
+** This is part of the CTSim program\r
+** Copyright (C) 1983-2000 Kevin Rosenberg\r
+**\r
+** $Id: sdf2img.c,v 1.6 2000/06/03 07:57:51 kevin Exp $\r
** $Log: sdf2img.c,v $
-** 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}
-};
-
-#define O_AUTO_FULL 1
-#define O_AUTO_STD0_1 2
-#define O_AUTO_STD0_5 3
-#define O_AUTO_STD1 4
-#define O_AUTO_STD2 5
-#define O_AUTO_STD3 6
-
-#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"
-
-#define O_CENTER_MEAN 1
-#define O_CENTER_MODE 2
-#define O_CENTER_MEAN_STR "mean"
-#define O_CENTER_MODE_STR "mode"
-
-#define O_FORMAT_GIF 1
-#define O_FORMAT_PNG 2
-#define O_FORMAT_PGM 3
-#define O_FORMAT_PGMASC 4
-#define O_FORMAT_GIF_STR "gif"
-#define O_FORMAT_PNG_STR "png"
-#define O_FORMAT_PGM_STR "pgm"
-#define O_FORMAT_PGMASC_STR "pgmasc"
-
-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, " --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 {
- 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 (optind + 2 != argc)
- {
- sdf2img_usage(argv[0]);
- return (1);
- }
-
- in_file = argv[optind];
- out_file = argv[optind+1];
-
- 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
- {
- 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
+**\r
+** Revision 1.5 2000/06/03 06:29:57 kevin\r
+** *** empty log message ***\r
+**\r
+** Revision 1.4 2000/05/24 22:50:04 kevin\r
+** Added support for new SGP library\r
+**\r
+** Revision 1.3 2000/05/16 04:33:59 kevin\r
+** Improved option processing\r
+**\r
+** Revision 1.2 2000/05/08 20:02:32 kevin\r
+** ANSI C changes\r
+**\r
+** Revision 1.1.1.1 2000/04/28 13:02:44 kevin\r
+** Initial CVS import for first public release\r
+**\r
+**\r
+**\r
+** This program is free software; you can redistribute it and/or modify\r
+** it under the terms of the GNU General Public License (version 2) as\r
+** published by the Free Software Foundation.\r
+**\r
+** This program is distributed in the hope that it will be useful,\r
+** but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+** GNU General Public License for more details.\r
+**\r
+** You should have received a copy of the GNU General Public License\r
+** along with this program; if not, write to the Free Software\r
+** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA\r
+******************************************************************************/\r
+\r
+/* FILE\r
+ * sdf2img.c Convert an SDF file to a viewable format image\r
+ */\r
+\r
+#include "ct.h"\r
+\r
+#if HAVE_PNG\r
+void sdf2d_to_png (IMAGE *im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax);\r
+#endif\r
+#if HAVE_GIF\r
+void sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);\r
+#endif\r
+void sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);\r
+void sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax);\r
+\r
+enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS, \r
+ O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };\r
+\r
+static struct option my_options[] =\r
+{\r
+ {"scale", 1, 0, O_SCALE},\r
+ {"min", 1, 0, O_MIN},\r
+ {"max", 1, 0, O_MAX},\r
+ {"auto", 1, 0, O_AUTO},\r
+ {"center", 1, 0, O_CENTER},\r
+ {"format", 1, 0, O_FORMAT},\r
+ {"labels", 0, 0, O_LABELS},\r
+ {"stats", 0, 0, O_STATS},\r
+ {"verbose", 0, 0, O_VERBOSE},\r
+ {"debug", 0, 0, O_DEBUG},\r
+ {"help", 0, 0, O_HELP},\r
+ {"version", 0, 0, O_VERSION},\r
+ {0, 0, 0, 0}\r
+};\r
+\r
+enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };\r
+#define O_AUTO_FULL_STR "full"\r
+#define O_AUTO_STD0_1_STR "std0.1"\r
+#define O_AUTO_STD0_5_STR "std0.5"\r
+#define O_AUTO_STD1_STR "std1"\r
+#define O_AUTO_STD2_STR "std2"\r
+#define O_AUTO_STD3_STR "std3"\r
+\r
+enum { O_CENTER_MEAN, O_CENTER_MODE };\r
+#define O_CENTER_MEAN_STR "mean"\r
+#define O_CENTER_MODE_STR "mode"\r
+\r
+enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };\r
+#define O_FORMAT_GIF_STR "gif"\r
+#define O_FORMAT_PNG_STR "png" \r
+#define O_FORMAT_PNG16_STR "png16" \r
+#define O_FORMAT_PGM_STR "pgm"\r
+#define O_FORMAT_PGMASC_STR "pgmasc"\r
+#define O_FORMAT_DISP_STR "disp"\r
+\r
+void \r
+sdf2img_usage (const char *program)\r
+{\r
+ fprintf(stdout, "sdf2img_usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));\r
+ fprintf(stdout, "Convert SDF2D file to an image file\n");\r
+ fprintf(stdout, "\n");\r
+ fprintf(stdout, " sdfname Name of input SDF file\n");\r
+ fprintf(stdout, " outfile Name of output file\n");\r
+ fprintf(stdout, " --format Output format\n");\r
+ fprintf(stdout, " pgm PGM (portable graymap) format (default)\n");\r
+ fprintf(stdout, " pgmasc PGM (portable graymap) ASCII format\n");\r
+#ifdef HAVE_PNG\r
+ fprintf(stdout, " png PNG (8-bit) format\n");\r
+ fprintf(stdout, " png16 PNG (16-bit) format\n");\r
+#endif\r
+#ifdef HAVE_GIF\r
+ fprintf(stdout, " gif GIF format\n");\r
+#endif\r
+ fprintf(stdout, " disp Display on screen\n");\r
+ fprintf(stdout, " --center Center of window\n");\r
+ fprintf(stdout, " mode Mode is center of window (default)\n");\r
+ fprintf(stdout, " mean Mean is center of window\n");\r
+ fprintf(stdout, " --auto Set auto window\n");\r
+ fprintf(stdout, " full Use full window (default)\n");\r
+ fprintf(stdout, " std0.1 Use 0.1 standard deviation about center\n");\r
+ fprintf(stdout, " std0.5 Use 0.5 standard deviation about center\n");\r
+ fprintf(stdout, " std1 Use one standard deviation about center\n");\r
+ fprintf(stdout, " std2 Use two standard deviations about center\n");\r
+ fprintf(stdout, " std3 Use three standard deviations about center\n");\r
+ fprintf(stdout, " --scale Scaling factor for output size\n");\r
+ fprintf(stdout, " --min Set minimum intensity\n");\r
+ fprintf(stdout, " --max Set maximum intensity\n");\r
+ fprintf(stdout, " --stats Print image statistics\n");\r
+ fprintf(stdout, " --labels Print image labels\n");\r
+ fprintf(stdout, " --debug Set debug mode\n");\r
+ fprintf(stdout, " --verbose Set verbose mode\n");\r
+ fprintf(stdout, " --version Print version\n");\r
+ fprintf(stdout, " --help Print this help message\n");\r
+}\r
+\r
+\r
+int \r
+sdf2img_main (int argc, char *const argv[])\r
+{\r
+ IMAGE *im;\r
+ double densmin = HUGE_VAL, densmax = -HUGE_VAL;\r
+ char *in_file, *out_file;\r
+ int opt_verbose = 0;\r
+ int opt_scale = 1;\r
+ int opt_auto = O_AUTO_FULL;\r
+ int opt_set_max = 0;\r
+ int opt_set_min = 0;\r
+ int opt_stats = 0;\r
+ int opt_debug = 0;\r
+ int opt_center = O_CENTER_MODE;\r
+ int opt_format = O_FORMAT_PGM;\r
+ int opt_labels = 0;\r
+\r
+ while (1)\r
+ {\r
+ int c = getopt_long (argc, argv, "", my_options, NULL);\r
+ char *endptr = NULL;\r
+ char *endstr;\r
+ \r
+ if (c == -1)\r
+ break;\r
+ \r
+ switch (c)\r
+ {\r
+ case O_MIN:\r
+ opt_set_min = 1;\r
+ densmin = strtod(optarg, &endptr);\r
+ endstr = optarg + strlen(optarg);\r
+ if (endptr != endstr)\r
+ {\r
+ fprintf(stderr, "Error setting --min to %s\n", optarg);\r
+ sdf2img_usage(argv[0]);\r
+ return (1);\r
+ }\r
+ break;\r
+ case O_MAX:\r
+ opt_set_max = 1;\r
+ densmax = strtod(optarg, &endptr);\r
+ endstr = optarg + strlen(optarg);\r
+ if (endptr != endstr)\r
+ {\r
+ fprintf(stderr, "Error setting --max to %s\n", optarg);\r
+ sdf2img_usage(argv[0]);\r
+ return (1);\r
+ }\r
+ break;\r
+ case O_SCALE:\r
+ opt_scale = strtol(optarg, &endptr, 10);\r
+ endstr = optarg + strlen(optarg);\r
+ if (endptr != endstr)\r
+ {\r
+ fprintf(stderr,"Error setting --scale to %s\n", optarg);\r
+ sdf2img_usage(argv[0]);\r
+ return (1);\r
+ }\r
+ break;\r
+ case O_AUTO:\r
+ if (strcmp(optarg, O_AUTO_FULL_STR) == 0)\r
+ opt_auto = O_AUTO_FULL;\r
+ else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)\r
+ opt_auto = O_AUTO_STD1;\r
+ else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)\r
+ opt_auto = O_AUTO_STD0_5;\r
+ else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)\r
+ opt_auto = O_AUTO_STD0_1;\r
+ else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)\r
+ opt_auto = O_AUTO_STD2;\r
+ else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)\r
+ opt_auto = O_AUTO_STD3;\r
+ else\r
+ {\r
+ fprintf(stderr, "Invalid auto mode %s\n", optarg);\r
+ sdf2img_usage(argv[0]);\r
+ return (1);\r
+ }\r
+ break;\r
+ case O_CENTER:\r
+ if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)\r
+ opt_center = O_CENTER_MEAN;\r
+ else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)\r
+ opt_center = O_CENTER_MODE;\r
+ else\r
+ {\r
+ fprintf(stderr, "Invalid center mode %s\n", optarg);\r
+ sdf2img_usage(argv[0]);\r
+ return (1);\r
+ }\r
+ break;\r
+ case O_FORMAT:\r
+ if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)\r
+ opt_format = O_FORMAT_PGM;\r
+ else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)\r
+ opt_format = O_FORMAT_PGMASC;\r
+#if HAVE_PNG\r
+ else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)\r
+ opt_format = O_FORMAT_PNG;\r
+ else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)\r
+ opt_format = O_FORMAT_PNG16;\r
+#endif\r
+#if HAVE_GIF\r
+ else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)\r
+ opt_format = O_FORMAT_GIF;\r
+#endif\r
+ else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)\r
+ opt_format = O_FORMAT_DISP;\r
+ else {\r
+ fprintf(stderr, "Invalid format mode %s\n", optarg);\r
+ sdf2img_usage(argv[0]);\r
+ return (1);\r
+ }\r
+ break;\r
+ case O_LABELS:\r
+ opt_labels = 1;\r
+ break;\r
+ case O_VERBOSE:\r
+ opt_verbose = 1;\r
+ break;\r
+ case O_DEBUG:\r
+ opt_debug = 1;\r
+ break;\r
+ case O_STATS:\r
+ opt_stats = 1;\r
+ break;\r
+ case O_VERSION:\r
+#ifdef VERSION\r
+ fprintf(stdout, "Version %s\n", VERSION);\r
+#else\r
+ fprintf(stderr, "Unknown version number");\r
+#endif\r
+ exit(0);\r
+ case O_HELP:\r
+ case '?':\r
+ sdf2img_usage(argv[0]);\r
+ return (0);\r
+ default:\r
+ sdf2img_usage(argv[0]);\r
+ return (1);\r
+ }\r
+ }\r
+\r
+ if ((opt_format == O_FORMAT_DISP && optind + 1 != argc) \r
+ || (opt_format != O_FORMAT_DISP && optind + 2 != argc))\r
+ {\r
+ sdf2img_usage(argv[0]);\r
+ return (1);\r
+ }\r
+ \r
+ in_file = argv[optind];\r
+\r
+ if (opt_format != O_FORMAT_DISP)\r
+ out_file = argv[optind+1];\r
+ else out_file = NULL;\r
+\r
+ if ((im = image_load (in_file)) == NULL)\r
+ {\r
+ fprintf(stderr, "File %s does not exist\n", in_file);\r
+ return (1);\r
+ }\r
+\r
+ if (opt_labels) \r
+ {\r
+ int nlabels = im->dfp_2d->dfp->num_label;\r
+ int i;\r
+\r
+ for (i = 0; i < nlabels; i++)\r
+ {\r
+ SDF_BLK blk;\r
+ if (sdf_read_label(&blk.lab, i, im->dfp_2d->dfp) != SDF_OK)\r
+ break;\r
+ if (blk.lab.label_type == LT_HISTORY) {\r
+ fprintf(stdout, "History: %s\n", blk.lab.label_str);\r
+ fprintf(stdout, " calc time = %.2f secs\n", blk.lab.calc_time);\r
+ fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));\r
+ } else if (blk.lab.label_type == LT_TITLE) {\r
+ fprintf(stdout, "Title: %s\n", blk.lab.label_str);\r
+ fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));\r
+ } else if (blk.lab.label_type == LT_NOTE) {\r
+ fprintf(stdout, "Note: %s\n", blk.lab.label_str);\r
+ fprintf(stdout, " Timestamp = %s\n", td_str_tmdt(&blk.lab.timedate));\r
+ }\r
+ }\r
+ }\r
+\r
+ if (opt_stats || (! (opt_set_max && opt_set_min)))\r
+ {\r
+ double minfound = HUGE_VAL, maxfound = -HUGE_VAL;\r
+ double mode = 0, mean = 0, stddev = 0, window = 0;\r
+ double spread;\r
+ int hist[256];\r
+ int ibin, nbin = 256;\r
+ int max_bin, max_binindex; \r
+ double maxbin;\r
+ int ix, iy;\r
+\r
+ maxbin = nbin - 1;\r
+ for (ix = 0; ix < im->nx; ix++)\r
+ {\r
+ for (iy = 0; iy < im->ny; iy++)\r
+ {\r
+ if (im->v[ix][iy] > maxfound)\r
+ maxfound = im->v[ix][iy];\r
+ if (im->v[ix][iy] < minfound)\r
+ minfound = im->v[ix][iy];\r
+ mean += im->v[ix][iy];\r
+ }\r
+ }\r
+ spread = maxfound - minfound;\r
+ if (spread == 0)\r
+ mode = minfound;\r
+ else\r
+ {\r
+ for (ibin = 0; ibin < nbin; ibin++)\r
+ hist[ibin] = 0;\r
+ for (ix = 0; ix < im->nx; ix++)\r
+ {\r
+ for (iy = 0; iy < im->ny; iy++)\r
+ {\r
+ int b = (int) ((((im->v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);\r
+ hist[b]++;\r
+ }\r
+ }\r
+ max_binindex = 0;\r
+ max_bin = -1;\r
+ for (ibin = 0; ibin < nbin; ibin++)\r
+ {\r
+ if (hist[ibin] > max_bin)\r
+ {\r
+ max_bin = hist[ibin];\r
+ max_binindex = ibin;\r
+ }\r
+ }\r
+ mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;\r
+ }\r
+\r
+ if (opt_auto == O_AUTO_FULL)\r
+ {\r
+ if (! opt_set_max)\r
+ densmax = maxfound;\r
+ if (! opt_set_min)\r
+ densmin = minfound;\r
+ }\r
+ if (opt_stats || opt_auto != O_AUTO_FULL)\r
+ {\r
+ mean /= im->nx * im->ny;\r
+ for (ix = 0; ix < im->nx; ix++)\r
+ {\r
+ for (iy = 0; iy < im->ny; iy++)\r
+ {\r
+ double diff = (im->v[ix][iy] - mean);\r
+ stddev += diff * diff;\r
+ }\r
+ }\r
+ stddev = sqrt(stddev / (im->nx * im->ny));\r
+ if (opt_auto == O_AUTO_FULL)\r
+ ;\r
+ else if (opt_auto == O_AUTO_STD1)\r
+ window = stddev;\r
+ else if (opt_auto == O_AUTO_STD0_1)\r
+ window = stddev * 0.1;\r
+ else if (opt_auto == O_AUTO_STD0_5)\r
+ window = stddev * 0.5;\r
+ else if (opt_auto == O_AUTO_STD2)\r
+ window = stddev * 2;\r
+ else if (opt_auto == O_AUTO_STD3)\r
+ window = stddev * 3;\r
+ else\r
+ {\r
+ fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);\r
+ return (1);\r
+ }\r
+ }\r
+ if (opt_stats)\r
+ {\r
+ fprintf(stdout,"nx=%d\n", im->nx);\r
+ fprintf(stdout,"ny=%d\n", im->ny);\r
+ fprintf(stdout,"min=%f\n", minfound);\r
+ fprintf(stdout,"max=%f\n", maxfound);\r
+ fprintf(stdout,"mean=%f\n", mean);\r
+ fprintf(stdout,"mode=%f\n", mode);\r
+ fprintf(stdout,"stddev=%f\n", stddev);\r
+ }\r
+ if (opt_auto != O_AUTO_FULL)\r
+ {\r
+ double center;\r
+\r
+ if (opt_center == O_CENTER_MODE)\r
+ center = mode;\r
+ else if (opt_center == O_CENTER_MEAN)\r
+ center = mean;\r
+ else\r
+ {\r
+ fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);\r
+ return (1);\r
+ }\r
+ if (! opt_set_max)\r
+ densmax = center + window;\r
+ if (! opt_set_min)\r
+ densmin = center - window;\r
+ }\r
+ }\r
+\r
+ if (opt_stats)\r
+ {\r
+ fprintf(stdout,"min_disp=%f\n", densmin);\r
+ fprintf(stdout,"max_disp=%f\n", densmax);\r
+ }\r
+ \r
+ if (opt_format == O_FORMAT_PGM)\r
+ sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);\r
+ else if (opt_format == O_FORMAT_PGMASC)\r
+ sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);\r
+#if HAVE_PNG\r
+ else if (opt_format == O_FORMAT_PNG)\r
+ sdf2d_to_png (im, out_file, 8, opt_scale, opt_scale, densmin, densmax);\r
+ else if (opt_format == O_FORMAT_PNG16)\r
+ sdf2d_to_png (im, out_file, 16, opt_scale, opt_scale, densmin, densmax);\r
+#endif\r
+#if HAVE_GIF\r
+ else if (opt_format == O_FORMAT_GIF) \r
+ sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);\r
+#endif\r
+ else if (opt_format == O_FORMAT_DISP) {\r
+#if HAVE_SGP\r
+ image_display_scale (im, opt_scale, densmin, densmax);\r
+ cio_kb_getc();\r
+ sgp2_close(sgp2_get_active_win());\r
+#endif\r
+ }\r
+ else\r
+ {\r
+ fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);\r
+ return (1);\r
+ }\r
+ return (0);\r
+}\r
+\r
+\r
+void \r
+sdf2d_to_pgm (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)\r
+{\r
+ FILE *fp;\r
+ int irow;\r
+ unsigned char *rowp;\r
+\r
+ rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");\r
+ if (rowp == NULL)\r
+ return;\r
+\r
+ if ((fp = fopen (outfile, "wb")) == NULL)\r
+ return;\r
+\r
+ fprintf(fp, "P5\n");\r
+ fprintf(fp, "%d %d\n", im->nx, im->ny);\r
+ fprintf(fp, "255\n");\r
+\r
+ for (irow = im->ny - 1; irow >= 0; irow--)\r
+ {\r
+ int icol, ir;\r
+\r
+ for (icol = 0; icol < im->nx; icol++)\r
+ {\r
+ double dens;\r
+ int p;\r
+ int pos = icol * nxcell;\r
+ dens = (im->v[icol][irow] - densmin) / (densmax - densmin);\r
+ if (dens < 0)\r
+ dens = 0;\r
+ else if (dens > 1)\r
+ dens = 1;\r
+ for (p = pos; p < pos + nxcell; p++)\r
+ {\r
+ rowp[p] = (unsigned int) (dens * 255.);\r
+ }\r
+ }\r
+ for (ir = 0; ir < nycell; ir++)\r
+ {\r
+ int ic;\r
+ for (ic = 0; ic < im->nx * nxcell; ic++) \r
+ fprintf(fp, "%c ", rowp[ic]);\r
+ }\r
+ }\r
+ sys_free(rowp, "sdf2img");\r
+\r
+ fclose(fp);\r
+}\r
+\r
+void \r
+sdf2d_to_pgmasc (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)\r
+{\r
+ FILE *fp;\r
+ int irow;\r
+ unsigned char *rowp;\r
+\r
+ rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");\r
+ if (rowp == NULL)\r
+ return;\r
+\r
+ if ((fp = fopen (outfile, "wb")) == NULL)\r
+ return;\r
+\r
+ fprintf(fp, "P2\n");\r
+ fprintf(fp, "%d %d\n", im->nx, im->ny);\r
+ fprintf(fp, "255\n");\r
+\r
+ for (irow = im->ny - 1; irow >= 0; irow--)\r
+ {\r
+ int icol, ir;\r
+\r
+ for (icol = 0; icol < im->nx; icol++)\r
+ {\r
+ double dens;\r
+ int p;\r
+ int pos = icol * nxcell;\r
+ dens = (im->v[icol][irow] - densmin) / (densmax - densmin);\r
+ if (dens < 0)\r
+ dens = 0;\r
+ else if (dens > 1)\r
+ dens = 1;\r
+ for (p = pos; p < pos + nxcell; p++)\r
+ {\r
+ rowp[p] = (unsigned int) (dens * 255.);\r
+ }\r
+ }\r
+ for (ir = 0; ir < nycell; ir++)\r
+ {\r
+ int ic;\r
+ for (ic = 0; ic < im->nx * nxcell; ic++) \r
+ fprintf(fp, "%d ", rowp[ic]);\r
+ fprintf(fp, "\n");\r
+ }\r
+ }\r
+ sys_free(rowp, "sdf2img");\r
+\r
+ fclose(fp);\r
+}\r
+\r
+\r
+#ifdef HAVE_PNG\r
+void \r
+sdf2d_to_png (IMAGE *im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)\r
+{\r
+ FILE *fp;\r
+ png_structp png_ptr;\r
+ png_infop info_ptr;\r
+ int irow;\r
+ unsigned char *rowp;\r
+ double max_out_level = (1 << bitdepth) - 1;\r
+\r
+ rowp = (unsigned char *) sys_alloc(im->nx * nxcell * (bitdepth / 8),"sdf2d_to_img");\r
+ if (rowp == NULL)\r
+ return;\r
+\r
+ if ((fp = fopen (outfile, "wb")) == NULL)\r
+ return;\r
+\r
+ png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);\r
+ if (! png_ptr)\r
+ return;\r
+\r
+ info_ptr = png_create_info_struct(png_ptr);\r
+ if (! info_ptr)\r
+ {\r
+ png_destroy_write_struct(&png_ptr, (png_infopp) NULL);\r
+ fclose(fp);\r
+ return;\r
+ }\r
+\r
+ if (setjmp(png_ptr->jmpbuf))\r
+ {\r
+ png_destroy_write_struct(&png_ptr, &info_ptr);\r
+ fclose(fp);\r
+ return;\r
+ }\r
+\r
+ png_init_io(png_ptr, fp);\r
+\r
+ 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);\r
+\r
+ png_write_info(png_ptr, info_ptr);\r
+ for (irow = im->ny - 1; irow >= 0; irow--)\r
+ {\r
+ png_bytep row_pointer = rowp;\r
+ int icol, ir;\r
+\r
+ for (icol = 0; icol < im->nx; icol++)\r
+ {\r
+ double dens;\r
+ int p;\r
+ unsigned int outval;\r
+ int pos = icol * nxcell;\r
+ dens = (im->v[icol][irow] - densmin) / (densmax - densmin);\r
+ if (dens < 0)\r
+ dens = 0;\r
+ else if (dens > 1)\r
+ dens = 1;\r
+ outval = dens * max_out_level;\r
+ for (p = pos; p < pos + nxcell; p++)\r
+ {\r
+ if (bitdepth == 8)\r
+ rowp[p] = outval;\r
+ else {\r
+ int rowpos = p * 2;\r
+ rowp[rowpos] = (outval >> 8) & 0xFF;\r
+ rowp[rowpos+1] = (outval & 0xFF);\r
+ }\r
+ }\r
+ }\r
+ for (ir = 0; ir < nycell; ir++)\r
+ {\r
+ png_write_rows (png_ptr, &row_pointer, 1);\r
+ }\r
+ }\r
+ sys_free(rowp, "sdf2img");\r
+\r
+ png_write_end(png_ptr, info_ptr);\r
+ png_destroy_write_struct(&png_ptr, &info_ptr);\r
+\r
+ fclose(fp);\r
+}\r
+#endif\r
+\r
+#ifdef HAVE_GD\r
+#include "gd.h"\r
+#define N_GRAYSCALE 256\r
+#endif\r
+\r
+void\r
+sdf2d_to_gif (IMAGE *im, char *outfile, int nxcell, int nycell, double densmin, double densmax)\r
+{\r
+#ifdef HAVE_GD\r
+ gdImagePtr gif;\r
+ FILE *out;\r
+ int gs_indices[N_GRAYSCALE];\r
+ int i;\r
+ int irow;\r
+ int lastrow;\r
+ unsigned char *rowp;\r
+\r
+ rowp = (unsigned char *) sys_alloc(im->nx * nxcell,"sdf2d_to_img");\r
+ if (rowp == NULL)\r
+ return;\r
+\r
+ gif = gdImageCreate(im->nx * nxcell, im->ny * nycell);\r
+ for (i = 0; i < N_GRAYSCALE; i++)\r
+ gs_indices[i] = gdImageColorAllocate(gif, i, i, i);\r
+\r
+ lastrow = im->ny * nycell - 1;\r
+ for (irow = 0; irow < im->ny; irow++)\r
+ {\r
+ int icol, ir;\r
+ int rpos = irow * nycell;\r
+ for (ir = rpos; ir < rpos + nycell; ir++)\r
+ {\r
+ for (icol = 0; icol < im->nx; icol++)\r
+ {\r
+ double dens;\r
+ int ic;\r
+ int cpos = icol * nxcell;\r
+ dens = (im->v[icol][irow] - densmin) / (densmax - densmin);\r
+ if (dens < 0)\r
+ dens = 0;\r
+ else if (dens > 1)\r
+ dens = 1;\r
+ for (ic = cpos; ic < cpos + nxcell; ic++)\r
+ {\r
+ rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));\r
+ gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ sys_free(rowp, "sdf2img");\r
+\r
+ if ((out = fopen(outfile,"w")) == NULL)\r
+ {\r
+ fprintf(stderr,"Error opening output file %s for writing\n", outfile);\r
+ exit(1);\r
+ }\r
+ gdImageGif(gif,out);\r
+ fclose(out);\r
+ gdImageDestroy(gif);\r
+#else\r
+ fprintf(stderr, "This version does not support GIF");\r
+#endif\r
+}\r
+\r
+#ifndef NO_MAIN\r
+int \r
+main (int argc, char *const argv[])\r
+{\r
+ return (sdf2img_main(argc, argv));\r
+}\r
+#endif\r