Changes.
[cairo/gpu.git] / src / cairo-xlib-visual.c
blob7dbe86c287b43b00875a170796cf34968d3a5a0a
1 /* Cairo - a vector graphics library with display and print output
3 * Copyright © 2008 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Red Hat, Inc.
32 * Contributor(s):
33 * Carl D. Worth <cworth@cworth.org>
36 #include "cairoint.h"
38 #include "cairo-xlib-private.h"
40 /* A perceptual distance metric between two colors. No sqrt needed
41 * since the square of the distance is still a valid metric. */
43 /* XXX: This is currently using linear distance in RGB space which is
44 * decidedly not perceptually linear. If someone cared a lot about the
45 * quality, they might choose something else here. Then again, they
46 * might also choose not to use a PseudoColor visual... */
47 static inline int
48 _color_distance (unsigned short r1, unsigned short g1, unsigned short b1,
49 unsigned short r2, unsigned short g2, unsigned short b2)
51 r1 >>= 8; g1 >>= 8; b1 >>= 8;
52 r2 >>= 8; g2 >>= 8; b2 >>= 8;
54 return ((r2 - r1) * (r2 - r1) +
55 (g2 - g1) * (g2 - g1) +
56 (b2 - b1) * (b2 - b1));
59 cairo_status_t
60 _cairo_xlib_visual_info_create (Display *dpy,
61 int screen,
62 VisualID visualid,
63 cairo_xlib_visual_info_t **out)
65 cairo_xlib_visual_info_t *info;
66 Colormap colormap = DefaultColormap (dpy, screen);
67 XColor color;
68 int gray, red, green, blue;
69 int i, j, distance, min_distance = 0;
70 XColor colors[256];
71 unsigned short cube_index_to_short[CUBE_SIZE];
72 unsigned short ramp_index_to_short[RAMP_SIZE];
73 unsigned char gray_to_pseudocolor[RAMP_SIZE];
75 for (i = 0; i < CUBE_SIZE; i++)
76 cube_index_to_short[i] = (0xffff * i + ((CUBE_SIZE-1)>>1)) / (CUBE_SIZE-1);
77 for (i = 0; i < RAMP_SIZE; i++)
78 ramp_index_to_short[i] = (0xffff * i + ((RAMP_SIZE-1)>>1)) / (RAMP_SIZE-1);
80 info = malloc (sizeof (cairo_xlib_visual_info_t));
81 if (unlikely (info == NULL))
82 return _cairo_error (CAIRO_STATUS_NO_MEMORY);
84 info->visualid = visualid;
86 /* Allocate a gray ramp and a color cube.
87 * Give up as soon as failures start. */
89 for (gray = 0; gray < RAMP_SIZE; gray++) {
90 color.red = color.green = color.blue = ramp_index_to_short[gray];
91 if (! XAllocColor (dpy, colormap, &color))
92 goto DONE_ALLOCATE;
95 /* XXX: Could do this in a more clever order to have the best
96 * possible results from early failure. Could also choose a cube
97 * uniformly distributed in a better space than RGB. */
98 for (red = 0; red < CUBE_SIZE; red++) {
99 for (green = 0; green < CUBE_SIZE; green++) {
100 for (blue = 0; blue < CUBE_SIZE; blue++) {
101 color.red = cube_index_to_short[red];
102 color.green = cube_index_to_short[green];
103 color.blue = cube_index_to_short[blue];
104 color.pixel = 0;
105 color.flags = 0;
106 color.pad = 0;
107 if (! XAllocColor (dpy, colormap, &color))
108 goto DONE_ALLOCATE;
112 DONE_ALLOCATE:
114 for (i = 0; i < ARRAY_LENGTH (colors); i++)
115 colors[i].pixel = i;
116 XQueryColors (dpy, colormap, colors, ARRAY_LENGTH (colors));
118 /* Search for nearest colors within allocated colormap. */
119 for (gray = 0; gray < RAMP_SIZE; gray++) {
120 for (i = 0; i < 256; i++) {
121 distance = _color_distance (ramp_index_to_short[gray],
122 ramp_index_to_short[gray],
123 ramp_index_to_short[gray],
124 colors[i].red,
125 colors[i].green,
126 colors[i].blue);
127 if (i == 0 || distance < min_distance) {
128 gray_to_pseudocolor[gray] = colors[i].pixel;
129 min_distance = distance;
130 if (!min_distance)
131 break;
135 for (red = 0; red < CUBE_SIZE; red++) {
136 for (green = 0; green < CUBE_SIZE; green++) {
137 for (blue = 0; blue < CUBE_SIZE; blue++) {
138 for (i = 0; i < 256; i++) {
139 distance = _color_distance (cube_index_to_short[red],
140 cube_index_to_short[green],
141 cube_index_to_short[blue],
142 colors[i].red,
143 colors[i].green,
144 colors[i].blue);
145 if (i == 0 || distance < min_distance) {
146 info->cube_to_pseudocolor[red][green][blue] = colors[i].pixel;
147 min_distance = distance;
148 if (!min_distance)
149 break;
156 for (i = 0, j = 0; i < 256; i++) {
157 if (j < CUBE_SIZE - 1 && (((i<<8)+i) - (int)cube_index_to_short[j]) > ((int)cube_index_to_short[j+1] - ((i<<8)+i)))
158 j++;
159 info->field8_to_cube[i] = j;
161 info->dither8_to_cube[i] = ((int)i - 128) / (CUBE_SIZE - 1);
163 for (i = 0, j = 0; i < 256; i++) {
164 if (j < RAMP_SIZE - 1 && (((i<<8)+i) - (int)ramp_index_to_short[j]) > ((int)ramp_index_to_short[j+1] - ((i<<8)+i)))
165 j++;
166 info->gray8_to_pseudocolor[i] = gray_to_pseudocolor[j];
169 for (i = 0; i < 256; i++) {
170 info->colors[i].a = 0xff;
171 info->colors[i].r = colors[i].red >> 8;
172 info->colors[i].g = colors[i].green >> 8;
173 info->colors[i].b = colors[i].blue >> 8;
176 *out = info;
177 return CAIRO_STATUS_SUCCESS;
180 void
181 _cairo_xlib_visual_info_destroy (Display *dpy, cairo_xlib_visual_info_t *info)
183 /* No need for XFreeColors() whilst using DefaultColormap */
184 free (info);