1 /* Copyright (C) 2001-2012 Artifex Software, Inc.
2    All Rights Reserved.
3 
4    This software is provided AS-IS with no warranty, either express or
5    implied.
6 
7    This software is distributed under license and may not be copied,
8    modified or distributed except as expressly authorized under the terms
9    of the license contained in the file LICENSE in this distribution.
10 
11    Refer to licensing information at http://www.artifex.com or contact
12    Artifex Software, Inc.,  7 Mt. Lassen Drive - Suite A-134, San Rafael,
13    CA  94903, U.S.A., +1(415)492-9861, for further information.
14 */
15 
16 
17 /* X Windows color mapping */
18 #include "math_.h"
19 #include "x_.h"
20 #include "gx.h"			/* for gx_bitmap; includes std.h */
21 #include "gserrors.h"
22 #include "gxdevice.h"
23 #include "gdevx.h"
24 
25 /* ---------------- Utilities ---------------- */
26 
27 static void
gs_x_free(gs_memory_t * mem,void * obj,client_name_t cname)28 gs_x_free(gs_memory_t *mem, void *obj, client_name_t cname)
29 {
30     gs_free(mem, obj, 0 /*ignored*/, 0 /*ignored*/, cname);
31 }
32 
33 /* ---------------- Color mapping setup / cleanup ---------------- */
34 
35 #if HaveStdCMap
36 
37 /* Install a standard color map in the device. */
38 /* Sets std_cmap.* except for free_map. */
39 static bool
set_cmap_values(x11_cmap_values_t * values,int maxv,int mult)40 set_cmap_values(x11_cmap_values_t *values, int maxv, int mult)
41 {
42     int i;
43 
44     if (maxv < 1 || maxv > 63 || (maxv & (maxv + 1)) ||
45         (mult & (mult - 1))
46         )
47         return false;
48     values->cv_shift = 16 - small_exact_log2(maxv + 1);
49     for (i = 0; i <= maxv; ++i)
50         values->nearest[i] = X_max_color_value * i / maxv;
51     for (i = 0; mult != (1 << i); ++i)
52         DO_NOTHING;
53     values->pixel_shift = i;
54     return true;
55 }
56 static void
set_std_cmap(gx_device_X * xdev,XStandardColormap * map)57 set_std_cmap(gx_device_X *xdev, XStandardColormap *map)
58 {
59     xdev->cman.std_cmap.map = map;
60     xdev->cman.std_cmap.fast =
61         set_cmap_values(&xdev->cman.std_cmap.red, map->red_max, map->red_mult) &&
62         set_cmap_values(&xdev->cman.std_cmap.green, map->green_max, map->green_mult) &&
63         set_cmap_values(&xdev->cman.std_cmap.blue, map->blue_max, map->blue_mult);
64 }
65 
66 /* Get the Standard colormap if available. */
67 /* Uses: dpy, scr, cmap. */
68 static XStandardColormap *
x_get_std_cmap(gx_device_X * xdev,Atom prop)69 x_get_std_cmap(gx_device_X * xdev, Atom prop)
70 {
71     int i;
72     XStandardColormap *scmap, *sp;
73     int nitems;
74 
75     if (XGetRGBColormaps(xdev->dpy, RootWindowOfScreen(xdev->scr),
76                          &scmap, &nitems, prop))
77         for (i = 0, sp = scmap; i < nitems; i++, sp++)
78             if (xdev->cmap == sp->colormap)
79                 return sp;
80 
81     return NULL;
82 }
83 
84 /* Create a Standard colormap for a TrueColor or StaticGray display. */
85 /* Return true if the allocation was successful. */
86 /* Uses: vinfo.  Sets: std_cmap.*. */
87 static bool
alloc_std_cmap(gx_device_X * xdev,bool colored)88 alloc_std_cmap(gx_device_X *xdev, bool colored)
89 {
90     XStandardColormap *cmap = XAllocStandardColormap();
91 
92     if (cmap == 0)
93         return false;		/* can't allocate */
94     /*
95      * Some buggy X servers (including XFree86) don't set any of the
96      * _mask values for StaticGray visuals.  Compensate for that here.
97      */
98     if ((cmap->red_max = xdev->vinfo->red_mask) == 0) {
99         cmap->red_max = (1 << xdev->vinfo->depth) - 1;
100         cmap->red_mult = 1;
101     } else {
102         for (cmap->red_mult = 1; (cmap->red_max & 1) == 0;) {
103             cmap->red_max >>= 1;
104             cmap->red_mult <<= 1;
105         }
106     }
107     if (colored) {
108         for (cmap->green_max = xdev->vinfo->green_mask, cmap->green_mult = 1;
109              (cmap->green_max & 1) == 0;
110              ) {
111             cmap->green_max >>= 1;
112             cmap->green_mult <<= 1;
113         }
114         for (cmap->blue_max = xdev->vinfo->blue_mask, cmap->blue_mult = 1;
115              (cmap->blue_max & 1) == 0;
116              ) {
117             cmap->blue_max >>= 1;
118             cmap->blue_mult <<= 1;
119         }
120     } else {
121         cmap->green_max = cmap->blue_max = cmap->red_max;
122         cmap->green_mult = cmap->blue_mult = cmap->red_mult;
123     }
124     set_std_cmap(xdev, cmap);
125     xdev->cman.std_cmap.free_map = true;
126     return true;
127 }
128 
129 #endif
130 
131 /* Allocate the dynamic color table, if needed and possible. */
132 /* Uses: vinfo, cman.num_rgb.  Sets: cman.dynamic.*. */
133 static void
alloc_dynamic_colors(gx_device_X * xdev,int num_colors)134 alloc_dynamic_colors(gx_device_X * xdev, int num_colors)
135 {
136     if (num_colors > 0) {
137         xdev->cman.dynamic.colors = (x11_color_t **)
138             gs_malloc(xdev->memory, sizeof(x11_color_t *), xdev->cman.num_rgb,
139                       "x11 cman.dynamic.colors");
140         if (xdev->cman.dynamic.colors) {
141             int i;
142 
143             xdev->cman.dynamic.size = xdev->cman.num_rgb;
144             xdev->cman.dynamic.shift = 16 - xdev->vinfo->bits_per_rgb;
145             for (i = 0; i < xdev->cman.num_rgb; i++)
146                 xdev->cman.dynamic.colors[i] = NULL;
147             xdev->cman.dynamic.max_used = min(256, num_colors);
148             xdev->cman.dynamic.used = 0;
149         }
150     }
151 }
152 
153 /* Allocate an X color, updating the reverse map. */
154 /* Return true if the allocation was successful. */
155 static bool
x_alloc_color(gx_device_X * xdev,XColor * xcolor)156 x_alloc_color(gx_device_X *xdev, XColor *xcolor)
157 {
158     x11_rgb_t rgb;
159 
160     rgb.rgb[0] = xcolor->red;
161     rgb.rgb[1] = xcolor->green;
162     rgb.rgb[2] = xcolor->blue;
163     if (!XAllocColor(xdev->dpy, xdev->cmap, xcolor))
164         return false;
165     if (xcolor->pixel < xdev->cman.color_to_rgb.size) {
166         x11_rgb_t *pxrgb = &xdev->cman.color_to_rgb.values[xcolor->pixel];
167 
168         memcpy(pxrgb->rgb, rgb.rgb, sizeof(rgb.rgb));
169         pxrgb->defined = true;
170     }
171     return true;
172 }
173 
174 /* Free X colors, updating the reverse map. */
175 static void
x_free_colors(gx_device_X * xdev,x_pixel * pixels,int count)176 x_free_colors(gx_device_X *xdev, x_pixel *pixels /*[count]*/, int count)
177 {
178     int i;
179     x_pixel pixel;
180 
181     XFreeColors(xdev->dpy, xdev->cmap, pixels, count, 0);
182     for (i = 0; i < count; ++i)
183         if ((pixel = pixels[i]) < xdev->cman.color_to_rgb.size)
184             xdev->cman.color_to_rgb.values[pixel].defined = false;
185 }
186 
187 /* Free a partially filled color cube or ramp. */
188 /* Uses: dpy, cmap.  Uses and sets: cman.dither_ramp. */
189 static void
free_ramp(gx_device_X * xdev,int num_used,int size)190 free_ramp(gx_device_X * xdev, int num_used, int size)
191 {
192     if (num_used - 1 > 0)
193         x_free_colors(xdev, xdev->cman.dither_ramp + 1, num_used - 1);
194     gs_x_free(xdev->memory, xdev->cman.dither_ramp, "x11_setup_colors");
195     xdev->cman.dither_ramp = NULL;
196 }
197 
198 /* Allocate and fill in a color cube or ramp. */
199 /* Return true if the operation succeeded. */
200 /* Uses: dpy, cmap, foreground, background, cman.color_mask. */
201 /* Sets: cman.dither_ramp. */
202 static bool
setup_cube(gx_device_X * xdev,int ramp_size,bool colors)203 setup_cube(gx_device_X * xdev, int ramp_size, bool colors)
204 {
205     int step, num_entries;
206     int max_rgb = ramp_size - 1;
207     int index;
208 
209     if (colors) {
210         num_entries = ramp_size * ramp_size * ramp_size;
211         step = 1;		/* all colors */
212     } else {
213         num_entries = ramp_size;
214         step = (ramp_size + 1) * ramp_size + 1;		/* gray only */
215     }
216 
217     xdev->cman.dither_ramp =
218         (x_pixel *) gs_malloc(xdev->memory, sizeof(x_pixel), num_entries,
219                               "gdevx setup_cube");
220     if (xdev->cman.dither_ramp == NULL)
221         return false;
222 
223     xdev->cman.dither_ramp[0] = xdev->foreground;
224     xdev->cman.dither_ramp[num_entries - 1] = xdev->background;
225     for (index = 1; index < num_entries - 1; index++) {
226         int rgb_index = index * step;
227         int q = rgb_index / ramp_size,
228             r = q / ramp_size,
229             g = q % ramp_size,
230             b = rgb_index % ramp_size;
231         XColor xc;
232 
233         xc.red = (X_max_color_value * r / max_rgb) & xdev->cman.color_mask.red;
234         xc.green = (X_max_color_value * g / max_rgb) & xdev->cman.color_mask.green;
235         xc.blue = (X_max_color_value * b / max_rgb) & xdev->cman.color_mask.blue;
236         if (!x_alloc_color(xdev, &xc)) {
237             free_ramp(xdev, index, num_entries);
238             return false;
239         }
240         xdev->cman.dither_ramp[index] = xc.pixel;
241     }
242 
243     return true;
244 }
245 
246 /* Setup color mapping. */
247 int
gdev_x_setup_colors(gx_device_X * xdev)248 gdev_x_setup_colors(gx_device_X * xdev)
249 {
250     char palette =
251         ((xdev->vinfo->class != StaticGray) &&
252          (xdev->vinfo->class != GrayScale) ? 'C' :	/* Color */
253          (xdev->vinfo->colormap_size > 2) ? 'G' :		/* GrayScale */
254          'M');		/* MonoChrome */
255 
256     if (xdev->ghostview) {
257         Atom gv_colors = XInternAtom(xdev->dpy, "GHOSTVIEW_COLORS", False);
258         Atom type;
259         int format;
260         unsigned long nitems, bytes_after;
261         char *buf;
262 
263         /* Delete property if explicit dest is given */
264         if (XGetWindowProperty(xdev->dpy, xdev->win, gv_colors, 0,
265                                256, (xdev->dest != 0), XA_STRING,
266                                &type, &format, &nitems, &bytes_after,
267                                (unsigned char **)&buf) == 0 &&
268             type == XA_STRING) {
269             nitems = sscanf(buf, "%*s %ld %ld", &(xdev->foreground),
270                             &(xdev->background));
271             if (nitems != 2 || (*buf != 'M' && *buf != 'G' && *buf != 'C')) {
272                 emprintf(xdev->memory, "Malformed GHOSTVIEW_COLOR property.\n");
273                 return_error(gs_error_rangecheck);
274             }
275             palette = max(palette, *buf);
276         }
277     } else {
278         if (xdev->palette[0] == 'c')
279             xdev->palette[0] = 'C';
280         else if (xdev->palette[0] == 'g')
281             xdev->palette[0] = 'G';
282         else if (xdev->palette[0] == 'm')
283             xdev->palette[0] = 'M';
284         palette = max(palette, xdev->palette[0]);
285     }
286 
287     /* set up color mappings here */
288     xdev->cman.color_mask.red = xdev->cman.color_mask.green =
289         xdev->cman.color_mask.blue = X_max_color_value -
290           (X_max_color_value >> xdev->vinfo->bits_per_rgb);
291     xdev->cman.match_mask = xdev->cman.color_mask; /* default */
292     xdev->cman.num_rgb = 1 << xdev->vinfo->bits_per_rgb;
293 
294 #if HaveStdCMap
295     xdev->cman.std_cmap.map = NULL;
296     xdev->cman.std_cmap.free_map = false;
297 #endif
298     xdev->cman.dither_ramp = NULL;
299     xdev->cman.dynamic.colors = NULL;
300     xdev->cman.dynamic.size = 0;
301     xdev->cman.dynamic.used = 0;
302     switch (xdev->vinfo->depth) {
303     case 1: case 2: case 4: case 8: case 16: case 24: case 32:
304         xdev->color_info.depth = xdev->vinfo->depth;
305         break;
306     case 15:
307         xdev->color_info.depth = 16;
308         break;
309     default:
310         emprintf1(xdev->memory,
311                   "Unsupported X visual depth: %d\n",
312                   xdev->vinfo->depth);
313         return_error(gs_error_rangecheck);
314     }
315     {	/* Set up the reverse map from pixel values to RGB. */
316         int count = 1 << min(xdev->color_info.depth, 8);
317 
318         xdev->cman.color_to_rgb.values =
319             (x11_rgb_t *)gs_malloc(xdev->memory, sizeof(x11_rgb_t), count,
320                                    "gdevx color_to_rgb");
321         if (xdev->cman.color_to_rgb.values) {
322             int i;
323 
324             for (i = 0; i < count; ++i)
325                 xdev->cman.color_to_rgb.values[i].defined = false;
326             xdev->cman.color_to_rgb.size = count;
327         } else
328             xdev->cman.color_to_rgb.size = 0;
329     }
330     switch ((int)palette) {
331     case 'C':
332         xdev->color_info.num_components = 3;
333         xdev->color_info.max_gray =
334             xdev->color_info.max_color = xdev->cman.num_rgb - 1;
335 #if HaveStdCMap
336         /* Get a standard color map if available */
337         if (xdev->vinfo->visual == DefaultVisualOfScreen(xdev->scr)) {
338             xdev->cman.std_cmap.map = x_get_std_cmap(xdev, XA_RGB_DEFAULT_MAP);
339         } else {
340             xdev->cman.std_cmap.map = x_get_std_cmap(xdev, XA_RGB_BEST_MAP);
341         }
342         if (xdev->cman.std_cmap.map ||
343             (xdev->vinfo->class == TrueColor && alloc_std_cmap(xdev, true))
344             ) {
345             xdev->color_info.dither_grays = xdev->color_info.dither_colors =
346                 min(xdev->cman.std_cmap.map->red_max,
347                     min(xdev->cman.std_cmap.map->green_max,
348                         xdev->cman.std_cmap.map->blue_max)) + 1;
349             if (xdev->cman.std_cmap.map)
350                 set_std_cmap(xdev, xdev->cman.std_cmap.map);
351         } else
352 #endif
353             /* Otherwise set up a rgb cube of our own */
354             /* The color cube is limited to about 1/2 of the available */
355             /* colormap, the user specified maxRGBRamp (usually 5), */
356             /* or the number of representable colors */
357 #define CUBE(r) (r*r*r)
358 #define CBRT(r) pow(r, 1.0/3.0)
359         {
360             int ramp_size =
361                 min((int)CBRT(xdev->vinfo->colormap_size / 2.0),
362                     min(xdev->maxRGBRamp, xdev->cman.num_rgb));
363 
364             while (!xdev->cman.dither_ramp && ramp_size >= 2) {
365                 xdev->color_info.dither_grays =
366                     xdev->color_info.dither_colors = ramp_size;
367                 if (!setup_cube(xdev, ramp_size, true)) {
368 #ifdef DEBUG
369                     emprintf3(xdev->memory,
370                               "Warning: failed to allocate %dx%dx%d RGB cube.\n",
371                               ramp_size,
372                               ramp_size,
373                               ramp_size);
374 #endif
375                     ramp_size--;
376                     continue;
377                 }
378             }
379 
380             if (!xdev->cman.dither_ramp) {
381                 goto grayscale;
382             }
383         }
384 
385         /* Allocate the dynamic color table. */
386         alloc_dynamic_colors(xdev, CUBE(xdev->cman.num_rgb) -
387                              CUBE(xdev->color_info.dither_colors));
388 #undef CUBE
389 #undef CBRT
390         break;
391     case 'G':
392 grayscale:
393         xdev->color_info.num_components = 1;
394         xdev->color_info.gray_index = 0;
395         xdev->color_info.max_gray = xdev->cman.num_rgb - 1;
396 #if HaveStdCMap
397         /* Get a standard color map if available */
398         xdev->cman.std_cmap.map = x_get_std_cmap(xdev, XA_RGB_GRAY_MAP);
399         if (xdev->cman.std_cmap.map ||
400             (xdev->vinfo->class == StaticGray && alloc_std_cmap(xdev, false))
401             ) {
402             xdev->color_info.dither_grays =
403                 xdev->cman.std_cmap.map->red_max + 1;
404             if (xdev->cman.std_cmap.map)
405                 set_std_cmap(xdev, xdev->cman.std_cmap.map);
406         } else
407 #endif
408             /* Otherwise set up a gray ramp of our own */
409             /* The gray ramp is limited to about 1/2 of the available */
410             /* colormap, the user specified maxGrayRamp (usually 128), */
411             /* or the number of representable grays */
412         {
413             int ramp_size = min(xdev->vinfo->colormap_size / 2,
414                                 min(xdev->maxGrayRamp, xdev->cman.num_rgb));
415 
416             while (!xdev->cman.dither_ramp && ramp_size >= 3) {
417                 xdev->color_info.dither_grays = ramp_size;
418                 if (!setup_cube(xdev, ramp_size, false)) {
419 #ifdef DEBUG
420                     emprintf1(xdev->memory,
421                               "Warning: failed to allocate %d level gray ramp.\n",
422                               ramp_size);
423 #endif
424                     ramp_size /= 2;
425                     continue;
426                 }
427             }
428             if (!xdev->cman.dither_ramp) {
429                 goto monochrome;
430             }
431         }
432 
433         /* Allocate the dynamic color table. */
434         alloc_dynamic_colors(xdev, xdev->cman.num_rgb -
435                              xdev->color_info.dither_grays);
436         break;
437     case 'M':
438 monochrome:
439         xdev->color_info.num_components = 1;
440         xdev->color_info.gray_index = 0;
441         xdev->color_info.max_gray = 1;
442         xdev->color_info.dither_grays = 2;
443         break;
444     default:
445         emprintf1(xdev->memory, "Unknown palette: %s\n", xdev->palette);
446         if (xdev->cman.color_to_rgb.values) {
447             gs_x_free(xdev->memory, xdev->cman.color_to_rgb.values, "gdevx color_to_rgb");
448             xdev->cman.color_to_rgb.values = 0;
449         }
450         return_error(gs_error_rangecheck);
451     }
452 
453 #if HaveStdCMap
454     /*
455      * When comparing colors, if not halftoning, we must only compare as
456      * many bits as actually fit in a pixel, even if the hardware has more.
457      */
458     if (!gx_device_must_halftone(xdev)) {
459         if (xdev->cman.std_cmap.map) {
460             xdev->cman.match_mask.red &=
461                 X_max_color_value << xdev->cman.std_cmap.red.cv_shift;
462             xdev->cman.match_mask.green &=
463                 X_max_color_value << xdev->cman.std_cmap.green.cv_shift;
464             xdev->cman.match_mask.blue &=
465                 X_max_color_value << xdev->cman.std_cmap.blue.cv_shift;
466         }
467     }
468 #endif
469 
470     return 0;
471 }
472 
473 /* Free the dynamic colors when doing an erasepage. */
474 /* Uses: cman.dynamic.*.  Sets: cman.dynamic.used. */
475 void
gdev_x_free_dynamic_colors(gx_device_X * xdev)476 gdev_x_free_dynamic_colors(gx_device_X *xdev)
477 {
478     if (xdev->cman.dynamic.colors) {
479         int i;
480         x11_color_t *xcp;
481         x11_color_t *next;
482 
483         for (i = 0; i < xdev->cman.dynamic.size; i++) {
484             for (xcp = xdev->cman.dynamic.colors[i]; xcp; xcp = next) {
485                 next = xcp->next;
486                 if (xcp->color.pad)
487                     x_free_colors(xdev, &xcp->color.pixel, 1);
488                 gs_x_free(xdev->memory, xcp, "x11_dynamic_color");
489             }
490             xdev->cman.dynamic.colors[i] = NULL;
491         }
492         xdev->cman.dynamic.used = 0;
493     }
494 }
495 
496 /*
497  * Free storage and color map entries when closing the device.
498  * Uses and sets: cman.{std_cmap.map, dither_ramp, dynamic.colors,
499  * color_to_rgb}.  Uses: cman.std_cmap.free_map.
500  */
501 void
gdev_x_free_colors(gx_device_X * xdev)502 gdev_x_free_colors(gx_device_X *xdev)
503 {
504     if (xdev->cman.std_cmap.free_map) {
505         /* XFree is declared as taking a char *, not a void *! */
506         XFree((void *)xdev->cman.std_cmap.map);
507         xdev->cman.std_cmap.free_map = false;
508     }
509     xdev->cman.std_cmap.map = 0;
510     if (xdev->cman.dither_ramp)
511         gs_x_free(xdev->memory, xdev->cman.dither_ramp, "x11 dither_colors");
512     if (xdev->cman.dynamic.colors) {
513         gdev_x_free_dynamic_colors(xdev);
514         gs_x_free(xdev->memory, xdev->cman.dynamic.colors, "x11 cman.dynamic.colors");
515         xdev->cman.dynamic.colors = NULL;
516     }
517     if (xdev->cman.color_to_rgb.values) {
518         gs_x_free(xdev->memory, xdev->cman.color_to_rgb.values, "x11 color_to_rgb");
519         xdev->cman.color_to_rgb.values = NULL;
520         xdev->cman.color_to_rgb.size = 0;
521     }
522 }
523 
524 /* ---------------- Driver color mapping calls ---------------- */
525 
526 /* Define a table for computing N * X_max_color_value / D for 0 <= N <= D, */
527 /* 1 <= D <= 7. */
528 /* This requires a multiply and a divide otherwise; */
529 /* integer multiply and divide are slow on all platforms. */
530 #define CV_FRACTION(n, d) ((X_color_value)(X_max_color_value * (n) / (d)))
531 #define ND(n, d) CV_FRACTION(n, d)
532 static const X_color_value cv_tab1[] = {
533     ND(0,1), ND(1,1)
534 };
535 static const X_color_value cv_tab2[] = {
536     ND(0,2), ND(1,2), ND(2,2)
537 };
538 static const X_color_value cv_tab3[] = {
539     ND(0,3), ND(1,3), ND(2,3), ND(3,3)
540 };
541 static const X_color_value cv_tab4[] = {
542     ND(0,4), ND(1,4), ND(2,4), ND(3,4), ND(4,4)
543 };
544 static const X_color_value cv_tab5[] = {
545     ND(0,5), ND(1,5), ND(2,5), ND(3,5), ND(4,5), ND(5,5)
546 };
547 static const X_color_value cv_tab6[] = {
548     ND(0,6), ND(1,6), ND(2,6), ND(3,6), ND(4,6), ND(5,6), ND(6,6)
549 };
550 static const X_color_value cv_tab7[] = {
551     ND(0,7), ND(1,7), ND(2,7), ND(3,7), ND(4,7), ND(5,7), ND(6,7), ND(7,7)
552 };
553 #undef ND
554 static const X_color_value *const cv_tables[] =
555 {
556     0, cv_tab1, cv_tab2, cv_tab3, cv_tab4, cv_tab5, cv_tab6, cv_tab7
557 };
558 
559 /* Some C compilers don't declare the abs function in math.h. */
560 /* Provide one of our own. */
561 static inline int
iabs(int x)562 iabs(int x)
563 {
564     return (x < 0 ? -x : x);
565 }
566 
567 /* Map RGB values to a pixel value. */
568 gx_color_index
gdev_x_map_rgb_color(gx_device * dev,const gx_color_value cv[])569 gdev_x_map_rgb_color(gx_device * dev, const gx_color_value cv[])
570 {
571     gx_device_X *const xdev = (gx_device_X *) dev;
572     gx_color_value r = cv[0];
573     gx_color_value g = cv[1];
574     gx_color_value b = cv[2];
575 
576     /* X and ghostscript both use shorts for color values. */
577     /* Set drgb to the nearest color that the device can represent. */
578     X_color_value dr = r & xdev->cman.color_mask.red;
579     X_color_value dg = g & xdev->cman.color_mask.green;
580     X_color_value db = b & xdev->cman.color_mask.blue;
581 
582     {
583         /* Foreground and background get special treatment: */
584         /* They may be mapped to other colors. */
585         /* Set mrgb to the color to be used for match testing. */
586         X_color_value mr = r & xdev->cman.match_mask.red;
587         X_color_value mg = g & xdev->cman.match_mask.green;
588         X_color_value mb = b & xdev->cman.match_mask.blue;
589 
590         if ((mr | mg | mb) == 0) {	/* i.e., all 0 */
591             if_debug4('C', "[cX]%u,%u,%u => foreground = %lu\n",
592                       r, g, b, (ulong) xdev->foreground);
593             return xdev->foreground;
594         }
595         if (mr == xdev->cman.match_mask.red &&
596             mg == xdev->cman.match_mask.green &&
597             mb == xdev->cman.match_mask.blue
598             ) {
599             if_debug4('C', "[cX]%u,%u,%u => background = %lu\n",
600                       r, g, b, (ulong) xdev->background);
601             return xdev->background;
602         }
603     }
604 
605 #define CV_DENOM (gx_max_color_value + 1)
606 
607 #if HaveStdCMap
608     /* check the standard colormap first */
609     if (xdev->cman.std_cmap.map) {
610         const XStandardColormap *cmap = xdev->cman.std_cmap.map;
611 
612         if (gx_device_has_color(xdev)) {
613             uint cr, cg, cb;	/* rgb cube indices */
614             X_color_value cvr, cvg, cvb;	/* color value on cube */
615 
616             if (xdev->cman.std_cmap.fast) {
617                 cr = r >> xdev->cman.std_cmap.red.cv_shift;
618                 cvr = xdev->cman.std_cmap.red.nearest[cr];
619                 cg = g >> xdev->cman.std_cmap.green.cv_shift;
620                 cvg = xdev->cman.std_cmap.green.nearest[cg];
621                 cb = b >> xdev->cman.std_cmap.blue.cv_shift;
622                 cvb = xdev->cman.std_cmap.blue.nearest[cb];
623             } else {
624                 cr = r * (cmap->red_max + 1) / CV_DENOM;
625                 cg = g * (cmap->green_max + 1) / CV_DENOM;
626                 cb = b * (cmap->blue_max + 1) / CV_DENOM;
627                 cvr = X_max_color_value * cr / cmap->red_max;
628                 cvg = X_max_color_value * cg / cmap->green_max;
629                 cvb = X_max_color_value * cb / cmap->blue_max;
630             }
631             if ((iabs((int)r - (int)cvr) & xdev->cman.match_mask.red) == 0 &&
632                 (iabs((int)g - (int)cvg) & xdev->cman.match_mask.green) == 0 &&
633                 (iabs((int)b - (int)cvb) & xdev->cman.match_mask.blue) == 0) {
634                 gx_color_index pixel =
635                     (xdev->cman.std_cmap.fast ?
636                      (cr << xdev->cman.std_cmap.red.pixel_shift) +
637                      (cg << xdev->cman.std_cmap.green.pixel_shift) +
638                      (cb << xdev->cman.std_cmap.blue.pixel_shift) :
639                      cr * cmap->red_mult + cg * cmap->green_mult +
640                      cb * cmap->blue_mult) + cmap->base_pixel;
641 
642                 if_debug4('C', "[cX]%u,%u,%u (std cmap) => %lu\n",
643                           r, g, b, pixel);  /* NB: gx_color_index size is 4 or 8 */
644                 return pixel;
645             }
646             if_debug3('C', "[cX]%u,%u,%u (std cmap fails)\n", r, g, b);
647         } else {
648             uint cr;
649             X_color_value cvr;
650 
651             cr = r * (cmap->red_max + 1) / CV_DENOM;
652             cvr = X_max_color_value * cr / cmap->red_max;
653             if ((iabs((int)r - (int)cvr) & xdev->cman.match_mask.red) == 0) {
654                 gx_color_index pixel = cr * cmap->red_mult + cmap->base_pixel;
655 
656                 if_debug2('C', "[cX]%u (std cmap) => %lu\n", r, pixel);
657                 return pixel;
658             }
659             if_debug1('C', "[cX]%u (std cmap fails)\n", r);
660         }
661     } else
662 #endif
663 
664         /* If there is no standard colormap, check the dither cube/ramp */
665     if (xdev->cman.dither_ramp) {
666         if (gx_device_has_color(xdev)) {
667             uint cr, cg, cb;	/* rgb cube indices */
668             X_color_value cvr, cvg, cvb;	/* color value on cube */
669             int dither_rgb = xdev->color_info.dither_colors;
670             uint max_rgb = dither_rgb - 1;
671 
672             cr = r * dither_rgb / CV_DENOM;
673             cg = g * dither_rgb / CV_DENOM;
674             cb = b * dither_rgb / CV_DENOM;
675             if (max_rgb < countof(cv_tables)) {
676                 const ushort *cv_tab = cv_tables[max_rgb];
677 
678                 cvr = cv_tab[cr];
679                 cvg = cv_tab[cg];
680                 cvb = cv_tab[cb];
681             } else {
682                 cvr = CV_FRACTION(cr, max_rgb);
683                 cvg = CV_FRACTION(cg, max_rgb);
684                 cvb = CV_FRACTION(cb, max_rgb);
685             }
686             if ((iabs((int)r - (int)cvr) & xdev->cman.match_mask.red) == 0 &&
687                 (iabs((int)g - (int)cvg) & xdev->cman.match_mask.green) == 0 &&
688                 (iabs((int)b - (int)cvb) & xdev->cman.match_mask.blue) == 0) {
689                 gx_color_index pixel =
690                     xdev->cman.dither_ramp[CUBE_INDEX(cr, cg, cb)];
691 
692                 if_debug4('C', "[cX]%u,%u,%u (dither cube) => %lu\n",
693                           r, g, b, pixel);
694                 return pixel;
695             }
696             if_debug3('C', "[cX]%u,%u,%u (dither cube fails)\n", r, g, b);
697         } else {
698             uint cr;
699             X_color_value cvr;
700             int dither_grays = xdev->color_info.dither_grays;
701             uint max_gray = dither_grays - 1;
702 
703             cr = r * dither_grays / CV_DENOM;
704             cvr = (X_max_color_value * cr / max_gray);
705             if ((iabs((int)r - (int)cvr) & xdev->cman.match_mask.red) == 0) {
706                 gx_color_index pixel = xdev->cman.dither_ramp[cr];
707 
708                 if_debug2('C', "[cX]%u (dither ramp) => %lu\n", r, pixel);
709                 return pixel;
710             }
711             if_debug1('C', "[cX]%u (dither ramp fails)\n", r);
712         }
713     }
714 
715     /* Finally look through the list of dynamic colors */
716     if (xdev->cman.dynamic.colors) {
717         int i = (dr ^ dg ^ db) >> xdev->cman.dynamic.shift;
718         x11_color_t *xcp = xdev->cman.dynamic.colors[i];
719         x11_color_t *prev = NULL;
720         XColor xc;
721 
722         for (; xcp; prev = xcp, xcp = xcp->next)
723             if (xcp->color.red == dr && xcp->color.green == dg &&
724                 xcp->color.blue == db) {
725                 /* Promote the found entry to the front of the list. */
726                 if (prev) {
727                     prev->next = xcp->next;
728                     xcp->next = xdev->cman.dynamic.colors[i];
729                     xdev->cman.dynamic.colors[i] = xcp;
730                 }
731                 if (xcp->color.pad) {
732                     if_debug4('C', "[cX]%u,%u,%u (dynamic) => %lu\n",
733                               r, g, b, (ulong) xcp->color.pixel);
734                     return xcp->color.pixel;
735                 } else {
736                     if_debug3('C', "[cX]%u,%u,%u (dynamic) => missing\n",
737                               r, g, b);
738                     return gx_no_color_index;
739                 }
740             }
741 
742         /* If not in our list of dynamic colors, */
743         /* ask the X server and add an entry. */
744         /* First check if dynamic table is exhausted */
745         if (xdev->cman.dynamic.used > xdev->cman.dynamic.max_used) {
746             if_debug3('C', "[cX]%u,%u,%u (dynamic) => full\n", r, g, b);
747             return gx_no_color_index;
748         }
749         xcp = (x11_color_t *)
750             gs_malloc(xdev->memory, sizeof(x11_color_t), 1, "x11_dynamic_color");
751         if (!xcp)
752             return gx_no_color_index;
753         xc.red = xcp->color.red = dr;
754         xc.green = xcp->color.green = dg;
755         xc.blue = xcp->color.blue = db;
756         xcp->next = xdev->cman.dynamic.colors[i];
757         xdev->cman.dynamic.colors[i] = xcp;
758         xdev->cman.dynamic.used++;
759         if (x_alloc_color(xdev, &xc)) {
760             xcp->color.pixel = xc.pixel;
761             xcp->color.pad = true;
762             if_debug5('c', "[cX]0x%x,0x%x,0x%x (dynamic) => added [%d]%lu\n",
763                       dr, dg, db, xdev->cman.dynamic.used - 1,
764                       (ulong)xc.pixel);
765             return xc.pixel;
766         } else {
767             xcp->color.pad = false;
768             if_debug3('c', "[cX]0x%x,0x%x,0x%x (dynamic) => can't alloc\n",
769                       dr, dg, db);
770             return gx_no_color_index;
771         }
772     }
773     if_debug3('C', "[cX]%u,%u,%u fails\n", r, g, b);
774     return gx_no_color_index;
775 #undef CV_DENOM
776 }
777 
778 /* Map a pixel value back to r-g-b. */
779 int
gdev_x_map_color_rgb(gx_device * dev,gx_color_index color,gx_color_value prgb[3])780 gdev_x_map_color_rgb(gx_device * dev, gx_color_index color,
781                      gx_color_value prgb[3])
782 {
783     const gx_device_X *const xdev = (const gx_device_X *) dev;
784 #if HaveStdCMap
785     const XStandardColormap *cmap = xdev->cman.std_cmap.map;
786 #endif
787 
788     if (color == xdev->foreground) {
789         prgb[0] = prgb[1] = prgb[2] = 0;
790         return 0;
791     }
792     if (color == xdev->background) {
793         prgb[0] = prgb[1] = prgb[2] = gx_max_color_value;
794         return 0;
795     }
796     if (color < xdev->cman.color_to_rgb.size) {
797         const x11_rgb_t *pxrgb = &xdev->cman.color_to_rgb.values[color];
798 
799         if (pxrgb->defined) {
800             prgb[0] = pxrgb->rgb[0];
801             prgb[1] = pxrgb->rgb[1];
802             prgb[2] = pxrgb->rgb[2];
803             return 0;
804         }
805 #if HaveStdCMap
806     }
807 
808     /* Check the standard colormap. */
809     if (cmap) {
810         if (color >= cmap->base_pixel) {
811             x_pixel value = color - cmap->base_pixel;
812             uint r = (value / cmap->red_mult) % (cmap->red_max + 1);
813             uint g = (value / cmap->green_mult) % (cmap->green_max + 1);
814             uint b = (value / cmap->blue_mult) % (cmap->blue_max + 1);
815 
816             if (value == r * cmap->red_mult + g * cmap->green_mult +
817                 b * cmap->blue_mult) {
818                 /* When mapping color buckets back to specific colors,
819                  * we can choose to map them to the darkest shades
820                  * (e.g., 0, 1/3, 2/3), to the lightest shades (e.g.,
821                  * 1/3-epsilon, 2/3-epsilon, 1-epsilon), to the middle
822                  * shades (e.g., 1/6, 1/2, 5/6), or for maximum range
823                  * (e.g., 0, 1/2, 1).  The last of these matches the
824                  * assumptions of the halftoning code, so that is what
825                  * we choose.
826                  */
827                 prgb[0] = r * gx_max_color_value / cmap->red_max;
828                 prgb[1] = g * gx_max_color_value / cmap->green_max;
829                 prgb[2] = b * gx_max_color_value / cmap->blue_max;
830                 return 0;
831             }
832         }
833     }
834     if (color < xdev->cman.color_to_rgb.size) {
835 #endif
836         /* Error -- undefined pixel value. */
837         return_error(gs_error_unknownerror);
838     }
839     /*
840      * Check the dither cube/ramp.  This is hardly ever used, since if
841      * there are few enough colors to require dithering, the pixel values
842      * are likely to be small enough to index color_to_rgb.
843      */
844     if (xdev->cman.dither_ramp) {
845         if (gx_device_has_color(xdev)) {
846             int size = xdev->color_info.dither_colors;
847             int size3 = size * size * size;
848             int i;
849 
850             for (i = 0; i < size3; ++i)
851                 if (xdev->cman.dither_ramp[i] == color) {
852                     uint max_rgb = size - 1;
853                     uint q = i / size,
854                         r = q / size,
855                         g = q % size,
856                         b = i % size;
857 
858                     /*
859                      * See above regarding the choice of color mapping
860                      * algorithm.
861                      */
862                     prgb[0] = r * gx_max_color_value / max_rgb;
863                     prgb[1] = g * gx_max_color_value / max_rgb;
864                     prgb[2] = b * gx_max_color_value / max_rgb;
865                     return 0;
866                 }
867         } else {
868             int size = xdev->color_info.dither_grays;
869             int i;
870 
871             for (i = 0; i < size; ++i)
872                 if (xdev->cman.dither_ramp[i] == color) {
873                     prgb[0] = prgb[1] = prgb[2] =
874                         i * gx_max_color_value / (size - 1);
875                     return 0;
876                 }
877         }
878     }
879 
880     /* Finally, search the list of dynamic colors. */
881     if (xdev->cman.dynamic.colors) {
882         int i;
883         const x11_color_t *xcp;
884 
885         for (i = xdev->cman.dynamic.size; --i >= 0;)
886             for (xcp = xdev->cman.dynamic.colors[i]; xcp; xcp = xcp->next)
887                 if (xcp->color.pixel == color && xcp->color.pad) {
888                     prgb[0] = xcp->color.red;
889                     prgb[1] = xcp->color.green;
890                     prgb[2] = xcp->color.blue;
891                     return 0;
892                 }
893     }
894 
895     /* Not found -- not possible! */
896     return_error(gs_error_unknownerror);
897 }
898