Qt 4.8
qcolormap_qpa.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qcolormap.h"
43 #include "qcolor.h"
44 #include "qpaintdevice.h"
45 #include "private/qapplication_p.h"
46 #include "private/qgraphicssystem_p.h"
47 
49 
50 class QColormapPrivate
51 {
52 public:
54  : ref(1), mode(QColormap::Direct), depth(0), numcolors(0)
55  { }
56 
58 
60  int depth;
61  int numcolors;
62 };
63 
65 
67 {
68  screenMap = new QColormapPrivate;
69 
70  QPlatformIntegration *pi = QApplicationPrivate::platformIntegration();
71  QList<QPlatformScreen*> screens = pi->screens();
72 
73  screenMap->depth = screens.at(0)->depth();
74  if (screenMap->depth < 8) {
75  screenMap->mode = QColormap::Indexed;
76  screenMap->numcolors = 256;
77  } else {
78  screenMap->mode = QColormap::Direct;
79  screenMap->numcolors = -1;
80  }
81 }
82 
83 void QColormap::cleanup()
84 {
85  delete screenMap;
86  screenMap = 0;
87 }
88 
89 QColormap QColormap::instance(int /*screen*/)
90 {
91  return QColormap();
92 }
93 
95  : d(screenMap)
96 { d->ref.ref(); }
97 
99  :d (colormap.d)
100 { d->ref.ref(); }
101 
103 {
104  if (!d->ref.deref())
105  delete d;
106 }
107 
109 { return d->mode; }
110 
111 
112 int QColormap::depth() const
113 { return d->depth; }
114 
115 
116 int QColormap::size() const
117 {
118  return d->numcolors;
119 }
120 
121 #ifndef QT_QWS_DEPTH16_RGB
122 #define QT_QWS_DEPTH16_RGB 565
123 #endif
124 static const int qt_rbits = (QT_QWS_DEPTH16_RGB/100);
125 static const int qt_gbits = (QT_QWS_DEPTH16_RGB/10%10);
126 static const int qt_bbits = (QT_QWS_DEPTH16_RGB%10);
127 static const int qt_red_shift = qt_bbits+qt_gbits-(8-qt_rbits);
128 static const int qt_green_shift = qt_bbits-(8-qt_gbits);
129 static const int qt_neg_blue_shift = 8-qt_bbits;
130 static const int qt_blue_mask = (1<<qt_bbits)-1;
131 static const int qt_green_mask = (1<<(qt_gbits+qt_bbits))-(1<<qt_bbits);
132 static const int qt_red_mask = (1<<(qt_rbits+qt_gbits+qt_bbits))-(1<<(qt_gbits+qt_bbits));
133 
137 
139 {
140  const int tr = qRed(c) << qt_red_shift;
141  const int tg = qGreen(c) << qt_green_shift;
142  const int tb = qBlue(c) >> qt_neg_blue_shift;
143 
144  return (tb & qt_blue_mask) | (tg & qt_green_mask) | (tr & qt_red_mask);
145 }
146 
148 {
149  const int r=(c & qt_red_mask);
150  const int g=(c & qt_green_mask);
151  const int b=(c & qt_blue_mask);
152  const int tr = r >> qt_red_shift | r >> qt_red_rounding_shift;
153  const int tg = g >> qt_green_shift | g >> qt_green_rounding_shift;
154  const int tb = b << qt_neg_blue_shift | b >> qt_blue_rounding_shift;
155 
156  return qRgb(tr,tg,tb);
157 }
158 
159 uint QColormap::pixel(const QColor &color) const
160 {
161  QRgb rgb = color.rgba();
162  if (d->mode == QColormap::Direct) {
163  switch(d->depth) {
164  case 16:
165  return qt_convRgbTo16(rgb);
166  case 24:
167  case 32:
168  {
169  const int r = qRed(rgb);
170  const int g = qGreen(rgb);
171  const int b = qBlue(rgb);
172  const int red_shift = 16;
173  const int green_shift = 8;
174  const int red_mask = 0xff0000;
175  const int green_mask = 0x00ff00;
176  const int blue_mask = 0x0000ff;
177  const int tg = g << green_shift;
178 #ifdef QT_QWS_DEPTH_32_BGR
180  const int tb = b << red_shift;
181  return 0xff000000 | (r & blue_mask) | (tg & green_mask) | (tb & red_mask);
182  }
183 #endif
184  const int tr = r << red_shift;
185  return 0xff000000 | (b & blue_mask) | (tg & green_mask) | (tr & red_mask);
186  }
187  }
188  }
189  //XXX
190  //return qt_screen->alloc(qRed(rgb), qGreen(rgb), qBlue(rgb));
191  return 0;
192 }
193 
194 const QColor QColormap::colorAt(uint pixel) const
195 {
196  if (d->mode == Direct) {
197  if (d->depth == 16) {
198  pixel = qt_conv16ToRgb(pixel);
199  }
200  const int red_shift = 16;
201  const int green_shift = 8;
202  const int red_mask = 0xff0000;
203  const int green_mask = 0x00ff00;
204  const int blue_mask = 0x0000ff;
205 #ifdef QT_QWS_DEPTH_32_BGR
207  return QColor((pixel & blue_mask),
208  (pixel & green_mask) >> green_shift,
209  (pixel & red_mask) >> red_shift);
210  }
211 #endif
212  return QColor((pixel & red_mask) >> red_shift,
213  (pixel & green_mask) >> green_shift,
214  (pixel & blue_mask));
215  }
216 #if 0 // XXX
217  Q_ASSERT_X(int(pixel) < qt_screen->numCols(), "QColormap::colorAt", "pixel out of bounds of palette");
218  return QColor(qt_screen->clut()[pixel]);
219 #endif
220  return QColor();
221 }
222 
224 {
225  return QVector<QColor>();
226 }
227 
228 QColormap &QColormap::operator=(const QColormap &colormap)
229 { qAtomicAssign(d, colormap.d); return *this; }
230 
Q_GUI_EXPORT QScreen * qt_screen
Definition: qscreen_qws.cpp:69
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
double d
Definition: qnumeric_p.h:62
static QColormap instance(int screen=-1)
unsigned int QRgb
Definition: qrgb.h:53
static const int qt_green_rounding_shift
unsigned char c[8]
Definition: qnumeric_p.h:62
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
int size() const
QRgb qt_conv16ToRgb(ushort c)
static const int qt_neg_blue_shift
static const int qt_green_mask
static const int qt_blue_mask
Q_GUI_EXPORT_INLINE int qRed(QRgb rgb)
Definition: qrgb.h:57
static QColormapPrivate * screenMap
QColormap::Mode mode
QT_DEPRECATED int numCols()
Definition: qscreen_qws.h:247
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
PixelType pixelType() const
Returns the pixel storage format of the screen.
Definition: qscreen_qws.h:231
const QColor colorAt(uint pixel) const
static const int qt_green_shift
static const int qt_red_rounding_shift
int depth() const
ushort qt_convRgbTo16(QRgb c)
virtual int depth() const =0
Reimplement in subclass to return current depth of the screen.
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
static const int qt_rbits
QColormapPrivate * d
Definition: qcolormap.h:90
unsigned int uint
Definition: qglobal.h:996
virtual QList< QPlatformScreen * > screens() const =0
Accessor function to a list of all the screens on the current system.
QColormap & operator=(const QColormap &colormap)
static const int qt_red_shift
Q_GUI_EXPORT_INLINE int qBlue(QRgb rgb)
Definition: qrgb.h:63
static const int qt_red_mask
uint pixel(const QColor &color) const
#define rgb(r, g, b)
Definition: qcolor_p.cpp:130
The QPlatformIntegration class is the entry for WindowSystem specific functionality.
#define QT_QWS_DEPTH16_RGB
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
unsigned short ushort
Definition: qglobal.h:995
const QVector< QColor > colormap() const
static void initialize()
Q_GUI_EXPORT_INLINE QRgb qRgb(int r, int g, int b)
Definition: qrgb.h:69
QRgb * clut()
Returns a pointer to the screen&#39;s color lookup table (i.
Definition: qscreen_qws.h:245
void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
Definition: qatomic.h:195
Q_GUI_EXPORT_INLINE int qGreen(QRgb rgb)
Definition: qrgb.h:60
Mode mode() const
QColormap()
Constructs a new colormap.
QRgb rgba() const
Returns the RGB value of the color, including its alpha.
Definition: qcolor.cpp:1019
static const int qt_bbits
static const int qt_blue_rounding_shift
static void cleanup()
static const int qt_gbits
The QList class is a template class that provides lists.
Definition: qdatastream.h:62