Define QT_OPEN_LARGEFILE on Symbian + WinCE
[qt-netbsd.git] / src / opengl / qglcolormap.cpp
blobb86f9e0398ca6cc692d7a8570960373453f5e8ca
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtOpenGL module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 /*!
43 \class QGLColormap
44 \brief The QGLColormap class is used for installing custom colormaps into
45 a QGLWidget.
47 \module OpenGL
48 \ingroup painting-3D
49 \ingroup shared
51 QGLColormap provides a platform independent way of specifying and
52 installing indexed colormaps for a QGLWidget. QGLColormap is
53 especially useful when using the OpenGL color-index mode.
55 Under X11 you must use an X server that supports either a \c
56 PseudoColor or \c DirectColor visual class. If your X server
57 currently only provides a \c GrayScale, \c TrueColor, \c
58 StaticColor or \c StaticGray visual, you will not be able to
59 allocate colorcells for writing. If this is the case, try setting
60 your X server to 8 bit mode. It should then provide you with at
61 least a \c PseudoColor visual. Note that you may experience
62 colormap flashing if your X server is running in 8 bit mode.
64 The size() of the colormap is always set to 256
65 colors. Note that under Windows you can also install colormaps
66 in child widgets.
68 This class uses \l{implicit sharing} as a memory and speed
69 optimization.
71 Example of use:
72 \snippet doc/src/snippets/code/src_opengl_qglcolormap.cpp 0
74 \sa QGLWidget::setColormap(), QGLWidget::colormap()
77 /*!
78 \fn Qt::HANDLE QGLColormap::handle()
80 \internal
82 Returns the handle for this color map.
85 /*!
86 \fn void QGLColormap::setHandle(Qt::HANDLE handle)
88 \internal
90 Sets the handle for this color map to \a handle.
93 #include "qglcolormap.h"
95 QT_BEGIN_NAMESPACE
97 QGLColormap::QGLColormapData QGLColormap::shared_null = { Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0 };
99 /*!
100 Construct a QGLColormap.
102 QGLColormap::QGLColormap()
103 : d(&shared_null)
105 d->ref.ref();
110 Construct a shallow copy of \a map.
112 QGLColormap::QGLColormap(const QGLColormap &map)
113 : d(map.d)
115 d->ref.ref();
119 Dereferences the QGLColormap and deletes it if this was the last
120 reference to it.
122 QGLColormap::~QGLColormap()
124 if (!d->ref.deref())
125 cleanup(d);
128 void QGLColormap::cleanup(QGLColormap::QGLColormapData *x)
130 delete x->cells;
131 x->cells = 0;
132 delete x;
136 Assign a shallow copy of \a map to this QGLColormap.
138 QGLColormap & QGLColormap::operator=(const QGLColormap &map)
140 map.d->ref.ref();
141 if (!d->ref.deref())
142 cleanup(d);
143 d = map.d;
144 return *this;
148 \fn void QGLColormap::detach()
149 \internal
151 Detaches this QGLColormap from the shared block.
154 void QGLColormap::detach_helper()
156 QGLColormapData *x = new QGLColormapData;
157 x->ref = 1;
158 x->cmapHandle = 0;
159 x->cells = 0;
160 if (d->cells) {
161 x->cells = new QVector<QRgb>(256);
162 *x->cells = *d->cells;
164 if (!d->ref.deref())
165 cleanup(d);
166 d = x;
170 Set cell at index \a idx in the colormap to color \a color.
172 void QGLColormap::setEntry(int idx, QRgb color)
174 detach();
175 if (!d->cells)
176 d->cells = new QVector<QRgb>(256);
177 d->cells->replace(idx, color);
181 Set an array of cells in this colormap. \a count is the number of
182 colors that should be set, \a colors is the array of colors, and
183 \a base is the starting index. The first element in \a colors
184 is set at \a base in the colormap.
186 void QGLColormap::setEntries(int count, const QRgb *colors, int base)
188 detach();
189 if (!d->cells)
190 d->cells = new QVector<QRgb>(256);
192 Q_ASSERT_X(colors && base >= 0 && (base + count) <= d->cells->size(), "QGLColormap::setEntries",
193 "preconditions not met");
194 for (int i = 0; i < count; ++i)
195 setEntry(base + i, colors[i]);
199 Returns the QRgb value in the colorcell with index \a idx.
201 QRgb QGLColormap::entryRgb(int idx) const
203 if (d == &shared_null || !d->cells)
204 return 0;
205 else
206 return d->cells->at(idx);
210 \overload
212 Set the cell with index \a idx in the colormap to color \a color.
214 void QGLColormap::setEntry(int idx, const QColor &color)
216 setEntry(idx, color.rgb());
220 Returns the QRgb value in the colorcell with index \a idx.
222 QColor QGLColormap::entryColor(int idx) const
224 if (d == &shared_null || !d->cells)
225 return QColor();
226 else
227 return QColor(d->cells->at(idx));
231 Returns true if the colormap is empty or it is not in use
232 by a QGLWidget; otherwise returns false.
234 A colormap with no color values set is considered to be empty.
235 For historical reasons, a colormap that has color values set
236 but which is not in use by a QGLWidget is also considered empty.
238 Compare size() with zero to determine if the colormap is empty
239 regardless of whether it is in use by a QGLWidget or not.
241 \sa size()
243 bool QGLColormap::isEmpty() const
245 return d == &shared_null || d->cells == 0 || d->cells->size() == 0 || d->cmapHandle == 0;
250 Returns the number of colorcells in the colormap.
252 int QGLColormap::size() const
254 return d->cells ? d->cells->size() : 0;
258 Returns the index of the color \a color. If \a color is not in the
259 map, -1 is returned.
261 int QGLColormap::find(QRgb color) const
263 if (d->cells)
264 return d->cells->indexOf(color);
265 return -1;
269 Returns the index of the color that is the closest match to color
270 \a color.
272 int QGLColormap::findNearest(QRgb color) const
274 int idx = find(color);
275 if (idx >= 0)
276 return idx;
277 int mapSize = size();
278 int mindist = 200000;
279 int r = qRed(color);
280 int g = qGreen(color);
281 int b = qBlue(color);
282 int rx, gx, bx, dist;
283 for (int i = 0; i < mapSize; ++i) {
284 QRgb ci = d->cells->at(i);
285 rx = r - qRed(ci);
286 gx = g - qGreen(ci);
287 bx = b - qBlue(ci);
288 dist = rx * rx + gx * gx + bx * bx; // calculate distance
289 if (dist < mindist) { // minimal?
290 mindist = dist;
291 idx = i;
294 return idx;
297 QT_END_NAMESPACE