r91: Made ImageFile inherit from Array2dFile
[ctsim.git] / src / if2img.cpp
1 /*****************************************************************************
2 ** FILE IDENTIFICATION
3 **
4 **   Name:          if2img.cpp
5 **   Purpose:       Convert an image file to a viewable image
6 **   Programmer:    Kevin Rosenberg
7 **   Date Started:  April 2000
8 **
9 **  This is part of the CTSim program
10 **  Copyright (C) 1983-2000 Kevin Rosenberg
11 **
12 **  $Id: if2img.cpp,v 1.3 2000/06/09 11:03:08 kevin Exp $
13 **
14 **  This program is free software; you can redistribute it and/or modify
15 **  it under the terms of the GNU General Public License (version 2) as
16 **  published by the Free Software Foundation.
17 **
18 **  This program is distributed in the hope that it will be useful,
19 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
20 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 **  GNU General Public License for more details.
22 **
23 **  You should have received a copy of the GNU General Public License
24 **  along with this program; if not, write to the Free Software
25 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 ******************************************************************************/
27
28 /* FILE
29  *   if2img.c           Convert an SDF file to a viewable format image
30  */
31
32 #include "ct.h"
33
34 #if HAVE_PNG
35 void sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax);
36 #endif
37 #if HAVE_GIF
38 void sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
39 #endif
40 void sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
41 void sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax);
42
43 enum { O_SCALE, O_MIN, O_MAX, O_AUTO, O_CENTER, O_STATS, O_FORMAT, O_LABELS, 
44        O_HELP, O_VERBOSE, O_VERSION, O_DEBUG };
45
46 static struct option my_options[] =
47 {
48   {"scale", 1, 0, O_SCALE},
49   {"min", 1, 0, O_MIN},
50   {"max", 1, 0, O_MAX},
51   {"auto", 1, 0, O_AUTO},
52   {"center", 1, 0, O_CENTER},
53   {"format", 1, 0, O_FORMAT},
54   {"labels", 0, 0, O_LABELS},
55   {"stats", 0, 0, O_STATS},
56   {"verbose", 0, 0, O_VERBOSE},
57   {"debug", 0, 0, O_DEBUG},
58   {"help", 0, 0, O_HELP},
59   {"version", 0, 0, O_VERSION},
60   {0, 0, 0, 0}
61 };
62
63 enum { O_AUTO_FULL, O_AUTO_STD0_1, O_AUTO_STD0_5, O_AUTO_STD1, O_AUTO_STD2, O_AUTO_STD3 };
64 #define O_AUTO_FULL_STR  "full"
65 #define O_AUTO_STD0_1_STR  "std0.1"
66 #define O_AUTO_STD0_5_STR  "std0.5"
67 #define O_AUTO_STD1_STR  "std1"
68 #define O_AUTO_STD2_STR  "std2"
69 #define O_AUTO_STD3_STR  "std3"
70
71 enum { O_CENTER_MEAN, O_CENTER_MODE };
72 #define O_CENTER_MEAN_STR  "mean"
73 #define O_CENTER_MODE_STR  "mode"
74
75 enum { O_FORMAT_GIF, O_FORMAT_PNG, O_FORMAT_PNG16, O_FORMAT_PGM, O_FORMAT_PGMASC, O_FORMAT_DISP };
76 #define O_FORMAT_GIF_STR   "gif"
77 #define O_FORMAT_PNG_STR   "png" 
78 #define O_FORMAT_PNG16_STR   "png16" 
79 #define O_FORMAT_PGM_STR   "pgm"
80 #define O_FORMAT_PGMASC_STR "pgmasc"
81 #define O_FORMAT_DISP_STR    "disp"
82
83 void 
84 if2img_usage (const char *program)
85 {
86   fprintf(stdout, "usage: %s sdfname outfile [OPTIONS]\n", kbasename(program));
87   fprintf(stdout, "Convert IF file to an image file\n");
88   fprintf(stdout, "\n");
89   fprintf(stdout, "     sdfname    Name of input SDF file\n");
90   fprintf(stdout, "     outfile    Name of output file\n");
91   fprintf(stdout, "     --format   Output format\n");
92   fprintf(stdout, "         pgm    PGM (portable graymap) format (default)\n");
93   fprintf(stdout, "         pgmasc PGM (portable graymap) ASCII format\n");
94 #ifdef HAVE_PNG
95   fprintf(stdout, "         png    PNG (8-bit) format\n");
96   fprintf(stdout, "         png16  PNG (16-bit) format\n");
97 #endif
98 #ifdef HAVE_GIF
99   fprintf(stdout, "         gif    GIF format\n");
100 #endif
101   fprintf(stdout, "         disp   Display on screen\n");
102   fprintf(stdout, "     --center   Center of window\n");
103   fprintf(stdout, "         mode   Mode is center of window (default)\n");
104   fprintf(stdout, "         mean   Mean is center of window\n");
105   fprintf(stdout, "     --auto     Set auto window\n");
106   fprintf(stdout, "         full   Use full window (default)\n");
107   fprintf(stdout, "         std0.1 Use 0.1 standard deviation about center\n");
108   fprintf(stdout, "         std0.5 Use 0.5 standard deviation about center\n");
109   fprintf(stdout, "         std1   Use one standard deviation about center\n");
110   fprintf(stdout, "         std2   Use two standard deviations about center\n");
111   fprintf(stdout, "         std3   Use three standard deviations about center\n");
112   fprintf(stdout, "     --scale    Scaling factor for output size\n");
113   fprintf(stdout, "     --min      Set minimum intensity\n");
114   fprintf(stdout, "     --max      Set maximum intensity\n");
115   fprintf(stdout, "     --stats    Print image statistics\n");
116   fprintf(stdout, "     --labels   Print image labels\n");
117   fprintf(stdout, "     --debug    Set debug mode\n");
118   fprintf(stdout, "     --verbose  Set verbose mode\n");
119   fprintf(stdout, "     --version  Print version\n");
120   fprintf(stdout, "     --help     Print this help message\n");
121 }
122
123
124 int 
125 if2img_main (int argc, char *const argv[])
126 {
127   ImageFile* pim = NULL;
128
129   double densmin = HUGE_VAL, densmax = -HUGE_VAL;
130   char *in_file, *out_file;
131   int opt_verbose = 0;
132   int opt_scale = 1;
133   int opt_auto = O_AUTO_FULL;
134   int opt_set_max = 0;
135   int opt_set_min = 0;
136   int opt_stats = 0;
137   int opt_debug = 0;
138   int opt_center = O_CENTER_MODE;
139   int opt_format = O_FORMAT_PGM;
140   int opt_labels = 0;
141
142   while (1)
143     {
144       int c = getopt_long (argc, argv, "", my_options, NULL);
145       char *endptr = NULL;
146       char *endstr;
147       
148       if (c == -1)
149         break;
150       
151       switch (c)
152         {
153         case O_MIN:
154           opt_set_min = 1;
155           densmin = strtod(optarg, &endptr);
156           endstr = optarg + strlen(optarg);
157           if (endptr != endstr)
158             {
159               fprintf(stderr, "Error setting --min to %s\n", optarg);
160               if2img_usage(argv[0]);
161               return (1);
162             }
163           break;
164         case O_MAX:
165           opt_set_max = 1;
166           densmax = strtod(optarg, &endptr);
167           endstr = optarg + strlen(optarg);
168           if (endptr != endstr)
169             {
170               fprintf(stderr, "Error setting --max to %s\n", optarg);
171               if2img_usage(argv[0]);
172               return (1);
173             }
174           break;
175         case O_SCALE:
176           opt_scale = strtol(optarg, &endptr, 10);
177           endstr = optarg + strlen(optarg);
178           if (endptr != endstr)
179             {
180               fprintf(stderr,"Error setting --scale to %s\n", optarg);
181               if2img_usage(argv[0]);
182               return (1);
183             }
184           break;
185         case O_AUTO:
186           if (strcmp(optarg, O_AUTO_FULL_STR) == 0)
187             opt_auto = O_AUTO_FULL;
188           else if (strcmp(optarg, O_AUTO_STD1_STR) == 0)
189             opt_auto = O_AUTO_STD1;
190           else if (strcmp(optarg, O_AUTO_STD0_5_STR) == 0)
191             opt_auto = O_AUTO_STD0_5;
192           else if (strcmp(optarg, O_AUTO_STD0_1_STR) == 0)
193             opt_auto = O_AUTO_STD0_1;
194           else if (strcmp(optarg, O_AUTO_STD2_STR) == 0)
195             opt_auto = O_AUTO_STD2;
196           else if (strcmp(optarg, O_AUTO_STD3_STR) == 0)
197             opt_auto = O_AUTO_STD3;
198           else
199             {
200               fprintf(stderr, "Invalid auto mode %s\n", optarg);
201               if2img_usage(argv[0]);
202               return (1);
203             }
204                 break;
205         case O_CENTER:
206           if (strcmp(optarg, O_CENTER_MEAN_STR) == 0)
207             opt_center = O_CENTER_MEAN;
208           else if (strcmp(optarg, O_CENTER_MODE_STR) == 0)
209             opt_center = O_CENTER_MODE;
210           else
211             {
212               fprintf(stderr, "Invalid center mode %s\n", optarg);
213               if2img_usage(argv[0]);
214               return (1);
215             }
216                 break;
217         case O_FORMAT:
218           if (strcmp(optarg, O_FORMAT_PGM_STR) == 0)
219             opt_format = O_FORMAT_PGM;
220           else if (strcmp(optarg, O_FORMAT_PGMASC_STR) == 0)
221             opt_format = O_FORMAT_PGMASC;
222 #if HAVE_PNG
223           else if (strcmp(optarg, O_FORMAT_PNG_STR) == 0)
224             opt_format = O_FORMAT_PNG;
225           else if (strcmp(optarg, O_FORMAT_PNG16_STR) == 0)
226             opt_format = O_FORMAT_PNG16;
227 #endif
228 #if HAVE_GIF
229           else if (strcmp(optarg, O_FORMAT_GIF_STR) == 0)
230             opt_format = O_FORMAT_GIF;
231 #endif
232           else if (strcmp(optarg, O_FORMAT_DISP_STR) == 0)
233             opt_format = O_FORMAT_DISP;
234           else {
235               fprintf(stderr, "Invalid format mode %s\n", optarg);
236               if2img_usage(argv[0]);
237               return (1);
238             }
239           break;
240         case O_LABELS:
241           opt_labels = 1;
242           break;
243         case O_VERBOSE:
244           opt_verbose = 1;
245           break;
246         case O_DEBUG:
247           opt_debug = 1;
248           break;
249         case O_STATS:
250           opt_stats = 1;
251           break;
252         case O_VERSION:
253 #ifdef VERSION
254           fprintf(stdout, "Version %s\n", VERSION);
255 #else
256           fprintf(stderr, "Unknown version number");
257 #endif
258           exit(0);
259         case O_HELP:
260         case '?':
261           if2img_usage(argv[0]);
262           return (0);
263         default:
264           if2img_usage(argv[0]);
265           return (1);
266         }
267     }
268
269   if ((opt_format == O_FORMAT_DISP && optind + 1 != argc) 
270       || (opt_format != O_FORMAT_DISP && optind + 2 != argc))
271     {
272         if2img_usage(argv[0]);
273         return (1);
274     }
275   
276   in_file = argv[optind];
277
278   if (opt_format != O_FORMAT_DISP)
279       out_file = argv[optind+1];
280   else out_file = NULL;
281
282   pim = new ImageFile (in_file);
283   ImageFile& im = *pim;
284   if (! im.fileRead()) {
285     fprintf(stderr, "File %s does not exist\n", in_file);
286     return (1);
287   }
288
289   if (opt_labels) {
290     int nlabels = im.getNumLabels();
291
292     for (int i = 0; i < nlabels; i++) {
293       Array2dFileLabel label;
294       im.labelRead (label, i);
295       string str;
296       label.getDateString (str);
297
298       if (label.getLabelType() == Array2dFileLabel::L_HISTORY) {
299         cout << "History: " << label.getLabelString() << endl;
300         cout << "  calc time = " << label.getCalcTime() << " secs" << endl;
301         cout << "  Timestamp = " << str << endl;
302       } else if (label.getLabelType() == Array2dFileLabel::L_USER) {
303         cout << "Note: " <<  label.getLabelString() << endl;
304         cout << "  Timestamp = %s" << str << endl;
305       }
306     }
307   }
308
309   if (opt_stats || (! (opt_set_max && opt_set_min))) {
310     double minfound = HUGE_VAL, maxfound = -HUGE_VAL;
311     double mode = 0, mean = 0, stddev = 0, window = 0;
312     double spread;
313     int hist[256];
314     int ibin, nbin = 256;
315     int max_bin, max_binindex; 
316     double maxbin;
317     int nx = im.nx();
318     int ny = im.ny();
319     ImageFileArray v = im.getArray();
320     
321     maxbin = nbin - 1;
322     for (int ix = 0; ix < nx; ix++) {
323       for (int iy = 0; iy < ny; iy++) {
324         if (v[ix][iy] > maxfound)
325           maxfound = v[ix][iy];
326         if (v[ix][iy] < minfound)
327           minfound = v[ix][iy];
328         mean += v[ix][iy];
329       }
330     }
331     spread = maxfound - minfound;
332     if (spread == 0)
333       mode = minfound;
334     else {
335       for (ibin = 0; ibin < nbin; ibin++)
336         hist[ibin] = 0;
337       for (int ix = 0; ix < nx; ix++) {
338         for (int iy = 0; iy < ny; iy++) {
339           int b = (int) ((((v[ix][iy] - minfound) / spread) * (double) maxbin) + 0.5);
340           hist[b]++;
341         }
342       }
343       max_binindex = 0;
344       max_bin = -1;
345       for (ibin = 0; ibin < nbin; ibin++) {
346         if (hist[ibin] > max_bin) {
347           max_bin = hist[ibin];
348           max_binindex = ibin;
349         }
350       }
351       mode = (((double) max_binindex) * spread / ((double) maxbin)) + minfound;
352     }
353     
354     if (opt_auto == O_AUTO_FULL) {
355         if (! opt_set_max)
356           densmax = maxfound;
357         if (! opt_set_min)
358           densmin = minfound;
359     }
360     if (opt_stats || opt_auto != O_AUTO_FULL) {
361       mean /= nx * ny;
362       for (int ix = 0; ix < nx; ix++) {
363         for (int iy = 0; iy < ny; iy++) {
364             double diff = (v[ix][iy] - mean);
365             stddev += diff * diff;
366         }
367       }
368       stddev = sqrt(stddev / (nx * ny));
369       if (opt_auto == O_AUTO_FULL)
370         ;
371       else if (opt_auto == O_AUTO_STD1)
372             window = stddev;
373       else if (opt_auto == O_AUTO_STD0_1)
374         window = stddev * 0.1;
375       else if (opt_auto == O_AUTO_STD0_5)
376         window = stddev * 0.5;
377           else if (opt_auto == O_AUTO_STD2)
378             window = stddev * 2;
379       else if (opt_auto == O_AUTO_STD3)
380         window = stddev * 3;
381       else {
382         fprintf(stderr, "Internal Error: Invalid auto mode %d\n", opt_auto);
383         return (1);
384       }
385     }
386     if (opt_stats) {
387       fprintf(stdout,"nx=%d\n", nx);
388       fprintf(stdout,"ny=%d\n", ny);
389       fprintf(stdout,"min=%f\n", minfound);
390       fprintf(stdout,"max=%f\n", maxfound);
391       fprintf(stdout,"mean=%f\n", mean);
392       fprintf(stdout,"mode=%f\n", mode);
393       fprintf(stdout,"stddev=%f\n", stddev);
394     }
395     if (opt_auto != O_AUTO_FULL) {
396       double center;
397       
398       if (opt_center == O_CENTER_MODE)
399         center = mode;
400       else if (opt_center == O_CENTER_MEAN)
401         center = mean;
402       else {
403         fprintf(stderr, "Internal Error: Invalid center mode %d\n", opt_center);
404         return (1);
405       }
406       if (! opt_set_max)
407         densmax = center + window;
408       if (! opt_set_min)
409         densmin = center - window;
410       }
411   }
412   
413   if (opt_stats) {
414     fprintf(stdout,"min_disp=%f\n", densmin);
415     fprintf(stdout,"max_disp=%f\n", densmax);
416   }
417   
418   if (opt_format == O_FORMAT_PGM)
419     sdf2d_to_pgm (im, out_file, opt_scale, opt_scale, densmin, densmax);
420   else if (opt_format == O_FORMAT_PGMASC)
421     sdf2d_to_pgmasc (im, out_file, opt_scale, opt_scale, densmin, densmax);
422 #if HAVE_PNG
423   else if (opt_format == O_FORMAT_PNG)
424     sdf2d_to_png (im, out_file, 8, opt_scale, opt_scale, densmin, densmax);
425   else if (opt_format == O_FORMAT_PNG16)
426     sdf2d_to_png (im, out_file, 16, opt_scale, opt_scale, densmin, densmax);
427 #endif
428 #if HAVE_GIF
429   else if (opt_format == O_FORMAT_GIF) 
430     sdf2d_to_gif (im, out_file, opt_scale, opt_scale, densmin, densmax);
431 #endif
432   else if (opt_format == O_FORMAT_DISP) {
433 #if HAVE_SGP
434     // image_display_scale (im, opt_scale, densmin, densmax);
435     //  cio_kb_getc();
436       sgp2_close(sgp2_get_active_win());
437 #endif
438   }
439   else
440     {
441       fprintf(stderr, "Internal Error: Invalid format mode %d\n", opt_format);
442       return (1);
443     }
444   return (0);
445 }
446
447
448 void 
449 sdf2d_to_pgm (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
450 {
451   FILE *fp;
452   int irow;
453   unsigned char *rowp;
454   int nx = im.nx();
455   int ny = im.ny();
456   ImageFileArray v = im.getArray();
457
458   rowp = (unsigned char *) sys_alloc(nx * nxcell,"sdf2d_to_img");
459   if (rowp == NULL)
460     return;
461
462   if ((fp = fopen (outfile, "wb")) == NULL)
463      return;
464
465   fprintf(fp, "P5\n");
466   fprintf(fp, "%d %d\n", nx, ny);
467   fprintf(fp, "255\n");
468
469   for (irow = ny - 1; irow >= 0; irow--)
470     {
471       int icol, ir;
472
473       for (icol = 0; icol < nx; icol++)
474         {
475           double dens;
476           int p;
477           int pos = icol * nxcell;
478           dens = (v[icol][irow] - densmin) / (densmax - densmin);
479           if (dens < 0)
480             dens = 0;
481           else if (dens > 1)
482             dens = 1;
483           for (p = pos; p < pos + nxcell; p++)
484             {
485               rowp[p] = (unsigned int) (dens * 255.);
486             }
487         }
488       for (ir = 0; ir < nycell; ir++)
489         {
490           int ic;
491           for (ic = 0; ic < nx * nxcell; ic++) 
492             fprintf(fp, "%c ", rowp[ic]);
493         }
494     }
495   sys_free(rowp, "if2img");
496
497   fclose(fp);
498 }
499
500 void 
501 sdf2d_to_pgmasc (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
502 {
503   FILE *fp;
504   int irow;
505   unsigned char *rowp;
506   int nx = im.nx();
507   int ny = im.ny();
508   ImageFileArray v = im.getArray();
509
510   rowp = (unsigned char *) sys_alloc(nx * nxcell,"sdf2d_to_img");
511   if (rowp == NULL)
512     return;
513
514   if ((fp = fopen (outfile, "wb")) == NULL)
515      return;
516
517   fprintf(fp, "P2\n");
518   fprintf(fp, "%d %d\n", nx, ny);
519   fprintf(fp, "255\n");
520
521   for (irow = ny - 1; irow >= 0; irow--)
522     {
523       int icol, ir;
524
525       for (icol = 0; icol < nx; icol++)
526         {
527           double dens;
528           int p;
529           int pos = icol * nxcell;
530           dens = (v[icol][irow] - densmin) / (densmax - densmin);
531           if (dens < 0)
532             dens = 0;
533           else if (dens > 1)
534             dens = 1;
535           for (p = pos; p < pos + nxcell; p++)
536             {
537               rowp[p] = (unsigned int) (dens * 255.);
538             }
539         }
540       for (ir = 0; ir < nycell; ir++)
541         {
542           int ic;
543           for (ic = 0; ic < nx * nxcell; ic++) 
544             fprintf(fp, "%d ", rowp[ic]);
545           fprintf(fp, "\n");
546         }
547     }
548   sys_free(rowp, "if2img");
549
550   fclose(fp);
551 }
552
553
554 #ifdef HAVE_PNG
555 void 
556 sdf2d_to_png (ImageFile& im, char *outfile, int bitdepth, int nxcell, int nycell, double densmin, double densmax)
557 {
558   FILE *fp;
559   png_structp png_ptr;
560   png_infop info_ptr;
561   int irow;
562   unsigned char *rowp;
563   double max_out_level = (1 << bitdepth) - 1;
564   int nx = im.nx();
565   int ny = im.ny();
566   ImageFileArray v = im.getArray();
567
568   rowp = (unsigned char *) sys_alloc(nx * nxcell * (bitdepth / 8),"sdf2d_to_img");
569   if (rowp == NULL)
570     return;
571
572   if ((fp = fopen (outfile, "wb")) == NULL)
573      return;
574
575   png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
576   if (! png_ptr)
577     return;
578
579   info_ptr = png_create_info_struct(png_ptr);
580   if (! info_ptr)
581     {
582       png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
583       fclose(fp);
584       return;
585     }
586
587   if (setjmp(png_ptr->jmpbuf))
588     {
589       png_destroy_write_struct(&png_ptr, &info_ptr);
590       fclose(fp);
591       return;
592     }
593
594   png_init_io(png_ptr, fp);
595
596   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);
597
598   png_write_info(png_ptr, info_ptr);
599   for (irow = ny - 1; irow >= 0; irow--)
600     {
601       png_bytep row_pointer = rowp;
602       int icol, ir;
603
604       for (icol = 0; icol < nx; icol++)
605         {
606           double dens;
607           int p;
608           unsigned int outval;
609           int pos = icol * nxcell;
610           dens = (v[icol][irow] - densmin) / (densmax - densmin);
611           if (dens < 0)
612             dens = 0;
613           else if (dens > 1)
614             dens = 1;
615           outval = (unsigned int) (dens * max_out_level);
616           for (p = pos; p < pos + nxcell; p++)
617                 {
618                 if (bitdepth == 8)
619                     rowp[p] = outval;
620                 else {
621                     int rowpos = p * 2;
622                     rowp[rowpos] = (outval >> 8) & 0xFF;
623                     rowp[rowpos+1] = (outval & 0xFF);
624                 }
625             }
626         }
627       for (ir = 0; ir < nycell; ir++)
628         {
629           png_write_rows (png_ptr, &row_pointer, 1);
630         }
631     }
632   sys_free(rowp, "if2img");
633
634   png_write_end(png_ptr, info_ptr);
635   png_destroy_write_struct(&png_ptr, &info_ptr);
636
637   fclose(fp);
638 }
639 #endif
640
641 #ifdef HAVE_GD
642 #include "gd.h"
643 #define N_GRAYSCALE 256
644 #endif
645
646 void
647 sdf2d_to_gif (ImageFile& im, char *outfile, int nxcell, int nycell, double densmin, double densmax)
648 {
649 #ifdef HAVE_GD
650   gdImagePtr gif;
651   FILE *out;
652   int gs_indices[N_GRAYSCALE];
653   int i;
654   int irow;
655   int lastrow;
656   unsigned char *rowp;
657   int nx = im.nx();
658   int ny = im.ny();
659   ImageFileArray v = im.getArray();
660
661   rowp = (unsigned char *) sys_alloc(nx * nxcell,"sdf2d_to_img");
662   if (rowp == NULL)
663     return;
664
665   gif = gdImageCreate(nx * nxcell, ny * nycell);
666   for (i = 0; i < N_GRAYSCALE; i++)
667     gs_indices[i] = gdImageColorAllocate(gif, i, i, i);
668
669   lastrow = ny * nycell - 1;
670   for (irow = 0; irow < ny; irow++)
671     {
672       int icol, ir;
673       int rpos = irow * nycell;
674       for (ir = rpos; ir < rpos + nycell; ir++)
675         {
676           for (icol = 0; icol < nx; icol++)
677             {
678               double dens;
679               int ic;
680               int cpos = icol * nxcell;
681               dens = (v[icol][irow] - densmin) / (densmax - densmin);
682               if (dens < 0)
683                 dens = 0;
684               else if (dens > 1)
685                 dens = 1;
686               for (ic = cpos; ic < cpos + nxcell; ic++)
687                 {
688                   rowp[ic] = (unsigned int) (dens * (double) (N_GRAYSCALE - 1));
689                   gdImageSetPixel(gif, ic, lastrow - ir, gs_indices[rowp[ic]]);
690                 }
691             }
692         }
693     }
694   sys_free(rowp, "if2img");
695
696   if ((out = fopen(outfile,"w")) == NULL)
697     {
698       fprintf(stderr,"Error opening output file %s for writing\n", outfile);
699       exit(1);
700     }
701   gdImageGif(gif,out);
702   fclose(out);
703   gdImageDestroy(gif);
704 #else
705   fprintf(stderr, "This version does not support GIF");
706 #endif
707 }
708
709 #ifndef NO_MAIN
710 int 
711 main (int argc, char *const argv[])
712 {
713   return (if2img_main(argc, argv));
714 }
715 #endif