Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcolormap.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qcolormap.h"
6#include "qcolor.h"
7#include "qpaintdevice.h"
8#include "qscreen.h"
10
11#include <QtCore/qmutex.h>
12
14
15#if QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
16
17QT_WARNING_PUSH
18QT_WARNING_DISABLE_DEPRECATED
19
20class QColormapPrivate
21{
22public:
23 inline QColormapPrivate()
24 : ref(1), mode(QColormap::Direct), depth(0), numcolors(0)
25 { }
26
27 QAtomicInt ref;
28
29 QColormap::Mode mode;
30 int depth;
31 int numcolors;
32};
33
34static QColormapPrivate *screenMap = nullptr;
35
36void QColormap::initialize()
37{
38 screenMap = new QColormapPrivate;
39 if (Q_UNLIKELY(!QGuiApplication::primaryScreen())) {
40 qWarning("no screens available, assuming 24-bit color");
41 screenMap->depth = 24;
42 screenMap->mode = QColormap::Direct;
43 return;
44 }
45 screenMap->depth = QGuiApplication::primaryScreen()->depth();
46 if (screenMap->depth < 8) {
47 screenMap->mode = QColormap::Indexed;
48 screenMap->numcolors = 256;
49 } else {
50 screenMap->mode = QColormap::Direct;
51 screenMap->numcolors = -1;
52 }
53
54 qAddPostRoutine(QColormap::cleanup);
55}
56
57void QColormap::cleanup()
58{
59 if (screenMap) {
60 if (!screenMap->ref.deref())
61 delete screenMap;
62 screenMap = nullptr;
63 }
64}
65
66QColormap QColormap::instance(int /*screen*/)
67{
68 static QMutex mutex;
69 QMutexLocker locker(&mutex);
70 if (!screenMap)
71 initialize();
72
73 return QColormap();
74}
75
76QColormap::QColormap()
77 : d(screenMap)
78{ d->ref.ref(); }
79
80QColormap::QColormap(const QColormap &colormap)
81 :d (colormap.d)
82{ d->ref.ref(); }
83
84QColormap::~QColormap()
85{
86 if (!d->ref.deref())
87 delete d;
88}
89
90QColormap::Mode QColormap::mode() const
91{ return d->mode; }
92
93
94int QColormap::depth() const
95{ return d->depth; }
96
97
98int QColormap::size() const
99{
100 return d->numcolors;
101}
102
103#ifndef QT_QWS_DEPTH16_RGB
104#define QT_QWS_DEPTH16_RGB 565
105#endif
106static const int qt_rbits = (QT_QWS_DEPTH16_RGB/100);
107static const int qt_gbits = (QT_QWS_DEPTH16_RGB/10%10);
108static const int qt_bbits = (QT_QWS_DEPTH16_RGB%10);
109static const int qt_red_shift = qt_bbits+qt_gbits-(8-qt_rbits);
110static const int qt_green_shift = qt_bbits-(8-qt_gbits);
111static const int qt_neg_blue_shift = 8-qt_bbits;
112static const int qt_blue_mask = (1<<qt_bbits)-1;
113static const int qt_green_mask = (1<<(qt_gbits+qt_bbits))-(1<<qt_bbits);
114static const int qt_red_mask = (1<<(qt_rbits+qt_gbits+qt_bbits))-(1<<(qt_gbits+qt_bbits));
115
116static const int qt_red_rounding_shift = qt_red_shift + qt_rbits;
117static const int qt_green_rounding_shift = qt_green_shift + qt_gbits;
118static const int qt_blue_rounding_shift = qt_bbits - qt_neg_blue_shift;
119
120inline ushort qt_convRgbTo16(QRgb c)
121{
122 const int tr = qRed(c) << qt_red_shift;
123 const int tg = qGreen(c) << qt_green_shift;
124 const int tb = qBlue(c) >> qt_neg_blue_shift;
125
126 return (tb & qt_blue_mask) | (tg & qt_green_mask) | (tr & qt_red_mask);
127}
128
129inline QRgb qt_conv16ToRgb(ushort c)
130{
131 const int r=(c & qt_red_mask);
132 const int g=(c & qt_green_mask);
133 const int b=(c & qt_blue_mask);
134 const int tr = r >> qt_red_shift | r >> qt_red_rounding_shift;
135 const int tg = g >> qt_green_shift | g >> qt_green_rounding_shift;
136 const int tb = b << qt_neg_blue_shift | b >> qt_blue_rounding_shift;
137
138 return qRgb(tr,tg,tb);
139}
140
141uint QColormap::pixel(const QColor &color) const
142{
143 QRgb rgb = color.rgba();
144 if (d->mode == QColormap::Direct) {
145 switch(d->depth) {
146 case 16:
147 return qt_convRgbTo16(rgb);
148 case 24:
149 case 32:
150 {
151 const int r = qRed(rgb);
152 const int g = qGreen(rgb);
153 const int b = qBlue(rgb);
154 const int red_shift = 16;
155 const int green_shift = 8;
156 const int red_mask = 0xff0000;
157 const int green_mask = 0x00ff00;
158 const int blue_mask = 0x0000ff;
159 const int tg = g << green_shift;
160 const int tr = r << red_shift;
161 return 0xff000000 | (b & blue_mask) | (tg & green_mask) | (tr & red_mask);
162 }
163 }
164 }
165 //XXX
166 //return qt_screen->alloc(qRed(rgb), qGreen(rgb), qBlue(rgb));
167 return 0;
168}
169
170const QColor QColormap::colorAt(uint pixel) const
171{
172 if (d->mode == Direct) {
173 if (d->depth == 16) {
174 pixel = qt_conv16ToRgb(pixel);
175 }
176 const int red_shift = 16;
177 const int green_shift = 8;
178 const int red_mask = 0xff0000;
179 const int green_mask = 0x00ff00;
180 const int blue_mask = 0x0000ff;
181 return QColor((pixel & red_mask) >> red_shift,
182 (pixel & green_mask) >> green_shift,
183 (pixel & blue_mask));
184 }
185#if 0 // XXX
186 Q_ASSERT_X(int(pixel) < qt_screen->numCols(), "QColormap::colorAt", "pixel out of bounds of palette");
187 return QColor(qt_screen->clut()[pixel]);
188#endif
189 return QColor();
190}
191
192const QList<QColor> QColormap::colormap() const
193{
194 return QList<QColor>();
195}
196
197QColormap &QColormap::operator=(const QColormap &colormap)
198{ qAtomicAssign(d, colormap.d); return *this; }
199
200QT_WARNING_POP
201
202#endif // QT_REMOVAL_QT7_DEPRECATED_SINCE(6, 11)
203
204QT_END_NAMESPACE