home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / ms-0.07 / lib / cmap.c next >
Encoding:
C/C++ Source or Header  |  1995-06-27  |  3.0 KB  |  117 lines

  1. /* cmap.c -- X-independent colormap support */
  2.    
  3. /*  
  4.     This file is part of MandelSpawn, a network Mandelbrot program.
  5.  
  6.     Copyright (C) 1993 Andreas Gustafsson
  7.  
  8.     MandelSpawn is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License, version 1,
  10.     as published by the Free Software Foundation.
  11.  
  12.     MandelSpawn is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License,
  18.     version 1, along with this program; if not, write to the Free 
  19.     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include <math.h>     /* for floor() declaration */
  23. #include <string.h>
  24.  
  25. #ifdef NO_STRDUP
  26. char *strdup();
  27. #endif
  28.  
  29. /*
  30.   It is impossible to declare malloc() in a portable way.
  31.   Be prepared to change these declarations. 
  32. */
  33. void *malloc(), *realloc(); 
  34. void free();
  35.  
  36. #include "color.h"
  37.  
  38. parse_spectrum(spectrum, ncolours, colors)
  39.     char *spectrum;
  40.     int ncolours;    /* number of colormap slots available */
  41.     color_t *colors;    /* buffer to write colors into (size ncolours) */
  42. {   
  43.     color_t *stops;
  44.     unsigned stops_allocated = 4; /* initial allocation of colour stops */
  45.     int nstops, i;
  46.     char *spectrum_copy, *p;
  47.  
  48.     spectrum_copy = strdup(spectrum);
  49.     p = spectrum_copy;
  50.  
  51.     stops = (color_t *) malloc(stops_allocated * sizeof(color_t));
  52.     
  53.     i = 0;
  54.     while(1)
  55.     {    char *end;
  56.     color_t dummy;
  57.     end = strchr(p, '-');
  58.     if(end)
  59.         *end = '\0';
  60.     if(i >= stops_allocated)
  61.     {   stops_allocated *= 2;
  62.         stops = (color_t *)
  63.         realloc((char *) stops, stops_allocated * sizeof(color_t));
  64.     }
  65.     if(! name_to_color(p, &stops[i]))
  66.     {   
  67.         static char *err = "unrecognized colour in spectrum: ";
  68.         char *msg = malloc(sizeof(err)+strlen(p));
  69.         strcpy(msg, err);
  70.         strcat(msg, p);
  71.         cmap_error(msg);
  72.         free(msg); /*NOTREACHED*/
  73.     }
  74.     i++;
  75.     if(end)
  76.         p = end+1; 
  77.     else
  78.         break;
  79.     }
  80.     
  81.     nstops = i;
  82.     
  83.     /* interpolate intermediate colours */
  84.     
  85.     for(i=0; i<ncolours; i++)
  86.     {
  87.     double flstop, stop, fpart;
  88.     int istop, jstop;
  89.     int comp;
  90.     
  91.     flstop = (double) i / (double) (ncolours-1) * (double) (nstops-1);
  92.  
  93.     /*
  94.       Calculate the number of the next lower stop 
  95.       (should be ==nstops-1 only for i==ncolours-1)
  96.     */
  97.     stop = floor(flstop);
  98.     fpart = flstop-stop;
  99.  
  100.     /*
  101.       Calculate the indices of the two stops to interpolate between 
  102.       If we are at the last stop, interpolate between it and itself 
  103.       to avoid accesses past the end of the array (the value will be 
  104.       multiplied by zero anyway)
  105.     */
  106.     istop = (int) stop;
  107.     jstop = (istop+1 >= nstops ? nstops-1 : istop+1);
  108.  
  109.     for(comp=0; comp<3; comp++)
  110.         colors[i].comps[comp] =
  111.         (1-fpart)*stops[istop].comps[comp] +
  112.             fpart*stops[jstop].comps[comp];
  113.     }
  114.     free(stops);
  115.     free(spectrum_copy);
  116. }
  117.