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