home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / server / ddx / cfb / cfbcmap.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-09  |  8.9 KB  |  312 lines

  1. /************************************************************
  2. Copyright 1987 by Sun Microsystems, Inc. Mountain View, CA.
  3.  
  4.                     All Rights Reserved
  5.  
  6. Permission  to  use,  copy,  modify,  and  distribute   this
  7. software  and  its documentation for any purpose and without
  8. fee is hereby granted, provided that the above copyright no-
  9. tice  appear  in all copies and that both that copyright no-
  10. tice and this permission notice appear in  supporting  docu-
  11. mentation,  and  that the names of Sun or MIT not be used in
  12. advertising or publicity pertaining to distribution  of  the
  13. software  without specific prior written permission. Sun and
  14. M.I.T. make no representations about the suitability of this
  15. software for any purpose. It is provided "as is" without any
  16. express or implied warranty.
  17.  
  18. SUN DISCLAIMS ALL WARRANTIES WITH REGARD TO  THIS  SOFTWARE,
  19. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT-
  20. NESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SUN BE  LI-
  21. ABLE  FOR  ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  22. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,  DATA  OR
  23. PROFITS,  WHETHER  IN  AN  ACTION OF CONTRACT, NEGLIGENCE OR
  24. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION  WITH
  25. THE USE OR PERFORMANCE OF THIS SOFTWARE.
  26.  
  27. ********************************************************/
  28.  
  29.  
  30. #include "X.h"
  31. #include "Xproto.h"
  32. #include "scrnintstr.h"
  33. #include "colormapst.h"
  34. #include "resource.h"
  35.  
  36. #ifdef    STATIC_COLOR
  37.  
  38. static ColormapPtr InstalledMaps[MAXSCREENS];
  39.  
  40. int
  41. cfbListInstalledColormaps(pScreen, pmaps)
  42.     ScreenPtr    pScreen;
  43.     Colormap    *pmaps;
  44. {
  45.     /* By the time we are processing requests, we can guarantee that there
  46.      * is always a colormap installed */
  47.     *pmaps = InstalledMaps[pScreen->myNum]->mid;
  48.     return (1);
  49. }
  50.  
  51.  
  52. void
  53. cfbInstallColormap(pmap)
  54.     ColormapPtr    pmap;
  55. {
  56.     int index = pmap->pScreen->myNum;
  57.     ColormapPtr oldpmap = InstalledMaps[index];
  58.  
  59.     if(pmap != oldpmap)
  60.     {
  61.     /* Uninstall pInstalledMap. No hardware changes required, just
  62.      * notify all interested parties. */
  63.     if(oldpmap != (ColormapPtr)None)
  64.         WalkTree(pmap->pScreen, TellLostMap, (char *)&oldpmap->mid);
  65.     /* Install pmap */
  66.     InstalledMaps[index] = pmap;
  67.     WalkTree(pmap->pScreen, TellGainedMap, (char *)&pmap->mid);
  68.  
  69.     }
  70. }
  71.  
  72. void
  73. cfbUninstallColormap(pmap)
  74.     ColormapPtr    pmap;
  75. {
  76.     int index = pmap->pScreen->myNum;
  77.     ColormapPtr curpmap = InstalledMaps[index];
  78.  
  79.     if(pmap == curpmap)
  80.     {
  81.     if (pmap->mid != pmap->pScreen->defColormap)
  82.     {
  83.         curpmap = (ColormapPtr) LookupIDByType(pmap->pScreen->defColormap,
  84.                            RT_COLORMAP);
  85.         (*pmap->pScreen->InstallColormap)(curpmap);
  86.     }
  87.     }
  88. }
  89.  
  90. #endif
  91.  
  92. void
  93. cfbResolveColor(pred, pgreen, pblue, pVisual)
  94.     unsigned short    *pred, *pgreen, *pblue;
  95.     register VisualPtr    pVisual;
  96. {
  97.     int shift = 16 - pVisual->bitsPerRGBValue;
  98.     unsigned lim = (1 << pVisual->bitsPerRGBValue) - 1;
  99.  
  100.     if ((pVisual->class == PseudoColor) || (pVisual->class == DirectColor))
  101.     {
  102.     /* rescale to rgb bits */
  103.     *pred = ((*pred >> shift) * 65535) / lim;
  104.     *pgreen = ((*pgreen >> shift) * 65535) / lim;
  105.     *pblue = ((*pblue >> shift) * 65535) / lim;
  106.     }
  107.     else if (pVisual->class == GrayScale)
  108.     {
  109.     /* rescale to gray then rgb bits */
  110.     *pred = (30L * *pred + 59L * *pgreen + 11L * *pblue) / 100;
  111.     *pblue = *pgreen = *pred = ((*pred >> shift) * 65535) / lim;
  112.     }
  113.     else if (pVisual->class == StaticGray)
  114.     {
  115.     unsigned limg = pVisual->ColormapEntries - 1;
  116.     /* rescale to gray then [0..limg] then [0..65535] then rgb bits */
  117.     *pred = (30L * *pred + 59L * *pgreen + 11L * *pblue) / 100;
  118.     *pred = ((((*pred * (limg + 1))) >> 16) * 65535) / limg;
  119.     *pblue = *pgreen = *pred = ((*pred >> shift) * 65535) / lim;
  120.     }
  121.     else
  122.     {
  123.     unsigned limr, limg, limb;
  124.  
  125.     limr = pVisual->redMask >> pVisual->offsetRed;
  126.     limg = pVisual->greenMask >> pVisual->offsetGreen;
  127.     limb = pVisual->blueMask >> pVisual->offsetBlue;
  128.     /* rescale to [0..limN] then [0..65535] then rgb bits */
  129.     *pred = ((((((*pred * (limr + 1)) >> 16) *
  130.             65535) / limr) >> shift) * 65535) / lim;
  131.     *pgreen = ((((((*pgreen * (limg + 1)) >> 16) *
  132.               65535) / limg) >> shift) * 65535) / lim;
  133.     *pblue = ((((((*pblue * (limb + 1)) >> 16) *
  134.              65535) / limb) >> shift) * 65535) / lim;
  135.     }
  136. }
  137.  
  138. Bool
  139. cfbInitializeColormap(pmap)
  140.     register ColormapPtr    pmap;
  141. {
  142.     register unsigned i;
  143.     register VisualPtr pVisual;
  144.     unsigned lim, maxent, shift;
  145.  
  146.     pVisual = pmap->pVisual;
  147.     lim = (1 << pVisual->bitsPerRGBValue) - 1;
  148.     shift = 16 - pVisual->bitsPerRGBValue;
  149.     maxent = pVisual->ColormapEntries - 1;
  150.     if (pVisual->class == TrueColor)
  151.     {
  152.     unsigned limr, limg, limb;
  153.  
  154.     limr = pVisual->redMask >> pVisual->offsetRed;
  155.     limg = pVisual->greenMask >> pVisual->offsetGreen;
  156.     limb = pVisual->blueMask >> pVisual->offsetBlue;
  157.     for(i = 0; i <= maxent; i++)
  158.     {
  159.         /* rescale to [0..65535] then rgb bits */
  160.         pmap->red[i].co.local.red =
  161.         ((((i * 65535) / limr) >> shift) * 65535) / lim;
  162.         pmap->green[i].co.local.green =
  163.         ((((i * 65535) / limg) >> shift) * 65535) / lim;
  164.         pmap->blue[i].co.local.blue =
  165.         ((((i * 65535) / limb) >> shift) * 65535) / lim;
  166.     }
  167.     }
  168.     else if (pVisual->class == StaticColor)
  169.     {
  170.     unsigned limr, limg, limb;
  171.  
  172.     limr = pVisual->redMask >> pVisual->offsetRed;
  173.     limg = pVisual->greenMask >> pVisual->offsetGreen;
  174.     limb = pVisual->blueMask >> pVisual->offsetBlue;
  175.     for(i = 0; i <= maxent; i++)
  176.     {
  177.         /* rescale to [0..65535] then rgb bits */
  178.         pmap->red[i].co.local.red =
  179.         ((((((i & pVisual->redMask) >> pVisual->offsetRed)
  180.             * 65535) / limr) >> shift) * 65535) / lim;
  181.         pmap->red[i].co.local.green =
  182.         ((((((i & pVisual->greenMask) >> pVisual->offsetGreen)
  183.             * 65535) / limg) >> shift) * 65535) / lim;
  184.         pmap->red[i].co.local.blue =
  185.         ((((((i & pVisual->blueMask) >> pVisual->offsetBlue)
  186.             * 65535) / limb) >> shift) * 65535) / lim;
  187.     }
  188.     }
  189.     else if (pVisual->class == StaticGray)
  190.     {
  191.     for(i = 0; i <= maxent; i++)
  192.     {
  193.         /* rescale to [0..65535] then rgb bits */
  194.         pmap->red[i].co.local.red = ((((i * 65535) / maxent) >> shift)
  195.                      * 65535) / lim;
  196.         pmap->red[i].co.local.green = pmap->red[i].co.local.red;
  197.         pmap->red[i].co.local.blue = pmap->red[i].co.local.red;
  198.     }
  199.     }
  200.     return TRUE;
  201. }
  202.  
  203. /* When simulating DirectColor on PseudoColor hardware, multiple
  204.    entries of the colormap must be updated
  205.  */
  206.  
  207. #define AddElement(mask) { \
  208.     pixel = red | green | blue; \
  209.     for (i = 0; i < nresult; i++) \
  210.       if (outdefs[i].pixel == pixel) \
  211.             break; \
  212.     if (i == nresult) \
  213.     { \
  214.        nresult++; \
  215.     outdefs[i].pixel = pixel; \
  216.     outdefs[i].flags = 0; \
  217.     } \
  218.     outdefs[i].flags |= (mask); \
  219.     outdefs[i].red = pmap->red[red >> pVisual->offsetRed].co.local.red; \
  220.     outdefs[i].green = pmap->green[green >> pVisual->offsetGreen].co.local.green; \
  221.     outdefs[i].blue = pmap->blue[blue >> pVisual->offsetBlue].co.local.blue; \
  222. }
  223.  
  224. cfbExpandDirectColors (pmap, ndef, indefs, outdefs)
  225.     ColormapPtr    pmap;
  226.     int        ndef;
  227.     xColorItem    *indefs, *outdefs;
  228. {
  229.     int            minred, mingreen, minblue;
  230.     register int    red, green, blue;
  231.     int            maxred, maxgreen, maxblue;
  232.     int            stepred, stepgreen, stepblue;
  233.     VisualPtr        pVisual;
  234.     register int    pixel;
  235.     register int    nresult;
  236.     register int    i;
  237.  
  238.     pVisual = pmap->pVisual;
  239.  
  240.     stepred = 1 << pVisual->offsetRed;
  241.     stepgreen = 1 << pVisual->offsetGreen;
  242.     stepblue = 1 << pVisual->offsetBlue;
  243.     maxred = pVisual->redMask;
  244.     maxgreen = pVisual->greenMask;
  245.     maxblue = pVisual->blueMask;
  246.     nresult = 0;
  247.     for (;ndef--; indefs++)
  248.     {
  249.     if (indefs->flags & DoRed)
  250.     {
  251.         red = indefs->pixel & pVisual->redMask;
  252.             for (green = 0; green <= maxgreen; green += stepgreen)
  253.             {
  254.             for (blue = 0; blue <= maxblue; blue += stepblue)
  255.             {
  256.             AddElement (DoRed)
  257.             }
  258.             }
  259.     }
  260.     if (indefs->flags & DoGreen)
  261.     {
  262.         green = indefs->pixel & pVisual->greenMask;
  263.             for (red = 0; red <= maxred; red += stepred)
  264.             {
  265.             for (blue = 0; blue <= maxblue; blue += stepblue)
  266.             {
  267.             AddElement (DoGreen)
  268.             }
  269.             }
  270.     }
  271.     if (indefs->flags & DoBlue)
  272.     {
  273.         blue = indefs->pixel & pVisual->blueMask;
  274.             for (red = 0; red <= maxred; red += stepred)
  275.             {
  276.             for (green = 0; green <= maxgreen; green += stepgreen)
  277.             {
  278.             AddElement (DoBlue)
  279.             }
  280.             }
  281.     }
  282.     }
  283.     return nresult;
  284. }
  285.  
  286. Bool
  287. cfbCreateDefColormap(pScreen)
  288.     ScreenPtr pScreen;
  289. {
  290.     unsigned short    zero = 0, ones = 0xFFFF;
  291.     VisualPtr    pVisual;
  292.     ColormapPtr    cmap;
  293.     
  294.     for (pVisual = pScreen->visuals;
  295.      pVisual->vid != pScreen->rootVisual;
  296.      pVisual++)
  297.     ;
  298.  
  299.     if (CreateColormap(pScreen->defColormap, pScreen, pVisual, &cmap,
  300.                (pVisual->class & DynamicClass) ? AllocNone : AllocAll,
  301.                0)
  302.     != Success)
  303.     return FALSE;
  304.     if ((AllocColor(cmap, &ones, &ones, &ones, &(pScreen->whitePixel), 0) !=
  305.               Success) ||
  306.         (AllocColor(cmap, &zero, &zero, &zero, &(pScreen->blackPixel), 0) !=
  307.               Success))
  308.         return FALSE;
  309.     (*pScreen->InstallColormap)(cmap);
  310.     return TRUE;
  311. }
  312.