~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/opengl/qglcolormap.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the opengl module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
/*!
 
30
    \class QGLColormap
 
31
    \brief The QGLColormap class is used for installing custom colormaps into
 
32
    QGLWidgets.
 
33
 
 
34
    \module OpenGL
 
35
    \ingroup multimedia
 
36
 
 
37
    QGLColormap provides a platform independent way of specifying and
 
38
    installing indexed colormaps into QGLWidgets. QGLColormap is
 
39
    especially useful when using the \link opengl.html OpenGL\endlink
 
40
    color-index mode.
 
41
 
 
42
    Under X11 you must use an X server that supports either a \c
 
43
    PseudoColor or \c DirectColor visual class. If your X server
 
44
    currently only provides a \c GrayScale, \c TrueColor, \c
 
45
    StaticColor or \c StaticGray visual, you will not be able to
 
46
    allocate colorcells for writing. If this is the case, try setting
 
47
    your X server to 8 bit mode. It should then provide you with at
 
48
    least a \c PseudoColor visual. Note that you may experience
 
49
    colormap flashing if your X server is running in 8 bit mode.
 
50
 
 
51
    Under Windows the size of the colormap is always set to 256
 
52
    colors. Note that under Windows you can also install colormaps
 
53
    in child widgets.
 
54
 
 
55
    This class uses explicit sharing (see \link shclass.html Shared
 
56
    Classes\endlink).
 
57
 
 
58
    Example of use:
 
59
    \code
 
60
    #include <qapplication.h>
 
61
    #include <qglcolormap.h>
 
62
 
 
63
    int main()
 
64
    {
 
65
        QApplication a(argc, argv);
 
66
 
 
67
        MySuperGLWidget widget(0); // A QGLWidget in color-index mode
 
68
        QGLColormap colormap;
 
69
 
 
70
        // This will fill the colormap with colors ranging from
 
71
        // black to white.
 
72
        for (int i = 0; i < colormap.size(); i++)
 
73
            colormap.setEntry(i, qRgb(i, i, i));
 
74
 
 
75
        widget.setColormap(colormap);
 
76
        widget.show();
 
77
        return a.exec();
 
78
    }
 
79
    \endcode
 
80
 
 
81
    \sa QGLWidget::setColormap(), QGLWidget::colormap()
 
82
*/
 
83
 
 
84
/*!
 
85
    \fn Qt::HANDLE QGLColormap::handle()
 
86
 
 
87
    \internal
 
88
 
 
89
    Returns the handle for this color map.
 
90
*/
 
91
 
 
92
/*!
 
93
    \fn void QGLColormap::setHandle(Qt::HANDLE handle)
 
94
 
 
95
    \internal
 
96
 
 
97
    Sets the handle for this color map to \a handle.
 
98
*/
 
99
 
 
100
#include "qglcolormap.h"
 
101
 
 
102
QGLColormap::QGLColormapData QGLColormap::shared_null = { Q_ATOMIC_INIT(1), 0, 0 };
 
103
 
 
104
/*!
 
105
    Construct a QGLColormap.
 
106
*/
 
107
QGLColormap::QGLColormap()
 
108
    : d(&shared_null)
 
109
{
 
110
    d->ref.ref();
 
111
}
 
112
 
 
113
 
 
114
/*!
 
115
    Construct a shallow copy of \a map.
 
116
*/
 
117
QGLColormap::QGLColormap(const QGLColormap &map)
 
118
    : d(map.d)
 
119
{
 
120
    d->ref.ref();
 
121
}
 
122
 
 
123
/*!
 
124
    Dereferences the QGLColormap and deletes it if this was the last
 
125
    reference to it.
 
126
*/
 
127
QGLColormap::~QGLColormap()
 
128
{
 
129
    if (!d->ref.deref())
 
130
        cleanup(d);
 
131
}
 
132
 
 
133
void QGLColormap::cleanup(QGLColormap::QGLColormapData *x)
 
134
{
 
135
    delete x->cells;
 
136
    x->cells = 0;
 
137
    delete x;
 
138
}
 
139
 
 
140
/*!
 
141
    Assign a shallow copy of \a map to this QGLColormap.
 
142
*/
 
143
QGLColormap & QGLColormap::operator=(const QGLColormap &map)
 
144
{
 
145
    QGLColormapData *x = map.d;
 
146
    x->ref.ref();
 
147
    x = qAtomicSetPtr(&d, x);
 
148
    if (!x->ref.deref())
 
149
        cleanup(x);
 
150
    return *this;
 
151
}
 
152
 
 
153
/*!
 
154
    \fn void QGLColormap::detach()
 
155
    Detaches this QGLColormap from the shared block.
 
156
*/
 
157
 
 
158
void QGLColormap::detach_helper()
 
159
{
 
160
    QGLColormapData *x = new QGLColormapData;
 
161
    x->ref.init(1);
 
162
    x->cmapHandle = 0;
 
163
    x->cells = 0;
 
164
    if (d->cells) {
 
165
        x->cells = new QVector<QRgb>(256);
 
166
        *x->cells = *d->cells;
 
167
    }
 
168
    x = qAtomicSetPtr(&d, x);
 
169
    if (!x->ref.deref())
 
170
        cleanup(x);
 
171
}
 
172
 
 
173
/*!
 
174
    Set cell at index \a idx in the colormap to color \a color.
 
175
*/
 
176
void QGLColormap::setEntry(int idx, QRgb color)
 
177
{
 
178
    detach();
 
179
    if (!d->cells)
 
180
        d->cells = new QVector<QRgb>(256);
 
181
    d->cells->insert(idx, color);
 
182
}
 
183
 
 
184
/*!
 
185
    Set an array of cells in this colormap. \a count is the number of
 
186
    colors that should be set, \a colors is the array of colors, and
 
187
    \a base is the starting index.
 
188
*/
 
189
void QGLColormap::setEntries(int count, const QRgb *colors, int base)
 
190
{
 
191
    detach();
 
192
    if (!d->cells)
 
193
        d->cells = new QVector<QRgb>(256);
 
194
 
 
195
    Q_ASSERT_X(!colors || base >= 0 || base + count < d->cells->size(), "QGLColormap::setEntries",
 
196
               "preconditions not met");
 
197
    for (int i = base; i < base + count; ++i)
 
198
        setEntry(i, colors[i]);
 
199
}
 
200
 
 
201
/*!
 
202
    Returns the QRgb value in the colorcell with index \a idx.
 
203
*/
 
204
QRgb QGLColormap::entryRgb(int idx) const
 
205
{
 
206
    if (d == &shared_null || !d->cells)
 
207
        return 0;
 
208
    else
 
209
        return d->cells->at(idx);
 
210
}
 
211
 
 
212
/*!
 
213
    \overload
 
214
 
 
215
    Set the cell with index \a idx in the colormap to color \a color.
 
216
*/
 
217
void QGLColormap::setEntry(int idx, const QColor &color)
 
218
{
 
219
    setEntry(idx, color.rgb());
 
220
}
 
221
 
 
222
/*!
 
223
    Returns the QRgb value in the colorcell with index \a idx.
 
224
*/
 
225
QColor QGLColormap::entryColor(int idx) const
 
226
{
 
227
    if (d == &shared_null || !d->cells)
 
228
        return QColor();
 
229
    else
 
230
        return QColor(d->cells->at(idx));
 
231
}
 
232
 
 
233
/*!
 
234
    Returns true if the colormap is empty; otherwise returns false. A
 
235
    colormap with no color values set is considered to be empty.
 
236
*/
 
237
bool QGLColormap::isEmpty() const
 
238
{
 
239
    return d == &shared_null || d->cells == 0 || d->cells->size() == 0 || d->cmapHandle == 0;
 
240
}
 
241
 
 
242
 
 
243
/*!
 
244
    Returns the number of colorcells in the colormap.
 
245
*/
 
246
int QGLColormap::size() const
 
247
{
 
248
    return d->cells ? d->cells->size() : 0;
 
249
}
 
250
 
 
251
/*!
 
252
    Returns the index of the color \a color. If \a color is not in the
 
253
    map, -1 is returned.
 
254
*/
 
255
int QGLColormap::find(QRgb color) const
 
256
{
 
257
    if (d->cells)
 
258
        return d->cells->indexOf(color);
 
259
    return -1;
 
260
}
 
261
 
 
262
/*!
 
263
    Returns the index of the color that is the closest match to color
 
264
    \a color.
 
265
*/
 
266
int QGLColormap::findNearest(QRgb color) const
 
267
{
 
268
    int idx = find(color);
 
269
    if (idx >= 0)
 
270
        return idx;
 
271
    int mapSize = size();
 
272
    int mindist = 200000;
 
273
    int r = qRed(color);
 
274
    int g = qGreen(color);
 
275
    int b = qBlue(color);
 
276
    int rx, gx, bx, dist;
 
277
    for (int i = 0; i < mapSize; ++i) {
 
278
        QRgb ci = d->cells->at(i);
 
279
        rx = r - qRed(ci);
 
280
        gx = g - qGreen(ci);
 
281
        bx = b - qBlue(ci);
 
282
        dist = rx * rx + gx * gx + bx * bx;        // calculate distance
 
283
        if (dist < mindist) {                // minimal?
 
284
            mindist = dist;
 
285
            idx = i;
 
286
        }
 
287
    }
 
288
    return idx;
 
289
}