** This is part of the CTSim program
** Copyright (C) 1983-2000 Kevin Rosenberg
**
-** $Id: if2img.cpp,v 1.11 2000/07/09 08:16:18 kevin Exp $
+** $Id: if2img.cpp,v 1.13 2000/07/11 10:32:44 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
#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
-#if HAVE_GIF
-void sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
-#endif
-void sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
-void sdf2d_to_pgmasc (ImageFile& 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 };
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 << " ifname Name of input file" << endl;
cout << " outfile Name of output file" << endl;
cout << " --format Output format" << endl;
cout << " pgm PGM (portable graymap) format (default)" << endl;
cout << " png PNG (8-bit) format" << endl;
cout << " png16 PNG (16-bit) format" << endl;
#endif
-#ifdef HAVE_GIF
+#if HAVE_G2
cout << " gif GIF format" << endl;
#endif
cout << " disp Display on screen" << endl;
if2img_main (int argc, char *const argv[])
{
ImageFile* pim = NULL;
-
double densmin = HUGE_VAL, densmax = -HUGE_VAL;
char *in_file, *out_file;
int opt_verbose = 0;
im.printLabels(cout);
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 nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- maxbin = nbin - 1;
- for (int ix = 0; ix < nx; ix++) {
- for (int iy = 0; iy < ny; iy++) {
- if (v[ix][iy] > maxfound)
- maxfound = v[ix][iy];
- if (v[ix][iy] < minfound)
- minfound = v[ix][iy];
- mean += v[ix][iy];
- }
- }
- spread = maxfound - minfound;
- if (spread == 0)
- mode = minfound;
- else {
- for (ibin = 0; ibin < nbin; ibin++)
- hist[ibin] = 0;
- for (int ix = 0; ix < nx; ix++) {
- for (int iy = 0; iy < ny; iy++) {
- int b = (int) ((((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;
- }
+ double min, max, mean, mode, median, stddev;
+ double window;
+ im.statistics(min, max, mean, mode, median, stddev);
- if (opt_auto == O_AUTO_FULL) {
+ if (opt_auto == O_AUTO_FULL) {
if (! opt_set_max)
- densmax = maxfound;
+ densmax = max;
if (! opt_set_min)
- densmin = minfound;
- }
- if (opt_stats || opt_auto != O_AUTO_FULL) {
- mean /= nx * ny;
- for (int ix = 0; ix < nx; ix++) {
- for (int iy = 0; iy < ny; iy++) {
- double diff = (v[ix][iy] - mean);
- stddev += diff * diff;
- }
+ densmin = min;
}
- stddev = sqrt(stddev / (nx * 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;
+ if (opt_stats || opt_auto != O_AUTO_FULL) {
+
+ if (opt_auto == O_AUTO_FULL)
+ ;
+ else if (opt_auto == O_AUTO_STD1)
+ window = stddev;
+ else if (opt_auto == O_AUTO_STD0_1)
+ window = stddev * 0.1;
+ else if (opt_auto == O_AUTO_STD0_5)
+ window = stddev * 0.5;
else if (opt_auto == O_AUTO_STD2)
- window = stddev * 2;
- else if (opt_auto == O_AUTO_STD3)
- window = stddev * 3;
- else {
- sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
- return (1);
+ window = stddev * 2;
+ else if (opt_auto == O_AUTO_STD3)
+ window = stddev * 3;
+ else {
+ sys_error (ERR_SEVERE, "Internal Error: Invalid auto mode %d", opt_auto);
+ return (1);
+ }
}
- }
- if (opt_stats) {
- cout <<"nx: " << 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;
-
- if (opt_center == O_CENTER_MODE)
- center = mode;
- else if (opt_center == O_CENTER_MEAN)
- center = mean;
- else {
- sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
- return (1);
+ if (opt_stats) {
+ cout <<"nx: " << im.nx() << endl;
+ cout <<"ny: " << im.ny() << endl;
+ cout <<"min: " << min << endl;
+ cout <<"max: " << max << endl;
+ cout <<"mean: " << mean << endl;
+ cout <<"mode: " << mode << endl;
+ cout <<"stddev: " << stddev << endl;
}
- if (! opt_set_max)
- densmax = center + window;
- if (! opt_set_min)
- densmin = center - window;
+ if (opt_auto != O_AUTO_FULL) {
+ double center;
+
+ if (opt_center == O_CENTER_MODE)
+ center = mode;
+ else if (opt_center == O_CENTER_MEAN)
+ center = mean;
+ else {
+ sys_error (ERR_SEVERE, "Internal Error: Invalid center mode %d", opt_center);
+ return (1);
+ }
+ if (! opt_set_max)
+ densmax = center + window;
+ if (! opt_set_min)
+ densmin = center - window;
}
}
}
if (opt_format == O_FORMAT_PGM)
- sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
+ im.writeImagePGM (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);
+ im.writeImagePGMASCII (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);
+ im.writeImagePNG (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);
+ im.writeImagePNG (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);
+ im.writeImageGIF (out_file, opt_scale, opt_scale, densmin, densmax);
#endif
else if (opt_format == O_FORMAT_DISP) {
#if HAVE_SGP
im.displayScaling (opt_scale, densmin, densmax);
+ cout << "Press enter to continue\n";
cio_kb_getc();
sgp2_close(sgp2_get_active_win());
#endif
}
-void
-sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
-{
- FILE *fp;
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- unsigned char rowp [nx * nxcell];
-
- if ((fp = fopen (outfile, "wb")) == NULL)
- return;
-
- fprintf(fp, "P5\n");
- fprintf(fp, "%d %d\n", nx, ny);
- fprintf(fp, "255\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<unsigned int> (dens * 255.);
- }
- }
- for (int ir = 0; ir < nycell; ir++) {
- for (int ic = 0; ic < nx * nxcell; ic++)
- fprintf(fp, "%c ", rowp[ic]);
- }
- }
-
- fclose(fp);
-}
-
-void
-sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
-{
- FILE *fp;
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- unsigned char rowp [nx * nxcell];
- if (rowp == NULL)
- return;
-
- if ((fp = fopen (outfile, "wb")) == NULL)
- return;
-
- fprintf(fp, "P2\n");
- fprintf(fp, "%d %d\n", nx, ny);
- fprintf(fp, "255\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<unsigned int> (dens * 255.);
- }
- }
- 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);
-}
-
-
-#ifdef HAVE_PNG
-void
-sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
-{
- FILE *fp;
- png_structp png_ptr;
- png_infop info_ptr;
- double max_out_level = (1 << bitdepth) - 1;
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- unsigned char rowp [nx * nxcell * (bitdepth / 8)];
-
- 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, 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 (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<unsigned int> (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);
- }
- }
- }
- 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);
-
- fclose(fp);
-}
-#endif
-
-#ifdef HAVE_GD
-#include "gd.h"
-static const int N_GRAYSCALE=256;
-#endif
-
-void
-sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
-{
-#ifdef HAVE_GD
- gdImagePtr gif;
- FILE *out;
- int gs_indices[N_GRAYSCALE];
- int nx = im.nx();
- int ny = im.ny();
- ImageFileArray v = im.getArray();
-
- unsigned char rowp [nx * nxcell];
- if (rowp == NULL)
- return;
-
- gif = gdImageCreate(nx * nxcell, ny * nycell);
- for (int i = 0; i < N_GRAYSCALE; i++)
- gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
-
- 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]]);
- }
- }
- }
- }
-
- 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
- cout << "This version does not support GIF" << endl;
-#endif
-}
-
#ifndef NO_MAIN
int
main (int argc, char *const argv[])