home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Graphics / Graphics.zip / xfitsvew.zip / XFITSview / color.c < prev    next >
Text File  |  1998-10-14  |  17KB  |  424 lines

  1. /*    Colormap functions for XFITSview */
  2. /*-----------------------------------------------------------------------
  3. *  Copyright (C) 1996,1998
  4. *  Associated Universities, Inc. Washington DC, USA.
  5. *  This program is free software; you can redistribute it and/or
  6. *  modify it under the terms of the GNU General Public License as
  7. *  published by the Free Software Foundation; either version 2 of
  8. *  the License, or (at your option) any later version.
  9. *
  10. *  This program is distributed in the hope that it will be useful,
  11. *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. *  GNU General Public License for more details.
  14. *-----------------------------------------------------------------------*/
  15. #include <Xm/Xm.h> 
  16. #include <Xm/DrawingA.h> 
  17. #include <Xm/MainW.h> 
  18. #include <Xm/Scale.h> 
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include "imagedisp.h"
  22. #include "color.h"
  23. #include "messagebox.h"
  24.  
  25. /* internal function prototypes */
  26. void SetColor1(ImageDisplay* IDdata);
  27. void SetColor2(ImageDisplay *IDdata);
  28. void RevColor(ImageDisplay *IDdata);
  29. void UpdateColor (ImageDisplay *IDdata);
  30.  
  31.  
  32. void ColContourCB (Widget w, XtPointer clientData, XtPointer callData)
  33. /* Color coutour colors callback */
  34. {
  35.   ImageDisplay *IDdata = (ImageDisplay *)clientData;
  36.   SetColor1(IDdata);     /* set new colors */
  37.   SetColorTable(IDdata); /* install new colors */
  38. } /* end ColContourCB */
  39.  
  40. void PhlameCB (Widget w, XtPointer clientData, XtPointer callData)
  41. /* Pseudo Flame colors callback */
  42. {
  43.   ImageDisplay *IDdata = (ImageDisplay *)clientData;
  44.   SetColor2(IDdata);     /* set new colors */
  45.   SetColorTable(IDdata); /* install new colors */
  46. } /* end PhlameCB */
  47.  
  48. void GrayscaleCB (Widget w, XtPointer clientData, XtPointer callData)
  49. /* Gray scale callback */
  50. {
  51.   ImageDisplay *IDdata = (ImageDisplay *)clientData;
  52.   InitColorTable(IDdata);     /* set new colors */
  53.   SetColorTable(IDdata); /* install new colors */
  54. } /* end GrayscaleCB */
  55.  
  56. void ReverseColCB (Widget w, XtPointer clientData, XtPointer callData)
  57. /* Reverse color table callback */
  58. {
  59.   ImageDisplay *IDdata = (ImageDisplay *)clientData;
  60.   RevColor(IDdata);     /* set new colors */
  61.   SetColorTable(IDdata); /* install new colors */
  62. } /* end ReverseColCB */
  63.  
  64. void ResetColCB (Widget w, XtPointer clientData, XtPointer callData)
  65. /* reset color table callback */
  66. {
  67.   ImageDisplay *IDdata = (ImageDisplay *)clientData;
  68.   IDdata->value[0] = 128; IDdata->value[1]=128;
  69. /* reset sliders */
  70.   XmScaleSetValue(IDdata->BriScroll, 128);
  71.   XmScaleSetValue(IDdata->ConScroll, 128);
  72.   InitColorTable(IDdata);     /* set new colors */
  73.   SetColorTable(IDdata); /* install new colors */
  74. } /* end ResetColCB */
  75.  
  76.  
  77. void SetupColorMap (Widget shell, ImageDisplay *IDdata)
  78. /* create and install a color map - fill with grayscale ramp */
  79. {
  80.   int         icol, i, ncolor;
  81.   Display     *dpy = XtDisplay (shell);
  82.   XColor      *Colors;
  83.   Window      windows [2];
  84.   float       frac;
  85.  
  86. /* allocate color array */
  87.   ncolor = XDisplayCells (dpy, XDefaultScreen (dpy));
  88.   Colors = (XColor*) XtMalloc (sizeof (XColor) * ncolor);
  89.  
  90.   /* The first time, create a colormap and install it in the
  91.      canvas widget's window. Also set up the window manager
  92.      colormap windows list so both colormaps get installed,
  93.      if the system is capable of handling multiple colormaps. */
  94.     
  95.   if (!IDdata->cmap) {
  96.     IDdata->cmap = DefaultColormap (dpy, DefaultScreen(dpy));
  97. /* Attempt to allocate colors in default color table */
  98.     if (!XAllocColorCells(dpy, IDdata->cmap, FALSE, NULL, 0, IDdata->colut, 
  99.               IDdata->ncolors)) {
  100. /* could not allocate colors - try a new color table */
  101.       MessageShow ("Colormap full, create new one");
  102.       IDdata->cmap = 
  103.     XCreateColormap (dpy, XtWindow(IDdata->canvas), 
  104.              DefaultVisual(dpy, DefaultScreen(dpy)), AllocAll);
  105.       if (!IDdata->cmap) {
  106.     MessageShow ("Could not create colormap");
  107.     return;}
  108. /* Copy as much of the default color table as possible */
  109.       for (i = 0; i < ncolor; i++) {
  110.          Colors[i].pixel = i;
  111.          Colors[i].flags = DoRed | DoGreen | DoBlue;
  112.          }
  113.       XQueryColors (dpy, DefaultColormap(dpy, DefaultScreen(dpy)),
  114.          Colors, ncolor);
  115.       XStoreColors (dpy, IDdata->cmap, Colors, ncolor);
  116. /* install color map */
  117.       XSetWindowColormap (dpy, XtWindow (shell), IDdata->cmap);
  118. /* let window see changes in colormap */
  119. /*ugly results      XSelectInput (dpy, XtWindow (shell), ColormapChangeMask);*/
  120. /* use middle of color map */
  121.      for (i=0; i<IDdata->ncolors;i++) 
  122. /* 64 colors       IDdata->colut[i] = i+ncolor/2-IDdata->ncolors;*/
  123.        IDdata->colut[i] = i+((3*ncolor)/4)-IDdata->ncolors; /* 128 colors */
  124.  
  125.     } /* end of install virtual colormap */
  126.   } /* end of allocation/installation of colormap */
  127.  
  128. /* fill with ramp in gray */
  129.  
  130.   InitColorTable (IDdata);
  131.  
  132. /* first color always black */
  133.   Colors[0].pixel = IDdata->colut[0];
  134.   Colors[0].flags = DoRed|DoGreen|DoBlue;
  135.   Colors[0].red   = Colors[0].blue = Colors[0].green =  BlackPixel(dpy, 0);
  136. /*  XStoreColor (dpy, IDdata->cmap, &Colors[0]);*/
  137.   IDdata->red[0] = Colors[0].red; 
  138.   IDdata->blue[0] = Colors[0].blue; 
  139.   IDdata->green[0] = Colors[0].green;
  140.  
  141. /* rest of color table */
  142.   for (i = 0; i < IDdata->ncolors; i++) {
  143.     Colors[i].pixel = IDdata->colut[i];
  144.     Colors[i].flags = DoRed|DoGreen|DoBlue;
  145.     Colors[i].red   = IDdata->red[i];
  146.     Colors[i].blue  = IDdata->blue[i];
  147.     Colors[i].green = IDdata->green[i];
  148. /*    XStoreColor (dpy, IDdata->cmap, &Colors[i]);*/
  149.     IDdata->red[i] = Colors[i].red; 
  150.     IDdata->blue[i] = Colors[i].blue; 
  151.     IDdata->green[i] = Colors[i].green;
  152.   } /* end of fill color table loop */
  153.  
  154.   XStoreColors (dpy, IDdata->cmap, Colors, IDdata->ncolors);
  155.  
  156.   XtFree ((char *) Colors);
  157. /* init Display */
  158.   ResetColCB (IDdata->display, (XtPointer)IDdata, NULL);
  159. } /* end of SetupColorMap */
  160.                             
  161. void SetColorTable (ImageDisplay* IDdata)
  162. /* routine to change the color table */
  163. /* IDdata->value[0] = Brightness control (0 - 255) */
  164. /* IDdata->value[1] = Contrast control (0- 255) */
  165. /* IDdata->ncolors = number of colors (e.g. 128) */
  166. /* IDdata->red = default red color table (0 - 65535) */
  167. /* IDdata->green = default green color table (0 - 65535) */
  168. /* IDdata->blue = default blue color table (0 - 65535) */
  169.  
  170. /* The new color table is the entries from the default table with  */
  171. /* 2(IDdata->value[0]-128) added to each and  then multiplied by  */
  172. /* 1 + 5*((IDdata->value[1]-128)/128); */
  173.  
  174. {
  175.    int i;
  176.    long lColor;
  177.    unsigned short cRed, cBlue, cGreen;
  178.    float scale, offset;
  179.    XColor *colors;
  180.    Display     *dpy = XtDisplay (IDdata->canvas);
  181.  
  182.    scale = 1.0 + 5.0*((IDdata->value[1]-128.0)/128.0);
  183.    offset=2.0*(IDdata->value[0]-128.0);
  184.  
  185. /* Don't bother if no colormap */
  186.    if (!IDdata->cmap) return;
  187.  
  188. /* allocate color array */
  189.   colors = (XColor*) XtMalloc (sizeof (XColor) * (IDdata->ncolors));
  190.  
  191. /* replace table / update colormap; reserve color 0=(0,0,0) for blanked */
  192.    for (i=0; i<IDdata->ncolors; i++) {
  193.       lColor = scale * ((float)i + offset) + 0.5;
  194.       if (lColor>=IDdata->ncolors) lColor = IDdata->ncolors-1; 
  195.       if (lColor<0) lColor = 0;
  196.       if ((i>0) && (lColor==0)) lColor = 1;
  197.       if (i==0) lColor = 0;
  198.       colors[i].pixel = IDdata->colut[i];
  199.       colors[i].flags = DoRed|DoGreen|DoBlue;
  200.       colors[i].red   = IDdata->red[lColor]; 
  201.       colors[i].green = IDdata->green[lColor]; 
  202.       colors[i].blue  = IDdata->blue[lColor]; 
  203.       if (i==0)
  204.     colors[i].red = colors[i].blue = colors[i].green = BlackPixel(dpy, 0);
  205. /*      XStoreColor (dpy, IDdata->cmap, &colors[i]);*/
  206.    }  /* end of loop over color table */
  207.   XStoreColors (dpy, IDdata->cmap, colors, IDdata->ncolors);
  208.    XtFree ((char *) colors);
  209.  
  210.    return;
  211. }  /* end of SetColorTable */
  212.  
  213.  
  214. void InitColorTable(ImageDisplay* IDdata)
  215. /* Set initial values in color table */
  216. /* IDdata->ncolors = number of colors (e.g. 128) */
  217. /* IDdata->red = default red color table (0 - 65535) */
  218. /* IDdata->green = default green color table (0 - 65535) */
  219. /* IDdata->blue = default blue color table (0 - 65535) */
  220. {
  221.    int i;
  222.    long lColor;
  223.    float frac;
  224.    Display     *dpy = XtDisplay (IDdata->canvas);
  225.  
  226.    for (i=0; i<IDdata->ncolors; i++) {
  227.      frac = (float)(i+1) / (float)IDdata->ncolors;
  228.      lColor = frac * 65536.0 + frac * 256.0;
  229.      if (lColor > 65535) lColor = 65535;
  230.      if (i==0) lColor = BlackPixel(dpy, 0);  /* reserve black for blanked */
  231.      IDdata->red[i] = lColor;
  232.      IDdata->green[i] = lColor;
  233.      IDdata->blue[i] = lColor;
  234.    }
  235.  } /* end InitColorTable */
  236.  
  237. void SetColor1(ImageDisplay* IDdata)
  238. /* color scheme lifted from AIPS */
  239. /* IDdata->ncolors = number of colors (e.g. 128) */
  240. /* IDdata->red =  red color table*/
  241. /* IDdata->green =  green color table*/
  242. /* IDdata->blue =  blue color table*/
  243. {
  244.   short i;
  245.   long    r, g, b, rt, gt, bt;
  246. #if MAXCOLOR==128   /* 128 color table */
  247. unsigned char bc_tab[128]={0,            /*blue table  */
  248.    15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15,   
  249.    72,72,72,72,72,72,72, 72,72,72,72,72,72,72,
  250.    127,127,127,127,127,127,127, 127,127,127,127,127,127,127,  
  251.    203,203,203,203,203,203,203, 203,203,203,203,203,203,203,
  252.    0,0,0,0,0,0,0, 0,0,0,0,0,0,0,
  253.    0,0,0,0,0,0,0, 0,0,0,0,0,0,0,
  254.    0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 
  255.    0,0,0,0,0,0,0, 0,0,0,0,0,0,0,
  256.    0,0,0,0,0,0,0, 0,0,0,0,0,0,0};
  257.    unsigned char gc_tab[128]={0,         /* green table  */
  258.    15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15, 
  259.    0,0,0,0,0,0,0,  0,0,0,0,0,0,0,
  260.    0,0,0,0,0,0,0,  0,0,0,0,0,0,0,
  261.    76,76,76,76,76,76,76, 76,76,76,76,76,76,76,
  262.    59,59,59,59,59,59,59,  59,59,59,59,59,59,59,  
  263.    229,229,229,229,229,229,229, 229,229,229,229,229,229,229,
  264.    255,255,255,255,255,255,255, 255,255,255,255,255,255,255, 
  265.    89,89,89,89,89,89,89, 89,89,89,89,89,89,89, 
  266.    0,0,0,0,0,0,0,  0,0,0,0,0,0,0};
  267.    unsigned char rc_tab[128]={0,          /*red table  */
  268.    15,15,15,15,15,15,15,  15, 15,15,15,15,15,15,15,  
  269.    36,36,36,36,36,36,36, 36,36,36,36,36,36,36,
  270.    0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 
  271.    15,15,15,15,15,15,15, 15,15,15,15,15,15,15,
  272.    0,0,0,0,0,0,0, 0,0,0,0,0,0,0, 
  273.    0,0,0,0,0,0,0, 0,0,0,0,0,0,0,
  274.    255,255,255,255,255,255,255, 255,255,255,255,255,255,255,  
  275.    255,255,255,255,255,255,255, 255,255,255,255,255,255,255,
  276.    255,255,255,255,255,255,255, 255,255,255,255,255,255,255};
  277. #else /* 64 color table */
  278. unsigned char bc_tab[64]={0,            /*blue table  */
  279.    15,15,15,15,15,15,15,   72,72,72,72,72,72,72,
  280.    127,127,127,127,127,127,127,  203,203,203,203,203,203,203,
  281.    0,0,0,0,0,0,0,  0,0,0,0,0,0,0,
  282.    0,0,0,0,0,0,0,  0,0,0,0,0,0,0,
  283.    0,0,0,0,0,0,0};
  284.    unsigned char gc_tab[64]={0,         /* green table  */
  285.    15,15,15,15,15,15,15,  0,0,0,0,0,0,0,
  286.    0,0,0,0,0,0,0,  76,76,76,76,76,76,76,
  287.    59,59,59,59,59,59,59,  229,229,229,229,229,229,229,
  288.    255,255,255,255,255,255,255,  89,89,89,89,89,89,89,
  289.    0,0,0,0,0,0,0};
  290.    unsigned char rc_tab[64]={0,          /*red table  */
  291.    15,15,15,15,15,15,15,  36,36,36,36,36,36,36,
  292.    0,0,0,0,0,0,0,  15,15,15,15,15,15,15,
  293.    0,0,0,0,0,0,0,  0,0,0,0,0,0,0,
  294.    255,255,255,255,255,255,255,  255,255,255,255,255,255,255,
  295.    255,255,255,255,255,255,255};  /*255  */
  296. #endif
  297.    for (i=1; i<(short)IDdata->ncolors; i++)
  298.      { rt = rc_tab[i]; gt = gc_tab[i]; bt = bc_tab[i];
  299.        r = rt + rt * 256; g = gt + gt * 256; b = bt + bt * 256;
  300.        if (r>65536) r = 65536;
  301.        if (g>65536) g = 65536;
  302.        if (b>65536) b = 65536;
  303.        IDdata->red[i] =   r;
  304.        IDdata->green[i] = g;
  305.        IDdata->blue[i] =  b;}
  306. }  /* End of SetColor1 */
  307.  
  308. void SetColor2(ImageDisplay *IDdata)
  309. /* color scheme lifted from AIPS (PHLAME) */
  310. /* IDdata->ncolors = number of colors (e.g. 128) */
  311. /* IDdata->red =  red color table*/
  312. /* IDdata->green =  green color table*/
  313. /* IDdata->blue =  blue color table */
  314. {
  315.   short i;
  316.   long    r, g, b, rt, gt, bt;
  317. #if MAXCOLOR==128   /* 128 color table */
  318.    unsigned char bc_tab[]=         /* green table  */
  319.   {0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  320.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  321.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  322.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  323.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  324.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  325.    0,  26,  53,  66,  79,  89,  99, 108, 117, 115, 133, 140, 147, 154, 161, 167,
  326.  174, 180, 186, 191, 197, 202, 208, 212, 219, 224, 229, 233, 238, 242, 248, 252};
  327.    unsigned char gc_tab[]=         /* green table  */
  328.   {0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  329.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  330.    0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
  331.    0,   0,   0,   0,   0,   0,   0,  31,   0,   0,   0,   0,   0,   0,   0,  15,
  332.   31,  38,  46,  51,  57,  62,  68,  72,  77,  81,  86,  89,  93,  97, 101, 104,
  333.  107, 110, 114, 117, 120, 123, 127, 130, 133, 135, 138, 141, 144, 146, 149, 151,
  334.  154, 156, 159, 162, 165, 167, 170, 172, 175, 179, 182, 185, 189, 192, 195, 198,
  335.  202, 205, 209, 212, 215, 218, 222, 225, 228, 231, 234, 237, 240, 243, 246, 251};
  336.    unsigned char rc_tab[]=          /*red table  */
  337.   {0,   0,   0,   0,  0,   0,    0,   0,   0,   0,   0,   0,   0,   0,   0,  15,
  338.   29,  36,  43,  49, 55,  59,   64,  68,  73,  77,  81,  84,  88,  91,  95,  98, 
  339.  102, 105, 109, 112, 114, 117, 120, 123, 126, 128, 131, 134, 137, 139, 142, 144,
  340.  147, 149, 152, 154, 156, 158, 161, 163, 165, 167, 170, 172, 174, 176, 178, 180,
  341.  183, 185, 187, 189, 191, 193, 195, 196, 198, 200, 202, 204, 207, 209, 211, 212,
  342.  214, 216, 218, 219, 221, 223, 225, 226, 228, 230, 232, 231, 235, 236, 238, 240,
  343.  242, 244, 246, 247, 248, 249, 251, 253, 255, 255, 255, 255, 255, 255, 255, 255,
  344.  255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
  345. #else /* 64 color table */
  346. unsigned char bc_tab[64]=            /*blue table  */
  347.   {0,   0,   0,   0,   0,   0,   0,   0,
  348.    0,   0,   0,   0,   0,   0,   0,   0,
  349.    0,   0,   0,   0,   0,   0,   0,   0,
  350.    0,   0,   0,   0,   0,   0,   0,   0,
  351.    0,   0,   0,   0,   0,   0,   0,   0,
  352.    0,   0,   0,   0,   0,   0,   0,   0,
  353.    0,  53,  79,  99, 117, 133, 147, 161,
  354.  174, 186, 197, 208, 219, 229, 238, 248};
  355.    unsigned char gc_tab[64]=         /* green table  */
  356.   {0,   0,   0,   0,   0,   0,   0,   0,
  357.    0,   0,   0,   0,   0,   0,   0,   0,
  358.    0,   0,   0,   0,   0,   0,   0,   0,
  359.    0,   0,   0,   0,   0,   0,   0,  31,
  360.   46,  57,  68,  77,  86,  93, 101, 107,
  361.  114, 120, 127, 133, 138, 144, 149, 154,
  362.  159, 165, 170, 175, 182, 189, 195, 202,
  363.  209, 215, 222, 228, 234, 240, 246, 251};
  364.    unsigned char rc_tab[64]=          /*red table  */
  365.   {0,   0,   0,   0,   0,   0,   0,  29,
  366.   43,  55,  64,  73,  81,  88,  95, 102,
  367.  109, 114, 120, 126, 131, 137, 142, 147,
  368.  152, 156, 161, 165, 170, 174, 178, 183,
  369.  187, 191, 195, 198, 202, 207, 211, 214,
  370.  218, 221, 225, 228, 232, 235, 238, 243,
  371.  246, 248, 251, 255, 255, 255, 255, 255,
  372.  255, 255, 255, 255, 255, 255, 255, 255};
  373. #endif
  374.  
  375.    for (i=1; i<(short)IDdata->ncolors; i++) 
  376.    { rt = rc_tab[i]; gt = gc_tab[i]; bt = bc_tab[i];
  377.        r = rt + rt * 256; g = gt + gt * 256; b = bt + bt * 256;
  378.        if (r>65536) r = 65536;
  379.        if (g>65536) g = 65536;
  380.        if (b>65536) b = 65536;
  381.        IDdata->red[i] =   r;
  382.        IDdata->green[i] = g;
  383.        IDdata->blue[i] =  b;}
  384. }  /* End of SetColor2 */
  385.  
  386. void RevColor(ImageDisplay *IDdata)
  387. /* IDdata->ncolors = number of colors (e.g. 128) */
  388. /* IDdata->red =  red color table  */
  389. /* IDdata->green =  green color table */
  390. /* IDdata->blue =  blue color table  */
  391. {
  392.   short i, j;
  393.   unsigned short cTemp;
  394.    for (i=1; i<IDdata->ncolors/2; i++)
  395.      {j = IDdata->ncolors - i;
  396.       cTemp = IDdata->red[i];
  397.       IDdata->red[i] =   IDdata->red[j];
  398.       IDdata->red[j] =   cTemp;
  399.       cTemp = IDdata->green[i];
  400.       IDdata->green[i] =   IDdata->green[j];
  401.       IDdata->green[j] =   cTemp;
  402.       cTemp = IDdata->blue[i];
  403.       IDdata->blue[i] =   IDdata->blue[j];
  404.       IDdata->blue[j] =   cTemp;
  405.      }
  406. }  /* End of RevColor */
  407.  
  408. void UpdateColor (ImageDisplay *IDdata)
  409. /* routine to update the color map */
  410. /* Calls SetColorTable, gives */
  411. /* IDdata->value[0] = Brightness control (0 - 255) */
  412. /* IDdata->value[1] = Contrast control (0- 255) */
  413. /* IDdata->ncolors = number of colors (e.g. 128) */
  414. /* IDdata->red = default red color table */
  415. /* IDdata->green = default green color table */
  416. /* IDdata->blue = default blue color table */
  417. /* The new color table is the entries from the default table with  */
  418. /* 2(IDdata->value[0]-128) added to each and  then multiplied by  */
  419. /* 1 + 5*((IDdata->value[1]-128)/128); */
  420. {
  421.    SetColorTable (IDdata);
  422. }  /* end of UpdateColor */
  423.  
  424.