1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtWidgets 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qcolormap.h"
41#include "qcolor.h"
42#include "qpaintdevice.h"
43#include "qscreen.h"
44#include "qguiapplication.h"
45
46QT_BEGIN_NAMESPACE
47
48class QColormapPrivate
49{
50public:
51 inline QColormapPrivate()
52 : ref(1), mode(QColormap::Direct), depth(0), numcolors(0)
53 { }
54
55 QAtomicInt ref;
56
57 QColormap::Mode mode;
58 int depth;
59 int numcolors;
60};
61
62static QColormapPrivate *screenMap = nullptr;
63
64void QColormap::initialize()
65{
66 screenMap = new QColormapPrivate;
67 if (Q_UNLIKELY(!QGuiApplication::primaryScreen())) {
68 qWarning(msg: "no screens available, assuming 24-bit color");
69 screenMap->depth = 24;
70 screenMap->mode = QColormap::Direct;
71 return;
72 }
73 screenMap->depth = QGuiApplication::primaryScreen()->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
83void QColormap::cleanup()
84{
85 delete screenMap;
86 screenMap = nullptr;
87}
88
89QColormap QColormap::instance(int /*screen*/)
90{
91 return QColormap();
92}
93
94QColormap::QColormap()
95 : d(screenMap)
96{ d->ref.ref(); }
97
98QColormap::QColormap(const QColormap &colormap)
99 :d (colormap.d)
100{ d->ref.ref(); }
101
102QColormap::~QColormap()
103{
104 if (!d->ref.deref())
105 delete d;
106}
107
108QColormap::Mode QColormap::mode() const
109{ return d->mode; }
110
111
112int QColormap::depth() const
113{ return d->depth; }
114
115
116int 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
124static const int qt_rbits = (QT_QWS_DEPTH16_RGB/100);
125static const int qt_gbits = (QT_QWS_DEPTH16_RGB/10%10);
126static const int qt_bbits = (QT_QWS_DEPTH16_RGB%10);
127static const int qt_red_shift = qt_bbits+qt_gbits-(8-qt_rbits);
128static const int qt_green_shift = qt_bbits-(8-qt_gbits);
129static const int qt_neg_blue_shift = 8-qt_bbits;
130static const int qt_blue_mask = (1<<qt_bbits)-1;
131static const int qt_green_mask = (1<<(qt_gbits+qt_bbits))-(1<<qt_bbits);
132static const int qt_red_mask = (1<<(qt_rbits+qt_gbits+qt_bbits))-(1<<(qt_gbits+qt_bbits));
133
134static const int qt_red_rounding_shift = qt_red_shift + qt_rbits;
135static const int qt_green_rounding_shift = qt_green_shift + qt_gbits;
136static const int qt_blue_rounding_shift = qt_bbits - qt_neg_blue_shift;
137
138inline ushort qt_convRgbTo16(QRgb c)
139{
140 const int tr = qRed(rgb: c) << qt_red_shift;
141 const int tg = qGreen(rgb: c) << qt_green_shift;
142 const int tb = qBlue(rgb: c) >> qt_neg_blue_shift;
143
144 return (tb & qt_blue_mask) | (tg & qt_green_mask) | (tr & qt_red_mask);
145}
146
147inline QRgb qt_conv16ToRgb(ushort c)
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(r: tr,g: tg,b: tb);
157}
158
159uint 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(c: 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
179 if (qt_screen->pixelType() == QScreen::BGRPixel) {
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
194const QColor QColormap::colorAt(uint pixel) const
195{
196 if (d->mode == Direct) {
197 if (d->depth == 16) {
198 pixel = qt_conv16ToRgb(c: 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
206 if (qt_screen->pixelType() == QScreen::BGRPixel) {
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
223const QVector<QColor> QColormap::colormap() const
224{
225 return QVector<QColor>();
226}
227
228QColormap &QColormap::operator=(const QColormap &colormap)
229{ qAtomicAssign(d, x: colormap.d); return *this; }
230
231QT_END_NAMESPACE
232

source code of qtbase/src/widgets/util/qcolormap.cpp