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
4#include "qcolormap.h"
5#include "qcolor.h"
6#include "qpaintdevice.h"
7#include "qscreen.h"
8#include "qguiapplication.h"
9
10QT_BEGIN_NAMESPACE
11
12class QColormapPrivate
13{
14public:
15 inline QColormapPrivate()
16 : ref(1), mode(QColormap::Direct), depth(0), numcolors(0)
17 { }
18
19 QAtomicInt ref;
20
21 QColormap::Mode mode;
22 int depth;
23 int numcolors;
24};
25
26static QColormapPrivate *screenMap = nullptr;
27
28void QColormap::initialize()
29{
30 screenMap = new QColormapPrivate;
31 if (Q_UNLIKELY(!QGuiApplication::primaryScreen())) {
32 qWarning(msg: "no screens available, assuming 24-bit color");
33 screenMap->depth = 24;
34 screenMap->mode = QColormap::Direct;
35 return;
36 }
37 screenMap->depth = QGuiApplication::primaryScreen()->depth();
38 if (screenMap->depth < 8) {
39 screenMap->mode = QColormap::Indexed;
40 screenMap->numcolors = 256;
41 } else {
42 screenMap->mode = QColormap::Direct;
43 screenMap->numcolors = -1;
44 }
45}
46
47void QColormap::cleanup()
48{
49 delete screenMap;
50 screenMap = nullptr;
51}
52
53QColormap QColormap::instance(int /*screen*/)
54{
55 return QColormap();
56}
57
58QColormap::QColormap()
59 : d(screenMap)
60{ d->ref.ref(); }
61
62QColormap::QColormap(const QColormap &colormap)
63 :d (colormap.d)
64{ d->ref.ref(); }
65
66QColormap::~QColormap()
67{
68 if (!d->ref.deref())
69 delete d;
70}
71
72QColormap::Mode QColormap::mode() const
73{ return d->mode; }
74
75
76int QColormap::depth() const
77{ return d->depth; }
78
79
80int QColormap::size() const
81{
82 return d->numcolors;
83}
84
85#ifndef QT_QWS_DEPTH16_RGB
86#define QT_QWS_DEPTH16_RGB 565
87#endif
88static const int qt_rbits = (QT_QWS_DEPTH16_RGB/100);
89static const int qt_gbits = (QT_QWS_DEPTH16_RGB/10%10);
90static const int qt_bbits = (QT_QWS_DEPTH16_RGB%10);
91static const int qt_red_shift = qt_bbits+qt_gbits-(8-qt_rbits);
92static const int qt_green_shift = qt_bbits-(8-qt_gbits);
93static const int qt_neg_blue_shift = 8-qt_bbits;
94static const int qt_blue_mask = (1<<qt_bbits)-1;
95static const int qt_green_mask = (1<<(qt_gbits+qt_bbits))-(1<<qt_bbits);
96static const int qt_red_mask = (1<<(qt_rbits+qt_gbits+qt_bbits))-(1<<(qt_gbits+qt_bbits));
97
98static const int qt_red_rounding_shift = qt_red_shift + qt_rbits;
99static const int qt_green_rounding_shift = qt_green_shift + qt_gbits;
100static const int qt_blue_rounding_shift = qt_bbits - qt_neg_blue_shift;
101
102inline ushort qt_convRgbTo16(QRgb c)
103{
104 const int tr = qRed(rgb: c) << qt_red_shift;
105 const int tg = qGreen(rgb: c) << qt_green_shift;
106 const int tb = qBlue(rgb: c) >> qt_neg_blue_shift;
107
108 return (tb & qt_blue_mask) | (tg & qt_green_mask) | (tr & qt_red_mask);
109}
110
111inline QRgb qt_conv16ToRgb(ushort c)
112{
113 const int r=(c & qt_red_mask);
114 const int g=(c & qt_green_mask);
115 const int b=(c & qt_blue_mask);
116 const int tr = r >> qt_red_shift | r >> qt_red_rounding_shift;
117 const int tg = g >> qt_green_shift | g >> qt_green_rounding_shift;
118 const int tb = b << qt_neg_blue_shift | b >> qt_blue_rounding_shift;
119
120 return qRgb(r: tr,g: tg,b: tb);
121}
122
123uint QColormap::pixel(const QColor &color) const
124{
125 QRgb rgb = color.rgba();
126 if (d->mode == QColormap::Direct) {
127 switch(d->depth) {
128 case 16:
129 return qt_convRgbTo16(c: rgb);
130 case 24:
131 case 32:
132 {
133 const int r = qRed(rgb);
134 const int g = qGreen(rgb);
135 const int b = qBlue(rgb);
136 const int red_shift = 16;
137 const int green_shift = 8;
138 const int red_mask = 0xff0000;
139 const int green_mask = 0x00ff00;
140 const int blue_mask = 0x0000ff;
141 const int tg = g << green_shift;
142 const int tr = r << red_shift;
143 return 0xff000000 | (b & blue_mask) | (tg & green_mask) | (tr & red_mask);
144 }
145 }
146 }
147 //XXX
148 //return qt_screen->alloc(qRed(rgb), qGreen(rgb), qBlue(rgb));
149 return 0;
150}
151
152const QColor QColormap::colorAt(uint pixel) const
153{
154 if (d->mode == Direct) {
155 if (d->depth == 16) {
156 pixel = qt_conv16ToRgb(c: pixel);
157 }
158 const int red_shift = 16;
159 const int green_shift = 8;
160 const int red_mask = 0xff0000;
161 const int green_mask = 0x00ff00;
162 const int blue_mask = 0x0000ff;
163 return QColor((pixel & red_mask) >> red_shift,
164 (pixel & green_mask) >> green_shift,
165 (pixel & blue_mask));
166 }
167#if 0 // XXX
168 Q_ASSERT_X(int(pixel) < qt_screen->numCols(), "QColormap::colorAt", "pixel out of bounds of palette");
169 return QColor(qt_screen->clut()[pixel]);
170#endif
171 return QColor();
172}
173
174const QList<QColor> QColormap::colormap() const
175{
176 return QList<QColor>();
177}
178
179QColormap &QColormap::operator=(const QColormap &colormap)
180{ qAtomicAssign(d, x: colormap.d); return *this; }
181
182QT_END_NAMESPACE
183

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