home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / ccs / ccs-11tl.lha / lbl / xview / genial / func / cmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-14  |  3.1 KB  |  122 lines

  1. /*
  2.  * cmap.c -- routines for manipulating colormap
  3.  *
  4.  * general steps involved in creating ximage with proper color:
  5.  *   1) read data
  6.  *   2) create buffer of gray-level lut indices (make_lut)
  7.  *   3) create colormap of these colors (build_colormap)
  8.  *   4) create buffer of colormap lut indices (map_image_to_colors)
  9.  *   5) create ximage (mk_x_img)
  10.  */
  11.  
  12. #include <math.h>
  13. #include "ui.h"
  14. #include "display.h"
  15.  
  16. char     *palnames[PALSIZE] =
  17. {"red", "yellow", "green", "cyan", "blue", "magenta", "black", "white"};
  18.  
  19. XColor    pallet[PALSIZE];
  20. u_long    standout;
  21. u_long   *colors;
  22.  
  23. Window    mainw;
  24. byte      red[256], green[256], blue[256];
  25.  
  26. void      make_lut();
  27. u_long *build_colormap();
  28.  
  29. /*************************************************************/
  30. void
  31. cmap_init()
  32. {
  33.     Colormap  cmap;
  34.     int       i, val;
  35.  
  36.     mainw = (Window) xv_get(img_win->d_win, XV_XID, NULL);
  37.  
  38.     cmap = XDefaultColormap(display, DefaultScreen(display));
  39.     /* set up standout colors (read-only colors) */
  40.     for (i = 0; i < PALSIZE; i++) {
  41.     if (XParseColor(display, cmap, palnames[i], &pallet[i]) == 0)
  42.         fprintf(stderr, "%s: Error parsing color \n", Progname);
  43.     else if (XAllocColor(display, cmap, &pallet[i]) == 0)
  44.         fprintf(stderr, "%s: couldn't allocate color: %s.\n",
  45.             Progname, palnames[i]);
  46.     }
  47.     standout = pallet[GREEN].pixel;
  48.  
  49.     for (i = 0; i < 256; i++) {
  50.     val = (int) ((i * 256.0 / (float) NCOLORS) + .5);
  51.     if (val > 255)
  52.         val = 255;
  53.     red[i] = green[i] = blue[i] = (byte) val;
  54.     }
  55. }
  56.  
  57. /*************************************************************/
  58. void
  59. build_cmap(image)
  60.     struct img_data *image;
  61. {
  62.     make_lut(image, NCOLORS);    /* creates image->lut from image->data */
  63.  
  64.     /* sorted lut is returned im image_lut */
  65.     colors = build_colormap(display, winv, mainw,
  66.                 (byte *)image->lut, image->width * image->height,
  67.                 red, green, blue, 256,
  68.                 0, 0, NCOLORS, 0, 1, 0, 1, Progname);
  69.  
  70.     return;
  71.  
  72. }
  73. /************************************************/
  74. void
  75. make_lut(image, ncols)
  76.     struct img_data *image;
  77.     int       ncols;
  78. {
  79.     register int i, j;
  80.     u_char   *c_ptr;
  81.     u_short  *s_ptr;
  82.     u_int    *i_ptr;
  83.     float     delta = (float) (ncols - 1) / (float) (image->maxv - image->minv);
  84.  
  85.     switch (image->dsize) {
  86.     case 1:
  87.     c_ptr = (u_char *) image->data;
  88.     for (i = 0; i < image->height * image->width; i++) {
  89.         j = (int) c_ptr[i];
  90.         image->lut[i] = (byte) (((j - image->minv) * delta));
  91.     }
  92.     break;
  93.     case 2:
  94.     s_ptr = (u_short *) image->data;
  95.     for (i = 0; i < image->height * image->width; i++) {
  96.         j = (int) s_ptr[i];
  97.         image->lut[i] = (byte) (((j - image->minv) * delta));
  98.     }
  99.     break;
  100.     case 4:
  101.     i_ptr = (u_int *) image->data;
  102.     for (i = 0; i < image->height * image->width; i++) {
  103.         j = (int) i_ptr[i];
  104.         image->lut[i] = (byte) (((j - image->minv) * delta));
  105.     }
  106.     break;
  107.     }
  108.     return;
  109. }
  110.  
  111. /*************************************************************/
  112. int
  113. get_gamma(val, gam)
  114.     byte      val;
  115.     float     gam;
  116. {
  117.     float     newval;
  118.  
  119.     newval = 256. * pow((double) val / 256., (double) (1.0 / gam));
  120.     return (MIN((int) (newval + .5), 255));    /* dont allow values > 255 */
  121. }
  122.