summaryrefslogtreecommitdiffstats
path: root/tests/auto/threed/qstereoimage/tst_qstereoimage.cpp
blob: eed1a71663e4d9ef8b6db4ef0daa541155c41f6b (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/****************************************************************************
**
** 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 "qstereoimage.h"
#include <QtGui/qpainter.h>

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

private slots:
    void separateNonEqual_data();
    void separateNonEqual();
    void createAndConvert_data();
    void createAndConvert();
    void painting_data();
    void painting();

private:
    static QImage generateImage
        (int width, int height, const QColor &color,
         QImage::Format format = QImage::Format_RGB32);
    static QImage generateStereoPair
        (int width, int height, const QColor &lcolor, const QColor &rcolor,
         QStereoImage::Layout layout = QStereoImage::LeftRight,
         QImage::Format format = QImage::Format_RGB32);
};

QImage tst_QStereoImage::generateImage
    (int width, int height, const QColor &color, QImage::Format format)
{
    QImage image(width, height, format);
    image.fill(color.rgba());
    return image;
}

QImage tst_QStereoImage::generateStereoPair
    (int width, int height, const QColor &lcolor, const QColor &rcolor,
     QStereoImage::Layout layout, QImage::Format format)
{
    QImage image;
    QPoint left, right;

    switch (layout) {
    default:
    case QStereoImage::LeftRight:
        image = QImage(width * 2, height, format);
        left = QPoint(0, 0);
        right = QPoint(width, 0);
        break;

    case QStereoImage::RightLeft:
        image = QImage(width * 2, height, format);
        left = QPoint(width, 0);
        right = QPoint(0, 0);
        break;

    case QStereoImage::TopBottom:
        image = QImage(width, height * 2, format);
        left = QPoint(0, 0);
        right = QPoint(0, height);
        break;

    case QStereoImage::BottomTop:
        image = QImage(width, height * 2, format);
        left = QPoint(0, height);
        right = QPoint(0, 0);
        break;
    }

    uint lpixel = lcolor.rgba();
    uint rpixel = rcolor.rgba();
    for (int y = 0; y < height; ++y) {
        for (int x = 0; x < width; ++x) {
            image.setPixel(x + left.x(), y + left.y(), lpixel);
            image.setPixel(x + right.x(), y + right.y(), rpixel);
        }
    }

    return image;
}

// Test creation of a separate image with non-equal sizes for left
// and right images, and check that conversions do what we expect.
void tst_QStereoImage::separateNonEqual_data()
{
    QTest::addColumn<int>("toLayout");

    QTest::newRow("Separate -> LeftRight")
        << int(QStereoImage::LeftRight);
    QTest::newRow("Separate -> RightLeft")
        << int(QStereoImage::RightLeft);
    QTest::newRow("Separate -> TopBottom")
        << int(QStereoImage::TopBottom);
    QTest::newRow("Separate -> BottomTop")
        << int(QStereoImage::BottomTop);
}
void tst_QStereoImage::separateNonEqual()
{
    QFETCH(int, toLayout);

    QStereoImage::Layout to = QStereoImage::Layout(toLayout);

    QStereoImage image1;
    QVERIFY(image1.isNull());
    QCOMPARE(image1.size(), QSize(0, 0));
    QCOMPARE(image1.width(), 0);
    QCOMPARE(image1.height(), 0);
    QVERIFY(image1.layout() == QStereoImage::Separate);

    QImage left(generateImage(120, 100, Qt::red));
    QImage right(generateImage(125, 90, Qt::blue));

    image1.setLeftImage(left);

    QVERIFY(!image1.isNull());
    QCOMPARE(image1.size(), QSize(120, 100));
    QCOMPARE(image1.width(), 120);
    QCOMPARE(image1.height(), 100);
    QVERIFY(image1.layout() == QStereoImage::Separate);
    QCOMPARE(image1.leftImage(), left);
    QVERIFY(image1.rightImage().isNull());
    QVERIFY(image1.wholeImage().isNull());

    QStereoImage image2(image1);

    image1.setRightImage(right);

    QVERIFY(!image1.isNull());
    QCOMPARE(image1.size(), QSize(125, 100));   // Expands to max(left,right).
    QCOMPARE(image1.width(), 125);
    QCOMPARE(image1.height(), 100);
    QVERIFY(image1.layout() == QStereoImage::Separate);
    QCOMPARE(image1.leftImage(), left);
    QCOMPARE(image1.rightImage(), right);
    QVERIFY(image1.wholeImage().isNull());

    QVERIFY(!image2.isNull());
    QCOMPARE(image2.size(), QSize(120, 100));
    QCOMPARE(image2.width(), 120);
    QCOMPARE(image2.height(), 100);
    QVERIFY(image2.layout() == QStereoImage::Separate);
    QCOMPARE(image2.leftImage(), left);
    QVERIFY(image2.rightImage().isNull());
    QVERIFY(image2.wholeImage().isNull());

    QImage wholeTo(generateStereoPair(125, 100, Qt::red, Qt::blue, to));

    image1.setLayout(to);
    QVERIFY(!image1.isNull());
    QCOMPARE(image1.size(), QSize(125, 100));
    QCOMPARE(image1.width(), 125);
    QCOMPARE(image1.height(), 100);
    QVERIFY(image1.layout() == to);
    QCOMPARE(image1.leftImage().size(), QSize(125, 100));
    QCOMPARE(image1.leftImage().copy(0, 0, 120, 100), left);
    QCOMPARE(image1.rightImage().size(), QSize(125, 100));
    QCOMPARE(image1.rightImage().copy(0, 0, 125, 90), right);
    QCOMPARE(image1.wholeImage().size(), wholeTo.size());
}

// Test creating stereo images in various formats and converting
// them to all other possible formats.
void tst_QStereoImage::createAndConvert_data()
{
    QTest::addColumn<int>("fromLayout");
    QTest::addColumn<int>("toLayout");

    // Foo -> Separate is handled in createAndConvert() below, so we
    // don't need to explicitly list it here.

    QTest::newRow("Separate -> LeftRight")
        << int(QStereoImage::Separate)
        << int(QStereoImage::LeftRight);
    QTest::newRow("Separate -> RightLeft")
        << int(QStereoImage::Separate)
        << int(QStereoImage::RightLeft);
    QTest::newRow("Separate -> TopBottom")
        << int(QStereoImage::Separate)
        << int(QStereoImage::TopBottom);
    QTest::newRow("Separate -> BottomTop")
        << int(QStereoImage::Separate)
        << int(QStereoImage::BottomTop);

    QTest::newRow("LeftRight -> LeftRight")
        << int(QStereoImage::LeftRight)
        << int(QStereoImage::LeftRight);
    QTest::newRow("LeftRight -> RightLeft")
        << int(QStereoImage::LeftRight)
        << int(QStereoImage::RightLeft);
    QTest::newRow("LeftRight -> TopBottom")
        << int(QStereoImage::LeftRight)
        << int(QStereoImage::TopBottom);
    QTest::newRow("LeftRight -> BottomTop")
        << int(QStereoImage::LeftRight)
        << int(QStereoImage::BottomTop);

    QTest::newRow("RightLeft -> LeftRight")
        << int(QStereoImage::RightLeft)
        << int(QStereoImage::LeftRight);
    QTest::newRow("RightLeft -> RightLeft")
        << int(QStereoImage::RightLeft)
        << int(QStereoImage::RightLeft);
    QTest::newRow("RightLeft -> TopBottom")
        << int(QStereoImage::RightLeft)
        << int(QStereoImage::TopBottom);
    QTest::newRow("RightLeft -> BottomTop")
        << int(QStereoImage::RightLeft)
        << int(QStereoImage::BottomTop);

    QTest::newRow("TopBottom -> LeftRight")
        << int(QStereoImage::TopBottom)
        << int(QStereoImage::LeftRight);
    QTest::newRow("TopBottom -> RightLeft")
        << int(QStereoImage::TopBottom)
        << int(QStereoImage::RightLeft);
    QTest::newRow("TopBottom -> TopBottom")
        << int(QStereoImage::TopBottom)
        << int(QStereoImage::TopBottom);
    QTest::newRow("TopBottom -> BottomTop")
        << int(QStereoImage::TopBottom)
        << int(QStereoImage::BottomTop);

    QTest::newRow("BottomTop -> LeftRight")
        << int(QStereoImage::BottomTop)
        << int(QStereoImage::LeftRight);
    QTest::newRow("BottomTop -> RightLeft")
        << int(QStereoImage::BottomTop)
        << int(QStereoImage::RightLeft);
    QTest::newRow("BottomTop -> TopBottom")
        << int(QStereoImage::BottomTop)
        << int(QStereoImage::TopBottom);
    QTest::newRow("BottomTop -> BottomTop")
        << int(QStereoImage::BottomTop)
        << int(QStereoImage::BottomTop);
}

void tst_QStereoImage::createAndConvert()
{
    QFETCH(int, fromLayout);
    QFETCH(int, toLayout);

    QStereoImage::Layout from = QStereoImage::Layout(fromLayout);
    QStereoImage::Layout to = QStereoImage::Layout(toLayout);

    QStereoImage image2;

    QStereoImage image1(QSize(120, 100), QImage::Format_RGB32, from);
    QVERIFY(!image1.isNull());
    QCOMPARE(image1.size(), QSize(120, 100));
    QCOMPARE(image1.width(), 120);
    QCOMPARE(image1.height(), 100);
    QVERIFY(image1.layout() == from);
    QCOMPARE(image1.leftImage().size(), QSize(120, 100));
    QCOMPARE(image1.rightImage().size(), QSize(120, 100));
    if (from == QStereoImage::Separate)
        QVERIFY(image1.wholeImage().isNull());
    else if (from == QStereoImage::LeftRight || from == QStereoImage::RightLeft)
        QCOMPARE(image1.wholeImage().size(), QSize(240, 100));
    else
        QCOMPARE(image1.wholeImage().size(), QSize(120, 200));

    QImage left(generateImage(120, 100, Qt::red));
    QImage right(generateImage(120, 100, Qt::blue));
    QImage whole(generateStereoPair(120, 100, Qt::red, Qt::blue, from));
    QImage wholeTo(generateStereoPair(120, 100, Qt::red, Qt::blue, to));

    image1.setLeftImage(left);
    image1.setRightImage(right);

    QCOMPARE(image1.leftImage(), left);
    QCOMPARE(image1.rightImage(), right);
    if (from == QStereoImage::Separate)
        QVERIFY(image1.wholeImage().isNull());
    else
        QCOMPARE(image1.wholeImage(), whole);

    image2 = image1;

    // Check separate conversion, which will also test copy-on-write.
    image1.setLayout(QStereoImage::Separate);

    QCOMPARE(image1.leftImage(), left);
    QCOMPARE(image1.rightImage(), right);
    QVERIFY(image1.wholeImage().isNull());
    QVERIFY(!image1.isNull());
    QCOMPARE(image1.size(), QSize(120, 100));
    QCOMPARE(image1.width(), 120);
    QCOMPARE(image1.height(), 100);
    QVERIFY(image1.layout() == QStereoImage::Separate);

    QVERIFY(image2.layout() == from);

    image2.setLayout(to);
    QVERIFY(!image2.isNull());
    QCOMPARE(image2.size(), QSize(120, 100));
    QCOMPARE(image2.width(), 120);
    QCOMPARE(image2.height(), 100);
    QVERIFY(image2.layout() == to);
    QCOMPARE(image2.leftImage(), left);
    QCOMPARE(image2.rightImage(), right);
    QCOMPARE(image2.wholeImage(), wholeTo);

    // Test creation with width and height instead of QSize.
    QStereoImage image3(120, 100, QImage::Format_RGB32, from);
    QVERIFY(!image3.isNull());
    QCOMPARE(image3.size(), QSize(120, 100));
    QCOMPARE(image3.width(), 120);
    QCOMPARE(image3.height(), 100);
    QVERIFY(image3.layout() == from);
    image3.setLeftImage(left);
    image3.setRightImage(right);
    QCOMPARE(image3.leftImage(), left);
    QCOMPARE(image3.rightImage(), right);
    if (from == QStereoImage::Separate)
        QVERIFY(image3.wholeImage().isNull());
    else
        QCOMPARE(image3.wholeImage(), whole);
}

// Test painting into the two halves of a stereo image.
void tst_QStereoImage::painting_data()
{
    QTest::addColumn<int>("layoutValue");

    QTest::newRow("Separate")
        << int(QStereoImage::Separate);
    QTest::newRow("LeftRight")
        << int(QStereoImage::LeftRight);
    QTest::newRow("RightLeft")
        << int(QStereoImage::RightLeft);
    QTest::newRow("TopBottom")
        << int(QStereoImage::TopBottom);
    QTest::newRow("BottomTop")
        << int(QStereoImage::BottomTop);
}
void tst_QStereoImage::painting()
{
    QFETCH(int, layoutValue);

    QStereoImage::Layout layout = QStereoImage::Layout(layoutValue);

    QStereoImage image1(QSize(120, 100), QImage::Format_ARGB32, layout);
    QVERIFY(image1.layout() == layout);

    QImage left(generateImage(120, 100, Qt::red, QImage::Format_ARGB32));
    QImage right(generateImage(120, 100, Qt::blue, QImage::Format_ARGB32));
    QImage leftTo(generateImage(120, 100, Qt::green, QImage::Format_ARGB32));
    QImage rightTo(generateImage(120, 100, Qt::yellow, QImage::Format_ARGB32));

    image1.setLeftImage(left);
    image1.setRightImage(right);

    // Fill a rectangle that is partially out of bounds to check
    // that painting is actually being constrained by a clip region.
    QPainter leftPainter;
    image1.beginPaintingLeft(&leftPainter);
    leftPainter.fillRect(-10, -10, 600, 300, Qt::green);
    leftPainter.end();

    QCOMPARE(image1.leftImage(), leftTo);
    QCOMPARE(image1.rightImage(), right);

    QPainter rightPainter;
    image1.beginPaintingRight(&rightPainter);
    rightPainter.fillRect(-10, -10, 600, 300, Qt::yellow);
    rightPainter.end();

    QCOMPARE(image1.leftImage(), leftTo);
    QCOMPARE(image1.rightImage(), rightTo);

    // Draw the left and right halves into a different painter.
    QImage whole(generateStereoPair(120, 100, Qt::green, Qt::yellow,
                                    QStereoImage::LeftRight,
                                    QImage::Format_ARGB32));
    QImage image2(240, 100, QImage::Format_ARGB32);
    QPainter painter(&image2);
    image1.drawLeft(&painter, QRectF(0, 0, 120, 100));
    image1.drawRight(&painter, QRectF(120, 0, 120, 100));
    painter.end();
    QCOMPARE(image2, whole);
}

QTEST_APPLESS_MAIN(tst_QStereoImage)

#include "tst_qstereoimage.moc"