home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Science / Science.zip / gmt_os2.zip / src / makecpt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-08  |  9.7 KB  |  301 lines

  1. /*--------------------------------------------------------------------
  2.  *    The GMT-system:    @(#)makecpt.c    2.12  08 Jun 1995
  3.  *
  4.  *    Copyright (c) 1991-1995 by P. Wessel and W. H. F. Smith
  5.  *    See README file for copying and redistribution conditions.
  6.  *--------------------------------------------------------------------*/
  7. /*
  8.  * makecpt -C<cont_int_or_file> -S<v1_color_table> [-M<mid_val>]
  9.  *
  10.  * Read a v1.0 color table format, and a contour interval or file
  11.  * of contours, [and a mid-value for contour interval], and produce
  12.  * a v2.0 cpt file.
  13.  * Designed to help people convert from GMTv1 to GMTv2.0 syntax.
  14.  *
  15.  * Author:    Walter H. F. Smith
  16.  * Date:    20 June 1991-1995
  17.  */
  18.  
  19. #include "gmt.h"
  20. #include <time.h>
  21.  
  22. struct    CTABLE {
  23.     int    r;
  24.     int    g;
  25.     int    b;
  26. };
  27.  
  28.  
  29. main(argc,argv)
  30. int    argc;
  31. char    **argv;
  32. {
  33.  
  34.     int    i, j, ic, is, ntable, nconts, nshades = -1, nfact, bg, fg;
  35.     BOOLEAN    error = FALSE, makehues = FALSE, continuous = FALSE;
  36.     double    cint = -1.0, midval = 0.0, start_hue, delta_hue, hue, sat, val, *contour, start_cont;
  37.     double    h1, s1, v1, h2, s2, v2;
  38.     FILE    *fpc = NULL, *fps = NULL;
  39.     struct    CTABLE    *ctable;
  40.     time_t    clock;
  41.     char    buffer[120];
  42.  
  43.     argc = gmt_begin(argc, argv);
  44.     
  45.     for (i = 1; i < argc; i++) {
  46.         if (argv[i][0] == '-') {
  47.             switch(argv[i][1]) {
  48.                 case 'V':
  49.                 case '\0':
  50.                     error += get_common_args(argv[i], 0, 0, 0, 0);
  51.                     break;
  52.                 case 'C':
  53.                     ic = i;
  54.                     fpc = fopen(&argv[i][2], "r");
  55.                     if (fpc == NULL) cint = atof(&argv[i][2]);
  56.                     break;
  57.                 case 'M':
  58.                     midval = atof(&argv[i][2]);
  59.                     break;
  60.                 case 'S':
  61.                     is = i;
  62.                     fps = fopen(&argv[i][2], "r");
  63.                     if (fps == NULL) {
  64.                         j = 2;
  65.                         while(argv[i][j]) j++;
  66.                         if (argv[i][j-1] == 'c' || argv[i][j-1] == 'C') makehues = TRUE;
  67.                         nshades = atoi(&argv[i][2]);
  68.                     }
  69.                     break;
  70.                 case 'Z':
  71.                     continuous = TRUE;
  72.                     break;
  73.                 default:
  74.                     error = TRUE;
  75.                     gmt_default_error (argv[i][1]);
  76.                     break;
  77.             }
  78.         }
  79.         else {
  80.             fprintf (stderr, "%s: GMT SYNTAX ERROR:  Options must start with a hyphen\n", gmt_program);
  81.             error = TRUE;
  82.         }
  83.     }
  84.  
  85.     if (argc == 1 || gmt_quick) {
  86.         fprintf(stderr,"makecpt %s - Make GMT color palette tables\n\n", GMT_VERSION);
  87.         fprintf(stderr,"usage:  makecpt -C<cint_or_file> -S<nshades_or_file>[c] [-M<midval>] [-Z]\n\n");
  88.         if (gmt_quick) exit(-1);
  89.         fprintf(stderr,"\t-C If <cint_or_file> can be opened as a file, it is read to get contour intervals;\n");
  90.         fprintf(stderr,"\t   else, <cint_or_file> should be a positive number, the contour interval desired.\n");
  91.         fprintf(stderr,"\t-S If <nshades_or_file> can be opened as a file, it is read as a V1-style color table;\n");
  92.         fprintf(stderr,"\t   else, <nshades_or_file> should be the # of colors/grayshades desired.  In this case,.\n");
  93.         fprintf(stderr,"\t   you may append c after the number to make a color table or default to a gray table.\n");
  94.         fprintf(stderr,"\t-M If you gave a number for -C, you may set the contour value at the middle of the range\n");
  95.         fprintf(stderr,"\t   to <midval>.  [Default is zero; and ignored if -C gives a file.]\n");
  96.         fprintf(stderr,"\t-Z will create a continuous color palette.  Ignored if c_int and n_shades are files\n");
  97.         fprintf(stderr,"\t   [Default is discontinuous, i.e., color is constant for each interval]\n");
  98.         exit(-1);
  99.     }
  100.  
  101.     if (fpc == NULL && cint <= 0.0) {
  102.         fprintf (stderr, "%s: GMT SYNTAX ERROR -C option:  Must specify contour interval or file\n", gmt_program);
  103.         error++;
  104.     }
  105.  
  106.     if (fps == NULL && nshades <= 0) {
  107.         fprintf (stderr, "%s: GMT SYNTAX ERROR -S option:  Must specify number of shades or file\n", gmt_program);
  108.         error++;
  109.     }
  110.  
  111.     if (error) exit (-1);
  112.  
  113.     /* First get color table.  Read it or make it.  */
  114.  
  115.     if (fps || fpc) continuous = FALSE;
  116.  
  117.     if (fps) {    /* Read user's old table  */
  118.         if ( (fgets (buffer, 120, fps) ) == NULL) {
  119.             fprintf(stderr, "makecpt:  Cannot read line 1 of v1 color table file.\n");
  120.             exit(-1);
  121.         }
  122.         nshades = atoi(buffer);
  123.         if (nshades <= 0) {
  124.             fprintf(stderr, "makecpt:  Cannot read 1st line of v1 color table file.\n");
  125.             exit(-1);
  126.         }
  127.         ntable = nshades + 3;
  128.         ctable = (struct CTABLE *) memory (CNULL, ntable, sizeof(struct CTABLE), "makecpt");
  129.  
  130.         for (i = 0; i < nshades; i++) {
  131.             if ( (fgets (buffer, 120, fps) ) == NULL) {
  132.                 fprintf(stderr, "makecpt:  Cannot read line %d of v1 color table file.\n", i+2);
  133.                 exit(-1);
  134.             }
  135.             if ((sscanf(buffer, "%d %d %d", &ctable[i].r, &ctable[i].g, &ctable[i].b)) != 3) {
  136.                  fprintf(stderr, "makecpt:  Cannot read line %d of v1 color table file.\n", i+2);
  137.                 exit(-1);
  138.             }
  139.         }
  140.         /* Try to read FG and BG colors, if any  */
  141.  
  142.         bg = i;
  143.         if ( (fgets (buffer, 120, fps) ) != NULL) {
  144.             if ((sscanf(buffer, "%d %d %d", &ctable[i].r, &ctable[i].g, &ctable[i].b)) != 3) {
  145.                  fprintf(stderr, "makecpt:  Cannot read line %d of v1 color table file.\n", i+2);
  146.                 exit(-1);
  147.             }
  148.             i++;
  149.         }
  150.         else {
  151.             ctable[i].r = ctable[i].g = ctable[i].b = 0;    /* Default to black  */
  152.             i++;
  153.         }
  154.  
  155.  
  156.         fg = i;
  157.         if ( (fgets (buffer, 120, fps) ) != NULL) {
  158.             if ((sscanf(buffer, "%d %d %d", &ctable[i].r, &ctable[i].g, &ctable[i].b)) != 3) {
  159.                  fprintf(stderr, "makecpt:  Cannot read line %d of v1 color table file.\n", i+2);
  160.                 exit(-1);
  161.             }
  162.             i++;
  163.         }
  164.         else {
  165.             ctable[i].r = ctable[i].g = ctable[i].b = 255;    /* Default to white  */
  166.         }
  167.         /* Get here when finished reading existing v1 table  */
  168.         fclose(fps);
  169.     }
  170.     else {
  171.         /* Get here if table has to be created.  */
  172.         is = 0;
  173.         ntable = nshades + 3;
  174.         ctable = (struct CTABLE *) memory (CNULL, ntable, sizeof(struct CTABLE), "makecpt");
  175.  
  176.         if (makehues) {
  177.             /* Decide arbitrarily that first entry will be purple 255 0 255 or H = 0.83333
  178.                 and decrease H to last entry where H = 0 (= 1) is red 255 0 0:  */
  179.             nfact = (continuous) ? nshades : nshades - 1;
  180.             start_hue = 5.0/6.0;
  181.             delta_hue = start_hue / nfact;    /* 0.83/nfact  */
  182.             if (gmtdefs.color_model) {
  183.                 sat = gmtdefs.hsv_min_saturation;
  184.                 val = gmtdefs.hsv_max_value;
  185.             }
  186.             else {
  187.                 sat = val = 1.0;
  188.             }
  189.             for (i = 0; i < nshades + continuous; i++) {
  190.                 hue = start_hue - i * delta_hue;
  191.                 gmt_hsv_to_rgb (&ctable[i].r, &ctable[i].g, &ctable[i].b, 360.0 * hue, sat, val);
  192.             }
  193.         }
  194.         else {
  195.             /* Divide gray scale so first and last entry are not quite black/white;
  196.                 thus use nshades, start_hue, delta_hue, hue, sat, val, rr, gg, bb+1 intervals of gray, with 1/2 interval start.  */
  197.             nfact = (continuous) ? nshades + 2 : nshades + 1;
  198.             delta_hue = 1.0 / nfact;
  199.             start_hue = 0.5 * delta_hue;
  200.             for (i = 0; i < nshades + continuous; i++) {
  201.                 hue = start_hue + i * delta_hue;
  202.                 ctable[i].r = ctable[i].g = ctable[i].b = floor (255.999*hue);
  203.             }
  204.         }
  205.         bg = (continuous) ? nshades+1 : nshades;
  206.         fg = bg + 1;
  207.         ctable[bg].r = ctable[bg].g = ctable[bg].b = 0;    /* Set BG to black  */
  208.         ctable[fg].r = ctable[fg].g = ctable[fg].b = 255;    /* Set FG to white  */
  209.     }
  210.  
  211.     /* Now we have a color table set up with a FG and BG and also nshades entries.
  212.         Need to read nshades+1 contour boundaries, or create them with cint and midval:  */
  213.  
  214.     nconts = nshades+1;
  215.     contour = (double *) memory (CNULL, nconts, sizeof(double), "makecpt");
  216.  
  217.     if (fpc) {
  218.         for (i = 0; i < nconts; i++) {
  219.             if ( (fgets (buffer, 120, fpc) ) == NULL) {
  220.                 fprintf(stderr, "makecpt:  Cannot read line %d of v1 contour file.\n", i+2);
  221.                 exit(-1);
  222.             }
  223.             if ((sscanf(buffer, "%lf", &contour[i])) != 3) {
  224.                  fprintf(stderr, "makecpt:  Cannot read line %d of v1 contour file.\n", i+2);
  225.                 exit(-1);
  226.             }
  227.         }
  228.         fclose(fpc);
  229.     }
  230.     else {
  231.         ic = 0;
  232.         start_cont = midval - cint * ( (int)(nshades/2) );
  233.         for (i = 0; i < nconts; i++) {
  234.             contour[i] = start_cont + i * cint;
  235.         }
  236.     }
  237.  
  238.  
  239.     /* Write to stdout.  */
  240.  
  241.     clock = time ((time_t *)NULL);
  242.     
  243.     printf("#\tcpt file created by makecpt on %s", ctime(&clock));
  244.     if (is) {
  245.         printf("#\tInput V1.0 shade table was %s\n", &argv[is][2]);
  246.     }
  247.     else if (makehues) {
  248.         printf("#\tNo input V1.0 shade table was given; an iso-hue rainbow was made.\n");
  249.     }
  250.     else {
  251.         printf("#\tNo input V1.0 shade table was given; a gray scale was made.\n");
  252.     }
  253.     if (ic) {
  254.         printf("#\tContours were read from %s\n", &argv[ic][2]);
  255.     }
  256.     else {
  257.         printf("#\tContours were made using a mid-value of %.8lg and a contour interval of %.8lg\n", midval, cint);
  258.     }
  259.     if (continuous) printf("#\tColor palette is continous\n");
  260.  
  261.     printf("#\n");
  262.     if (continuous) {
  263.         if (gmtdefs.color_model) {
  264.             /* Conversion.  This is a new feature:  */
  265.             for (i = 0; i < nshades; i++) {
  266.                 gmt_rgb_to_hsv(ctable[i].r, ctable[i].g, ctable[i].b, &h1, &s1, &v1);
  267.                 gmt_rgb_to_hsv(ctable[i+1].r, ctable[i+1].g, ctable[i+1].b, &h2, &s2, &v2);
  268.                 printf("%.8lg\t%.8lg\t%.8lg\t%.8lg\t%.8lg\t%.8lg\t%.8lg\t%.8lg\n", contour[i], h1, s1, v1,
  269.                     contour[i+1], h2, s2, v2);
  270.             }
  271.         }
  272.         else {
  273.             for (i = 0; i < nshades; i++) {
  274.                 printf("%.8lg\t%d\t%d\t%d\t%.8lg\t%d\t%d\t%d\n", contour[i], ctable[i].r, ctable[i].g, ctable[i].b,
  275.                     contour[i+1], ctable[i+1].r, ctable[i+1].g, ctable[i+1].b);
  276.             }
  277.         }
  278.     }
  279.     else {
  280.         for (i = 0; i < nshades; i++) {
  281.             printf("%.8lg\t%d\t%d\t%d\t%.8lg\t%d\t%d\t%d\n", contour[i], ctable[i].r, ctable[i].g, ctable[i].b,
  282.                 contour[i+1], ctable[i].r, ctable[i].g, ctable[i].b);
  283.         }
  284.     }
  285.     if (gmtdefs.color_model) {
  286.         gmt_rgb_to_hsv(ctable[bg].r, ctable[bg].g, ctable[bg].b, &h1, &s1, &v1);
  287.         gmt_rgb_to_hsv(ctable[fg].r, ctable[fg].g, ctable[fg].b, &h2, &s2, &v2);
  288.         printf("B\t%.8lg\t%.8lg\t%.8lg\n", h1, s1, v1);
  289.         printf("F\t%.8lg\t%.8lg\t%.8lg\n", h2, s2, v2);
  290.     }
  291.     else {
  292.         printf("B\t%d\t%d\t%d\n", ctable[bg].r, ctable[bg].g, ctable[bg].b);
  293.         printf("F\t%d\t%d\t%d\n", ctable[fg].r, ctable[fg].g, ctable[fg].b);
  294.     }
  295.  
  296.     free((char *)ctable);
  297.     free((char *)contour);
  298.     exit(0);
  299. }
  300.  
  301.