1 #include "SUMA_suma.h"
2 
SUMA_MakeColorMap_usage()3 void SUMA_MakeColorMap_usage ()
4    {
5       static char FuncName[]={"SUMA_MakeColorMap_usage"};
6       char * s = NULL;
7 
8       SUMA_ENTRY;
9 
10       s = SUMA_help_basics();
11       fprintf (SUMA_STDOUT,
12 "\n"
13  "Usage1: \n"
14  "MakeColorMap <-fn Fiducials_Ncol> [-pos] [-ah prefix] [-h/-help]\n"
15  "    Creates a colormap of N colors that contains the fiducial colors.\n"
16  "    -fn Fiducials_Ncol: Fiducial colors and their indices in the color\n"
17  "                        map are listed in file Fiducials_Ncol.\n"
18  "       Each row contains 4 tab delimited values:\n"
19  "       R G B i\n"
20  "       R G B values are between 0 and 1 and represent the \n"
21  "       i-th color in the colormap. i should be between 0 and\n"
22  "       N-1, N being the total number of colors in the colormap.\n"
23  "\n"
24  "Usage2: \n"
25  "MakeColorMap <-f Fiducials> <-nc N> [-sl] [-ah prefix] [-h/-help]\n"
26  "    Creates a colormap of N colors that contains the fiducial colors.\n"
27  "    -f Fiducials:  Fiducial colors are listed in an ascii file Fiducials. \n"
28  "       Each row contains 3 tab delimited R G B values between 0 and 1.\n"
29  "    -nc N: Total number of colors in the color map.\n"
30  "    -sl: (optional, default is NO) if used, the last color in the Fiducial \n"
31  "       list is omitted. This is useful in creating cyclical color maps.\n"
32  "\n"
33  "Usage3: \n"
34  "MakeColorMap <-std MapName>\n"
35  "    Returns one of SUMA's standard colormaps. Choose from:\n"
36  "    rgybr20, ngray20, gray20, bw20, bgyr19, \n"
37  "    matlab_default_byr64, roi128, roi256, roi64\n"
38  " or if the colormap is in a .pal file:  \n"
39  "MakeColorMap -cmapdb Palfile -cmap MapName\n"
40  "\n"
41  "Usage4:\n"
42  "MakeColorMap <-fscolut lbl0 lbl1> \n"
43  "             [<-fscolutfile FS_COL_LUT>]\n"
44  "   Create AFNI/SUMA colormaps of FreeSurfer colors\n"
45  "   indexed between lbl0 and lbl1. \n"
46  "   -fscolut lbl0 lbl1: Get colors indexed between\n"
47  "                        lbl0 and lbl1, non existing\n"
48  "                        integer labels are given a \n"
49  "                        gray color. Use -fscolut -1 -1 to\n"
50  "                        get all the colors and labels.\n"
51  "   -fscolutfile FS_COL_LUT: Use color LUT file FS_COL_LUT\n"
52  "                            Default is to use \n"
53  "                            $FREESURFER_HOME/FreeSurferColorLUT.txt\n"
54  "   -show_fscolut: Show all of the LUT\n"
55  "\n"
56  "Common options to all usages:\n"
57  "    -ah prefix: (optional, Afni Hex format.\n"
58  "                 default is RGB values in decimal form)\n"
59  "       use this option if you want a color map formatted to fit \n"
60  "       in AFNI's .afnirc file. The colormap is written out as \n"
61  "      prefix_01 = #xxxxxxx \n      prefix_02 = #xxxxxxx\n       etc...\n"
62  "    -ahc prefix: optional, Afni Hex format, ready to go into.\n"
63  "                 pbardefs.h \n"
64  "    -h or -help: displays this help message.\n"
65  "    -flipud: Flip the map upside down. If the colormap is being \n"
66  "             created for interactive loading into SUMA with the 'New'\n"
67  "             button from the 'Surface Controller' you will need\n"
68  "             to flip it upside down. \n"
69  "    -usercolutfile USER_COL_LUT: A user's own color lookup file.\n"
70  "             The format of the file is similar to FreeSurfer's ColorLUT.txt\n"
71  "             It is an ascii file whith each line containing the following:\n"
72  "                Key   R  G  B  A  Label\n"
73  "             With Key being an integer color/region identifier,\n"
74  "             Label is the string identifier and R,G,B,A are the colors \n"
75  "             and alpha values either between 0 and 1, or 0 and 255.\n"
76  "             Alpha values are ignored at the moment, but they must be \n"
77  "             in the file.\n"
78  "    -suma_cmap: write colormap in SUMA's format\n"
79  "    -sdset DSET: Add colormap to surface-based dataset DSET, making it a\n"
80  "                 Labeled data set, which gets special treatment in SUMA.\n"
81  "                 A labeled data set can only have one value per node.\n"
82  "    -sdset_prefix DSET_PREF: Prefix of dset for writing labeled version\n"
83  "                             of DSET. Without it, the new name is based on\n"
84  "                             DSET's name\n"
85  "\n"
86  "Example Usage 1: Creating a colormap of 20 colors that goes from \n"
87  "Red to Green to Blue to Yellow to Red.\n"
88  "\n"
89  "   The file FidCol_Nind contains the following:\n"
90  "   1 0 0 0\n   0 1 0 5\n   0 0 1 10\n   1 1 0 15\n   1 0 0 19\n"
91  "\n"
92  "   The following command will generate the RGB colormap in decimal form:\n"
93  "   MakeColorMap -fn FidCol_Nind \n"
94  "\n"
95  "   The following command will generate the colormap and write it as \n"
96  "   an AFNI color palette file:\n"
97  "   MakeColorMap -fn FidCol_Nind -ah TestPalette > TestPalette.pal\n"
98  "\n"
99  "Example Usage 2: Creating a cyclical version of the colormap in usage 1:\n"
100  "\n"
101  "   The file FidCol contains the following:\n"
102  "   1 0 0\n   0 1 0\n   0 0 1\n   1 1 0\n   1 0 0\n"
103  "\n"
104  "   The following command will generate the RGB colormap in decimal form:\n"
105  "   MakeColorMap -f FidCol -sl -nc 20 \n"
106  "\n"
107  "Example Usage 3: \n"
108  "   MakeColorMap -std ngray20 \n"
109  "\n"
110  "Example Usage 4: \n"
111  "   MakeColorMap -fscolut 0 255\n"
112  "\n"
113  "Example Usage 5: Make your own colormap and add it to a surface-based dset\n"
114  "   Say you have your own color lookup table formatted much like FreeSurfer's\n"
115  "   color lookup files. The content of this sample colut.txt file is:\n"
116  "    #integer label    String Label      R    G    B    A\n"
117  "     1                Big_House         0.3  0.1  1    1\n"
118  "     2                Small_Face        1    0.2  0.4  1\n"
119  "     3                Electric          1    1    0    1\n"
120  "     4                Atomic            0.1  1    0.3  1\n"
121  "\n"
122  "   The command to create a SUMA formatted colormap would be:\n"
123  "       MakeColorMap -usercolutfile colut.txt -suma_cmap toylut \n"
124  "\n"
125  "   You can attach the colormap to a surface-based datatset with \n"
126  "   ConvertDset's -labelize option, or you can also do it here in one\n"
127  "   pass with:\n"
128  "       MakeColorMap -usercolutfile colut.txt -suma_cmap toylut \\\n"
129  "                    -sdset you_look_marvellous.niml.dset\n"
130  "\n"
131  "Adding a new colormap into AFNI:"
132  "To read in a new colormap into AFNI, either paste the contents of \n"
133  "TestPalette.pal in your .afnirc file or read the .pal file using \n"
134  "AFNI as follows:\n"
135  "1- run afni\n2- Define Function --> right click on Inten (over colorbar) \n"
136  "   --> Read in palette (choose TestPalette.pal)\n"
137  "3- set the #colors chooser (below colorbar) to 20 (the number of colors in \n"
138  "   TestPalette.pal).\n"
139                             "%s",s);
140       SUMA_free(s); s = NULL;
141       s = SUMA_New_Additions(0, 1); printf("%s\n", s);SUMA_free(s); s = NULL;
142       fprintf (SUMA_STDOUT, "    Ziad S. Saad & Rick R. Reynolds SSCC/NIMH/NIH saadz@mail.nih.gov    Tue Apr 23 14:14:48 EDT 2002\n\n");
143 
144       SUMA_RETURNe;
145    }
146 
main(int argc,char * argv[])147 int main (int argc,char *argv[])
148 {/* Main */
149    static char  FuncName[]={"MakeColorMap"};
150    char  *fscolutname = NULL, *FidName = NULL,
151          *Prfx = NULL, h[9], *StdType=NULL, *dbfile=NULL, *MapName=NULL;
152    int Ncols = 0, N_Fid = 0, kar, i, ifact, *Nind = NULL,
153        imap = -1, MapSpecified = 0;
154    int fsbl0, fsbl1, showfscolut, exists=0;
155    float **Fid=NULL, **M=NULL;
156    MRI_IMAGE *im = NULL;
157    float *far=NULL;
158    int AfniHex=0, freesm;
159    int suc, idISi=0;
160    char stmp[256], *s=NULL, *ooo=NULL, *sdset_prefix;
161    SUMA_PARSED_NAME *sname=NULL;
162    NI_group *ngr=NULL;
163    SUMA_Boolean   brk, SkipLast, PosMap,
164                Usage1, Usage2, Usage3, Usage4, flipud, fscolut,
165                LocalHead = NOPE;
166    SUMA_COLOR_MAP *SM=NULL;
167    SUMA_DSET_FORMAT iform;
168    SUMA_DSET *sdset=NULL;
169 
170    SUMA_STANDALONE_INIT;
171 
172    SUMA_mainENTRY;
173 
174 
175 
176    if (argc < 2) {
177       SUMA_MakeColorMap_usage();
178       exit (0);
179    }
180 
181    kar = 1;
182    freesm = 1;
183    fscolutname = NULL;
184    fsbl0 = -1;
185    fsbl1 = -1;
186    brk = NOPE;
187    SkipLast = NOPE;
188    AfniHex = 0;
189    PosMap = NOPE;
190    Usage1 = NOPE;
191    Usage2 = NOPE;
192    Usage3 = NOPE;
193    Usage4 = NOPE;
194    flipud = NOPE;
195    fscolut = NOPE;
196    showfscolut = 0;
197    MapSpecified = NOPE;
198    idISi=0;
199    iform = SUMA_NO_DSET_FORMAT;
200    sdset_prefix=NULL;
201    while (kar < argc) { /* loop accross command ine options */
202       if (strcmp(argv[kar], "-h") == 0 || strcmp(argv[kar], "-help") == 0) {
203          SUMA_MakeColorMap_usage();
204          exit (0);
205       }
206 
207       SUMA_SKIP_COMMON_OPTIONS(brk, kar);
208 
209       if (!brk && (strcmp(argv[kar], "-v") == 0))
210       {
211          LocalHead = NOPE;
212          brk = YUP;
213       }
214       if (!brk && (strcmp(argv[kar], "-flipud") == 0))
215       {
216          flipud = YUP;
217          brk = YUP;
218       }
219       if (!brk && (strcmp(argv[kar], "-f") == 0))
220       {
221          kar ++;
222          if (kar >= argc)  {
223               fprintf (SUMA_STDERR, "need argument after -f ");
224             exit (1);
225          }
226          FidName = argv[kar];
227          Usage1 = YUP;
228          brk = YUP;
229       }
230 
231       if (!brk && (strcmp(argv[kar], "-fscolutfile") == 0))
232       {
233          Usage4=YUP;
234          kar ++;
235          if (kar >= argc)  {
236               fprintf (SUMA_STDERR, "need 1 argument after -fscolutfile ");
237             exit (1);
238          }
239          fscolutname = argv[kar];
240          if (fsbl0 < 0) {
241             fsbl0 = 0;
242             fsbl1 = 255;
243          }
244          brk = YUP;
245       }
246       if (!brk && (strcmp(argv[kar], "-usercolutfile") == 0))
247       {
248          Usage4=YUP;
249          kar ++;
250          if (kar >= argc)  {
251               fprintf (SUMA_STDERR, "need 1 argument after -fscolutfile ");
252             exit (1);
253          }
254          fscolutname = argv[kar];
255          if (fsbl0 < 0) {
256             fsbl0 = 0;
257             fsbl1 = -1;
258          }
259          idISi=1;
260          brk = YUP;
261       }
262       if (!brk && (strcmp(argv[kar], "-fscolut") == 0))
263       {
264          fscolut = YUP;
265          Usage4=YUP;
266          kar ++;
267          if (kar+1 >= argc)  {
268               fprintf (SUMA_STDERR, "need 2 arguments after -fscolut ");
269             exit (1);
270          }
271          fsbl0 = atoi(argv[kar]); ++kar;
272          fsbl1 = atoi(argv[kar]);
273          if (fsbl0 > fsbl1 || fsbl0 < -1 || fsbl1 > 10000) {
274             SUMA_S_Errv("-fscolut values of %d and %d either\n"
275                         "do not make sense or exceed range 0 to 10000\n",
276                         fsbl0, fsbl1);
277             exit(1);
278          }
279          brk = YUP;
280       }
281       if (!brk && (strcmp(argv[kar], "-show_fscolut") == 0))
282       {
283          showfscolut = 1;
284          brk = YUP;
285       }
286       if (!brk && (strcmp(argv[kar], "-fn") == 0))
287       {
288          kar ++;
289          if (kar >= argc)  {
290               fprintf (SUMA_STDERR, "need argument after -fn ");
291             exit (1);
292          }
293          FidName = argv[kar];
294          Usage2 = YUP;
295          brk = YUP;
296       }
297 
298       if (!brk && (strcmp(argv[kar], "-nc") == 0))
299       {
300          kar ++;
301          if (kar >= argc)  {
302               fprintf (SUMA_STDERR, "need argument after -nc ");
303             exit (1);
304          }
305          Ncols = atoi(argv[kar]);
306          Usage1 = YUP;
307          brk = YUP;
308       }
309 
310       if (!brk && (strcmp(argv[kar], "-ah") == 0))
311       {
312          kar ++;
313          if (kar >= argc)  {
314               fprintf (SUMA_STDERR, "need argument after -ah ");
315             exit (1);
316          }
317          Prfx = argv[kar];
318          AfniHex = 1;
319          brk = YUP;
320       }
321 
322       if (!brk && (strcmp(argv[kar], "-ahc") == 0))
323       {
324          kar ++;
325          if (kar >= argc)  {
326               fprintf (SUMA_STDERR, "need argument after -ahc ");
327             exit (1);
328          }
329          Prfx = argv[kar];
330          AfniHex = 2;
331          brk = YUP;
332       }
333       if (!brk && (strcmp(argv[kar], "-suma_cmap") == 0))
334       {
335          kar ++;
336          if (kar >= argc)  {
337               fprintf (SUMA_STDERR, "need argument after -suma_cmap");
338             exit (1);
339          }
340          Prfx = argv[kar];
341          AfniHex = 3;
342          brk = YUP;
343       }
344 
345       if (!brk && (strcmp(argv[kar], "-std") == 0))
346       {
347          kar ++;
348          if (MapSpecified) {
349             SUMA_S_Err( "Color map already specified.\n"
350                         "-cmap and -std are mutually exclusive\n");
351             exit (1);
352          }
353          if (kar >= argc)  {
354               fprintf (SUMA_STDERR, "need argument after -std ");
355             exit (1);
356          }
357          MapSpecified = YUP;
358          StdType = argv[kar];
359          Usage3 = YUP;
360          brk = YUP;
361       }
362 
363       if (!brk && (strcmp(argv[kar], "-cmapdb") == 0))
364       {
365          kar ++;
366          if (kar >= argc)  {
367               fprintf (SUMA_STDERR, "need argument after -cmapdb ");
368             exit (1);
369          }
370          SUMAg_CF->isGraphical = YUP;
371                         /* WILL NEED X DISPLAY TO RESOLVE COLOR NAMES */
372          dbfile = argv[kar];
373          brk = YUP;
374       }
375 
376       if (!brk && (strcmp(argv[kar], "-cmap") ==0)) {
377          if (MapSpecified) {
378             SUMA_S_Err( "Color map already specified.\n"
379                         "-cmap and -std are mutually exclusive\n");
380             exit (1);
381          }
382          MapSpecified = YUP;
383          kar ++;
384          if (kar >= argc)  {
385             fprintf (SUMA_STDERR, "need 1 arguments after -cmap ");
386             exit (1);
387          }
388          Usage3 = YUP;
389          MapName = argv[kar];
390          brk = YUP;
391       }
392 
393       if (!brk && (strcmp(argv[kar], "-sl") == 0))
394       {
395          SkipLast = YUP;
396          brk = YUP;
397       }
398 
399       if (!brk && (strcmp(argv[kar], "-pos") == 0))
400       {
401          /* obsolete */
402          PosMap = YUP;
403 
404          brk = YUP;
405       }
406 
407       if (!brk && (strcmp(argv[kar], "-sdset") == 0)) {
408          kar ++;
409          if (kar >= argc)  {
410             fprintf (SUMA_STDERR, "need surface dataset after -sdset \n");
411             exit (1);
412          }
413          iform = SUMA_NO_DSET_FORMAT;
414          if (!(sdset = SUMA_LoadDset_s (argv[kar], &iform, 0))) {
415             SUMA_S_Err("Failed to load surface dset");
416             exit(1);
417          }
418          brk = YUP;
419       }
420       if (!brk && (strcmp(argv[kar], "-sdset_prefix") == 0)) {
421          kar ++;
422          if (kar >= argc)  {
423             fprintf (SUMA_STDERR, "need prefix dataset after -sdset_prefix \n");
424             exit (1);
425          }
426          sdset_prefix = argv[kar];
427          brk = YUP;
428       }
429       if (!brk) {
430          SUMA_S_Errv("Option %s not understood. Try -help for usage\n",
431                      argv[kar]);
432          suggest_best_prog_option(argv[0], argv[kar]);
433          exit (1);
434       } else {
435          brk = NOPE;
436          kar ++;
437       }
438 
439    }/* loop accross command ine options */
440 
441    /* check input */
442    if (  (Usage1 && (Usage2 || Usage3 || Usage4)) ||
443          (Usage2 && (Usage1 || Usage3 || Usage4)) ||
444          (Usage3 && (Usage1 || Usage2 || Usage4)) ||
445          (Usage4 && (Usage1 || Usage2 || Usage3)) ) {
446       SUMA_S_Err("Mixing options from multiple usage modes.\n");
447       exit(1);
448    }
449 
450    if (!Usage1 && !Usage2 && !Usage3 && !Usage4) {
451       SUMA_S_Err("One of these options must be used:\n"
452                            "-f, -fn,  -std, or -fscolut.\n");
453       exit(1);
454    }
455 
456    /* are there database files to read */
457    if (dbfile) {
458       SUMA_LH("Now trying to read db file");
459       if (!SUMAg_CF->scm) {
460          SUMAg_CF->scm = SUMA_Build_Color_maps();
461          if (!SUMAg_CF->scm) {
462             SUMA_SL_Err("Failed to build color maps.\n");
463             exit(1);
464          }
465       }
466       if (SUMA_AFNI_Extract_Colors ( dbfile, SUMAg_CF->scm ) < 0) {
467          SUMA_S_Errv("Failed to read %s colormap file.\n", dbfile);
468          exit(1);
469       }
470    }
471 
472    if (Usage1 || Usage2) {
473       if (!SUMA_filexists (FidName)) {
474          SUMA_S_Errv("File %s could not be found.\n", FidName);
475          exit(1);
476       }
477 
478       /* read the fiducials file */
479       im = mri_read_1D (FidName);
480       if (!im) {
481          SUMA_S_Err("Failed to read file");
482          exit(1);
483       }
484 
485       far = MRI_FLOAT_PTR(im);
486       N_Fid = im->nx * im->ny;
487    }
488 
489    if (PosMap) {
490       fprintf (SUMA_STDERR,"\nWarning %s: -pos option is obsolete.\n", FuncName);
491    }
492 
493 
494    /* allocate for fiducials */
495    if (Usage1) {
496       if (N_Fid % 3) {
497          fprintf (SUMA_STDERR,
498                   "Error %s: Not all rows in %s appear to have RGB triplets.\n",
499                   FuncName, FidName);
500          exit (1);
501       }
502 
503       Fid = (float **) SUMA_allocate2D (N_Fid / 3, 3, sizeof(float));
504       if (Fid == NULL) {
505          fprintf (SUMA_STDERR,
506                   "Error %s: Could not allocate for Fid.\n", FuncName);
507          exit(1);
508       }
509 
510       for (i=0; i < im->nx; ++i) {
511          Fid[i][0] = far[i];
512          Fid[i][1] = far[i+im->nx];
513          Fid[i][2] = far[i+2*im->nx];
514       }
515 
516       mri_free(im); im = NULL;
517       /* now create the color map */
518       SM = SUMA_MakeColorMap (Fid, N_Fid/3, 0, Ncols, SkipLast, FuncName);
519       if (SM == NULL) {
520          fprintf (SUMA_STDERR,
521                   "Error %s: Error in SUMA_MakeColorMap.\n", FuncName);
522          exit(1);
523       }
524    }
525    if (Usage2) { /* second usage */
526       if (N_Fid % 4) {
527          fprintf (SUMA_STDERR,
528                   "Error %s: Not all rows in %s appear to have "
529                   "RGB N quadruplets.\n", FuncName, FidName);
530          exit (1);
531       }
532 
533       Fid = (float **) SUMA_allocate2D (N_Fid / 4, 3, sizeof(float));
534       Nind = (int *) SUMA_calloc (N_Fid/4, sizeof(int));
535       if (Fid == NULL || !Nind) {
536          fprintf (SUMA_STDERR,
537                   "Error %s: Could not allocate for Fid or Nind.\n", FuncName);
538          exit(1);
539       }
540 
541       for (i=0; i < im->nx; ++i) {
542          Fid[i][0] = far[i];
543          Fid[i][1] = far[i+im->nx];
544          Fid[i][2] = far[i+2*im->nx];
545          Nind[i] = (int)far[i+3*im->nx];
546       }
547 
548       mri_free(im); im = NULL;
549 
550       /* now create the color map */
551       SM = SUMA_MakeColorMap_v2 (Fid, N_Fid/4, 0, Nind, SkipLast, FuncName);
552       if (SM == NULL) {
553          fprintf (SUMA_STDERR,
554                   "Error %s: Error in SUMA_MakeColorMap.\n", FuncName);
555          exit(1);
556       }
557       Ncols = SM->N_M[0];
558    }
559 
560    if (Usage3) { /* third usage */
561       if (!MapName) {
562          SM = SUMA_FindNamedColMap (StdType);
563          freesm = 0;
564          if (SM == NULL) {
565             fprintf (SUMA_STDERR,
566                      "Error %s: Error in SUMA_MakeColorMap.\n", FuncName);
567             exit(1);
568          }
569          Ncols = SM->N_M[0];
570       } else {
571          imap = SUMA_Find_ColorMap ( MapName, SUMAg_CF->scm->CMv,
572                                      SUMAg_CF->scm->N_maps, -2);
573          if (imap < 0) {
574             fprintf (SUMA_STDERR,
575                      "Error %s: Could not find colormap %s.\n",
576                      FuncName, MapName);
577             exit (1);
578          }
579          SM = SUMAg_CF->scm->CMv[imap];
580          Ncols = SM->N_M[0];
581       }
582    }
583 
584    if (Usage4) { /* 4th usage */
585       if (!(SM = SUMA_FScolutToColorMap(fscolutname, fsbl0,
586                                          fsbl1, showfscolut, idISi))) {
587          SUMA_S_Err("Failed to get FreeSurfer colormap.");
588          exit(1);
589       }
590       Ncols = SM->N_M[0];
591    }
592 
593    if (flipud) {
594       SUMA_Flip_Color_Map (SM);
595    }
596 
597    M = SM->M;
598 
599    if (AfniHex && Ncols > 20) {
600       if (!Usage4) {
601          SUMA_S_Note("Writing colormap in colorscale format.\n");
602       }
603    }
604 
605 
606 
607    if (!AfniHex) {
608          SUMA_disp_mat (M, Ncols, 3, 1);
609          /*SUMA_Show_ColorMapVec (&SM, 1, NULL, 2);*/
610    } else {
611          if (Usage4 || Ncols > 20) {
612             if (AfniHex == 1) {
613                fprintf (stdout, "%s \n", Prfx);
614                for (i=0; i < Ncols; ++i) {
615 
616                   /* Now create the hex form */
617                   r_sprintf_long_to_hex (h,
618                         (unsigned long)rint((M[i][0]*255)), 1, 0);
619                   fprintf (stdout, "#%s", h);
620 
621                   r_sprintf_long_to_hex (h,
622                         (unsigned long)rint((M[i][1]*255)), 1, 0);
623                   fprintf (stdout, "%s", h);
624 
625                   r_sprintf_long_to_hex (h,
626                         (unsigned long)rint((M[i][2]*255)), 1, 0);
627                   fprintf (stdout, "%s \n", h);
628                }
629                 fprintf (stdout, "\n") ;
630             } else if (AfniHex == 2){  /* to go in the C code
631                               (see pbardef.h and pbar.c)*/
632                char *p2 = SUMA_copy_string(Prfx);
633                SUMA_TO_UPPER(p2);
634                fprintf (stdout, "static char %s[] = {\n   \"%s \"\n   \"",
635                   p2, Prfx); SUMA_free(p2); p2 = NULL;
636                for (i=0; i < Ncols; ++i) {
637                   if (i) {
638                      if (!(i % 4)) { fprintf (stdout, " \"\n   \""); }
639                      else { fprintf (stdout, " "); }
640                   }
641                   /* Now create the hex form */
642                   r_sprintf_long_to_hex (h,
643                         (unsigned long)rint((M[i][0]*255)), 1, 0);
644                   fprintf (stdout, "#%s", h);
645 
646                   r_sprintf_long_to_hex (h,
647                         (unsigned long)rint((M[i][1]*255)), 1, 0);
648                   fprintf (stdout, "%s", h);
649 
650                   r_sprintf_long_to_hex (h,
651                         (unsigned long)rint((M[i][2]*255)), 1, 0);
652                   fprintf (stdout, "%s", h);
653                }
654                 fprintf (stdout, " \"\n};\n") ;
655             } else if (AfniHex == 3){
656                SUMA_LHv("Now turn %s to niml\n", SM->Name);
657                sname = SUMA_ParseFname(Prfx, NULL);
658                snprintf(stmp, 128*sizeof(char),
659                         "file:%s.niml.cmap", sname->FileName_NoExt);
660                if (SM->Name) SUMA_free(SM->Name);
661                SM->Name = SUMA_copy_string(sname->FileName_NoExt);
662                ngr = SUMA_CmapToNICmap(SM);
663                NEL_WRITE_TX(ngr, stmp, suc);
664                if (!suc) {
665                   SUMA_S_Errv("Failed to write %s\n", stmp);
666                }
667                SUMA_Free_Parsed_Name(sname); sname = NULL;
668             } else {
669                SUMA_S_Err("AfniHex should be 0, 1, or 2\n");
670                exit(1);
671             }
672          } else {
673             fprintf (stdout, "\n***COLORS\n");
674             for (i=0; i < Ncols; ++i) {
675                /* Now create the hex form */
676                r_sprintf_long_to_hex (h,
677                      (unsigned long)rint((M[i][0]*255)), 1, 0);
678                if (i<10) fprintf (stdout, "%s_0%d = #%s", Prfx, i, h);
679                   else fprintf (stdout, "%s_%d = #%s", Prfx, i, h);
680 
681                r_sprintf_long_to_hex (h,
682                      (unsigned long)rint((M[i][1]*255)), 1, 0);
683                fprintf (stdout, "%s", h);
684 
685                r_sprintf_long_to_hex (h,
686                      (unsigned long)rint((M[i][2]*255)), 1, 0);
687                fprintf (stdout, "%s\n", h);
688             }
689 
690             /* color map */
691 
692             fprintf (stdout, "\n***PALETTES %s [%d]\n//1 to -1 range\n",
693                      Prfx, Ncols);
694             ifact = 2;
695             for (i=0; i < Ncols; ++i) {
696                fprintf (stdout, "%f -> ", 1.0 - (float)(ifact*i)/Ncols);
697                if (i<10) fprintf (stdout, "%s_0%d\n", Prfx, i);
698                   else fprintf (stdout, "%s_%d\n", Prfx, i);
699             }
700             fprintf (stdout,
701                      "\n***PALETTES %s [%d+]\n//1 to 0 range\n", Prfx, Ncols);
702             ifact = 1;
703             for (i=0; i < Ncols; ++i) {
704                fprintf (stdout, "%f -> ", 1.0 - (float)(ifact*i)/Ncols);
705                if (i<10) fprintf (stdout, "%s_0%d\n", Prfx, i);
706                   else fprintf (stdout, "%s_%d\n", Prfx, i);
707             }
708          }
709    }
710 
711    /* free allocated space */
712    if (Usage1)  {
713       if (Fid) SUMA_free2D((char **)Fid, N_Fid / 3);
714    } else {
715       if (Fid) SUMA_free2D((char **)Fid, N_Fid / 4);
716       if (Nind) SUMA_free(Nind);
717    }
718 
719    /* add colormap to a surface dset ? */
720    if (sdset) {
721       SUMA_DSET *idset;
722       if (!SUMA_is_AllConsistentCastType_dset(sdset, SUMA_int)) {
723          idset = SUMA_CoercedCopyofDset(sdset, SUMA_int, NULL);
724       } else {
725          idset = sdset;
726       }
727       if (!(SUMA_dset_to_Label_dset_cmap(idset, SM))) {
728          SUMA_S_Err("Failed to make change");
729          exit(1);
730       }
731       s = SUMA_OutputDsetFileStatus(
732          sdset_prefix?sdset_prefix:SDSET_FILENAME(sdset),
733                                  NULL, &iform,
734                                  NULL, ".lbl", &exists);
735       SUMA_AddNgrHist(sdset->ngr, FuncName, argc, argv);
736       ooo = SUMA_WriteDset_s(s, idset, iform,
737                         THD_ok_overwrite(), 0);
738       SUMA_free(ooo); ooo=NULL; SUMA_free(s); s = NULL;
739 
740       if (idset != sdset) SUMA_FreeDset(idset);
741       SUMA_FreeDset(sdset); sdset=NULL;
742    }
743    if (SM && !MapName && freesm) SUMA_Free_ColorMap(SM);
744    if (!SUMA_Free_CommonFields(SUMAg_CF)) {
745       SUMA_SL_Err("Failed to free commonfields.");
746    }
747 
748    SUMA_RETURN (0);
749 }
750