summaryrefslogtreecommitdiffstats
path: root/tests/auto/threed/qglpickcolors/tst_qglpickcolors.cpp
blob: 28d179ea6a8da65ecd93d2f997cc376cf535b28b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtTest/QtTest>
#include <QtCore/qset.h>
#include "qglpickcolors_p.h"

class tst_QGLPickColors : public QObject
{
    Q_OBJECT
public:
    tst_QGLPickColors() {}
    ~tst_QGLPickColors() {}

private slots:
    void distinctColors();
    void colorSpread();

    // Round-trip tests check various kinds of ways in which GPU's
    // may take the incoming RGB888 value for the pick color and turn
    // it into a value in the framebuffer, and then convert that
    // back into an RGB888 value later.
    //
    // For example, converting RGB888 -> RGB565 -> RGB888 will lose some
    // precision.  The amount of lost precision will depend upon the
    // specific way in which the GPU does the conversion.  The function
    // qt_qgl_normalize_pick_color() corrects for this and must always
    // return to the same value irrespective of how the GPU works.
    void roundTripExact();
    void roundTripTrunc565();
    void roundTripScale565();
    void roundTripDup565();
    void roundTripTrunc555();
    void roundTripScale555();
    void roundTripDup555();
    void roundTripScale444();       // Trunc doesn't work well with 444.
    void roundTripDup444();
};

// Check that all pick colors are distinct, opaque, and not black.
void tst_QGLPickColors::distinctColors()
{
    QSet<QRgb> used;
    int index = 0;
    QRgb rgb;
    while ((rgb = qt_qgl_pick_color(index)) != 0) {
        QVERIFY(!used.contains(rgb));
        used += rgb;
        QVERIFY(qAlpha(rgb) == 255);                     // Opaque.
        QVERIFY(qRed(rgb) || qGreen(rgb) || qBlue(rgb)); // Not black.
        ++index;
    }
    QCOMPARE(rgb, QRgb(0));
    QCOMPARE(qt_qgl_pick_color(-1), QRgb(0));
    QVERIFY(index >= 1024);
}

// Check that the colors are well spread out so that accidental
// mismatches will not occur.
void tst_QGLPickColors::colorSpread()
{
    int index1 = 0;
    int index2;
    QRgb rgb1, rgb2;
    while ((rgb1 = qt_qgl_pick_color(index1)) != 0) {
        index2 = qMax(index1 - 50, 0);
        while (index2 < index1) {
            rgb2 = qt_qgl_pick_color(index2);
            int rdiff = qRed(rgb1) - qRed(rgb2);
            int gdiff = qGreen(rgb1) - qGreen(rgb2);
            int bdiff = qBlue(rgb1) - qBlue(rgb2);
            int dist = rdiff * rdiff + gdiff * gdiff + bdiff * bdiff;
            if (dist < 15 * 15) {   // FIXME: need more distance.
                qDebug() << index1 << index2 << dist
                         << QString::number(rgb1, 16)
                         << QString::number(rgb2, 16);
            }
            QVERIFY(dist >= 15 * 15);
            ++index2;
        }
        ++index1;
    }
}

// Check that round-tripping a color via exact representation works.
void tst_QGLPickColors::roundTripExact()
{
    int index = 0;
    QRgb rgb;
    while ((rgb = qt_qgl_pick_color(index++)) != 0)
        QVERIFY(qt_qgl_normalize_pick_color(rgb) == rgb);
}

// Check round-tripping when RGB888 -> RGB565 uses truncation.
void tst_QGLPickColors::roundTripTrunc565()
{
    int index = 0;
    QRgb rgb;
    while ((rgb = qt_qgl_pick_color(index++)) != 0) {
        QRgb truncrgb = qRgb(qRed(rgb) & 0xF8,
                             qGreen(rgb) & 0xFC,
                             qBlue(rgb) & 0xF8);

        QVERIFY(qt_qgl_normalize_pick_color(truncrgb) == rgb);
    }
}

// Check round-tripping when RGB888 -> RGB565 scales with floating point.
void tst_QGLPickColors::roundTripScale565()
{
    int index = 0;
    QRgb rgb;
    while ((rgb = qt_qgl_pick_color(index++)) != 0) {
        int r, g, b;
        r = int(qRed(rgb) * 31.0f / 255.0f);
        g = int(qGreen(rgb) * 63.0f / 255.0f);
        b = int(qBlue(rgb) * 31.0f / 255.0f);

        QRgb scalergb = qRgb(r * 255.0f / 31.0f,
                             g * 255.0f / 63.0f,
                             b * 255.0f / 31.0f);

        QVERIFY(qt_qgl_normalize_pick_color(scalergb) == rgb);

        // Scale up, but duplicate down.
        QRgb duprgb = qRgb((r << 3) | (r >> 2),
                           (g << 2) | (g >> 4),
                           (b << 3) | (b >> 2));

        QVERIFY(qt_qgl_normalize_pick_color(duprgb) == rgb);
    }
}

// Check round-tripping when RGB565 -> RGB888 uses bit-duplication.
void tst_QGLPickColors::roundTripDup565()
{
    int index = 0;
    QRgb rgb;
    while ((rgb = qt_qgl_pick_color(index++)) != 0) {
        int r, g, b;
        r = qRed(rgb) & 0xF8;
        r = r | (r >> 5);
        g = qGreen(rgb) & 0xFC;
        g = g | (g >> 6);
        b = qBlue(rgb) & 0xF8;
        b = b | (b >> 5);

        QRgb duprgb = qRgb(r, g, b);

        QVERIFY(qt_qgl_normalize_pick_color(duprgb) == rgb);
    }
}

// Check round-tripping when RGB888 -> RGB555 uses truncation.
void tst_QGLPickColors::roundTripTrunc555()
{
    int index = 0;
    QRgb rgb;
    while ((rgb = qt_qgl_pick_color(index++)) != 0) {
        QRgb truncrgb = qRgb(qRed(rgb) & 0xF8,
                             qGreen(rgb) & 0xF8,
                             qBlue(rgb) & 0xF8);

        QVERIFY(qt_qgl_normalize_pick_color(truncrgb) == rgb);
    }
}

// Check round-tripping when RGB888 -> RGB555 scales with floating point.
void tst_QGLPickColors::roundTripScale555()
{
    int index = 0;
    QRgb rgb;
    while ((rgb = qt_qgl_pick_color(index++)) != 0) {
        int r, g, b;
        r = int(qRed(rgb) * 31.0f / 255.0f);
        g = int(qGreen(rgb) * 31.0f / 255.0f);
        b = int(qBlue(rgb) * 31.0f / 255.0f);

        QRgb scalergb = qRgb(r * 255.0f / 31.0f,
                             g * 255.0f / 31.0f,
                             b * 255.0f / 31.0f);

        QVERIFY(qt_qgl_normalize_pick_color(scalergb) == rgb);

        // Scale up, but duplicate down.
        QRgb duprgb = qRgb((r << 3) | (r >> 2),
                           (g << 3) | (g >> 2),
                           (b << 3) | (b >> 2));

        QVERIFY(qt_qgl_normalize_pick_color(duprgb) == rgb);
    }
}

// Check round-tripping when RGB555 -> RGB888 uses bit-duplication.
void tst_QGLPickColors::roundTripDup555()
{
    int index = 0;
    QRgb rgb;
    while ((rgb = qt_qgl_pick_color(index++)) != 0) {
        int r, g, b;
        r = qRed(rgb) & 0xF8;
        r = r | (r >> 5);
        g = qGreen(rgb) & 0xF8;
        g = g | (g >> 5);
        b = qBlue(rgb) & 0xF8;
        b = b | (b >> 5);

        QRgb duprgb = qRgb(r, g, b);

        QVERIFY(qt_qgl_normalize_pick_color(duprgb) == rgb);
    }
}

// Note: the RGB444 conversion falls apart after 1330 entries
// in the table.  It is no longer possible to distinguish the
// colors while retaining stability in the face of GPU differences.
#define RGB_444_LIMIT   1330

// Check round-tripping when RGB888 -> RGB444 scales with floating point.
void tst_QGLPickColors::roundTripScale444()
{
    int index = 0;
    QRgb rgb;
    while (index < RGB_444_LIMIT && (rgb = qt_qgl_pick_color(index++)) != 0) {
        int r, g, b;
        r = int(qRed(rgb) * 15.0f / 255.0f);
        g = int(qGreen(rgb) * 15.0f / 255.0f);
        b = int(qBlue(rgb) * 15.0f / 255.0f);

        QRgb scalergb = qRgb(r * 255.0f / 15.0f,
                             g * 255.0f / 15.0f,
                             b * 255.0f / 15.0f);

        QVERIFY(qt_qgl_normalize_pick_color(scalergb, true) == rgb);
    }
}

// Check round-tripping when RGB444 -> RGB888 uses bit-duplication.
// We assume that the values are scaled up, but duplicated down.
void tst_QGLPickColors::roundTripDup444()
{
    int index = 0;
    QRgb rgb;
    while (index < RGB_444_LIMIT && (rgb = qt_qgl_pick_color(index++)) != 0) {
        int r, g, b;
        r = int(qRed(rgb) * 15.0f / 255.0f);
        r = r | (r << 4);
        g = int(qGreen(rgb) * 15.0f / 255.0f);
        g = g | (g << 4);
        b = int(qBlue(rgb) * 15.0f / 255.0f);
        b = b | (b << 4);

        QRgb duprgb = qRgb(r, g, b);

        QVERIFY(qt_qgl_normalize_pick_color(duprgb, true) == rgb);
    }
}

QTEST_APPLESS_MAIN(tst_QGLPickColors)

#include "tst_qglpickcolors.moc"