Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/opengl/qglcolormap.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||||||||
2 | ** | - | ||||||||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||||||||
5 | ** | - | ||||||||||||||||||||||||
6 | ** This file is part of the QtOpenGL module of the Qt Toolkit. | - | ||||||||||||||||||||||||
7 | ** | - | ||||||||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||||||||
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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||||||||
15 | ** information use the contact form at http://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 2.1 or version 3 as published by the Free | - | ||||||||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||||||||
25 | ** | - | ||||||||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||||||||
29 | ** | - | ||||||||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||||||||
31 | ** | - | ||||||||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||||||||
33 | - | |||||||||||||||||||||||||
34 | /*! | - | ||||||||||||||||||||||||
35 | \class QGLColormap | - | ||||||||||||||||||||||||
36 | \brief The QGLColormap class is used for installing custom colormaps into | - | ||||||||||||||||||||||||
37 | a QGLWidget. | - | ||||||||||||||||||||||||
38 | - | |||||||||||||||||||||||||
39 | \obsolete | - | ||||||||||||||||||||||||
40 | \inmodule QtOpenGL | - | ||||||||||||||||||||||||
41 | \ingroup painting-3D | - | ||||||||||||||||||||||||
42 | \ingroup shared | - | ||||||||||||||||||||||||
43 | - | |||||||||||||||||||||||||
44 | QGLColormap provides a platform independent way of specifying and | - | ||||||||||||||||||||||||
45 | installing indexed colormaps for a QGLWidget. QGLColormap is | - | ||||||||||||||||||||||||
46 | especially useful when using the OpenGL color-index mode. | - | ||||||||||||||||||||||||
47 | - | |||||||||||||||||||||||||
48 | Under X11 you must use an X server that supports either a \c | - | ||||||||||||||||||||||||
49 | PseudoColor or \c DirectColor visual class. If your X server | - | ||||||||||||||||||||||||
50 | currently only provides a \c GrayScale, \c TrueColor, \c | - | ||||||||||||||||||||||||
51 | StaticColor or \c StaticGray visual, you will not be able to | - | ||||||||||||||||||||||||
52 | allocate colorcells for writing. If this is the case, try setting | - | ||||||||||||||||||||||||
53 | your X server to 8 bit mode. It should then provide you with at | - | ||||||||||||||||||||||||
54 | least a \c PseudoColor visual. Note that you may experience | - | ||||||||||||||||||||||||
55 | colormap flashing if your X server is running in 8 bit mode. | - | ||||||||||||||||||||||||
56 | - | |||||||||||||||||||||||||
57 | The size() of the colormap is always set to 256 | - | ||||||||||||||||||||||||
58 | colors. Note that under Windows you can also install colormaps | - | ||||||||||||||||||||||||
59 | in child widgets. | - | ||||||||||||||||||||||||
60 | - | |||||||||||||||||||||||||
61 | This class uses \l{implicit sharing} as a memory and speed | - | ||||||||||||||||||||||||
62 | optimization. | - | ||||||||||||||||||||||||
63 | - | |||||||||||||||||||||||||
64 | Example of use: | - | ||||||||||||||||||||||||
65 | \snippet code/src_opengl_qglcolormap.cpp 0 | - | ||||||||||||||||||||||||
66 | - | |||||||||||||||||||||||||
67 | \sa QGLWidget::setColormap(), QGLWidget::colormap() | - | ||||||||||||||||||||||||
68 | */ | - | ||||||||||||||||||||||||
69 | - | |||||||||||||||||||||||||
70 | /*! | - | ||||||||||||||||||||||||
71 | \fn Qt::HANDLE QGLColormap::handle() | - | ||||||||||||||||||||||||
72 | - | |||||||||||||||||||||||||
73 | \internal | - | ||||||||||||||||||||||||
74 | - | |||||||||||||||||||||||||
75 | Returns the handle for this color map. | - | ||||||||||||||||||||||||
76 | */ | - | ||||||||||||||||||||||||
77 | - | |||||||||||||||||||||||||
78 | /*! | - | ||||||||||||||||||||||||
79 | \fn void QGLColormap::setHandle(Qt::HANDLE handle) | - | ||||||||||||||||||||||||
80 | - | |||||||||||||||||||||||||
81 | \internal | - | ||||||||||||||||||||||||
82 | - | |||||||||||||||||||||||||
83 | Sets the handle for this color map to \a handle. | - | ||||||||||||||||||||||||
84 | */ | - | ||||||||||||||||||||||||
85 | - | |||||||||||||||||||||||||
86 | #include "qglcolormap.h" | - | ||||||||||||||||||||||||
87 | - | |||||||||||||||||||||||||
88 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
89 | - | |||||||||||||||||||||||||
90 | QGLColormap::QGLColormapData QGLColormap::shared_null = { Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0 }; | - | ||||||||||||||||||||||||
91 | - | |||||||||||||||||||||||||
92 | /*! | - | ||||||||||||||||||||||||
93 | Construct a QGLColormap. | - | ||||||||||||||||||||||||
94 | */ | - | ||||||||||||||||||||||||
95 | QGLColormap::QGLColormap() | - | ||||||||||||||||||||||||
96 | : d(&shared_null) | - | ||||||||||||||||||||||||
97 | { | - | ||||||||||||||||||||||||
98 | d->ref.ref(); | - | ||||||||||||||||||||||||
99 | } executed 12 times by 3 tests: end of block Executed by:
| 12 | ||||||||||||||||||||||||
100 | - | |||||||||||||||||||||||||
101 | - | |||||||||||||||||||||||||
102 | /*! | - | ||||||||||||||||||||||||
103 | Construct a shallow copy of \a map. | - | ||||||||||||||||||||||||
104 | */ | - | ||||||||||||||||||||||||
105 | QGLColormap::QGLColormap(const QGLColormap &map) | - | ||||||||||||||||||||||||
106 | : d(map.d) | - | ||||||||||||||||||||||||
107 | { | - | ||||||||||||||||||||||||
108 | d->ref.ref(); | - | ||||||||||||||||||||||||
109 | } never executed: end of block | 0 | ||||||||||||||||||||||||
110 | - | |||||||||||||||||||||||||
111 | /*! | - | ||||||||||||||||||||||||
112 | Dereferences the QGLColormap and deletes it if this was the last | - | ||||||||||||||||||||||||
113 | reference to it. | - | ||||||||||||||||||||||||
114 | */ | - | ||||||||||||||||||||||||
115 | QGLColormap::~QGLColormap() | - | ||||||||||||||||||||||||
116 | { | - | ||||||||||||||||||||||||
117 | if (!d->ref.deref())
| 0-12 | ||||||||||||||||||||||||
118 | cleanup(d); never executed: cleanup(d); | 0 | ||||||||||||||||||||||||
119 | } executed 12 times by 3 tests: end of block Executed by:
| 12 | ||||||||||||||||||||||||
120 | - | |||||||||||||||||||||||||
121 | void QGLColormap::cleanup(QGLColormap::QGLColormapData *x) | - | ||||||||||||||||||||||||
122 | { | - | ||||||||||||||||||||||||
123 | delete x->cells; | - | ||||||||||||||||||||||||
124 | x->cells = 0; | - | ||||||||||||||||||||||||
125 | delete x; | - | ||||||||||||||||||||||||
126 | } never executed: end of block | 0 | ||||||||||||||||||||||||
127 | - | |||||||||||||||||||||||||
128 | /*! | - | ||||||||||||||||||||||||
129 | Assign a shallow copy of \a map to this QGLColormap. | - | ||||||||||||||||||||||||
130 | */ | - | ||||||||||||||||||||||||
131 | QGLColormap & QGLColormap::operator=(const QGLColormap &map) | - | ||||||||||||||||||||||||
132 | { | - | ||||||||||||||||||||||||
133 | map.d->ref.ref(); | - | ||||||||||||||||||||||||
134 | if (!d->ref.deref())
| 0 | ||||||||||||||||||||||||
135 | cleanup(d); never executed: cleanup(d); | 0 | ||||||||||||||||||||||||
136 | d = map.d; | - | ||||||||||||||||||||||||
137 | return *this; never executed: return *this; | 0 | ||||||||||||||||||||||||
138 | } | - | ||||||||||||||||||||||||
139 | - | |||||||||||||||||||||||||
140 | /*! | - | ||||||||||||||||||||||||
141 | \fn void QGLColormap::detach() | - | ||||||||||||||||||||||||
142 | \internal | - | ||||||||||||||||||||||||
143 | - | |||||||||||||||||||||||||
144 | Detaches this QGLColormap from the shared block. | - | ||||||||||||||||||||||||
145 | */ | - | ||||||||||||||||||||||||
146 | - | |||||||||||||||||||||||||
147 | void QGLColormap::detach_helper() | - | ||||||||||||||||||||||||
148 | { | - | ||||||||||||||||||||||||
149 | QGLColormapData *x = new QGLColormapData; | - | ||||||||||||||||||||||||
150 | x->ref.store(1); | - | ||||||||||||||||||||||||
151 | x->cmapHandle = 0; | - | ||||||||||||||||||||||||
152 | x->cells = 0; | - | ||||||||||||||||||||||||
153 | if (d->cells) {
| 0 | ||||||||||||||||||||||||
154 | x->cells = new QVector<QRgb>(256); | - | ||||||||||||||||||||||||
155 | *x->cells = *d->cells; | - | ||||||||||||||||||||||||
156 | } never executed: end of block | 0 | ||||||||||||||||||||||||
157 | if (!d->ref.deref())
| 0 | ||||||||||||||||||||||||
158 | cleanup(d); never executed: cleanup(d); | 0 | ||||||||||||||||||||||||
159 | d = x; | - | ||||||||||||||||||||||||
160 | } never executed: end of block | 0 | ||||||||||||||||||||||||
161 | - | |||||||||||||||||||||||||
162 | /*! | - | ||||||||||||||||||||||||
163 | Set cell at index \a idx in the colormap to color \a color. | - | ||||||||||||||||||||||||
164 | */ | - | ||||||||||||||||||||||||
165 | void QGLColormap::setEntry(int idx, QRgb color) | - | ||||||||||||||||||||||||
166 | { | - | ||||||||||||||||||||||||
167 | detach(); | - | ||||||||||||||||||||||||
168 | if (!d->cells)
| 0 | ||||||||||||||||||||||||
169 | d->cells = new QVector<QRgb>(256); never executed: d->cells = new QVector<QRgb>(256); | 0 | ||||||||||||||||||||||||
170 | d->cells->replace(idx, color); | - | ||||||||||||||||||||||||
171 | } never executed: end of block | 0 | ||||||||||||||||||||||||
172 | - | |||||||||||||||||||||||||
173 | /*! | - | ||||||||||||||||||||||||
174 | Set an array of cells in this colormap. \a count is the number of | - | ||||||||||||||||||||||||
175 | colors that should be set, \a colors is the array of colors, and | - | ||||||||||||||||||||||||
176 | \a base is the starting index. The first element in \a colors | - | ||||||||||||||||||||||||
177 | is set at \a base in the colormap. | - | ||||||||||||||||||||||||
178 | */ | - | ||||||||||||||||||||||||
179 | void QGLColormap::setEntries(int count, const QRgb *colors, int base) | - | ||||||||||||||||||||||||
180 | { | - | ||||||||||||||||||||||||
181 | detach(); | - | ||||||||||||||||||||||||
182 | if (!d->cells)
| 0 | ||||||||||||||||||||||||
183 | d->cells = new QVector<QRgb>(256); never executed: d->cells = new QVector<QRgb>(256); | 0 | ||||||||||||||||||||||||
184 | - | |||||||||||||||||||||||||
185 | Q_ASSERT_X(colors && base >= 0 && (base + count) <= d->cells->size(), "QGLColormap::setEntries", | - | ||||||||||||||||||||||||
186 | "preconditions not met"); | - | ||||||||||||||||||||||||
187 | for (int i = 0; i < count; ++i)
| 0 | ||||||||||||||||||||||||
188 | setEntry(base + i, colors[i]); never executed: setEntry(base + i, colors[i]); | 0 | ||||||||||||||||||||||||
189 | } never executed: end of block | 0 | ||||||||||||||||||||||||
190 | - | |||||||||||||||||||||||||
191 | /*! | - | ||||||||||||||||||||||||
192 | Returns the QRgb value in the colorcell with index \a idx. | - | ||||||||||||||||||||||||
193 | */ | - | ||||||||||||||||||||||||
194 | QRgb QGLColormap::entryRgb(int idx) const | - | ||||||||||||||||||||||||
195 | { | - | ||||||||||||||||||||||||
196 | if (d == &shared_null || !d->cells)
| 0 | ||||||||||||||||||||||||
197 | return 0; never executed: return 0; | 0 | ||||||||||||||||||||||||
198 | else | - | ||||||||||||||||||||||||
199 | return d->cells->at(idx); never executed: return d->cells->at(idx); | 0 | ||||||||||||||||||||||||
200 | } | - | ||||||||||||||||||||||||
201 | - | |||||||||||||||||||||||||
202 | /*! | - | ||||||||||||||||||||||||
203 | \overload | - | ||||||||||||||||||||||||
204 | - | |||||||||||||||||||||||||
205 | Set the cell with index \a idx in the colormap to color \a color. | - | ||||||||||||||||||||||||
206 | */ | - | ||||||||||||||||||||||||
207 | void QGLColormap::setEntry(int idx, const QColor &color) | - | ||||||||||||||||||||||||
208 | { | - | ||||||||||||||||||||||||
209 | setEntry(idx, color.rgb()); | - | ||||||||||||||||||||||||
210 | } never executed: end of block | 0 | ||||||||||||||||||||||||
211 | - | |||||||||||||||||||||||||
212 | /*! | - | ||||||||||||||||||||||||
213 | Returns the QRgb value in the colorcell with index \a idx. | - | ||||||||||||||||||||||||
214 | */ | - | ||||||||||||||||||||||||
215 | QColor QGLColormap::entryColor(int idx) const | - | ||||||||||||||||||||||||
216 | { | - | ||||||||||||||||||||||||
217 | if (d == &shared_null || !d->cells)
| 0 | ||||||||||||||||||||||||
218 | return QColor(); never executed: return QColor(); | 0 | ||||||||||||||||||||||||
219 | else | - | ||||||||||||||||||||||||
220 | return QColor(d->cells->at(idx)); never executed: return QColor(d->cells->at(idx)); | 0 | ||||||||||||||||||||||||
221 | } | - | ||||||||||||||||||||||||
222 | - | |||||||||||||||||||||||||
223 | /*! | - | ||||||||||||||||||||||||
224 | Returns \c true if the colormap is empty or it is not in use | - | ||||||||||||||||||||||||
225 | by a QGLWidget; otherwise returns \c false. | - | ||||||||||||||||||||||||
226 | - | |||||||||||||||||||||||||
227 | A colormap with no color values set is considered to be empty. | - | ||||||||||||||||||||||||
228 | For historical reasons, a colormap that has color values set | - | ||||||||||||||||||||||||
229 | but which is not in use by a QGLWidget is also considered empty. | - | ||||||||||||||||||||||||
230 | - | |||||||||||||||||||||||||
231 | Compare size() with zero to determine if the colormap is empty | - | ||||||||||||||||||||||||
232 | regardless of whether it is in use by a QGLWidget or not. | - | ||||||||||||||||||||||||
233 | - | |||||||||||||||||||||||||
234 | \sa size() | - | ||||||||||||||||||||||||
235 | */ | - | ||||||||||||||||||||||||
236 | bool QGLColormap::isEmpty() const | - | ||||||||||||||||||||||||
237 | { | - | ||||||||||||||||||||||||
238 | return d == &shared_null || d->cells == 0 || d->cells->size() == 0 || d->cmapHandle == 0; never executed: return d == &shared_null || d->cells == 0 || d->cells->size() == 0 || d->cmapHandle == 0;
| 0 | ||||||||||||||||||||||||
239 | } | - | ||||||||||||||||||||||||
240 | - | |||||||||||||||||||||||||
241 | - | |||||||||||||||||||||||||
242 | /*! | - | ||||||||||||||||||||||||
243 | Returns the number of colorcells in the colormap. | - | ||||||||||||||||||||||||
244 | */ | - | ||||||||||||||||||||||||
245 | int QGLColormap::size() const | - | ||||||||||||||||||||||||
246 | { | - | ||||||||||||||||||||||||
247 | return d->cells ? d->cells->size() : 0; never executed: return d->cells ? d->cells->size() : 0;
| 0 | ||||||||||||||||||||||||
248 | } | - | ||||||||||||||||||||||||
249 | - | |||||||||||||||||||||||||
250 | /*! | - | ||||||||||||||||||||||||
251 | Returns the index of the color \a color. If \a color is not in the | - | ||||||||||||||||||||||||
252 | map, -1 is returned. | - | ||||||||||||||||||||||||
253 | */ | - | ||||||||||||||||||||||||
254 | int QGLColormap::find(QRgb color) const | - | ||||||||||||||||||||||||
255 | { | - | ||||||||||||||||||||||||
256 | if (d->cells)
| 0 | ||||||||||||||||||||||||
257 | return d->cells->indexOf(color); never executed: return d->cells->indexOf(color); | 0 | ||||||||||||||||||||||||
258 | return -1; never executed: return -1; | 0 | ||||||||||||||||||||||||
259 | } | - | ||||||||||||||||||||||||
260 | - | |||||||||||||||||||||||||
261 | /*! | - | ||||||||||||||||||||||||
262 | Returns the index of the color that is the closest match to color | - | ||||||||||||||||||||||||
263 | \a color. | - | ||||||||||||||||||||||||
264 | */ | - | ||||||||||||||||||||||||
265 | int QGLColormap::findNearest(QRgb color) const | - | ||||||||||||||||||||||||
266 | { | - | ||||||||||||||||||||||||
267 | int idx = find(color); | - | ||||||||||||||||||||||||
268 | if (idx >= 0)
| 0 | ||||||||||||||||||||||||
269 | return idx; never executed: return idx; | 0 | ||||||||||||||||||||||||
270 | int mapSize = size(); | - | ||||||||||||||||||||||||
271 | int mindist = 200000; | - | ||||||||||||||||||||||||
272 | int r = qRed(color); | - | ||||||||||||||||||||||||
273 | int g = qGreen(color); | - | ||||||||||||||||||||||||
274 | int b = qBlue(color); | - | ||||||||||||||||||||||||
275 | int rx, gx, bx, dist; | - | ||||||||||||||||||||||||
276 | for (int i = 0; i < mapSize; ++i) {
| 0 | ||||||||||||||||||||||||
277 | QRgb ci = d->cells->at(i); | - | ||||||||||||||||||||||||
278 | rx = r - qRed(ci); | - | ||||||||||||||||||||||||
279 | gx = g - qGreen(ci); | - | ||||||||||||||||||||||||
280 | bx = b - qBlue(ci); | - | ||||||||||||||||||||||||
281 | dist = rx * rx + gx * gx + bx * bx; // calculate distance | - | ||||||||||||||||||||||||
282 | if (dist < mindist) { // minimal?
| 0 | ||||||||||||||||||||||||
283 | mindist = dist; | - | ||||||||||||||||||||||||
284 | idx = i; | - | ||||||||||||||||||||||||
285 | } never executed: end of block | 0 | ||||||||||||||||||||||||
286 | } never executed: end of block | 0 | ||||||||||||||||||||||||
287 | return idx; never executed: return idx; | 0 | ||||||||||||||||||||||||
288 | } | - | ||||||||||||||||||||||||
289 | - | |||||||||||||||||||||||||
290 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |