qglcolormap.cpp

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

Generated by Squish Coco Non-Commercial