r94: finished c++ conversions
[ctsim.git] / src / if2img.cpp
index 1132e7383d649235168b53ba83f7183909a7f3d9..6971fbbbab5c92c91a5806184e84a4d209f0b9d3 100644 (file)
@@ -1,34 +1,15 @@
 /*****************************************************************************
-**  This is part of the CTSim program
-**  Copyright (C) 1983-2000 Kevin Rosenberg
-**
-**  $Id: if2img.cpp,v 1.1 2000/06/07 07:55:30 kevin Exp $
-**  $Log: if2img.cpp,v $
-**  Revision 1.1  2000/06/07 07:55:30  kevin
-**  Initial import
-**
-**  Revision 1.1  2000/06/07 02:29:05  kevin
-**  Initial C++ versions
-**
-**  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
+** FILE IDENTIFICATION
 **
-**  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
+**   Name:          if2img.cpp
+**   Purpose:       Convert an image file to a viewable image
+**   Programmer:    Kevin Rosenberg
+**   Date Started:  April 2000
 **
+**  This is part of the CTSim program
+**  Copyright (C) 1983-2000 Kevin Rosenberg
 **
+**  $Id: if2img.cpp,v 1.4 2000/06/13 16:20:31 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
@@ -102,7 +83,7 @@ enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC
 void 
 if2img_usage (const char *program)
 {
-  fprintf(stdout, "usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
+  fprintf(stdout, "usage: %s sdfname outfile [OPTIONS]\n", fileBasename(program));
   fprintf(stdout, "Convert IF file to an image file\n");
   fprintf(stdout, "\n");
   fprintf(stdout, "     sdfname    Name of input SDF file\n");
@@ -300,17 +281,17 @@ if2img_main (int argc, char *const argv[])
 
   pim = new ImageFile (in_file);
   ImageFile& im = *pim;
-  if (! im.adf.fileRead()) {
+  if (! im.fileRead()) {
     fprintf(stderr, "File %s does not exist\n", in_file);
     return (1);
   }
 
   if (opt_labels) {
-    int nlabels = im.adf.getNumLabels();
+    int nlabels = im.getNumLabels();
 
     for (int i = 0; i < nlabels; i++) {
       Array2dFileLabel label;
-      im.adf.labelRead (label, i);
+      im.labelRead (label, i);
       string str;
       label.getDateString (str);
 
@@ -468,15 +449,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 = new unsigned char [nx * nxcell];
 
   if ((fp = fopen (outfile, "wb")) == NULL)
      return;
@@ -485,33 +462,21 @@ 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<unsigned int> (dens * 255.);
+      }
     }
-  sys_free(rowp, "if2img");
+    for (int ir = 0; ir < nycell; ir++) {
+      for (int ic = 0; ic < nx * nxcell; ic++) 
+       fprintf(fp, "%c ", rowp[ic]);
+    }
+  }
+  delete rowp;
 
   fclose(fp);
 }
@@ -520,13 +485,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 = new unsigned char [nx * nxcell];
   if (rowp == NULL)
     return;
 
@@ -537,34 +500,22 @@ 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<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");
     }
-  sys_free(rowp, "if2img");
+  }
+  delete rowp;
 
   fclose(fp);
 }
@@ -577,16 +528,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 = new unsigned char [nx * nxcell * (bitdepth / 8)];
 
   if ((fp = fopen (outfile, "wb")) == NULL)
      return;
@@ -596,59 +543,46 @@ 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<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);
        }
+      }
     }
-  sys_free(rowp, "if2img");
+    for (int ir = 0; ir < nycell; ir++)
+      png_write_rows (png_ptr, &row_pointer, 1);
+  }
+  delete rowp;
 
   png_write_end(png_ptr, info_ptr);
   png_destroy_write_struct(&png_ptr, &info_ptr);
@@ -669,54 +603,39 @@ 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");
+  usnigned char* rowp = new unsigned char [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");
+  }
+  delete rowp;
 
-  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) {
+    fprintf(stderr,"Error opening output file %s for writing\n", outfile);
+    exit(1);
+  }
   gdImageGif(gif,out);
   fclose(out);
   gdImageDestroy(gif);