Qt 4.8
Classes | Functions | Variables
qcolormap_x11.cpp File Reference
#include "qcolormap.h"
#include "qapplication.h"
#include "qdebug.h"
#include "qdesktopwidget.h"
#include "qvarlengtharray.h"
#include "qx11info_x11.h"
#include <private/qt_x11_p.h>
#include <limits.h>

Go to the source code of this file.

Classes

class  QColormapPrivate
 

Functions

static int cube_root (int v)
 
static Visual * find_visual (Display *display, int screen, int visual_class, int visual_id, int *depth, bool *defaultVisual)
 
static void init_direct (QColormapPrivate *d, bool ownColormap)
 
static void init_gray (QColormapPrivate *d, int screen)
 
static void init_indexed (QColormapPrivate *d, int screen)
 
static int lowest_bit (uint v)
 
static void query_colormap (QColormapPrivate *d, int screen)
 
static uint right_align (uint v)
 

Variables

static QColormap ** cmaps = 0
 

Function Documentation

◆ cube_root()

static int cube_root ( int  v)
static

Definition at line 106 of file qcolormap_x11.cpp.

107 {
108  if (v == 1)
109  return 1;
110  // brute force algorithm
111  int i = 1;
112  for (;;) {
113  const int b = i * i * i;
114  if (b <= v) {
115  ++i;
116  } else {
117  --i;
118  break;
119  }
120  }
121  return i;
122 }

◆ find_visual()

static Visual* find_visual ( Display display,
int  screen,
int  visual_class,
int  visual_id,
int *  depth,
bool *  defaultVisual 
)
static

Definition at line 124 of file qcolormap_x11.cpp.

130 {
131  XVisualInfo *vi, rvi;
132  int count;
133 
134  uint mask = VisualScreenMask;
135  rvi.screen = screen;
136 
137  if (visual_class != -1) {
138  rvi.c_class = visual_class;
139  mask |= VisualClassMask;
140  }
141  if (visual_id != -1) {
142  rvi.visualid = visual_id;
143  mask |= VisualIDMask;
144  }
145 
146  Visual *visual = DefaultVisual(display, screen);
147  *defaultVisual = true;
148  *depth = DefaultDepth(display, screen);
149 
150  vi = XGetVisualInfo(display, mask, &rvi, &count);
151  if (vi) {
152  int best = 0;
153  for (int x = 0; x < count; ++x) {
154  if (vi[x].depth > vi[best].depth)
155  best = x;
156  }
157  if (best >= 0 && best <= count && vi[best].visualid != XVisualIDFromVisual(visual)) {
158  visual = vi[best].visual;
159  *defaultVisual = (visual == DefaultVisual(display, screen));
160  *depth = vi[best].depth;
161  }
162  }
163  if (vi)
164  XFree((char *)vi);
165  return visual;
166 }
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
unsigned int uint
Definition: qglobal.h:996

◆ init_direct()

static void init_direct ( QColormapPrivate d,
bool  ownColormap 
)
static

Definition at line 304 of file qcolormap_x11.cpp.

305 {
306  if (d->visual->c_class != DirectColor || !ownColormap)
307  return;
308 
309  // preallocate 768 on the stack, so that we don't have to malloc
310  // for the common case (<= 24 bpp)
311  QVarLengthArray<XColor, 768> colorTable(d->r_max + d->g_max + d->b_max);
312  int i = 0;
313 
314  for (int r = 0; r < d->r_max; ++r) {
315  colorTable[i].red = r << 8 | r;
316  colorTable[i].pixel = r << d->r_shift;
317  colorTable[i].flags = DoRed;
318  ++i;
319  }
320 
321  for (int g = 0; g < d->g_max; ++g) {
322  colorTable[i].green = g << 8 | g;
323  colorTable[i].pixel = g << d->g_shift;
324  colorTable[i].flags = DoGreen;
325  ++i;
326  }
327 
328  for (int b = 0; b < d->b_max; ++b) {
329  colorTable[i].blue = (b << 8 | b);
330  colorTable[i].pixel = b << d->b_shift;
331  colorTable[i].flags = DoBlue;
332  ++i;
333  }
334 
335  XStoreColors(X11->display, d->colormap, colorTable.data(), colorTable.count());
336 }
#define X11
Definition: qt_x11_p.h:724

◆ init_gray()

static void init_gray ( QColormapPrivate d,
int  screen 
)
static

Definition at line 248 of file qcolormap_x11.cpp.

249 {
250  d->pixels.resize(d->r_max);
251 
252  for (int g = 0; g < d->g_max; ++g) {
253  const int gray = (g * 0xff + (d->r_max - 1) / 2) / (d->r_max - 1);
254  const QRgb rgb = qRgb(gray, gray, gray);
255 
256  d->pixels[g] = -1;
257 
258  if (d->visual->c_class & 1) {
259  XColor xcolor;
260  xcolor.red = qRed(rgb) * 0x101;
261  xcolor.green = qGreen(rgb) * 0x101;
262  xcolor.blue = qBlue(rgb) * 0x101;
263  xcolor.pixel = 0ul;
264 
265  if (XAllocColor(QX11Info::display(), d->colormap, &xcolor))
266  d->pixels[g] = xcolor.pixel;
267  }
268  }
269 
270  query_colormap(d, screen);
271 }
unsigned int QRgb
Definition: qrgb.h:53
QVector< int > pixels
int qRed(QRgb rgb)
Returns the red component of the ARGB quadruplet rgb.
Definition: qrgb.h:57
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
static void query_colormap(QColormapPrivate *d, int screen)
#define rgb(r, g, b)
Definition: qcolor_p.cpp:130
QRgb qRgb(int r, int g, int b)
Returns the ARGB quadruplet (255, {r}, {g}, {b}).
Definition: qrgb.h:69
int qGreen(QRgb rgb)
Returns the green component of the ARGB quadruplet rgb.
Definition: qrgb.h:60
int qBlue(QRgb rgb)
Returns the blue component of the ARGB quadruplet rgb.
Definition: qrgb.h:63
static Display * display()
Returns the default display for the application.

◆ init_indexed()

static void init_indexed ( QColormapPrivate d,
int  screen 
)
static

Definition at line 273 of file qcolormap_x11.cpp.

274 {
275  d->pixels.resize(d->r_max * d->g_max * d->b_max);
276 
277  // create color cube
278  for (int x = 0, r = 0; r < d->r_max; ++r) {
279  for (int g = 0; g < d->g_max; ++g) {
280  for (int b = 0; b < d->b_max; ++b, ++x) {
281  const QRgb rgb = qRgb((r * 0xff + (d->r_max - 1) / 2) / (d->r_max - 1),
282  (g * 0xff + (d->g_max - 1) / 2) / (d->g_max - 1),
283  (b * 0xff + (d->b_max - 1) / 2) / (d->b_max - 1));
284 
285  d->pixels[x] = -1;
286 
287  if (d->visual->c_class & 1) {
288  XColor xcolor;
289  xcolor.red = qRed(rgb) * 0x101;
290  xcolor.green = qGreen(rgb) * 0x101;
291  xcolor.blue = qBlue(rgb) * 0x101;
292  xcolor.pixel = 0ul;
293 
294  if (XAllocColor(QX11Info::display(), d->colormap, &xcolor))
295  d->pixels[x] = xcolor.pixel;
296  }
297  }
298  }
299  }
300 
301  query_colormap(d, screen);
302 }
unsigned int QRgb
Definition: qrgb.h:53
QVector< int > pixels
int qRed(QRgb rgb)
Returns the red component of the ARGB quadruplet rgb.
Definition: qrgb.h:57
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
static void query_colormap(QColormapPrivate *d, int screen)
#define rgb(r, g, b)
Definition: qcolor_p.cpp:130
QRgb qRgb(int r, int g, int b)
Returns the ARGB quadruplet (255, {r}, {g}, {b}).
Definition: qrgb.h:69
int qGreen(QRgb rgb)
Returns the green component of the ARGB quadruplet rgb.
Definition: qrgb.h:60
int qBlue(QRgb rgb)
Returns the blue component of the ARGB quadruplet rgb.
Definition: qrgb.h:63
static Display * display()
Returns the default display for the application.

◆ lowest_bit()

static int lowest_bit ( uint  v)
static

Definition at line 97 of file qcolormap_x11.cpp.

98 {
99  int i;
100  uint b = 1u;
101  for (i = 0; ((v & b) == 0u) && i < 32; ++i)
102  b <<= 1u;
103  return i == 32 ? -1 : i;
104 }
quint16 u
unsigned int uint
Definition: qglobal.h:996

◆ query_colormap()

static void query_colormap ( QColormapPrivate d,
int  screen 
)
static

Definition at line 168 of file qcolormap_x11.cpp.

Referenced by init_gray(), and init_indexed().

169 {
171 
172  // query existing colormap
173  int q_colors = (((1u << d->depth) > 256u) ? 256u : (1u << d->depth));
174  XColor queried[256];
175  memset(queried, 0, sizeof(queried));
176  for (int x = 0; x < q_colors; ++x)
177  queried[x].pixel = x;
178  XQueryColors(display, d->colormap, queried, q_colors);
179 
180  d->colors.resize(q_colors);
181  for (int x = 0; x < q_colors; ++x) {
182  if (queried[x].red == 0
183  && queried[x].green == 0
184  && queried[x].blue == 0
185  && queried[x].pixel != BlackPixel(display, screen)) {
186  // unallocated color cell, skip it
187  continue;
188  }
189 
190  d->colors[x] = QColor::fromRgbF(queried[x].red / float(USHRT_MAX),
191  queried[x].green / float(USHRT_MAX),
192  queried[x].blue / float(USHRT_MAX));
193  }
194 
195  // for missing colors, find the closest color in the existing colormap
196  Q_ASSERT(d->pixels.size());
197  for (int x = 0; x < d->pixels.size(); ++x) {
198  if (d->pixels.at(x) != -1)
199  continue;
200 
201  QRgb rgb;
202  if (d->mode == QColormap::Indexed) {
203  const int r = (x / (d->g_max * d->b_max)) % d->r_max;
204  const int g = (x / d->b_max) % d->g_max;
205  const int b = x % d->b_max;
206  rgb = qRgb((r * 0xff + (d->r_max - 1) / 2) / (d->r_max - 1),
207  (g * 0xff + (d->g_max - 1) / 2) / (d->g_max - 1),
208  (b * 0xff + (d->b_max - 1) / 2) / (d->b_max - 1));
209  } else {
210  rgb = qRgb(x, x, x);
211  }
212 
213  // find closest color
214  int mindist = INT_MAX, best = -1;
215  for (int y = 0; y < q_colors; ++y) {
216  int r = qRed(rgb) - (queried[y].red >> 8);
217  int g = qGreen(rgb) - (queried[y].green >> 8);
218  int b = qBlue(rgb) - (queried[y].blue >> 8);
219  int dist = (r * r) + (g * g) + (b * b);
220  if (dist < mindist) {
221  mindist = dist;
222  best = y;
223  }
224  }
225 
226  Q_ASSERT(best >= 0 && best < q_colors);
227  if (d->visual->c_class & 1) {
228  XColor xcolor;
229  xcolor.red = queried[best].red;
230  xcolor.green = queried[best].green;
231  xcolor.blue = queried[best].blue;
232  xcolor.pixel = queried[best].pixel;
233 
234  if (XAllocColor(display, d->colormap, &xcolor)) {
235  d->pixels[x] = xcolor.pixel;
236  } else {
237  // some weird stuff is going on...
238  d->pixels[x] = (qGray(rgb) < 127
239  ? BlackPixel(display, screen)
240  : WhitePixel(display, screen));
241  }
242  } else {
243  d->pixels[x] = best;
244  }
245  }
246 }
unsigned int QRgb
Definition: qrgb.h:53
QVector< int > pixels
quint16 u
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
int qRed(QRgb rgb)
Returns the red component of the ARGB quadruplet rgb.
Definition: qrgb.h:57
void resize(int size)
Sets the size of the vector to size.
Definition: qvector.h:342
QColormap::Mode mode
Q_GUI_EXPORT EGLDisplay display()
Definition: qegl.cpp:589
int qGray(int r, int g, int b)
Definition: qrgb.h:75
static QColor fromRgbF(qreal r, qreal g, qreal b, qreal a=1.0)
Static convenience function that returns a QColor constructed from the RGB color values, r (red), g (green), b (blue), and a (alpha-channel, i.e.
Definition: qcolor.cpp:2017
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
#define rgb(r, g, b)
Definition: qcolor_p.cpp:130
QRgb qRgb(int r, int g, int b)
Returns the ARGB quadruplet (255, {r}, {g}, {b}).
Definition: qrgb.h:69
struct _XDisplay Display
Definition: qwindowdefs.h:115
int qGreen(QRgb rgb)
Returns the green component of the ARGB quadruplet rgb.
Definition: qrgb.h:60
int qBlue(QRgb rgb)
Returns the blue component of the ARGB quadruplet rgb.
Definition: qrgb.h:63
QVector< QColor > colors
int size() const
Returns the number of items in the vector.
Definition: qvector.h:137
#define INT_MAX
static Display * display()
Returns the default display for the application.

◆ right_align()

static uint right_align ( uint  v)
static

Definition at line 90 of file qcolormap_x11.cpp.

91 {
92  while (!(v & 0x1))
93  v >>= 1;
94  return v;
95 }

Variable Documentation

◆ cmaps

QColormap** cmaps = 0
static

Definition at line 338 of file qcolormap_x11.cpp.