home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_Colormap.c < prev    next >
C/C++ Source or Header  |  2001-05-21  |  11KB  |  375 lines

  1.  
  2. /* $XConsortium: StNColor.c /main/20 1996/10/22 14:23:13 kaleb $ */
  3. /*
  4.  
  5. Copyright (c) 1986  X Consortium
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in
  15. all copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  20. X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  21. AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  
  24. Except as contained in this notice, the name of the X Consortium shall not be
  25. used in advertising or otherwise to promote the sale, use or other dealings
  26. in this Software without prior written authorization from the X Consortium.
  27.  
  28. */
  29.  
  30. #include <stdio.h>
  31. #include "Xlib_private.h"
  32. #include "Xcmsint.h"
  33.  
  34. extern void _XcmsCopyCmapRecAndFree();
  35. extern void _XcmsDeleteCmapRec();
  36. extern void _XcmsAddCmapRec();
  37. extern void _XcmsRGB_to_XColor();
  38. extern Status _XcmsResolveColorString (XcmsCCC ccc, _Xconst char **color_string, XcmsColor *pColor_exact_return, XcmsColorFormat result_format);
  39.  
  40.  
  41. #define COLORMAP_RGB 0
  42. #define COLORMAP_PAL 1
  43.  
  44. typedef struct {
  45.     HPS    prevhps;
  46.     LONG    maptype;
  47.     LONG    count;
  48.     HPAL    hpal, oldpal;
  49.     USHORT *allocbmp;
  50.     ULONG  *Colors;
  51. } Xlib_Colormap;
  52.  
  53. void Xlib_SetColormap(HPS hps, Xlib_Colormap *cmap)
  54. {
  55.     DBUG_ENTER("Xlib_SetColormap")
  56.         cmap->prevhps = hps;
  57.     if(cmap->count && !cmap->oldpal)
  58.         cmap->oldpal = GpiSelectPalette(hps, cmap->hpal);
  59.     else if(cmap->count && cmap->oldpal)
  60.         GpiSelectPalette(hps, cmap->hpal);
  61.     else if (!cmap->count && cmap->oldpal)
  62.         GpiSelectPalette(hps, cmap->oldpal);
  63.     DBUG_VOID_RETURN;
  64. }
  65.  
  66. void Xlib_ApplyColormap(Drawable d, Colormap colormap)
  67. {
  68.     DBUG_ENTER("Xlib_ApplyColormap")
  69.         HPS hps;
  70.     ULONG pc;
  71.  
  72.     if (GetDrawableHeight(d, 0, &hps, GC_NOPATH))
  73.     {
  74.         Xlib_SetColormap(hps, (Xlib_Colormap *)colormap);
  75.         if(WinIsWindow(mainhab, d))
  76.             WinRealizePalette(d, hps, &pc);
  77.     }
  78.  
  79.     DBUG_VOID_RETURN;
  80. }
  81.  
  82. Colormap Xlib_DupColormap(Display *dpy, Colormap colormap)
  83. {
  84.     DBUG_ENTER("Xlib_DupColormap")
  85.     Xlib_Colormap *result = malloc(sizeof(Xlib_Colormap));
  86.     memcpy(result,(void *)colormap,sizeof(Xlib_Colormap));
  87.     result->Colors = malloc(sizeof(ULONG)*result->count);
  88.     memcpy(result->Colors, ((Xlib_Colormap *)colormap)->Colors, sizeof(ULONG)*result->count);
  89.     result->oldpal = result->prevhps = NULLHANDLE;
  90.     DBUG_RETURN((Colormap)result);
  91. }
  92.  
  93. Colormap Xlib_CreateDefaultColormap(Display *dpy)
  94. {
  95.     DBUG_ENTER("Xlib_CreateDefaultColormap")
  96.         Xlib_Colormap *result = malloc(sizeof(Xlib_Colormap));
  97.     HPS hps = WinGetScreenPS(hwndDesktop);
  98.     ULONG *array = NULL;
  99.     int t;
  100.  
  101.     result->hpal = GpiQueryPalette(hps);
  102.     if(!result->hpal || (result->count = GpiQueryPaletteInfo(result->hpal, NULLHANDLE, 0L, 0L, 0L, array)) < 1)
  103.     {
  104.         /* If we don't have a palette get the logical color table and create a palette with it */
  105.         ULONG aul[16];
  106.         result->count = GpiQueryLogColorTable(hps, 0L, 0L, 16L, aul);
  107.         result->Colors = malloc(sizeof(ULONG)*result->count);
  108.         memcpy(result->Colors, aul, sizeof(LONG)*result->count);
  109.         result->hpal = GpiCreatePalette(mainhab, LCOL_PURECOLOR, LCOLF_CONSECRGB,result->count, result->Colors);
  110.     }
  111.     else
  112.     {
  113.         result->Colors = malloc(sizeof(ULONG)*result->count);
  114.         memcpy(result->Colors, array, sizeof(ULONG)*result->count);
  115.     }
  116.     result->allocbmp = malloc(result->count*sizeof(USHORT));
  117.     for(t=0;t<result->count;t++)
  118.         result->allocbmp[t] = 1;
  119.     WinReleasePS(hps);
  120.     DBUG_RETURN((Colormap)result);
  121. }
  122.  
  123. Colormap Xlib_CreateColormap(Display* dpy, Window w, Visual* visual, int alloc)
  124. {
  125.     DBUG_ENTER("Xlib_CreateColormap")
  126.         Xlib_Colormap *result;
  127.     result = calloc(1, sizeof(Xlib_Colormap));
  128.     if (!alloc || !visual->map_entries) {
  129.         result->maptype = (!visual->map_entries)?0:1;
  130.         result->count = 0;
  131.         result->Colors = NULL;
  132.         result->allocbmp = NULL;
  133.     } else if (alloc == AllocAll){
  134.         result->Colors = calloc(1, sizeof(ULONG)*visual->map_entries);
  135.         result->allocbmp = calloc(1, sizeof(USHORT)*visual->map_entries);
  136.         result->count = visual->map_entries;
  137.         result->maptype = 1;
  138.     } else {
  139.         result->Colors = calloc(1, sizeof(ULONG)*alloc);
  140.         result->allocbmp = calloc(1, sizeof(USHORT)*alloc);
  141.         result->count = alloc;
  142.         result->maptype = 1;
  143.     }
  144.     result->hpal = result->oldpal = result->prevhps = 0;;
  145.     if(result->count > 0)
  146.         result->hpal = GpiCreatePalette(mainhab, LCOL_PURECOLOR, LCOLF_CONSECRGB,result->count, result->Colors);
  147.     DBUG_RETURN((Colormap)result);
  148. }
  149.  
  150. void Xlib_FreeColormap(Xlib_Colormap *colormap)
  151. {
  152.     DBUG_ENTER("Xlib_FreeColormap")
  153.         if(colormap->hpal)
  154.             GpiDeletePalette(colormap->hpal);
  155.     if(colormap->Colors)
  156.         free(colormap->Colors);
  157.     free(colormap);
  158.     DBUG_VOID_RETURN;
  159. }
  160.  
  161. Colormap XCopyColormapAndFree(Display* dpy, Colormap colormap)
  162. {
  163.     DBUG_ENTER("XCopyColormapAndFree")
  164.         Colormap result = Xlib_DupColormap(dpy, colormap);
  165.     Xlib_FreeColormap((Xlib_Colormap *)colormap);
  166.     SyncHandle();
  167.     _XcmsCopyCmapRecAndFree(dpy, colormap, result);
  168.     DBUG_RETURN(result);
  169. }
  170.  
  171. Colormap XCreateColormap(Display* dpy, Window w, Visual* visual, int alloc)
  172. {
  173.     DBUG_ENTER("XCreateColormap")
  174.         Colormap result = Xlib_CreateColormap(dpy,w,visual,alloc);
  175.     SyncHandle();
  176.     _XcmsAddCmapRec(dpy, result, w, visual);
  177.     DBUG_RETURN(result);
  178. }
  179.  
  180. Status XAllocColor(Display *display, Colormap colormap, XColor *screen_in_out)
  181. {
  182.     DBUG_ENTER("XAllocColor")
  183.         HPS hps = WinGetScreenPS(hwndDesktop);
  184.     LONG newcol = GpiQueryNearestColor(hps, 0L, (PC_RESERVED*16777216) + (screen_in_out->red*65536) + (screen_in_out->green*256) + screen_in_out->blue);
  185.     if(newcol < 0)
  186.         screen_in_out->pixel = ((screen_in_out->red*65536) + (screen_in_out->green*256) + screen_in_out->blue) >> 8;
  187.     else
  188.     {
  189.         screen_in_out->blue = (newcol & 0xFF);
  190.         screen_in_out->green = ((newcol >> 8) & 0xFF);
  191.         screen_in_out->red =  ((newcol >> 16) & 0xFF);
  192.         screen_in_out->pixel = newcol;
  193.     }
  194.     WinReleasePS(hps);
  195.     DBUG_RETURN(True);
  196. }
  197.  
  198. Status XStoreColor(Display* display, Colormap colormap, XColor* screen_in)
  199. {
  200.     DBUG_ENTER("XStoreColor")
  201.         Xlib_Colormap *cmap = (Xlib_Colormap *)colormap;
  202.     int index;
  203.     int blue = 0, red = 0, green = 0;
  204.     if(!cmap)
  205.         DBUG_RETURN(True);
  206.  
  207.     index = screen_in->pixel;
  208.     if(index >= cmap->count)
  209.         DBUG_RETURN(False);
  210.  
  211.     if(cmap->allocbmp[index] == 1)
  212.     {
  213.         blue = (cmap->Colors[index] & 0xFF);
  214.         green = ((cmap->Colors[index] >> 8) & 0xFF);
  215.         red =  ((cmap->Colors[index] >> 16) & 0xFF);
  216.     }
  217.  
  218.     if(screen_in->flags & DoRed)
  219.         red = screen_in->red;
  220.     if(screen_in->flags & DoGreen)
  221.         green = screen_in->green;
  222.     if(screen_in->flags & DoBlue)
  223.         blue = screen_in->blue;
  224.  
  225.     cmap->Colors[index] = ((red*65536) + (green*256) + blue) >> 8;
  226.     cmap->allocbmp[index] = 1;
  227.     if(GpiSetPaletteEntries(cmap->hpal, LCOLF_CONSECRGB, index, 1, &cmap->Colors[index]) == FALSE)
  228.         DBUG_RETURN(False);
  229.     DBUG_RETURN(True);
  230. }
  231.  
  232. Status XAllocColorCells(Display* display, Colormap colormap, Bool contig,
  233.                         unsigned long* plane_masks_return, unsigned int nplanes,
  234.                         unsigned long* pixels_return, unsigned int npixels)
  235. {
  236.     DBUG_ENTER("XAllocColorCells")
  237.         Xlib_Colormap *cmap = (Xlib_Colormap *)colormap;
  238.     int t, x=0;
  239.  
  240.     if(!cmap)
  241.         DBUG_RETURN(BadColor);
  242.     if(nplanes < 0 || npixels < 1)
  243.         DBUG_RETURN(BadValue);
  244.     if(!contig)
  245.     {
  246.         for(t=0;t<cmap->count;t++) {
  247.             if(!cmap->allocbmp[t]) {
  248.                 pixels_return[x] = t;
  249.                 x++;
  250.                 if(x == npixels)
  251.                     break;
  252.             }
  253.             DBUG_RETURN(False);
  254.         }
  255.     } else {
  256.         for(t=0;t<cmap->count;t++) {
  257.             if(t+npixels < cmap->count) {
  258.                 int z, failed = 0;;
  259.                 for(z=0;z<npixels;z++)
  260.                     if(cmap->allocbmp[z+t])
  261.                         failed = 1;
  262.                 if(failed == 0) {
  263.                     for(z=0;z<npixels;z++)
  264.                         pixels_return[z] = z+t;
  265.                 } else {
  266.                     DBUG_RETURN(False);
  267.                 }
  268.             }
  269.         }
  270.     }
  271.     /* Not sure about planes yet */
  272.  
  273.     DBUG_RETURN(True);
  274. }
  275.  
  276. Status XAllocColorPlanes(Display* display, Colormap colormap, Bool contig,
  277.                          unsigned long* pixels_return, int ncolors, int nreds, int ngreens, int nblues,
  278.                          unsigned long* rmask_return, unsigned long* gmask_return, unsigned long* bmask_return)
  279. {
  280.     DBUG_ENTER("XAllocColorPlanes")
  281.         /* Still working on the plane code... check back soon ;) Brian */
  282.         DBUG_RETURN(True);
  283. }
  284.  
  285. int XFreeColormap(Display* dpy, Colormap colormap)
  286. {
  287.     DBUG_ENTER("XFreeColormap")
  288.         Xlib_FreeColormap((Xlib_Colormap *)colormap);
  289.     SyncHandle();
  290.     _XcmsDeleteCmapRec(dpy, colormap);
  291.     DBUG_RETURN(1);
  292. }
  293.  
  294. int XFreeColors(Display* display, Colormap colormap,
  295.                 unsigned long* pixels, int npixels, unsigned long planes)
  296. {
  297.     DBUG_ENTER("XFreeColors")
  298.         Xlib_Colormap *cmap = (Xlib_Colormap *)colormap;
  299.     int t;
  300.  
  301.     if(!cmap)
  302.         DBUG_RETURN(BadColor);
  303.     /* I am making an untested assumption that if you try to allocate
  304.      a color that is already in the palette it will not use a new
  305.      palette entry - Brian */
  306.     for(t=0;t<npixels;t++) {
  307.         if(pixels[t] < cmap->count) {
  308.             cmap->Colors[pixels[t]] = 0L;
  309.             cmap->allocbmp[pixels[t]] = 0;
  310.         }
  311.     }
  312.     DBUG_RETURN(True);
  313. }
  314.  
  315.  
  316. int XSetWindowColormap(Display* dpy, Window w, Colormap colormap)
  317. {
  318.     DBUG_ENTER("XSetWindowColormap")
  319.         XSetWindowAttributes attrib;
  320.     attrib.colormap = colormap;
  321.     XChangeWindowAttributes(dpy, w, CWColormap, &attrib);
  322.     DBUG_RETURN(1);
  323. }
  324.  
  325. int XStoreColors(Display* display, Colormap colormap, XColor* color, int ncolors)
  326. {
  327.     DBUG_ENTER("XStoreColors")
  328.         int t;
  329.     for(t=0;t<ncolors;t++)
  330.         XStoreColor(display, colormap, &color[t]);
  331.     DBUG_RETURN(True);
  332. }
  333.  
  334.  
  335. int XStoreNamedColor(Display* dpy, Colormap cmap,
  336.                      _Xconst char* name, unsigned long pixel, int flags)
  337. {
  338.     XcmsCCC ccc;
  339.     XcmsColor cmsColor_exact;
  340.     XColor scr_def;
  341.  
  342.     /*
  343.      * Let's Attempt to use Xcms approach to Parse Color
  344.      */
  345.     if ((ccc = XcmsCCCOfColormap(dpy, cmap)) != (XcmsCCC)NULL) {
  346.         if (_XcmsResolveColorString(ccc, &name, &cmsColor_exact,
  347.                                     XcmsRGBFormat) >= XcmsSuccess) {
  348.             _XcmsRGB_to_XColor(&cmsColor_exact, &scr_def, 1);
  349.             scr_def.pixel = pixel;
  350.             scr_def.flags = flags;
  351.             return XStoreColor(dpy, cmap, &scr_def);
  352.         }
  353.     }
  354.  
  355.     return 0;
  356. }
  357.  
  358. Colormap *XListInstalledColormaps(Display* display, Window w, int* num_return)
  359. {
  360.     DBUG_ENTER("XListInstalledColormaps")
  361.         WinAttribData *attrib = WinQueryWindowPtr(w, QWP_WINATTRIB);
  362.  
  363.     if(attrib && attrib->winattrib.colormap) {
  364.         *num_return = 1;
  365.         /* We may need to make a copy but I don't think so */
  366.         DBUG_RETURN((Colormap)&attrib->winattrib.colormap);
  367.     }
  368.     *num_return = 0;
  369.     DBUG_RETURN(False);
  370. }
  371.  
  372. int XInstallColormap(Display* display, Colormap colormap);
  373. int XUninstallColormap(Display* display, Colormap colormap);
  374.  
  375.