1 #include <extUtil.h>
2 
3 /*
4   8.8.99 wi
5 
6   readStdCmap allociert den groessten noch freien zusammenhaengenden Speicherbereich,
7   bzw. anzCells wenn mehr als anzCells frei ist,
8   in der default colormap (max 256 colorcells). Und liefert den Inhalt der colormap.
9 
10 */
11 
12 #define TEST        0
13 #define MAX_BLOCKS  300
14 #define MAX_CELLS   256
15 #define MAX_PLANES  100
16 
readStdCmap(Display ** ptr_dpy,int * ptr_dpycells,Colormap * ptr_cmap,XColor ** ptr_c,unsigned long ** ptr_pix,unsigned int * ptr_npixels,int anzCells)17 void readStdCmap( Display **ptr_dpy, int *ptr_dpycells, Colormap *ptr_cmap, XColor **ptr_c,
18                  unsigned long **ptr_pix, unsigned int *ptr_npixels, int anzCells )
19   {
20   register int i, j, k, n, nmax;
21   int maxBlock=0, maxCells=0;
22 
23   Display *dpy;                              /* **ptr_dpy (Display Nr.) */
24   int      scn;
25   XColor *xcolor;                            /* **ptr_c (full actual colormap) */
26 
27   int           dpycells;                    /* *ptr_dpycells (amount of colormap-cells) */
28   Colormap      cmap;
29   unsigned int  npixels;                     /* *ptr_npixels (amount of allocated colormap-cells */
30 
31   unsigned long plane_masks_return[MAX_PLANES];
32   unsigned long *pixels_return;              /* **ptr_pix (all allocated colormap-cells)*/
33   unsigned int  nplanes, status;
34 
35   unsigned long pix_return[MAX_BLOCKS][MAX_CELLS];
36   unsigned int  npix[MAX_BLOCKS];
37 
38 
39   nplanes= 0;
40 
41   /* get a connection */
42   dpy = XOpenDisplay (0);
43 
44   /* get the default screen */
45   scn = DefaultScreen (dpy);
46 
47   /* create a colormap */
48   cmap = XDefaultColormap( dpy, scn);
49 
50   /* Nr of available DisplayCells in the default colormap*/
51   dpycells = DisplayCells( dpy, scn );
52 
53   /* read the colormap */
54   /* max color = 65535 */
55   if ( ( xcolor = (XColor *)malloc( (dpycells+1) * sizeof(XColor))) == NULL )
56     printf("\n\n ERROR: malloc Fehler in readStdCmap (xcolor)\n\n" ) ;
57   else
58     printf ("\n xcolor in readStdCmap allocated \n");
59 
60 
61   for (i=0; i<dpycells; i++)
62     {
63     xcolor[i].pixel=(unsigned long)i;
64     XQueryColor(dpy, cmap, &xcolor[i]);
65     }
66 
67   /* determine and allocate the biggest contiguous range of colorcells */
68 
69   if ( ( pixels_return = (unsigned long *)malloc( (dpycells+1) * sizeof(unsigned long))) == NULL )
70     printf("\n\n ERROR: malloc Fehler in readStdCmap (pixels_return)\n\n" ) ;
71   else
72     printf ("\n pixels_return in readStdCmap allocated \n");
73   for (i=0; i<dpycells; i++) pixels_return[i]=(unsigned long)0;
74 
75   /* look how much collor-cells are free (not necessarily continous) */
76   for (npixels=1; npixels<dpycells; npixels++)
77     {
78     status = XAllocColorCells( dpy, cmap, 1, plane_masks_return, nplanes, pixels_return, npixels );
79 #if TEST
80     printf ("status:%u: found %u free cells, 1st cell:%u\n",  status, npixels, pixels_return[0]);
81 #endif
82     if (status==0)  break;
83     XFreeColors( dpy, cmap, pixels_return, npixels, nplanes );
84     }
85 #if TEST
86   printf ("status:%u: %u cells could not be allocated\n", status, npixels );
87 #endif
88 
89   n=1; nmax=0, j=0;
90   /* check one cell to much (npixels) to make shure that the last cont. block will also be allocated */
91   for (i=1; i<npixels; i++)
92     {
93     /* look if the cells are continuous */
94     if (pixels_return[i]==pixels_return[i-1]+1)
95       {
96 #if TEST
97       printf("pixels_return[%d]=%u %d\n", i, pixels_return[i], pixels_return[i]);
98 #endif
99       n++ ;
100       }
101     else
102       {
103 #if TEST
104       printf(" not continous: pixels_return[%d]=%u pixels_return[%d-1]+1=%u\n",
105         i, pixels_return[i], i, pixels_return[i-1]+1);
106 #endif
107       /* allocate the continuous block */
108       if (n<anzCells) nmax=n; else nmax=anzCells;
109       npix[j]=nmax;
110       status = 0;
111       status = XAllocColorCells( dpy, cmap, 1, plane_masks_return, nplanes,
112                                  pix_return[j], npix[j] );
113       if (status==0)
114         {
115         printf ("WARNING: Could not alloc %d color cells for step=%d! \n", nmax, j);
116         npix[j]=0;
117         }
118 #if TEST
119       else printf (" Block:%d with %d cells, start with cell %d allocated\n",
120         j, npix[j], pix_return[j][0] );
121 #endif
122       /* keep track of the biggest block */
123       if (maxCells<npix[j]) { maxCells=npix[j]; maxBlock=j; }
124       j++;
125       if (nmax==anzCells) goto found_enough;
126       n=1;
127       }
128     }
129   found_enough:;
130   for (i=0; i<j; i++)
131     {
132     if (maxBlock==i)
133       {
134 #if TEST
135       printf ("biggest continuous block %d with %u cells saved\n", maxBlock, maxCells );
136 #endif
137       for (k=0; k<npix[i]; k++)
138         {
139         pixels_return[k]=pix_return[i][k];
140 #if TEST
141         printf ("%d. cell=%lu\n", k, pixels_return[k]);
142 #endif
143         }
144       }
145     else
146       {
147       XFreeColors( dpy, cmap, pix_return[i], npix[i], nplanes );
148 #if TEST
149       printf ("free block %d with %u cells\n", maxBlock, maxCells );
150       for (k=0; k<npix[i]; k++)
151         {
152         printf ("%d. cell=%lu\n", k, pix_return[i][k]);
153         }
154 #endif
155       }
156     }
157 
158   if (nmax==1) nmax=0;
159   /* return pointer */
160   *ptr_c   = xcolor;
161   *ptr_dpy = dpy;
162   *ptr_pix = pixels_return;
163   *ptr_dpycells = dpycells;
164   *ptr_cmap     = cmap;
165   *ptr_npixels  = maxCells;
166 
167 
168   /* nice to know .. */
169 
170   printf ("DefaultDepth=%d\n", DefaultDepth( dpy, scn ));
171   printf ("DisplayCells=%d\n", dpycells);
172   printf ("DisplayPlanes=%d\n", DisplayPlanes( dpy, scn ));
173 
174 }
175 
176 
177 
178