home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / x / xibm.zip / common / ibmColor.c < prev    next >
C/C++ Source or Header  |  1992-02-11  |  10KB  |  341 lines

  1. /*
  2.  * $Id: ibmColor.c,v 5.1 1992/02/12 00:27:10 jfc Exp $
  3.  *
  4.  * Copyright IBM Corporation 1987,1988,1989
  5.  *
  6.  * All Rights Reserved
  7.  *
  8.  * Permission to use, copy, modify, and distribute this software and its
  9.  * documentation for any purpose and without fee is hereby granted,
  10.  * provided that the above copyright notice appear in all copies and that 
  11.  * both that copyright notice and this permission notice appear in
  12.  * supporting documentation, and that the name of IBM not be
  13.  * used in advertising or publicity pertaining to distribution of the
  14.  * software without specific, written prior permission.
  15.  *
  16.  * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17.  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18.  * IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19.  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21.  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22.  * SOFTWARE.
  23.  *
  24. */
  25. /***********************************************************
  26. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  27. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  28.  
  29.                         All Rights Reserved
  30.  
  31. Permission to use, copy, modify, and distribute this software and its 
  32. documentation for any purpose and without fee is hereby granted, 
  33. provided that the above copyright notice appear in all copies and that
  34. both that copyright notice and this permission notice appear in 
  35. supporting documentation, and that the names of Digital or MIT not be
  36. used in advertising or publicity pertaining to distribution of the
  37. software without specific, written prior permission.  
  38.  
  39. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  40. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  41. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  42. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  43. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  44. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  45. SOFTWARE.
  46.  
  47. ******************************************************************/
  48.  
  49. #include "X.h"
  50. #include "Xproto.h"
  51. #include "screenint.h"
  52. #include "scrnintstr.h"
  53. #include "colormapst.h"
  54. #include "colormap.h"
  55.  
  56. #include "OScompiler.h"
  57.  
  58. #include "ibmTrace.h"
  59.  
  60. /* These May Be Modified by ddxProcessArgument() in ibmInit.c */
  61. char *ibmBlackPixelText = "black" ;
  62. char *ibmWhitePixelText = "white" ;
  63.  
  64. static int
  65. ibmGetColorFromString( pCmap, name, pRed, pGreen, pBlue )
  66. ColormapPtr const pCmap ;
  67. char *name ;
  68. unsigned short int *pRed ;
  69. unsigned short int *pGreen ;
  70. unsigned short int *pBlue ;
  71. {
  72. int ndx, ch, red, green, blue ;
  73. int len = strlen( name ) ;
  74.  
  75.     if ( name[0] == '#' ) {
  76.     len-- ;
  77.     name++ ;
  78.     if ( len % 3 ) /* length not multiple of 3 */
  79.         return 0 ;
  80.     len /= 3 ;
  81.     red = green = blue = 0 ;
  82.     do {
  83.         red = green ;
  84.         green = blue ;
  85.         blue = 0 ;
  86.         for ( ndx = len ; ndx-- ; ) {
  87.         blue *= 16 ;
  88.         ch = *name++ ;
  89.         if ( ch >= '0' && ch <= '9' )
  90.             blue += ch - '0' ;
  91.         else if ( ch >= 'A' && ch <= 'F' )
  92.             blue += ch - 'A' + 10 ;
  93.         else if ( ch >= 'a' && ch <= 'f' )
  94.             blue += ch - 'a' + 10 ;
  95.         else
  96.             return 0 ;
  97.         }
  98.     } while ( *name ) ;
  99.     len = 16 - ( len * 4 ) ;
  100.     *pRed = red << len ;
  101.     *pGreen = green << len ;
  102.     *pBlue = blue << len ;
  103.     return 1 ;
  104.     }
  105.     else
  106.     return OsLookupColor( pCmap->pScreen, name, len, pRed, pGreen, pBlue ) ;
  107. }
  108.  
  109. /* Common Initialization Routine */
  110. /* NOTE: ibmBlackPixelText & ibmWhitePixelText are ddx-arguments from
  111.  *     the command line. ( or so I hope )
  112.  */
  113. void
  114. ibmAllocBlackAndWhitePixels( pCmap )
  115. ColormapPtr const pCmap ;
  116. {
  117. unsigned short int r, g, b ;
  118.  
  119. #define SERVER_ID 0
  120.  
  121.     /* Check and see if I can use the command-line BlackPixel */
  122.     if ( !ibmGetColorFromString( pCmap, ibmBlackPixelText, &r, &g, &b ) ) {
  123.     ibmInfoMsg( ( ibmBlackPixelText[0] == '#' )
  124.             ? "illegal numeric color specification \"%s\"\n"
  125.             : "Couldn't find \"%s\" in rgb database\n",
  126.             ibmBlackPixelText ) ;
  127.     ibmInfoMsg( "using default BlackPixel\n" ) ;
  128.     r = g = b = 0 ; /* zeros oughta be pretty black, I think */
  129.     }
  130.     AllocColor( pCmap, &r, &g, &b, &(pCmap->pScreen->blackPixel), SERVER_ID ) ;
  131.  
  132.     /* Check and see if I can use the command-line WhitePixel */
  133.     if ( !ibmGetColorFromString( pCmap, ibmWhitePixelText, &r, &g, &b ) ) {
  134.     ibmInfoMsg( ( ibmWhitePixelText[0] == '#' )
  135.             ? "illegal numeric color specification \"%s\"\n"
  136.             : "Couldn't find \"%s\" in rgb database\n",
  137.             ibmWhitePixelText ) ;
  138.     ibmInfoMsg( "using default WhitePixel\n" ) ;
  139.     r = g = b = 0xFFFF ; /* should be pretty white, I think */
  140.     }
  141.     AllocColor( pCmap, &r, &g, &b, &(pCmap->pScreen->whitePixel), SERVER_ID ) ;
  142.  
  143. return ;
  144. }
  145.  
  146. /* ******** Utilities For MonoChrome Heads -- i.e. 2 colormapEntries ******** */
  147.  
  148. void
  149. ibmResolveColorMono( pred, pgreen, pblue, pVisual )
  150.     unsigned short int *pred, *pgreen, *pblue ;
  151.     VisualPtr pVisual ;
  152. {
  153.     register unsigned long int tmp ;
  154.  
  155.     TRACE(("ibmResolveColorMono(pred=0x%x,pgreen=0x%x,pblue=0x%x,pVis=0x%x)\n",
  156.                         pred,pgreen,pblue,pVisual)) ;
  157.     /* Gets intensity from RGB.  If intensity is >= half, pick white, else
  158.      * pick black.  This may well be more trouble than it's worth. */
  159.      tmp = (((30L * *pred +
  160.         59L * *pgreen +
  161.         11L * *pblue) >> 8) >= (((1<<8)-1)*50))
  162.     ? ~0 : 0 ;
  163.     *pred = *pgreen = *pblue = tmp ;
  164.     return ;
  165. }
  166.  
  167. Bool
  168. ibmCreateStaticGrayColormap( pCmap )
  169. register ColormapPtr const pCmap ;
  170. {
  171.     TRACE( ( "ibmCreateColormapMono(pCmap=0x%x)\n", pCmap ) ) ;
  172.     ibmAllocBlackAndWhitePixels( pCmap ) ;
  173.     return TRUE ;
  174. }
  175.  
  176. /*ARGSUSED*/
  177. void
  178. ibmDestroyColormapMono( pCmap )
  179. ColormapPtr const pCmap ;
  180. {
  181.     TRACE( ( "ibmDestroyColormapMono(pCmap=0x%x)\n", pCmap ) ) ;
  182.     return ;
  183. }
  184.  
  185. /* ********************* Generalized FindColor Function ********************* */
  186.  
  187. unsigned long int
  188. ibmFindColor( cmap, r, g, b )
  189. ColormapPtr cmap ;
  190. unsigned short int r, g, b ;
  191. {
  192.     register int i ;
  193.     register EntryPtr pent ;
  194.     register unsigned long int delta ;
  195.     register unsigned long int bestdiff ;
  196.     unsigned long int best ;
  197.     VisualPtr pVisual ;
  198.     Pixel retval ;
  199.  
  200.     TRACE( ( "ibmFindColor(0x%x,%d,%d,%d,0x%x)\n", cmap, r, g, b ) ) ;
  201.  
  202.     best = 0 ;
  203.     bestdiff = (unsigned long int) ~ 0 ; /* MAXINT for unsigned */
  204.     pVisual = cmap->pVisual ;
  205.  
  206.     switch ( cmap->class ) {
  207.     case StaticColor:
  208.     case PseudoColor:
  209.     case StaticGray:
  210.     case GrayScale:
  211.     for ( i = pVisual->ColormapEntries ; i-- ; ) {
  212.         pent = &(cmap->red[i]) ;
  213.             if ( pent->refcnt != 0 ) {
  214.             delta = ABS((int) pent->co.local.red - (int)r)
  215.                   + ABS((int) pent->co.local.green - (int)g)
  216.                   + ABS((int) pent->co.local.blue - (int)b);
  217.             if (!delta)
  218.                 return i;
  219.             else if (delta < bestdiff) {
  220.                 bestdiff = delta;
  221.                 best = i;
  222.             }
  223.         }
  224.     }
  225.     return best ;
  226.  
  227.     case TrueColor:
  228.     case DirectColor:
  229.     /* red */
  230.     for ( i = pVisual->ColormapEntries ; i-- ; ) {
  231.         delta = ABS((int)cmap->red[i].co.local.red - (int)r);
  232.         if ( delta < bestdiff ) {
  233.         bestdiff = delta ;
  234.         best = i ;
  235.         }
  236.     }
  237.     retval = best << pVisual->offsetRed ;
  238.  
  239.     /* green */
  240.     best = 0 ;
  241.     bestdiff = (unsigned long int) ~ 0 ;
  242.     for ( i = pVisual->ColormapEntries ; i-- ; ) {
  243.         delta = ABS((int)cmap->green[i].co.local.green - (int)g);
  244.         if ( delta < bestdiff ) {
  245.         bestdiff = delta ;
  246.         best = i ;
  247.         }
  248.     }
  249.     retval |= best << pVisual->offsetGreen ;
  250.  
  251.     /* blue */
  252.     best = 0 ;
  253.     bestdiff = (unsigned long int) ~ 0 ;
  254.     for ( i = pVisual->ColormapEntries ; i-- ; ) {
  255.         delta = ABS((int)cmap->blue[i].co.local.blue - (int)b);
  256.         if ( delta < bestdiff ) {
  257.         bestdiff = delta ;
  258.         best = i ;
  259.         }
  260.     }
  261.     return retval | ( best << pVisual->offsetBlue ) ;
  262.  
  263.     default:
  264.     ErrorF( "ibmFindColor: Unsupported Visual Class\n" ) ;
  265.     return 0 ;
  266.     }
  267.     /*NOTREACHED*/
  268. }
  269.  
  270. /* ************************ Initialization Function ************************ */
  271.  
  272. /* Remember that unallocated Colors are UNDEFINED in X, so this routine
  273.    is merely being nice by existing at all.  It turns out that it makes
  274.    life easier in general for a lot of applications which are subject
  275.    to existing colormaps.
  276.  
  277.    You cannot say that these values are bad or wrong, even in the case
  278.    of GrayScale (note ResolveColor is not called), because they are merely
  279.    substitutes for indeterminate uninitialized hardware.  Even in gray,
  280.    they make life nice, it turns out.
  281.  
  282.    T. Paquin  9/87
  283. */
  284.  
  285. #define LOWINTEN 0x8000
  286. #define HI_INTEN 0xFFFF
  287.  
  288. void
  289. ibmDefineDefaultColormapColors( pVisual, pCmap )
  290. register VisualPtr pVisual ;
  291. register ColormapPtr pCmap ;
  292. {
  293. register int i ;
  294.  
  295. /* Read The Note about classes in <X11/X.h> */
  296. if ( pVisual->class & 1 ) /* Dynamic classes have odd values */
  297.     if ( pVisual->class == DirectColor ) /* Can't Handle Yet */
  298.     ibmInfoMsg(
  299.      "ibmDefineDefaultColormapColors: No defaults for DirectColor Visual\n" ) ;
  300.     else /* Must be PseudoColor or GrayScale */
  301.     switch ( i = pVisual->ColormapEntries ) {
  302.        case 2: /* Monochrome */
  303.         pCmap->red[0].co.local.red   = 0 ;
  304.         pCmap->red[0].co.local.green = 0 ;
  305.         pCmap->red[0].co.local.blue  = 0 ;
  306.         pCmap->red[1].co.local.red   = HI_INTEN ;
  307.         pCmap->red[1].co.local.green = HI_INTEN ;
  308.         pCmap->red[1].co.local.blue  = HI_INTEN ;
  309.         break ;
  310.        case 16: /* DEFINING bits to be IRGB */
  311.         while ( i-- > 8 ) {
  312.             pCmap->red[i].co.local.red   = (HI_INTEN*((i&4)>>2)) ;
  313.             pCmap->red[i].co.local.green = (HI_INTEN*((i&2)>>1)) ;
  314.             pCmap->red[i].co.local.blue  = (HI_INTEN*(i&1)) ;
  315.         }
  316.         while ( i-- ) {
  317.             pCmap->red[i].co.local.red   = (LOWINTEN*((i&4)>>2)) ;
  318.             pCmap->red[i].co.local.green = (LOWINTEN*((i&2)>>1)) ;
  319.             pCmap->red[i].co.local.blue  = (LOWINTEN*(i&1)) ;
  320.         }
  321.         break ;
  322.        case 256: /* defining to be RRRGGGBB */
  323.         while ( i-- ) {
  324.             pCmap->red[i].co.local.red  =
  325.                 ( ( i & 0xE0 ) >> 5 ) * ( HI_INTEN / 7 ) ;
  326.             pCmap->red[i].co.local.green=
  327.                 ( ( i & 0x1C ) >> 2 ) * ( HI_INTEN / 7 ) ;
  328.             pCmap->red[i].co.local.blue =
  329.                 ( i & 0x03 ) * ( HI_INTEN / 3 ) ;
  330.         }
  331.         break ;
  332.        default:
  333.         ErrorF(
  334.     "ibmDefineDefaultColormapColors: Bad num of map entries\n" ) ;
  335.         /* But no big deal ; who cares? */
  336.         break ;
  337.     } /* end switch */
  338.  
  339. return ;
  340. }
  341.